Skip to content

Commit

Permalink
Fixed an issue with events received from callback actors not having t…
Browse files Browse the repository at this point in the history
…he appropriate `_event.origin` set
  • Loading branch information
Andarist committed Dec 22, 2020
1 parent 7348614 commit 497c543
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-poets-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with events received from callback actors not having the appropriate `_event.origin` set.
2 changes: 1 addition & 1 deletion packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ export class Interpreter<
if (canceled) {
return;
}
this.send(e);
this.send(toSCXMLEvent(e, { origin: id }));
};

let callbackStop;
Expand Down
40 changes: 37 additions & 3 deletions packages/core/test/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Machine, sendParent, interpret, assign } from '../src';
import { respond, send } from '../src/actions';

describe('SCXML events', () => {
it('should have the origin (id) from the sending service', (done) => {
it('should have the origin (id) from the sending machine service', (done) => {
const childMachine = Machine({
initial: 'active',
states: {
Expand Down Expand Up @@ -40,14 +40,46 @@ describe('SCXML events', () => {
.start();
});

it('should have the origin (id) from the sending callback service', () => {
const machine = Machine<{ childOrigin?: string }>({
initial: 'active',
context: {},
states: {
active: {
invoke: {
id: 'callback_child',
src: () => (send) => send({ type: 'EVENT' })
},
on: {
EVENT: {
target: 'success',
actions: assign({
childOrigin: (_, __, { _event }) => _event.origin
})
}
}
},
success: {
type: 'final'
}
}
});

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

expect(service.state.context.childOrigin).toBe('callback_child');
});

it('respond() should be able to respond to sender', (done) => {
const authServerMachine = Machine({
initial: 'waitingForCode',
states: {
waitingForCode: {
on: {
CODE: {
actions: respond('TOKEN', { delay: 10 })
actions: respond('TOKEN', {
delay: 10
})
}
}
}
Expand All @@ -65,7 +97,9 @@ describe('SCXML events', () => {
id: 'auth-server',
src: authServerMachine
},
entry: send('CODE', { to: 'auth-server' }),
entry: send('CODE', {
to: 'auth-server'
}),
on: {
TOKEN: 'authorized'
}
Expand Down

0 comments on commit 497c543

Please sign in to comment.