From 6a9fa1f11864e42a986822f407a136a92ae6567c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 30 Nov 2023 20:40:40 +0100 Subject: [PATCH] Remove `escalate` (#4535) * Remove `escalate` * Changeset --------- Co-authored-by: David Khourshid --- .changeset/lazy-shirts-juggle.md | 5 ++ packages/core/src/actions.ts | 1 - packages/core/src/actions/send.ts | 38 ------------- packages/core/test/invoke.test.ts | 92 +------------------------------ 4 files changed, 6 insertions(+), 130 deletions(-) create mode 100644 .changeset/lazy-shirts-juggle.md diff --git a/.changeset/lazy-shirts-juggle.md b/.changeset/lazy-shirts-juggle.md new file mode 100644 index 0000000000..52440a7c5d --- /dev/null +++ b/.changeset/lazy-shirts-juggle.md @@ -0,0 +1,5 @@ +--- +'xstate': major +--- + +The `escalate()` action is removed. Just throw an error normally. diff --git a/packages/core/src/actions.ts b/packages/core/src/actions.ts index eb13626168..2ab7e7ae6d 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -11,7 +11,6 @@ export { export { log, type LogAction } from './actions/log.ts'; export { raise, type RaiseAction } from './actions/raise.ts'; export { - escalate, forwardTo, sendParent, sendTo, diff --git a/packages/core/src/actions/send.ts b/packages/core/src/actions/send.ts index 951d582730..275189a12f 100644 --- a/packages/core/src/actions/send.ts +++ b/packages/core/src/actions/send.ts @@ -334,41 +334,3 @@ export function forwardTo< TDelay >(target, ({ event }: any) => event, options); } - -/** - * Escalates an error by sending it as an event to this machine's parent. - * - * @param errorData The error data to send, or the expression function that - * takes in the `context`, `event`, and `meta`, and returns the error data to send. - * @param options Options to pass into the send action creator. - */ -export function escalate< - TContext extends MachineContext, - TExpressionEvent extends EventObject, - TParams extends ParameterizedObject['params'] | undefined, - TErrorData = any, - TEvent extends EventObject = AnyEventObject ->( - errorData: - | TErrorData - | ((args: UnifiedArg) => TErrorData), - options?: SendToActionParams< - TContext, - TExpressionEvent, - TParams, - EventObject, - TEvent, - string - > -) { - return sendParent( - (arg) => { - return { - type: XSTATE_ERROR, - data: - typeof errorData === 'function' ? (errorData as any)(arg) : errorData - }; - }, - options - ); -} diff --git a/packages/core/test/invoke.test.ts b/packages/core/test/invoke.test.ts index 11d524634e..3cea9cfb06 100644 --- a/packages/core/test/invoke.test.ts +++ b/packages/core/test/invoke.test.ts @@ -1,6 +1,6 @@ import { interval, of } from 'rxjs'; import { map, take } from 'rxjs/operators'; -import { escalate, forwardTo, raise, sendTo } from '../src/actions.ts'; +import { forwardTo, raise, sendTo } from '../src/actions.ts'; import { PromiseActorLogic, fromCallback, @@ -2845,96 +2845,6 @@ describe('invoke', () => { }); }); - describe('error handling', () => { - it('handles escalated errors', (done) => { - const child = createMachine({ - initial: 'die', - - states: { - die: { - entry: [escalate('oops')] - } - } - }); - - const parent = createMachine({ - initial: 'one', - - states: { - one: { - invoke: { - id: 'child', - src: child, - onError: { - target: 'two', - guard: ({ event }) => event.data === 'oops' - } - } - }, - two: { - type: 'final' - } - } - }); - - const actor = createActor(parent); - actor.subscribe({ - complete: () => { - done(); - } - }); - actor.start(); - }); - - it('handles escalated errors as an expression', (done) => { - interface ChildContext { - id: number; - } - - const child = createMachine({ - types: {} as { context: ChildContext }, - initial: 'die', - context: { id: 42 }, - states: { - die: { - entry: escalate(({ context }) => context.id) - } - } - }); - - const parent = createMachine({ - initial: 'one', - - states: { - one: { - invoke: { - id: 'child', - src: child, - onError: { - target: 'two', - guard: ({ event }) => { - expect(event.data).toEqual(42); - return true; - } - } - } - }, - two: { - type: 'final' - } - } - }); - - const actor = createActor(parent); - actor.subscribe({ - complete: () => { - done(); - } - }); - actor.start(); - }); - }); - it('invoke `src` can be used with invoke `input`', (done) => { const machine = createMachine( {