Skip to content

Commit

Permalink
Remove escalate (#4535)
Browse files Browse the repository at this point in the history
* Remove `escalate`

* Changeset

---------

Co-authored-by: David Khourshid <[email protected]>
  • Loading branch information
Andarist and davidkpiano authored Nov 30, 2023
1 parent e85ee76 commit 6a9fa1f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 130 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-shirts-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': major
---

The `escalate()` action is removed. Just throw an error normally.
1 change: 0 additions & 1 deletion packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 0 additions & 38 deletions packages/core/src/actions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TContext, TExpressionEvent, TEvent>) => TErrorData),
options?: SendToActionParams<
TContext,
TExpressionEvent,
TParams,
EventObject,
TEvent,
string
>
) {
return sendParent<TContext, TExpressionEvent, TParams, EventObject, TEvent>(
(arg) => {
return {
type: XSTATE_ERROR,
data:
typeof errorData === 'function' ? (errorData as any)(arg) : errorData
};
},
options
);
}
92 changes: 1 addition & 91 deletions packages/core/test/invoke.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
{
Expand Down

0 comments on commit 6a9fa1f

Please sign in to comment.