Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): raise null events on root object, but only if machine is not "done". Fixes #754 #832

Merged
merged 4 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 30 additions & 22 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,14 @@ class StateNode<
{} as Record<string, string>
);

const resolvedConfiguration = resolvedStateValue
? stateTransition.configuration
: currentState
? currentState.configuration
: [];

const isDone = isInFinalState(resolvedConfiguration, this);

const nextState = new State<TContext, TEvent, TStateSchema, TState>({
value: resolvedStateValue || currentState!.value,
context: updatedContext,
Expand Down Expand Up @@ -1301,11 +1309,7 @@ class StateNode<
? currentState.meta
: undefined,
events: [],
configuration: resolvedStateValue
? stateTransition.configuration
: currentState
? currentState.configuration
: [],
configuration: resolvedConfiguration,
transitions: stateTransition.transitions,
children
});
Expand All @@ -1324,25 +1328,29 @@ class StateNode<
}

let maybeNextState = nextState;
const isTransient = stateNodes.some(stateNode => stateNode._transient);

if (isTransient) {
maybeNextState = this.resolveRaisedTransition(
maybeNextState,
{
type: actionTypes.nullEvent
},
_event
);
}
if (!isDone) {
const isTransient =
davidkpiano marked this conversation as resolved.
Show resolved Hide resolved
this._transient || stateNodes.some(stateNode => stateNode._transient);

if (isTransient) {
maybeNextState = this.resolveRaisedTransition(
maybeNextState,
{
type: actionTypes.nullEvent
},
_event
);
}

while (raisedEvents.length) {
const raisedEvent = raisedEvents.shift()!;
maybeNextState = this.resolveRaisedTransition(
maybeNextState,
raisedEvent._event,
_event
);
while (raisedEvents.length) {
const raisedEvent = raisedEvents.shift()!;
maybeNextState = this.resolveRaisedTransition(
maybeNextState,
raisedEvent._event,
_event
);
}
}

// Detect if state changed
Expand Down
40 changes: 39 additions & 1 deletion packages/core/test/transient.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Machine } from '../src/index';
import { Machine, createMachine, interpret } from '../src/index';
import { assign, raise } from '../src/actions';

const greetingContext = { hour: 10 };
Expand Down Expand Up @@ -398,4 +398,42 @@ describe('transient states (eventless transitions)', () => {
const state = machine.transition('a', 'FOO');
expect(state.value).toBe('pass');
});

it('should work with transient transition on root', done => {
const machine = createMachine<any, any>({
id: 'machine',
initial: 'first',
context: { count: 0 },
states: {
first: {
on: {
ADD: {
actions: assign({ count: ctx => ctx.count + 1 })
}
}
},
success: {
type: 'final'
}
},
on: {
'': [
{
target: '.success',
Andarist marked this conversation as resolved.
Show resolved Hide resolved
cond: ctx => {
return ctx.count > 0;
}
}
]
}
});

const service = interpret(machine).onDone(() => {
done();
});

service.start();

service.send('ADD');
});
});