Skip to content

Commit

Permalink
node.data basic, closes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
devanshj committed Jul 5, 2021
1 parent e9cb24d commit cf86521
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 17 deletions.
54 changes: 54 additions & 0 deletions src/MachineDefinition/MachineDefinition.StateNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,57 @@ createMachine({
createMachine({
context: () => ({ foo: 1 })
})

let t0 = createMachine({
context: { foo: 1 },
initial: "a",
states: {
a: {
type: "final",
on: {
X: "b"
},
data: (c, e) => {
A.tests([
A.areEqual<typeof c, { foo: number }>(),
A.areEqual<typeof e, { type: "X" }>()
])
return "foo" as const
}
},
b: {
type: "final",
data: {
bar: (c, e) => {
A.tests([
A.areEqual<typeof c, { foo: number }>(),
A.areEqual<typeof e, { type: "X" }>()
])
return c.foo + 2
},
x: 100
}
}
}
})
A.tests([
A.areEqual<
typeof t0.config.states.a.data,
(c: { foo: number }, e: { type: "X" }) => "foo"
>(),
A.areEqual<
typeof t0.config.states.a.data,
(c: { foo: number }, e: { type: "X" }) => "foo"
>()
])

createMachine({
context: { foo: 1 },
initial: "a",
states: {
a: {
// @ts-expect-error
data: { x: 100 }
}
}
})
48 changes: 31 additions & 17 deletions src/MachineDefinition/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,15 @@ namespace MachineDefinition {
States = A.Get<Self, "states">,
Type = A.Get<Self, "type", "compound">,
On = A.Get<Self, "on">,
EventIdentifierSpec = A.Get<Definition, ["schema", "events", "type"], never>
EventIdentifierSpec = A.Get<Definition, ["schema", "events", "type"], never>,
Data = A.Get<Self, "data">
> =
& { type?:
| "compound"
| "parallel"
| "final"
| "history"
| "atomic"
, states?:
Type extends "atomic"
? "Error: atomic state node can't have states property"
: { [StateIdentifier in keyof States]:
StateIdentifier extends A.String
? S.DoesContain<StateIdentifier, "."> extends B.True
? `Error: identifiers can't have '.' as it's use as a path delimiter`
: StateNode.Of<Definition, L.Concat<Path, ["states", StateIdentifier]>, Precomputed>
: `Error: only string identifiers allowed`
}
| "compound"
| "parallel"
| "final"
| "history"
| "atomic"
}
& ( Type extends "atomic" ?
{ initial?: "Error: atomic state node can't have initial property" } :
Expand All @@ -132,7 +123,17 @@ namespace MachineDefinition {
>
}
)
& { on?:
& { states?:
Type extends "atomic"
? "Error: atomic state node can't have states property"
: { [StateIdentifier in keyof States]:
StateIdentifier extends A.String
? S.DoesContain<StateIdentifier, "."> extends B.True
? `Error: identifiers can't have '.' as it's use as a path delimiter`
: StateNode.Of<Definition, L.Concat<Path, ["states", StateIdentifier]>, Precomputed>
: `Error: only string identifiers allowed`
}
, on?:
{ [EventIdentifier in keyof On]:
EventIdentifier extends A.String
? L.Some<
Expand Down Expand Up @@ -166,6 +167,19 @@ namespace MachineDefinition {
, meta?: unknown
, strict?: boolean
, history?: "shallow" | "deep" | boolean
, data?:
A.DoesExtend<Type, "final"> extends false
? "Error: data can be only set for final nodes" :
| (( context: Machine.Context.Of<Definition, Precomputed>
, event: Machine.Event.Of<Definition, Precomputed>
) => unknown)
| { [K in A.String]:
| ( ( context: Machine.Context.Of<Definition, Precomputed>
, event: Machine.Event.Of<Definition, Precomputed>
) => unknown
)
| U.Exclude<A.Universal, A.Function>
}
}

export type Desugar<N, R> =
Expand Down
4 changes: 4 additions & 0 deletions src/extras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export namespace A {
export type Object = object;
export type String = string;
export type Number = number;
export type Universal =
| string | number | boolean | undefined | null | bigint
| object | ((...a: any[]) => any)


export type Get<T, P, F = undefined> =
P extends any[]
Expand Down

0 comments on commit cf86521

Please sign in to comment.