Skip to content

Commit

Permalink
Fixed an issue with not being able to send events to initially starte…
Browse files Browse the repository at this point in the history
…d child actors when using `predictableActionArguments` (#3549)
  • Loading branch information
Andarist committed Aug 26, 2022
1 parent 93c0353 commit 768c4e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-terms-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with not being able to send events to initially started child actors when using `predictableActionArguments`.
11 changes: 6 additions & 5 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,8 @@ export class Interpreter<

private sendTo = (
event: SCXML.Event<AnyEventObject>,
to: string | number | ActorRef<any>
to: string | number | ActorRef<any>,
immediate: boolean
) => {
const isParent =
this.parent && (to === SpecialTargets.Parent || this.parent.id === to);
Expand Down Expand Up @@ -855,15 +856,15 @@ export class Interpreter<
event.name === actionTypes.error ? `${error(this.id)}` : event.name,
origin: this.sessionId
};
if (this.machine.config.predictableActionArguments) {
if (!immediate && this.machine.config.predictableActionArguments) {
this._outgoingQueue.push([target, scxmlEvent]);
} else {
(target as AnyInterpreter).send(scxmlEvent);
}
}
} else {
// Send normal events to other targets
if (this.machine.config.predictableActionArguments) {
if (!immediate && this.machine.config.predictableActionArguments) {
this._outgoingQueue.push([target, event.data]);
} else {
target.send(event.data);
Expand Down Expand Up @@ -927,7 +928,7 @@ export class Interpreter<
private defer(sendAction: SendActionObject<TContext, TEvent>): void {
this.delayedEventsMap[sendAction.id] = this.clock.setTimeout(() => {
if (sendAction.to) {
this.sendTo(sendAction._event, sendAction.to);
this.sendTo(sendAction._event, sendAction.to, true);
} else {
this.send(
(sendAction as SendActionObject<TContext, TEvent, TEvent>)._event
Expand Down Expand Up @@ -991,7 +992,7 @@ export class Interpreter<
return;
} else {
if (sendAction.to) {
this.sendTo(sendAction._event, sendAction.to);
this.sendTo(sendAction._event, sendAction.to, _event === initEvent);
} else {
this.send(
(sendAction as SendActionObject<TContext, TEvent, TEvent>)._event
Expand Down
35 changes: 35 additions & 0 deletions packages/core/test/predictableExec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,39 @@ describe('predictableExec', () => {
})
.start();
});

it('should be possible to send immediate events to initially invoked actors', () => {
const child = createMachine({
predictableActionArguments: true,
on: {
PING: {
actions: sendParent({ type: 'PONG' })
}
}
});

const machine = createMachine({
predictableActionArguments: true,
initial: 'waiting',
states: {
waiting: {
invoke: {
id: 'ponger',
src: child
},
entry: send({ type: 'PING' }, { to: 'ponger' }),
on: {
PONG: 'done'
}
},
done: {
type: 'final'
}
}
});

const service = interpret(machine).start();

expect(service.getSnapshot().value).toBe('done');
});
});

0 comments on commit 768c4e9

Please sign in to comment.