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

Fix simulating events on child actors. #326 #348 #347

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
54 changes: 54 additions & 0 deletions cypress/integration/events-panel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,58 @@ createMachine({
'MY_EVENT',
);
});

it('should process events sent to child actors', () => {
// Plant a fake entry in localStorage
cy.setLocalStorage(
'xstate_viz_raw_source|no_source',
JSON.stringify({
date: new Date(),
sourceRawContent: `
import { createMachine } from "xstate";

const child = createMachine({
id: "child",
initial: "a",
states: {
a: { on: { MY_EVENT: "b" } },
b: { on: { OTHER_EVENT: "a" } },
},
});

createMachine({
id: "parent",
initial: "p",
states: {
p: {
invoke: {
src: child
}
},
},
});

`,
}),
);

cy.visit('/viz');

cy.getCanvas();

// Switch to actors tab and select the child actor
cy.findByRole('tab', { name: /Actors/ }).click();
cy.findByRole('tabpanel', { name: /Actors/ })
.findAllByTestId(/actor/)
.last()
.click()

cy.getCanvas().findByRole('button', { name: 'MY_EVENT' }).click();
cy.getCanvas().findByRole('button', { name: 'OTHER_EVENT' }).click();

cy.findByRole('tab', { name: 'Events' }).click();
cy.findByRole('tabpanel', { name: 'Events' })
.should('contain', 'MY_EVENT')
.should('contain', 'OTHER_EVENT');
});
});
3 changes: 2 additions & 1 deletion src/simulationMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { devTools } from './devInterface';
import { notifMachine } from './notificationMachine';
import { AnyState, AnyStateMachine, ServiceData } from './types';
import { isOnClientSide } from './isOnClientSide';
import { findChildService } from './utils';

export interface SimEvent extends SCXML.Event<any> {
timestamp: number;
Expand Down Expand Up @@ -180,7 +181,7 @@ export const simulationMachine = simModel.createMachine(
}
});
} else if (event.type === 'xstate.event') {
const service = serviceMap.get(event.sessionId);
const service = serviceMap.get(event.sessionId) || findChildService(Array.from(serviceMap.values()), event.sessionId);
if (service) {
try {
service.send(event.event);
Expand Down
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import * as React from 'react';
import {
ActionObject,
ActionTypes,
ActorRef,
AnyEventObject,
AnyInterpreter,
CancelAction,
Interpreter,
SendAction,
Expand Down Expand Up @@ -429,3 +431,21 @@ export const isAcceptingSpaceNatively = (
el.tagName === 'INPUT' ||
isTextInputLikeElement(el) ||
getRoles(el).includes('button');

export const findChildService = (services: ActorRef<any>[], sessionId: string, maxDepth = 100): AnyInterpreter | undefined => {
return services.reduce<AnyInterpreter | undefined>((prev, child) => {
if (maxDepth === 0) {
return prev
}
if (prev) {
return prev
}
if (child instanceof Interpreter) {
if (child.sessionId === sessionId) {
return child
}
return findChildService(Array.from(child.children.values()), sessionId, maxDepth - 1)
}
return undefined
}, undefined)
}