Skip to content

Commit

Permalink
Add a passthrough schema property to machines (#4688)
Browse files Browse the repository at this point in the history
* Add a passthrough `schema` property to machines

* juggle types

* Add quick test + changeset

---------

Co-authored-by: David Khourshid <[email protected]>
  • Loading branch information
Andarist and davidkpiano authored Jan 24, 2024
1 parent cc5c67e commit 14902e1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-readers-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': minor
---

The `schemas` property in `setup(...)` is now passed through to the resulting machine. This property is meant to be used with future developer tooling, and is typed as `unknown` for now.
7 changes: 6 additions & 1 deletion packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class StateMachine<
*/
public version?: string;

public schemas: unknown;

public implementations: MachineImplementationsSimplified<TContext, TEvent>;

/** @internal */
Expand Down Expand Up @@ -117,7 +119,9 @@ export class StateMachine<
any,
TOutput,
any
>,
> & {
schemas?: unknown;
},
implementations?: MachineImplementationsSimplified<TContext, TEvent>
) {
this.id = config.id || '(machine)';
Expand All @@ -128,6 +132,7 @@ export class StateMachine<
guards: implementations?.guards ?? {}
};
this.version = this.config.version;
this.schemas = this.config.schemas;

this.transition = this.transition.bind(this);
this.getInitialSnapshot = this.getInitialSnapshot.bind(this);
Expand Down
21 changes: 18 additions & 3 deletions packages/core/src/createMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IsNever,
MachineConfig,
MachineContext,
MachineTypes,
NonReducibleUnknown,
ParameterizedObject,
Prop,
Expand Down Expand Up @@ -68,10 +69,10 @@ type ToStateValue<TTestValue extends string | TestValue> =
* Creates a state machine (statechart) with the given configuration.
*
* The state machine represents the pure logic of a state machine actor.
*
*
* @param config The state machine configuration.
* @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
*
*
* @example
```ts
import { createMachine } from 'xstate';
Expand Down Expand Up @@ -119,7 +120,21 @@ export function createMachine<
// we should be able to remove this when we start inferring TConfig, with it we'll always have an inference candidate
TTypesMeta extends TypegenConstraint = TypegenDisabled
>(
config: MachineConfig<
config: {
types?: MachineTypes<
TContext,
TEvent,
TActor,
TAction,
TGuard,
TDelay,
TTag,
TInput,
TOutput,
TTypesMeta
>;
schemas?: unknown;
} & MachineConfig<
TContext,
TEvent,
TActor,
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ export function setup<
TOutput extends NonReducibleUnknown,
TChildrenMap extends Record<string, string> = never
>({
schemas,
actors,
actions,
guards,
delays
}: {
schemas?: unknown;
types?: SetupTypes<TContext, TEvent, TChildrenMap, TTag, TInput, TOutput>;
actors?: {
[K in keyof TActors]: TActors[K];
Expand Down Expand Up @@ -204,11 +206,14 @@ export function setup<
} {
return {
createMachine: (config) =>
(createMachine as any)(config, {
actors,
actions,
guards,
delays
})
(createMachine as any)(
{ ...config, schemas },
{
actors,
actions,
guards,
delays
}
)
};
}
12 changes: 0 additions & 12 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,18 +1363,6 @@ export type MachineConfig<
* The machine's own version.
*/
version?: string;
types?: MachineTypes<
TContext,
TEvent,
TActor,
TAction,
TGuard,
TDelay,
TTag,
TInput,
TOutput,
TTypesMeta
>;
// TODO: make it conditionally required
output?: Mapper<TContext, DoneStateEvent, TOutput, TEvent> | TOutput;
}) &
Expand Down
14 changes: 13 additions & 1 deletion packages/core/test/machine.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createActor, createMachine, assign } from '../src/index.ts';
import { createActor, createMachine, assign, setup } from '../src/index.ts';

const pedestrianStates = {
initial: 'walk',
Expand Down Expand Up @@ -392,6 +392,18 @@ describe('machine', () => {
expect(actorRef.getSnapshot().context.value).toEqual(43);
});
});

it('should pass through schemas', () => {
const machine = setup({
schemas: {
context: { count: { type: 'number' } }
}
}).createMachine({});

expect(machine.schemas).toEqual({
context: { count: { type: 'number' } }
});
});
});

describe('StateNode', () => {
Expand Down

0 comments on commit 14902e1

Please sign in to comment.