Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v5] Remove autoForward #3889

Merged
merged 7 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .changeset/fresh-garlics-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'xstate': major
---

Autoforwarding events is no longer supported and the `autoForward` property has been removed.

Instead of autoforwarding, events should be explicitly sent to actors:

```diff
invoke: {
id: 'child',
src: 'someSrc',
- autoForward: true
},
// ...
on: {
// ...
+ EVENT_TO_FORWARD: {
+ actions: sendTo('child', (_, event) => event)
+ }
}
```
6 changes: 1 addition & 5 deletions packages/core/src/actions/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function invoke<

resolvedInvokeAction.execute = (actorCtx) => {
const interpreter = actorCtx.self as AnyInterpreter;
const { id, autoForward, ref } = resolvedInvokeAction.params;
const { id, ref } = resolvedInvokeAction.params;
if (!ref) {
if (!IS_PRODUCTION) {
warn(
Expand All @@ -100,10 +100,6 @@ export function invoke<
return;
}
try {
if (autoForward) {
interpreter._forwardTo.add(actorRef);
}

actorRef.start?.();
} catch (err) {
interpreter.send(error(id, err));
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/scxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,7 @@ function toConfig(

return {
...(element.attributes!.id && { id: element.attributes!.id as string }),
src: scxmlToMachine(content, options),
autoForward: element.attributes!.autoforward === 'true'
src: scxmlToMachine(content, options)
};
});

Expand Down
21 changes: 0 additions & 21 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,6 @@ export interface InvokeDefinition<
* The source of the actor's behavior to be invoked
*/
src: string;
/**
* If `true`, events sent to the parent service will be forwarded to the invoked service.
*
* Default: `false`
*/
autoForward?: boolean;

input?: Mapper<TContext, TEvent, any> | any;
/**
Expand Down Expand Up @@ -530,12 +524,6 @@ export interface InvokeConfig<
* The source of the machine to be invoked, or the machine itself.
*/
src: string | ActorBehavior<any, any>; // TODO: fix types
/**
* If `true`, events sent to the parent service will be forwarded to the invoked service.
*
* Default: `false`
*/
autoForward?: boolean;

input?: Mapper<TContext, TEvent, any> | any;
/**
Expand Down Expand Up @@ -1142,7 +1130,6 @@ export interface InvokeAction {
type: ActionTypes.Invoke;
src: string | ActorRef<any>;
id: string;
autoForward?: boolean;
exec?: undefined;
meta: MetaObject | undefined;
}
Expand All @@ -1160,7 +1147,6 @@ export interface InvokeActionObject extends BaseActionObject {
params: {
src: string | ActorRef<any>;
id: string;
autoForward?: boolean;
exec?: undefined;
ref?: ActorRef<any>;
meta: MetaObject | undefined;
Expand Down Expand Up @@ -1620,13 +1606,6 @@ export interface InterpreterOptions<_TActorBehavior extends AnyActorBehavior> {
*/
devTools?: boolean | DevToolsAdapter; // TODO: add enhancer options

/**
* If `true`, events from the parent will be sent to this interpreter.
*
* Default: `false`
*/
autoForward?: boolean;

sync?: boolean;

/**
Expand Down
39 changes: 22 additions & 17 deletions packages/core/test/invoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,18 @@ describe('invoke', () => {
start: {
invoke: {
src: 'child',
id: 'someService',
autoForward: true
id: 'someService'
davidkpiano marked this conversation as resolved.
Show resolved Hide resolved
},
always: {
target: 'stop',
guard: (ctx) => ctx.count === 2
},
on: {
INC: {
actions: assign({ count: (ctx) => ctx.count + 1 })
actions: [
assign({ count: (ctx) => ctx.count + 1 }),
sendTo('someService', { type: 'INC' })
]
}
}
},
Expand Down Expand Up @@ -162,7 +164,7 @@ describe('invoke', () => {
.start();
});

it('should forward events to services if autoForward: true', () => {
it('can forward events to services', () => {
davidkpiano marked this conversation as resolved.
Show resolved Hide resolved
const childMachine = createMachine({
id: 'child',
initial: 'init',
Expand Down Expand Up @@ -190,16 +192,17 @@ describe('invoke', () => {
start: {
invoke: {
src: 'child',
id: 'someService',
autoForward: true
id: 'someService'
},
always: {
target: 'stop',
guard: (ctx) => ctx.count === -3
},
on: {
DEC: { actions: assign({ count: (ctx) => ctx.count - 1 }) },
FORWARD_DEC: undefined
FORWARD_DEC: {
actions: sendTo('child', { type: 'FORWARD_DEC' })
}
}
},
stop: {
Expand All @@ -221,7 +224,7 @@ describe('invoke', () => {
})
.onDone(() => {
// 1. The 'parent' machine will not do anything (inert transition)
// 2. The 'FORWARD_DEC' event will be forwarded to the 'child' machine (autoForward: true)
// 2. The 'FORWARD_DEC' event will be "forwarded" to the 'child' machine
// 3. On the 'child' machine, the 'FORWARD_DEC' event sends the 'DEC' action to the 'parent' thrice
// 4. The context of the 'parent' machine will be updated from 2 to -1

Expand All @@ -232,7 +235,7 @@ describe('invoke', () => {
service.send({ type: 'FORWARD_DEC' });
});

it('should forward events to services if autoForward: true before processing them', (done) => {
it('another forwarding pattern', (done) => {
davidkpiano marked this conversation as resolved.
Show resolved Hide resolved
const actual: string[] = [];

const childMachine = createMachine<{ count: number }>({
Expand Down Expand Up @@ -284,8 +287,8 @@ describe('invoke', () => {
},
invokeChild: {
invoke: {
id: 'child',
src: childMachine,
autoForward: true,
onDone: {
target: 'done',
actions: assign((_ctx, event) => ({
Expand All @@ -295,9 +298,12 @@ describe('invoke', () => {
},
on: {
INCREMENT: {
actions: () => {
actual.push('parent got INCREMENT');
}
actions: [
() => {
actual.push('parent got INCREMENT');
},
sendTo('child', { type: 'INCREMENT' })
]
}
}
},
Expand All @@ -315,12 +321,12 @@ describe('invoke', () => {
.onDone(() => {
expect(state.context).toEqual({ countedTo: 3 });
expect(actual).toEqual([
'child got INCREMENT',
'parent got INCREMENT',
'child got INCREMENT',
'parent got INCREMENT',
'child got INCREMENT',
'parent got INCREMENT'
'parent got INCREMENT',
'child got INCREMENT'
]);
done();
})
Expand Down Expand Up @@ -516,8 +522,7 @@ describe('invoke', () => {
start: {
invoke: {
src: 'child',
id: 'someService',
autoForward: true
id: 'someService'
},
on: {
STOP: 'stop'
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ describe('json', () => {
number: 0,
string: 'hello'
},
invoke: [{ id: 'invokeId', src: 'invokeSrc', autoForward: true }],
invoke: [{ id: 'invokeId', src: 'invokeSrc' }],
states: {
testActions: {
invoke: [{ id: 'invokeId', src: 'invokeSrc', autoForward: true }],
invoke: [{ id: 'invokeId', src: 'invokeSrc' }],
entry: [
'stringActionType',
{
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/scxml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ const testGroups: Record<string, string[]> = {
// 'test225.txml', // unique invokeids generated at invoke time
// 'test226.txml', // <invoke src="...">
'test228.txml',
'test229.txml',
// 'test230.txml', // Manual test (TODO: check)
// 'test229.txml', // autoForward not supported in v5
// 'test230.txml', // autoForward not supported in v5
'test232.txml',
// 'test233.txml', // <finalize> not implemented yet
// 'test234.txml', // <finalize> not implemented yet
Expand Down
3 changes: 1 addition & 2 deletions packages/xstate-scxml/src/scxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,7 @@ function toConfig(

return {
...(element.attributes!.id && { id: element.attributes!.id as string }),
src: scxmlToMachine(content, options),
autoForward: element.attributes!.autoforward === 'true'
src: scxmlToMachine(content, options)
};
});

Expand Down