Skip to content

Commit

Permalink
Do not resolve output of final states directly nested in parallel s…
Browse files Browse the repository at this point in the history
…tates (#4386)

* Do not resolve `output` of final states directly nested in parallel states

* simplify the while loop through parallel ancestry chain

* rewrite test to fix the CI
  • Loading branch information
Andarist authored Oct 23, 2023
1 parent e0bbe33 commit 731799a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
32 changes: 11 additions & 21 deletions packages/core/src/stateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,16 +1157,11 @@ function enterStates(
if (stateNodeToEnter.type === 'final') {
const parent = stateNodeToEnter.parent;

if (completedNodes.has(parent)) {
continue;
}
completedNodes.add(parent);

let ancestorMarker =
parent?.type === 'parallel' ? parent : parent?.parent;
let rootCompletionNode = ancestorMarker || stateNodeToEnter;

if (ancestorMarker) {
if (parent?.type === 'compound') {
internalQueue.push(
createDoneStateEvent(
parent!.id,
Expand All @@ -1180,21 +1175,16 @@ function enterStates(
: undefined
)
);
while (
ancestorMarker?.type === 'parallel' &&
isInFinalState(mutConfiguration, ancestorMarker)
) {
const completedNode: typeof ancestorMarker = ancestorMarker;

ancestorMarker = completedNode.parent;
rootCompletionNode = completedNode;

if (completedNodes.has(completedNode)) {
break;
}
completedNodes.add(completedNode);
internalQueue.push(createDoneStateEvent(completedNode.id));
}
}
while (
ancestorMarker?.type === 'parallel' &&
!completedNodes.has(ancestorMarker) &&
isInFinalState(mutConfiguration, ancestorMarker)
) {
completedNodes.add(ancestorMarker);
internalQueue.push(createDoneStateEvent(ancestorMarker.id));
rootCompletionNode = ancestorMarker;
ancestorMarker = ancestorMarker.parent;
}
if (ancestorMarker) {
continue;
Expand Down
29 changes: 29 additions & 0 deletions packages/core/test/final.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1080,4 +1080,33 @@ describe('final states', () => {

expect(actorRef.getSnapshot().status).toBe('active');
});

it('should not resolve output of a final state if its parent is a parallel state', () => {
const spy = jest.fn();

const machine = createMachine({
initial: 'A',
states: {
A: {
type: 'parallel',
states: {
B: {
type: 'final',
output: spy
},
C: {
initial: 'C1',
states: {
C1: {}
}
}
}
}
}
});

createActor(machine).start();

expect(spy).not.toHaveBeenCalled();
});
});

0 comments on commit 731799a

Please sign in to comment.