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

[v5] Routable states #4184

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/core/src/stateUtils.ts
Expand Up @@ -423,6 +423,19 @@ export function formatTransitions<
}
existing.push(delayedTransition);
}
Object.values(stateNode.states).forEach((sn) => {
if (sn.config.route) {
const eventType = `xstate.route.${sn.path.join('.')}`;
const transition: AnyTransitionConfig = {
...sn.config.route,
target: `.${sn.key}`
};

transitions.set(eventType, [
formatTransition(stateNode, eventType, transition)
]);
}
});
return transitions as Map<string, TransitionDefinition<TContext, any>[]>;
}

Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/types.ts
Expand Up @@ -1037,6 +1037,43 @@ export interface StateNodeConfig<
* A default target for a history state
*/
target?: string;
route?: RouteTransitionConfig<
TContext,
TEvent,
TEvent,
TActor,
TAction,
TGuard,
TDelay,
TEmitted
>;
}

export interface RouteTransitionConfig<
TContext extends MachineContext,
TExpressionEvent extends EventObject,
TEvent extends EventObject,
TActor extends ProvidedActor,
TAction extends ParameterizedObject,
TGuard extends ParameterizedObject,
TDelay extends string,
TEmitted extends EventObject
> {
guard?: Guard<TContext, TExpressionEvent, undefined, TGuard>;
actions?: Actions<
TContext,
TExpressionEvent,
TEvent,
undefined,
TActor,
TAction,
TGuard,
TDelay,
TEmitted
>;
reenter?: boolean;
meta?: Record<string, any>;
description?: string;
}

export type AnyStateNodeConfig = StateNodeConfig<
Expand Down
112 changes: 112 additions & 0 deletions packages/core/test/route.test.ts
@@ -0,0 +1,112 @@
import { createActor, createMachine } from '../src';

describe('route', () => {
it('should transition directly to a route if route is an empty transition config', () => {
const machine = createMachine({
id: 'routeTest',
initial: 'a',
states: {
a: {},
b: {
route: {}
},
c: {}
}
});

const actor = createActor(machine).start();

actor.send({
type: 'xstate.route.b'
});

expect(actor.getSnapshot().value).toEqual('b');

actor.send({
type: 'xstate.route.c'
});

expect(actor.getSnapshot().value).toEqual('b');
});

it('should transition directly to a route if guard passes', () => {
const machine = createMachine({
id: 'routeTest',
initial: 'a',
states: {
a: {},
b: {
route: {
guard: () => false
}
},
c: {
route: {
guard: () => true
}
}
}
});

const actor = createActor(machine).start();

expect(actor.getSnapshot().value).toEqual('a');

actor.send({
type: 'xstate.route.b'
});

expect(actor.getSnapshot().value).toEqual('a');

actor.send({
type: 'xstate.route.c'
});

expect(actor.getSnapshot().value).toEqual('c');
});

it('should work with parallel states', () => {
const todoMachine = createMachine({
type: 'parallel',
states: {
todo: {
initial: 'new',
states: {
new: {},
editing: {}
}
},
filter: {
initial: 'all',
states: {
all: {
route: {}
},
active: {
route: {}
},
completed: {
route: {}
}
}
}
}
});

const todoActor = createActor(todoMachine).start();

expect(todoActor.getSnapshot().value).toEqual({
todo: 'new',
filter: 'all'
});

todoActor.send({
type: 'xstate.route.filter.active'
});

expect(todoActor.getSnapshot().value).toEqual({
todo: 'new',
filter: 'active'
});
});
});