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

Enables viz to send events in child machines #352

Open
wants to merge 1 commit 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
45 changes: 35 additions & 10 deletions src/simulationMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ export const simulationMachine = simModel.createMachine(
const serviceMap: Map<string, AnyInterpreter> = new Map();
const machines = new Set<AnyStateMachine>();
const rootServices = new Set<AnyInterpreter>();
function addChildService(service: AnyInterpreter) {
if (!serviceMap.has(service.sessionId)) {
serviceMap.set(service.sessionId, service);
}
}

function locallyInterpret(machine: AnyStateMachine) {
machines.add(machine);
Expand Down Expand Up @@ -179,6 +184,16 @@ export const simulationMachine = simModel.createMachine(
sendBack(simModel.events.ERROR((e as Error).message));
}
});
} else if (event.type === 'ADDSERVICE') {
event.services.forEach((service: AnyInterpreter) => {
if (service?.sessionId) {
try {
addChildService(service);
} catch (e) {
sendBack(simModel.events.ERROR((e as Error).message));
}
}
});
} else if (event.type === 'xstate.event') {
const service = serviceMap.get(event.sessionId);
if (service) {
Expand Down Expand Up @@ -281,16 +296,25 @@ export const simulationMachine = simModel.createMachine(
],
},
'SERVICE.REGISTER': {
actions: simModel.assign({
serviceDataMap: (ctx, { type, ...data }) => {
return produce(ctx.serviceDataMap, (draft) => {
draft[data.sessionId] = data;
});
},
currentSessionId: (ctx, e) => {
return ctx.currentSessionId ?? e.sessionId;
},
}),
actions: [
send(
(_, e) => ({
type: 'ADDSERVICE',
services: [e.service],
}),
{ to: 'proxy' },
),
simModel.assign({
serviceDataMap: (ctx, { type, ...data }) => {
return produce(ctx.serviceDataMap, (draft) => {
draft[data.sessionId] = data;
});
},
currentSessionId: (ctx, e) => {
return ctx.currentSessionId ?? e.sessionId;
},
}),
],
},
'SERVICES.UNREGISTER_ALL': {
actions: simModel.assign({
Expand Down Expand Up @@ -344,6 +368,7 @@ export const simulationMachine = simModel.createMachine(
if (service.parent) {
sendBack(
simModel.events['SERVICE.REGISTER']({
service: service,
sessionId: service.sessionId,
machine: service.machine,
state: service.state || service.initialState,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type ServiceRefEvents =
};

export interface ServiceData {
service?: AnyInterpreter;
sessionId: string;
machine: AnyStateMachine;
state: AnyState;
Expand Down