@menapo/machine 0.0.0-stage → 0.1.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,335 @@
1
+ import { E as Payload, N as Unnamed, S as NamesOf, T as PathOf, _ as InstanceOf, b as MachineConfig, h as Implementations, i as AnyMachine, k as StartOptions, n as AnyEvent, r as AnyEvents, v as LeafOf, y as Machine } from "./types-Bk8svtO6.js";
2
+ //#region src/core/store.d.ts
3
+ /**
4
+ * Context, props and derived values share one shape: values in an array
5
+ * (`v`; a derived value's read bits and value, in pairs), the mask of
6
+ * changed fields and the instance.
7
+ */
8
+ interface Store {
9
+ v: unknown[];
10
+ m: number;
11
+ /** The instance, for bindable props and derived values. */
12
+ readonly i: unknown;
13
+ }
14
+ type StoreClass = (new (values: unknown[], instance: unknown) => Store) & {
15
+ /** Context's only: the defaults with the fields `start` was given (`start(m, { context })`). */
16
+ s?(machine: {
17
+ readonly df?: readonly unknown[];
18
+ readonly nm: readonly string[];
19
+ readonly name: string;
20
+ }, given?: object): unknown[];
21
+ };
22
+ /**
23
+ * Context (`writable`) or props, over `this.v[k]`. Context is written by
24
+ * actions only (D5: anywhere else throws in dev); props are the owner's,
25
+ * and only bindable ones (which `bindable` redefines) can be written.
26
+ */
27
+ declare function storeClass(names: readonly string[], first: number, writable: boolean): StoreClass;
28
+ /**
29
+ * The derived values' class: each getter returns the cached value while no
30
+ * field it read has changed, else recomputes it with its reads recorded.
31
+ * A value computed while a field it read is changed but not yet told is
32
+ * recomputed on every read until the notification forgets it.
33
+ */
34
+ declare function derivedClass(names: readonly string[], fns: readonly ((view: never) => unknown)[]): StoreClass;
35
+ //#endregion
36
+ //#region src/core/instance.d.ts
37
+ type Owner = Readonly<Record<string, unknown>>;
38
+ declare class Interpreter {
39
+ readonly M: Tables;
40
+ /** The active leaf; unset until started. */
41
+ s: State;
42
+ readonly context: Store;
43
+ readonly props: Store;
44
+ event: AnyEvent;
45
+ /**
46
+ * The last notification's change mask, for framework glue: bit 0 is the
47
+ * state, then one bit per context field and per prop, in declaration
48
+ * order (fields past the 31st share the last bit). Internal: `changed`
49
+ * names it. Set by the constructor.
50
+ */
51
+ mask: number;
52
+ /** IDLE, BUSY or STOPPED. */
53
+ b: number;
54
+ /** STATE_BIT once the state changed in this event. */
55
+ d: number;
56
+ /** Events sent while busy. */
57
+ q: AnyEvent[] | undefined;
58
+ l: ((instance: Interpreter) => void)[] | undefined;
59
+ /** What runs while a state is active: `[state, cleanup or timer, …]` (`effects.ts`). */
60
+ r: unknown[] | undefined;
61
+ /** States entered in this event whose timers and effects start once it settles. */
62
+ x: State[] | undefined;
63
+ /** The owner's props: the shared frozen empty object until an owner gives some (only read or replaced). */
64
+ o: Owner;
65
+ /** Owner props to take once the event being handled is done. */
66
+ n: Owner | undefined;
67
+ /** A move waiting for the owner (`bindable`): the rule's target and the event that asked. */
68
+ p: {
69
+ readonly to: State;
70
+ readonly event: AnyEvent;
71
+ readonly b?: unknown;
72
+ } | undefined;
73
+ /** `send` and `is`, bound on first use so they can be destructured. */
74
+ sf: ((event: string | AnyEvent, payload?: object) => void) | undefined;
75
+ wf: ((state: string) => boolean) | undefined;
76
+ /** Derived values and `done`, made on first read. */
77
+ dq: Store | undefined;
78
+ dn: Promise<void> | undefined;
79
+ constructor(machine: Tables, context?: object, owner?: Owner);
80
+ get state(): string;
81
+ get send(): (event: string | AnyEvent, payload?: object) => void;
82
+ get is(): (state: string) => boolean;
83
+ get derived(): Store;
84
+ /**
85
+ * Each part's id and element (`ids.content`, `el.content`): a component's
86
+ * (`parts/component.ts`). An instance `start` made renders nothing, so
87
+ * every part's id and element reads `undefined`, as the types say.
88
+ */
89
+ get ids(): object;
90
+ get el(): object;
91
+ get changed(): string[];
92
+ get done(): Promise<void>;
93
+ within(path: string): boolean;
94
+ /** Bits changed in the event being handled and not yet told. */
95
+ live(): number;
96
+ subscribe(listener: (instance: Interpreter) => void): () => void;
97
+ dispatch(event: string | AnyEvent, payload?: object): void;
98
+ /**
99
+ * Handles one thing from idle (an event's rules; else, before start, the
100
+ * initial state; else the owner props in `n`), then runs to completion.
101
+ * After a throw in user code the queue is dropped and the instance accepts
102
+ * events again.
103
+ */
104
+ go(rules: readonly Rule[] | undefined, event: AnyEvent): void;
105
+ /** Takes the first rule whose guard passes; true if one did and nothing waits for the owner. */
106
+ take(rules: readonly Rule[], event: AnyEvent): boolean;
107
+ /**
108
+ * Exits up to the nearest common ancestor strictly above the target, runs
109
+ * the rule's actions, enters down to the target's leaf. A target the
110
+ * machine is in is exited and entered again, so its timers restart.
111
+ */
112
+ move(to: State, actions: readonly Fn[] | undefined): void;
113
+ /** Enters every state below `from` down to `to`, outermost first; timers and effects start once the event settles. */
114
+ enter(from: State | undefined, to: State): void;
115
+ /** Stops what state `at` runs, latest first, then runs its exit actions. */
116
+ exit(at: State): void;
117
+ /**
118
+ * Sets the timers and starts the effects of states entered in this event
119
+ * that are still active. The glue's subclass defers it until the
120
+ * framework has committed the DOM, and never runs it on a server (D13).
121
+ */
122
+ activate(states: readonly State[]): void;
123
+ /**
124
+ * Runs to completion: `always` rules, queued events, owner props that
125
+ * arrived meanwhile and new states' effects, then one notification;
126
+ * again while listeners send.
127
+ */
128
+ settle(): void;
129
+ setProps(owner: Owner): void;
130
+ stop(): void;
131
+ }
132
+ /**
133
+ * Creates and starts an instance (PLAN §4.1, §4.8): enters the initial
134
+ * state, runs its entry actions, starts its effects and timers.
135
+ */
136
+ declare const start: <M extends AnyMachine>(machine: M, options?: StartOptions<M>) => InstanceOf<M>;
137
+ //#endregion
138
+ //#region src/core/machine.d.ts
139
+ /** A guard, action or effect as the interpreter calls it. */
140
+ type Fn = (view: Interpreter) => unknown;
141
+ type List = readonly Fn[] | undefined;
142
+ /** A normalised transition: no target stays in the state and runs only the actions. */
143
+ interface Rule {
144
+ readonly to: State | undefined;
145
+ readonly if: Fn | undefined;
146
+ readonly do: List;
147
+ /**
148
+ * Set by opt-in features on rules they route themselves (DevTools shows
149
+ * them): the history state a rule targets, or the rules a parallel
150
+ * machine's timer stands for.
151
+ */
152
+ readonly h?: State;
153
+ readonly w?: readonly Rule[];
154
+ }
155
+ /** What a state does with an event: the shared event object for sends without data, and the rules. */
156
+ interface Handler {
157
+ /** The shared event object, for sends without data. */
158
+ readonly ev: AnyEvent;
159
+ readonly ru: readonly Rule[];
160
+ }
161
+ /**
162
+ * A state, normalised. Its keys are short because they ship in every
163
+ * bundle, the interpreter's and, for a compiled machine, its tables (PLAN
164
+ * §5.6): the minifier keeps property names.
165
+ */
166
+ interface State {
167
+ /** Depth-first index; the machine's root is -1. */
168
+ readonly i: number;
169
+ readonly path: string;
170
+ /** The parent; the root has none. */
171
+ readonly up: State | undefined;
172
+ /** A top-level final state: the machine stops there and handles nothing. */
173
+ readonly fi: boolean;
174
+ /** The leaf entering this state settles in. */
175
+ lf: State;
176
+ /** Entry, exit and effect lists. */
177
+ en: List;
178
+ ex: List;
179
+ ef: List;
180
+ /** `after` timers: delay and rules. */
181
+ af: readonly (readonly [number, readonly Rule[]])[] | undefined;
182
+ /** Its eventless rules (`always`), then its ancestors'. */
183
+ al: readonly Rule[] | undefined;
184
+ /** D20: event name → what this state does with it, ancestors folded in. */
185
+ on: ReadonlyMap<string, Handler>;
186
+ /**
187
+ * Set only on a parallel configuration (`src/features/chart.ts`), which
188
+ * stands in for the leaf: every active state, in document order. Parts and
189
+ * DevTools read it; the core never needs it.
190
+ */
191
+ readonly a?: readonly State[];
192
+ }
193
+ /**
194
+ * What a state's `type` holds besides `"final"`: the marker of an opt-in
195
+ * feature (`parallel`, `history()`), which that feature's module reads. It
196
+ * carries the feature itself (`f`), which the normaliser calls when it meets
197
+ * the mark: importing a feature does nothing, so a bundle that re-exports
198
+ * every feature (a packed entry) still ships only the ones its machines mark.
199
+ */
200
+ interface Mark {
201
+ readonly kind: "parallel" | "history";
202
+ readonly deep?: boolean;
203
+ readonly f: Feature;
204
+ }
205
+ /**
206
+ * The strategy `bindable` plugs in (D9). The core calls it only for a
207
+ * machine that declares a bindable prop, so apps that never import
208
+ * `bindable` do not ship controlled values.
209
+ */
210
+ interface Control {
211
+ /** Records a bindable prop and defines its accessor on the props prototype. */
212
+ bind(machine: Tables, prop: number, name: string, bindable: object, proto: object): void;
213
+ /** The leaf to start in, given the owner's props. */
214
+ initial(instance: Interpreter, leaf: State): State;
215
+ /** True when `rule` would change a controlled value: its actions ran, the move waits for the owner. */
216
+ hold(instance: Interpreter, rule: Rule): boolean;
217
+ /** After a move from leaf `from`: tells the owner of uncontrolled values it changed. */
218
+ moved(instance: Interpreter, from: State): void;
219
+ /** After new owner props: moves the owner granted or made, and `changed` events. */
220
+ own(instance: Interpreter, previous: Readonly<Record<string, unknown>>): void;
221
+ }
222
+ /**
223
+ * The normalised machine: what `start` runs. Short keys, as {@link State}'s;
224
+ * a compiled machine leaves out the optional ones it does not use, which
225
+ * only the code of the features that use them reads.
226
+ */
227
+ interface Tables {
228
+ /** Every state, depth first. */
229
+ readonly ss: readonly State[];
230
+ /** The leaf a started instance enters. */
231
+ readonly i0: State;
232
+ /** Context: the defaults, the store class. */
233
+ readonly df?: readonly unknown[];
234
+ readonly C?: StoreClass | undefined;
235
+ /** Props: names, defaults, the store class. */
236
+ readonly pn: readonly string[];
237
+ readonly pd?: unknown[];
238
+ readonly Q?: StoreClass | undefined;
239
+ /** The indices of props that are not bindable: owner props set them directly. */
240
+ readonly pl?: number[];
241
+ /** Context fields then props: bit `n + 1` belongs to `nm[n]`. */
242
+ readonly nm: readonly string[];
243
+ /** Derived values' store class. */
244
+ readonly D?: StoreClass | undefined;
245
+ /** `bindable`'s {@link Control}, when a prop is bindable. */
246
+ k: Control | undefined;
247
+ /** {@link Control}'s own records of the bindable props. */
248
+ readonly bd?: unknown[];
249
+ /** Dev only: the machine's name, for messages. */
250
+ readonly name: string;
251
+ /** Dev only: every event some state handles, for the unhandled-event warning. */
252
+ readonly handled: Set<string>;
253
+ /**
254
+ * The config as written (a compiled machine keeps `name`, `parts`,
255
+ * `expose` and its named implementations): `src/parts/` reads `parts`,
256
+ * `expose` and `name` from it when the glue renders the machine, and
257
+ * `provide` hands {@link r} a copy with its replacements.
258
+ */
259
+ readonly c: RawMachine;
260
+ /** Makes the tables again from a config: the normaliser, or a compiled machine's own maker (M7). */
261
+ readonly r: (config: RawMachine) => Tables;
262
+ /** What starts and stops states' timers and effects (`effects.ts`): set when a state has some. */
263
+ readonly A?: ((instance: Interpreter, states: readonly State[]) => void) | undefined;
264
+ readonly X?: ((instance: Interpreter, at?: State) => void) | undefined;
265
+ /** What takes the owner's props (`props.ts`): set when the machine declares props. */
266
+ readonly P?: ((instance: Interpreter, owner: Readonly<Record<string, unknown>>, queue?: boolean) => void) | undefined;
267
+ }
268
+ type Raw = Readonly<Record<string, unknown>>;
269
+ /** A function, or the name of one in `actions`, `guards` or `effects` (D8). */
270
+ type RawFn = Fn | string;
271
+ type RawFns = RawFn | readonly RawFn[];
272
+ type RawRule = {
273
+ readonly to?: string;
274
+ readonly if?: RawFn;
275
+ readonly do?: RawFns;
276
+ };
277
+ type RawTransitions = string | Fn | RawRule | readonly (string | Fn | RawRule)[];
278
+ interface RawState {
279
+ readonly initial?: string;
280
+ readonly states?: Readonly<Record<string, RawState>>;
281
+ readonly on?: Readonly<Record<string, RawTransitions>>;
282
+ readonly entry?: RawFns;
283
+ readonly exit?: RawFns;
284
+ readonly effect?: RawFns;
285
+ readonly after?: Readonly<Record<string, RawTransitions>>;
286
+ readonly always?: RawTransitions;
287
+ readonly type?: "final" | Mark;
288
+ /** A parallel state's: taken once every region is in a final state. */
289
+ readonly done?: RawTransitions;
290
+ }
291
+ interface RawMachine extends RawState {
292
+ readonly name?: string;
293
+ readonly context?: Raw;
294
+ readonly props?: Raw;
295
+ readonly events?: Raw;
296
+ readonly derived?: Readonly<Record<string, Fn>>;
297
+ readonly actions?: Readonly<Record<string, Fn>>;
298
+ readonly guards?: Readonly<Record<string, Fn>>;
299
+ readonly effects?: Readonly<Record<string, Fn>>;
300
+ readonly parts?: Raw;
301
+ readonly expose?: Fn;
302
+ }
303
+ /** The normaliser's own target resolution, for features that normalise transitions of their own. */
304
+ type Rules = (transitions: RawTransitions, from: State) => Rule[];
305
+ /**
306
+ * What an opt-in feature does to a machine whose config marks it: adds to
307
+ * the tables once they are normalised, from the raw config, data only
308
+ * (fields on states and rules) so the plugin's compilation can emit it with
309
+ * the tables (M7); then, unless {@link wiring} is off, makes its runtime of
310
+ * them (selection rules, timers it routes), as a compiled machine does when
311
+ * it is loaded. Called for each state that carries its mark: it does its
312
+ * work on the first call.
313
+ */
314
+ type Feature = (tables: Tables, raws: readonly RawState[], rules: Rules) => void;
315
+ /**
316
+ * Defines a machine (PLAN §4.1): states, transitions, context, props and
317
+ * events in one object, normalised once into tables. Context, props,
318
+ * events, derived values, named implementations and the state tree are
319
+ * inferred from it.
320
+ */
321
+ declare const defineMachine: <C extends object = {}, PC extends object = {}, E extends object = AnyEvents, DC extends object = {}, A extends object = Unnamed, G extends object = Unnamed, F extends object = Unnamed, S = unknown, const PS extends object = {}, X extends object = {}, PN extends string = never, Q = unknown>(config: MachineConfig<C, PC, E, DC, A, G, F, S, PS, X, PN, Q>) => Machine<C, PC, E, DC, PathOf<S, Q>, LeafOf<S, Q>, {
322
+ readonly actions: NamesOf<A>;
323
+ readonly guards: NamesOf<G>;
324
+ readonly effects: NamesOf<F>;
325
+ }, PS, X>;
326
+ /**
327
+ * The machine with some of its named `actions`, `guards` and `effects`
328
+ * replaced (D8), for tests or reuse; `machine` itself is unchanged. Each
329
+ * function is typed from the machine, as the ones it replaces.
330
+ */
331
+ declare function provide<M extends AnyMachine>(machine: M, implementations: Implementations<M>): M;
332
+ /** Declares an event's data in `events` (D7): `ADD: payload<{ by: number }>()`. */
333
+ declare const payload: <T extends object>() => Payload<T>;
334
+ //#endregion
335
+ export { provide as a, derivedClass as c, payload as i, storeClass as l, Tables as n, Interpreter as o, defineMachine as r, start as s, State as t };