@defold-typescript/types 0.58.0 → 0.59.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ // Generated from packages/hsm/src/builder.ts by `bun run --cwd packages/transpiler declarations`; do not edit.
2
+ import { type AfterRule, type AlwaysConfig, type EventObject, type Machine, type MachineSkeleton, type MoveAction, type PrivateCtxConfig, type RunAction, type StateHook, type TaskStart, type TransitionConfig, type TreePath, type UnhandledHook, type UpdateHook, type WithPrivate } from "./index";
3
+ type Delay<Ctx> = number | ((ctx: Readonly<Ctx>) => number);
4
+ type EventGuard<Ctx, E extends EventObject, V extends E, P extends string, Tag extends string> = NonNullable<TransitionConfig<Ctx, E, V, P, Tag, P>["when"]>;
5
+ type TimerGuard<Ctx, E extends EventObject, P extends string, Tag extends string> = NonNullable<AfterRule<Ctx, P, E, Tag, P>["when"]>;
6
+ type AlwaysGuard<Ctx, E extends EventObject, P extends string, Tag extends string> = NonNullable<AlwaysConfig<Ctx, P, E, Tag, P>["when"]>;
7
+ /**
8
+ * Adds behavior to a machine whose structure is a skeleton, returned by the second `buildMachine`
9
+ * call. `P` is the union of the skeleton's state paths, and every path a statement takes is one
10
+ * of them.
11
+ * @noSelf
12
+ */
13
+ export interface MachineBuilder<Ctx, E extends EventObject, P extends string, Tag extends string = never, Priv = never> {
14
+ /**
15
+ * The builder of the state at the full path `path`, such as `/on/bright`. A path the skeleton
16
+ * lacks throws.
17
+ */
18
+ readonly state: (path: P) => StateBuilder<WithPrivate<Ctx, Priv>, E, P, Tag>;
19
+ /** The builder of the root, whose rules take an event no state below it takes. */
20
+ readonly root: StateBuilder<WithPrivate<Ctx, Priv>, E, P, Tag>;
21
+ /**
22
+ * Writes the statements onto the skeleton's own state objects, in list form, and passes the
23
+ * skeleton and the key to `defineMachine`, returning its machine. It throws, naming the state,
24
+ * on a rule left without its ending, and a second call throws.
25
+ */
26
+ readonly build: () => Machine<Ctx, E, P, Tag>;
27
+ }
28
+ /**
29
+ * Adds behavior to one state. Each `on`, `after`, `every` and `always` call adds one entry, in
30
+ * call order. A state's `on`, `every` and `always` entries are tried in that order, and its
31
+ * `after` timers in delay order, call order breaking ties. `enter`, `exit`, `update`, `task` and
32
+ * `unhandled` each take one function: a second call on the same state throws. Every call throws
33
+ * after `build()`.
34
+ * @noSelf
35
+ */
36
+ export interface StateBuilder<Ctx, E extends EventObject, P extends string, Tag extends string> {
37
+ /**
38
+ * Adds a rule for events of type `type`, whose `when` and `run` see that variant. End it with
39
+ * `to`, `run` or `ignore`.
40
+ */
41
+ readonly on: <K extends E["type"]>(type: K) => EventRuleBuilder<Ctx, E, Extract<E, {
42
+ type: K;
43
+ }>, P, Tag>;
44
+ /**
45
+ * Adds a timer that fires once `delay` seconds of game time have passed in the state: a number,
46
+ * or a function of the context read each time the state is entered. A state's timers are tried
47
+ * in delay order, call order breaking ties. End it with `to` or `run`.
48
+ */
49
+ readonly after: (delay: Delay<Ctx>) => TimerRuleBuilder<Ctx, E, P, Tag>;
50
+ /**
51
+ * Adds functions run each time `delay` seconds of game time pass in the state: a number, or a
52
+ * function of the context read each time the state is entered. End it with `run`.
53
+ */
54
+ readonly every: (delay: Delay<Ctx>) => RunBuilder<Ctx, E, P, Tag>;
55
+ /** Adds a rule taken right after any move that leaves the state active. End it with `to`. */
56
+ readonly always: () => AlwaysRuleBuilder<Ctx, E, P, Tag>;
57
+ /** Sets the function run each time the state is entered. */
58
+ readonly enter: (hook: StateHook<Ctx, E, Tag, P>) => void;
59
+ /** Sets the function run each time the state is left. */
60
+ readonly exit: (hook: StateHook<Ctx, E, Tag, P>) => void;
61
+ /**
62
+ * Sets the function run on every `update(dt)` while the state is active, which returns a full
63
+ * path to move there, or `undefined` to stay.
64
+ */
65
+ readonly update: (hook: UpdateHook<Ctx, E, P, Tag, P>) => void;
66
+ /** Sets the work started on each entry; see `TaskStart`. */
67
+ readonly task: (start: TaskStart<Ctx, E, Tag, P>) => void;
68
+ /** Sets the function run when the machine drops an event while the state is active. */
69
+ readonly unhandled: (hook: UnhandledHook<Ctx, E, Tag, P>) => void;
70
+ /** Adds tag names that hold while the state is active, read with `hasTag`. */
71
+ readonly tags: (...names: Tag[]) => void;
72
+ }
73
+ /**
74
+ * The rule an `on` call added, before its ending. `build()` throws on a rule left without `to`,
75
+ * `run` or `ignore`.
76
+ * @noSelf
77
+ */
78
+ export interface EventRuleBuilder<Ctx, E extends EventObject, V extends E, P extends string, Tag extends string> {
79
+ /**
80
+ * Takes the rule only when `guard` returns `true`. It reads `ctx` and `event` and changes
81
+ * neither.
82
+ */
83
+ readonly when: (guard: EventGuard<Ctx, E, V, P, Tag>) => GuardedEventRuleBuilder<Ctx, E, V, P, Tag>;
84
+ /** Ends the rule with the full path of the state to move to. */
85
+ readonly to: (path: P) => EventRunBuilder<Ctx, E, V, P, Tag>;
86
+ /** Ends the rule with functions run in order, with no move: no state exits or enters. */
87
+ readonly run: (...actions: MoveAction<Ctx, E, V, Tag, P>[]) => void;
88
+ /**
89
+ * Ends the rule with nothing to do: the state takes the event, so it stops there and no rule
90
+ * of a parent sees it.
91
+ */
92
+ readonly ignore: () => void;
93
+ }
94
+ /**
95
+ * An `on` rule after its `when`, before its ending.
96
+ * @noSelf
97
+ */
98
+ export interface GuardedEventRuleBuilder<Ctx, E extends EventObject, V extends E, P extends string, Tag extends string> {
99
+ /** Ends the rule with the full path of the state to move to. */
100
+ readonly to: (path: P) => EventRunBuilder<Ctx, E, V, P, Tag>;
101
+ /** Ends the rule with functions run in order, with no move: no state exits or enters. */
102
+ readonly run: (...actions: MoveAction<Ctx, E, V, Tag, P>[]) => void;
103
+ }
104
+ /**
105
+ * An `on` rule after its `to`.
106
+ * @noSelf
107
+ */
108
+ export interface EventRunBuilder<Ctx, E extends EventObject, V extends E, P extends string, Tag extends string> {
109
+ /** Adds functions run in order during the move, after the `exit` hooks and before `enter`. */
110
+ readonly run: (...actions: MoveAction<Ctx, E, V, Tag, P>[]) => void;
111
+ }
112
+ /**
113
+ * The timer an `after` call added, before its ending. `build()` throws on a timer left without
114
+ * `to` or `run`.
115
+ * @noSelf
116
+ */
117
+ export interface TimerRuleBuilder<Ctx, E extends EventObject, P extends string, Tag extends string> {
118
+ /**
119
+ * Fires the timer only when `guard` returns `true`, checked once the delay has passed and then
120
+ * on every `update` call until it passes. It reads `ctx` and changes nothing.
121
+ */
122
+ readonly when: (guard: TimerGuard<Ctx, E, P, Tag>) => GuardedTimerRuleBuilder<Ctx, E, P, Tag>;
123
+ /** Ends the timer with the full path of the state to move to. */
124
+ readonly to: (path: P) => RunBuilder<Ctx, E, P, Tag>;
125
+ /** Ends the timer with functions run in order when it fires, with no move. */
126
+ readonly run: (...actions: RunAction<Ctx, E, Tag, P>[]) => void;
127
+ }
128
+ /**
129
+ * An `after` timer after its `when`, before its ending.
130
+ * @noSelf
131
+ */
132
+ export interface GuardedTimerRuleBuilder<Ctx, E extends EventObject, P extends string, Tag extends string> {
133
+ /** Ends the timer with the full path of the state to move to. */
134
+ readonly to: (path: P) => RunBuilder<Ctx, E, P, Tag>;
135
+ /** Ends the timer with functions run in order when it fires, with no move. */
136
+ readonly run: (...actions: RunAction<Ctx, E, Tag, P>[]) => void;
137
+ }
138
+ /**
139
+ * An `every` entry, or an `after` timer or `always` rule after its `to`.
140
+ * @noSelf
141
+ */
142
+ export interface RunBuilder<Ctx, E extends EventObject, P extends string, Tag extends string> {
143
+ /**
144
+ * Adds functions run in order: each time the delay passes for an `every` entry, and during the
145
+ * move for a timer or an `always` rule.
146
+ */
147
+ readonly run: (...actions: RunAction<Ctx, E, Tag, P>[]) => void;
148
+ }
149
+ /**
150
+ * The rule an `always` call added, before its `to`. `build()` throws on a rule left without one.
151
+ * @noSelf
152
+ */
153
+ export interface AlwaysRuleBuilder<Ctx, E extends EventObject, P extends string, Tag extends string> {
154
+ /** Takes the rule only when `guard` returns `true`. It reads `ctx` and changes nothing. */
155
+ readonly when: (guard: AlwaysGuard<Ctx, E, P, Tag>) => GuardedAlwaysRuleBuilder<Ctx, E, P, Tag>;
156
+ /** Ends the rule with the full path of the state to move to. */
157
+ readonly to: (path: P) => RunBuilder<Ctx, E, P, Tag>;
158
+ }
159
+ /**
160
+ * An `always` rule after its `when`, before its `to`.
161
+ * @noSelf
162
+ */
163
+ export interface GuardedAlwaysRuleBuilder<Ctx, E extends EventObject, P extends string, Tag extends string> {
164
+ /** Ends the rule with the full path of the state to move to. */
165
+ readonly to: (path: P) => RunBuilder<Ctx, E, P, Tag>;
166
+ }
167
+ /**
168
+ * Defines a machine as a skeleton plus statements, in three steps. `buildMachine<Ctx, E>()` fixes
169
+ * the context and event types and takes an optional hot reload key, as `defineMachine` does; a
170
+ * third type argument is the union of tag names. The second call takes the skeleton: the root's
171
+ * `initial` and `states`, an optional `privateCtx`, and on each state only `initial`, `states`,
172
+ * `type` and `restoreDepth`. It returns a `MachineBuilder`, whose `state(path)` and `root` add
173
+ * behavior one statement at a time, and whose `build()` defines the machine.
174
+ *
175
+ * @example
176
+ * ```ts
177
+ * type LampEvent = { type: "TOGGLE" } | { type: "DIM" };
178
+ *
179
+ * const m = buildMachine<object, LampEvent>("lamp")({
180
+ * initial: "/off",
181
+ * states: {
182
+ * off: {},
183
+ * on: { initial: "/on/bright", states: { bright: {}, dim: {} } },
184
+ * },
185
+ * });
186
+ * m.state("/off").on("TOGGLE").to("/on");
187
+ * m.state("/on").on("TOGGLE").to("/off");
188
+ * m.state("/on/bright").on("DIM").to("/on/dim");
189
+ *
190
+ * export const lamp = m.build();
191
+ * ```
192
+ */
193
+ export declare function buildMachine<Ctx, E extends EventObject, Tag extends string = never>(key?: string): <T, Priv extends object = never>(skeleton: MachineSkeleton<T> & PrivateCtxConfig<Ctx, Priv>) => MachineBuilder<Ctx, E, TreePath<T>, Tag, Priv>;
194
+ export {};
package/hsm/index.d.ts CHANGED
@@ -345,7 +345,12 @@ type NextCount = [never, 2, 3, 4, 5, 6];
345
345
  type TreePathsBelow<T, D extends number> = {
346
346
  [K in keyof T & string]: K | `${K}/${D extends 1 ? keyof T[K] extends never ? never : string : TreePathsBelow<T[K], PathDepth[D]>}`;
347
347
  }[keyof T & string];
348
- type TreePath<T> = `/${TreePathsBelow<T, 4>}`;
348
+ /**
349
+ * The union of the state paths of the tree `T`, each with its leading `/`: `T` holds one key per
350
+ * state name, and a leaf is `unknown`. Paths are spelled out four levels deep; below that, any
351
+ * string under a fourth-level path is accepted.
352
+ */
353
+ export type TreePath<T> = `/${TreePathsBelow<T, 4>}`;
349
354
  type TreeOf<C> = C extends {
350
355
  readonly states: infer Children;
351
356
  } ? {
@@ -515,7 +520,87 @@ export type MachineConfig<Ctx, E extends EventObject, Tag extends string = never
515
520
  readonly [name: string]: StateConfig<Ctx, E, Tag>;
516
521
  };
517
522
  };
518
- type WithPrivate<Ctx, Priv> = [Priv] extends [never] ? Ctx : Ctx & Priv;
523
+ interface SkeletonBehavior {
524
+ /** Not taken in a skeleton: add tags with the state builder's `tags`. */
525
+ readonly tags?: never;
526
+ /** Not taken in a skeleton: add rules with the state builder's `on`. */
527
+ readonly on?: never;
528
+ /** Not taken in a skeleton: add timers with the state builder's `after`. */
529
+ readonly after?: never;
530
+ /** Not taken in a skeleton: add repeated runs with the state builder's `every`. */
531
+ readonly every?: never;
532
+ /** Not taken in a skeleton: add rules with the state builder's `always`. */
533
+ readonly always?: never;
534
+ /** Not taken in a skeleton: add the hook with the state builder's `enter`. */
535
+ readonly enter?: never;
536
+ /** Not taken in a skeleton: add the hook with the state builder's `exit`. */
537
+ readonly exit?: never;
538
+ /** Not taken in a skeleton: add the hook with the state builder's `update`. */
539
+ readonly update?: never;
540
+ /** Not taken in a skeleton: add the task with the state builder's `task`. */
541
+ readonly task?: never;
542
+ /** Not taken in a skeleton: add the hook with the state builder's `unhandled`. */
543
+ readonly unhandled?: never;
544
+ }
545
+ interface SkeletonState<T, Self extends string> extends SkeletonBehavior {
546
+ /** Child states, by name. A name is non-empty and contains no `/`. */
547
+ readonly states?: {
548
+ readonly [K in keyof T]: SkeletonNode<T[K], `${Self}/${K & string}`>;
549
+ };
550
+ }
551
+ interface CompoundSkeleton<T, Self extends string> extends SkeletonState<T, Self> {
552
+ /**
553
+ * `"parallel"` keeps every child active at once, each as a region with its own active state.
554
+ */
555
+ readonly type?: never;
556
+ /**
557
+ * The full path of the child entered with this state, such as `/on/bright` inside `on`.
558
+ * Required when `states` is set, unless the state is parallel. Not taken on a leaf.
559
+ */
560
+ readonly initial?: NoInfer<unknown extends T ? never : `${Self}/${keyof T & string}`>;
561
+ /**
562
+ * Makes the state remember which child was active when it was last left, and enter it again
563
+ * instead of `initial`; see `restoreDepth` on a state config. Not taken on a leaf.
564
+ */
565
+ readonly restoreDepth?: NoInfer<unknown extends T ? never : TreeDepth<T, 4> | "all">;
566
+ }
567
+ interface ParallelSkeleton<T, Self extends string> extends SkeletonState<T, Self> {
568
+ /**
569
+ * `"parallel"` keeps every child active at once, each as a region with its own active state.
570
+ */
571
+ readonly type: "parallel";
572
+ /** Not taken on a parallel state: every child is entered. */
573
+ readonly initial?: never;
574
+ /** Not taken on a parallel state: only a compound state resumes a child. */
575
+ readonly restoreDepth?: never;
576
+ }
577
+ type SkeletonNode<T, Self extends string> = CompoundSkeleton<T, Self> | ParallelSkeleton<T, Self>;
578
+ /**
579
+ * The skeleton `buildMachine` takes: the structure of a machine with no behavior. The root holds
580
+ * `initial` and `states`, and each state only `initial`, `states`, `type` and `restoreDepth`, each
581
+ * checked as in a `defineMachine` config. `T` is the state tree read from the `states` keys, and
582
+ * fixes the paths every statement takes.
583
+ */
584
+ export type MachineSkeleton<T> = SkeletonBehavior & {
585
+ /** The full path of the child entered at `start`, such as `/idle`. */
586
+ readonly initial: NoInfer<`/${keyof T & string}`>;
587
+ /** The machine's states, by name. A name is non-empty and contains no `/`. */
588
+ readonly states: {
589
+ readonly [K in keyof T]: SkeletonNode<T[K], `/${K & string}`>;
590
+ };
591
+ /** Not allowed on the root: put the regions in a child state. */
592
+ readonly type?: never;
593
+ /**
594
+ * Makes every state the count reaches remember its last child; see `restoreDepth` on a state
595
+ * config.
596
+ */
597
+ readonly restoreDepth?: NoInfer<TreeDepth<T, 4> | "all">;
598
+ };
599
+ /**
600
+ * The context a machine's hooks see: `Ctx` for a machine without `privateCtx`, and `Ctx & Priv`
601
+ * for one whose `privateCtx` returns `Priv`.
602
+ */
603
+ export type WithPrivate<Ctx, Priv> = [Priv] extends [never] ? Ctx : Ctx & Priv;
519
604
  /**
520
605
  * Defines a machine in two calls: `defineMachine<Ctx, E>()` fixes the context and event types and
521
606
  * takes an optional hot-reload key, and the second call takes the config. The config is checked
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defold-typescript/types",
3
- "version": "0.58.0",
3
+ "version": "0.59.0",
4
4
  "description": "TypeScript types for the Defold engine's Lua APIs.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -51,6 +51,9 @@
51
51
  "./hsm/async": {
52
52
  "types": "./hsm/async.d.ts"
53
53
  },
54
+ "./hsm/builder": {
55
+ "types": "./hsm/builder.d.ts"
56
+ },
54
57
  "./hsm/debug": {
55
58
  "types": "./hsm/debug.d.ts"
56
59
  },