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] Restrict context to object #2294

Merged
merged 8 commits into from
Jun 13, 2021
Merged
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
Next Next commit
Restrict context to object or undefined
davidkpiano committed Jun 10, 2021
commit 4f803e368d350cd09589988e2d62ae8151c56226
9 changes: 5 additions & 4 deletions packages/core/src/Machine.ts
Original file line number Diff line number Diff line change
@@ -3,30 +3,31 @@ import {
MachineConfig,
EventObject,
AnyEventObject,
Typestate
Typestate,
MachineContext
} from './types';
import { StateMachine } from './StateMachine';
import { Model, ModelContextFrom, ModelEventsFrom } from './model';

export function createMachine<
TModel extends Model<any, any, any>,
TContext = ModelContextFrom<TModel>,
TContext extends MachineContext = ModelContextFrom<TModel>,
TEvent extends EventObject = ModelEventsFrom<TModel>,
TTypestate extends Typestate<TContext> = { value: any; context: TContext }
>(
config: MachineConfig<TContext, TEvent>,
options?: Partial<MachineImplementations<TContext, TEvent>>
): StateMachine<TContext, TEvent, TTypestate>;
export function createMachine<
TContext,
TContext extends MachineContext,
TEvent extends EventObject = AnyEventObject,
TTypestate extends Typestate<TContext> = { value: any; context: TContext }
>(
config: MachineConfig<TContext, TEvent>,
options?: Partial<MachineImplementations<TContext, TEvent>>
): StateMachine<TContext, TEvent, TTypestate>;
export function createMachine<
TContext,
TContext extends MachineContext,
TEvent extends EventObject = AnyEventObject,
TTypestate extends Typestate<TContext> = { value: any; context: TContext }
>(
3 changes: 1 addition & 2 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ import {
Event,
EventObject,
CancelActionObject,
DefaultContext,
ActionObject,
SpecialTargets,
ActionTypes,
@@ -846,7 +845,7 @@ export class Interpreter<
* @param options Interpreter options
*/
export function interpret<
TContext = DefaultContext,
TContext,
TEvent extends EventObject = EventObject,
TTypestate extends Typestate<TContext> = { value: any; context: TContext }
>(
12 changes: 7 additions & 5 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@ import type {
Assigner,
PropertyAssigner,
ExtractEvent,
EventObject
EventObject,
MachineContext
} from './types';
import { mapValues } from './utils';

@@ -85,11 +86,12 @@ type EventFromEventCreators<EventCreators> = {
: never;
}[keyof EventCreators];

export function createModel<TContext, TEvent extends EventObject>(
initialContext: TContext
): Model<TContext, TEvent, void>;
export function createModel<
TContext,
TContext extends MachineContext,
TEvent extends EventObject
>(initialContext: TContext): Model<TContext, TEvent, void>;
export function createModel<
TContext extends MachineContext,
TModelCreators extends ModelCreators<TModelCreators>,
TFinalModelCreators = FinalModelCreators<TModelCreators>
>(
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ export interface ActionObject<TContext, TEvent extends EventObject> {
[other: string]: any;
}

export type DefaultContext = Record<string, any> | undefined;
export type MachineContext = object | undefined;

/**
* The specified string event types or the specified event objects.
4 changes: 2 additions & 2 deletions packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1003,7 +1003,7 @@ describe('purely defined actions', () => {

describe('forwardTo()', () => {
it('should forward an event to a service', (done) => {
const child = createMachine<void, { type: 'EVENT'; value: number }>({
const child = createMachine<undefined, { type: 'EVENT'; value: number }>({
id: 'child',
initial: 'active',
states: {
@@ -1048,7 +1048,7 @@ describe('forwardTo()', () => {
});

it('should forward an event to a service (dynamic)', (done) => {
const child = createMachine<void, { type: 'EVENT'; value: number }>({
const child = createMachine<undefined, { type: 'EVENT'; value: number }>({
id: 'child',
initial: 'active',
states: {
4 changes: 2 additions & 2 deletions packages/core/test/invoke.test.ts
Original file line number Diff line number Diff line change
@@ -492,7 +492,7 @@ describe('invoke', () => {

it('should start services (machine as invoke config)', (done) => {
const machineInvokeMachine = createMachine<
void,
undefined,
{ type: 'SUCCESS'; data: number }
>({
id: 'machine-invoke',
@@ -532,7 +532,7 @@ describe('invoke', () => {

it('should start deeply nested service (machine as invoke config)', (done) => {
const machineInvokeMachine = createMachine<
void,
undefined,
{ type: 'SUCCESS'; data: number }
>({
id: 'parent',
15 changes: 15 additions & 0 deletions packages/core/test/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assign, createMachine, interpret } from '../src/index';
import { raise } from '../src/actions';
import { createModel } from '../src/model';

function noop(_x) {
return;
@@ -297,3 +298,17 @@ describe('Typestates', () => {
expect(failed).toEqual({ result: none, error: 'oops' });
});
});

describe('types', () => {
it('defined context in createMachine() should be an object', () => {
createMachine({
// @ts-expect-error
context: 'string'
});
});

it('defined context passed to createModel() should be an object', () => {
// @ts-expect-error
createModel('string');
});
});
Comment on lines +302 to +314
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❕ New tests

10 changes: 5 additions & 5 deletions packages/xstate-graph/src/graph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
StateNode,
State,
DefaultContext,
MachineContext,
Event,
EventObject,
AnyEventObject,
@@ -100,7 +100,7 @@ function getValueAdjMapOptions<TContext, TEvent extends EventObject>(
}

export function getAdjacencyMap<
TContext = DefaultContext,
TContext = MachineContext,
TEvent extends EventObject = AnyEventObject
>(
node: MachineNode<TContext, TEvent>,
@@ -170,7 +170,7 @@ export function getAdjacencyMap<
}

export function getShortestPaths<
TContext = DefaultContext,
TContext = MachineContext,
TEvent extends EventObject = EventObject
>(
machine: MachineNode<TContext, TEvent>,
@@ -259,7 +259,7 @@ export function getShortestPaths<
}

export function getSimplePaths<
TContext = DefaultContext,
TContext = MachineContext,
TEvent extends EventObject = EventObject
>(
machine: MachineNode<TContext, TEvent>,
@@ -332,7 +332,7 @@ export function getSimplePaths<
}

export function getSimplePathsAsArray<
TContext = DefaultContext,
TContext = MachineContext,
TEvent extends EventObject = EventObject
>(
machine: MachineNode<TContext, TEvent>,
2 changes: 1 addition & 1 deletion packages/xstate-test/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -395,7 +395,7 @@ describe('events', () => {
| { type: 'CLOSE' }
| { type: 'ESC' }
| { type: 'SUBMIT'; value: string };
const feedbackMachine = createMachine<void, Events>({
const feedbackMachine = createMachine<undefined, Events>({
id: 'feedback',
initial: 'question',
states: {