Skip to content
This repository was archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #303 from statelyai/matt/fixed-duplicated-events-i…
Browse files Browse the repository at this point in the history
…ssue

Fixed duplicated events issue
  • Loading branch information
mattpocock authored Oct 22, 2021
2 parents 9f9b0de + 01bb94b commit b377410
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-cameras-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"xstate-viz-app": patch
---

Fixed an issue where events were being duplicated in the right-hand events panel.
72 changes: 39 additions & 33 deletions src/simulationMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,7 @@ export const simulationMachine = simModel.createMachine(
: 'visualizing',
entry: assign({ notifRef: () => spawn(notifMachine) }),
invoke: {
src: () => (sendBack) => {
devTools.onRegister((service) => {
sendBack(
simModel.events['SERVICE.REGISTER']({
sessionId: service.sessionId,
machine: service.machine,
state: service.state || service.initialState,
parent: service.parent?.sessionId,
source: 'in-app',
}),
);

service.subscribe((state) => {
// `onRegister`'s callback gets called from within `.start()`
// `subscribe` calls the callback immediately with the current state
// but the `service.state` state has not yet been set when this gets called for the first time from within `.start()`
if (!state) {
return;
}

sendBack(
simModel.events['SERVICE.STATE'](service.sessionId, state),
);
});

service.onStop(() => {
sendBack(simModel.events['SERVICE.STOP'](service.sessionId));
});
});
},
src: 'captureEventsFromChildServices',
},
states: {
inspecting: {
Expand Down Expand Up @@ -283,14 +254,15 @@ export const simulationMachine = simModel.createMachine(
e.state,
);
}),
events: (ctx, e) =>
produce(ctx.events, (draft) => {
events: (ctx, e) => {
return produce(ctx.events, (draft) => {
draft.push({
...e.state._event,
timestamp: Date.now(),
sessionId: e.sessionId,
});
}),
});
},
}),
],
},
Expand Down Expand Up @@ -365,6 +337,40 @@ export const simulationMachine = simModel.createMachine(
},
},
{
services: {
captureEventsFromChildServices: () => (sendBack) => {
devTools.onRegister((service) => {
// Only capture machines that are spawned or invoked
if (service.parent) {
sendBack(
simModel.events['SERVICE.REGISTER']({
sessionId: service.sessionId,
machine: service.machine,
state: service.state || service.initialState,
parent: service.parent?.sessionId,
source: 'child',
}),
);
service.subscribe((state) => {
// `onRegister`'s callback gets called from within `.start()`
// `subscribe` calls the callback immediately with the current state
// but the `service.state` state has not yet been set when this gets called for the first time from within `.start()`
if (!state) {
return;
}

sendBack(
simModel.events['SERVICE.STATE'](service.sessionId, state),
);
});

service.onStop(() => {
sendBack(simModel.events['SERVICE.STOP'](service.sessionId));
});
}
});
},
},
actions: {
resetVisualizationState: simModel.assign<
EventFrom<typeof simModel>['type']
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface ServiceData {
machine: AnyStateMachine;
state: AnyState;
status: AnyInterpreter['status'];
source: 'inspector' | 'visualizer' | 'in-app';
source: 'inspector' | 'visualizer' | 'child';
parent: string | undefined;
}

Expand Down

0 comments on commit b377410

Please sign in to comment.