@lazily-hub/lazily-js 0.4.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.
- package/.github/workflows/release.yml +48 -0
- package/README.md +356 -0
- package/package.json +64 -0
- package/src/collections.d.ts +110 -0
- package/src/collections.js +413 -0
- package/src/index.d.ts +416 -0
- package/src/index.js +1109 -0
- package/src/reactive.d.ts +43 -0
- package/src/reactive.js +531 -0
- package/src/sem-tree.d.ts +21 -0
- package/src/sem-tree.js +130 -0
- package/src/seq-crdt.d.ts +44 -0
- package/src/seq-crdt.js +364 -0
- package/src/stable-id.d.ts +45 -0
- package/src/stable-id.js +214 -0
- package/src/state-machine.d.ts +14 -0
- package/src/state-machine.js +78 -0
- package/src/state-projection.d.ts +114 -0
- package/src/state-projection.js +256 -0
- package/src/statechart.d.ts +154 -0
- package/src/statechart.js +678 -0
- package/src/text-crdt.d.ts +23 -0
- package/src/text-crdt.js +317 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Type declarations for the native Harel/SCXML state chart (compute, not protocol).
|
|
2
|
+
// Conforms to `lazily-spec/docs/state-charts.md` and the Lean `StateChart` model.
|
|
3
|
+
|
|
4
|
+
/** Structural kind of a state node. */
|
|
5
|
+
export type StateKind = "atomic" | "compound" | "parallel" | "history" | "final";
|
|
6
|
+
|
|
7
|
+
/** A transition. A bare target string is shorthand for `{ target }`. */
|
|
8
|
+
export interface ChartTransition {
|
|
9
|
+
/** Target state id. MAY be compound/parallel (descended on entry) or history. */
|
|
10
|
+
target: string;
|
|
11
|
+
/** Named guard resolved by the caller (fail-closed if absent/unknown). */
|
|
12
|
+
guard?: string;
|
|
13
|
+
/** Ordered actions fired after the exit set and before the enter set. */
|
|
14
|
+
action?: string[];
|
|
15
|
+
/** If true, an internal transition does not exit/re-enter the source. */
|
|
16
|
+
internal?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Declarative form of a single state (per `schemas/statechart.json`). */
|
|
20
|
+
export interface ChartState {
|
|
21
|
+
parent?: string;
|
|
22
|
+
kind?: StateKind;
|
|
23
|
+
parallel?: boolean;
|
|
24
|
+
initial?: string;
|
|
25
|
+
history?: "shallow" | "deep";
|
|
26
|
+
default?: string;
|
|
27
|
+
on?: Record<string, string | ChartTransition>;
|
|
28
|
+
entry?: string[];
|
|
29
|
+
exit?: string[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The declarative chart object. */
|
|
33
|
+
export interface ChartJson {
|
|
34
|
+
initial: string;
|
|
35
|
+
context?: unknown;
|
|
36
|
+
states: Record<string, ChartState>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Per-step named-guard outcomes for one `send`. */
|
|
40
|
+
export type GuardMap = Record<string, boolean>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A parsed, immutable chart definition. Built via {@link ChartDef.fromChart}.
|
|
44
|
+
*
|
|
45
|
+
* `kind` is inferred when not stated: `history` when `history` is set;
|
|
46
|
+
* `parallel` when `parallel` is true; `compound` when the state has an
|
|
47
|
+
* `initial`; otherwise `atomic`.
|
|
48
|
+
*/
|
|
49
|
+
export class ChartDef {
|
|
50
|
+
private constructor();
|
|
51
|
+
/** Parse and validate the declarative chart form. Throws on malformed
|
|
52
|
+
* charts and unsupported features (`run` actions, `{expr: …}` guards). */
|
|
53
|
+
static fromChart(value: ChartJson): ChartDef;
|
|
54
|
+
/** Structural kind of a state node (defaults to `atomic` if unknown). */
|
|
55
|
+
kind(id: string): StateKind;
|
|
56
|
+
/** `true` for active-leaf kinds (atomic / final). */
|
|
57
|
+
isLeaf(id: string): boolean;
|
|
58
|
+
/** Ancestor chain `[id, ..., root]`. */
|
|
59
|
+
ancestorsInclusive(id: string): string[];
|
|
60
|
+
/** Lowest common ancestor (inclusive) of two states; falls back to root. */
|
|
61
|
+
lca(a: string, b: string): string;
|
|
62
|
+
/** `true` iff `desc` is a proper descendant of `anc`. */
|
|
63
|
+
isProperDescendant(desc: string, anc: string): boolean;
|
|
64
|
+
/** Depth of a state (root = 0). */
|
|
65
|
+
depthOf(id: string): number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A native full-Harel state chart. lazily-js is a state-projection consumer
|
|
70
|
+
* with no reactive graph, so the active configuration is a plain `Set`.
|
|
71
|
+
*
|
|
72
|
+
* Deterministic by construction (mirroring the Lean `StateChart.send` total
|
|
73
|
+
* function): a given `(chart, history, configuration, event, guards)` yields a
|
|
74
|
+
* unique result.
|
|
75
|
+
*/
|
|
76
|
+
export class StateChart {
|
|
77
|
+
/** Enter the initial configuration by descending from the root; records
|
|
78
|
+
* initial entry actions in {@link lastActions}. */
|
|
79
|
+
constructor(def: ChartDef);
|
|
80
|
+
/** Ordered action names fired by initial entry or the most recent `send`
|
|
81
|
+
* (exit → transition → entry). Empty after a rejected event. */
|
|
82
|
+
lastActions(): string[];
|
|
83
|
+
/** Full active configuration (leaves plus all active ancestors), sorted. */
|
|
84
|
+
configuration(): string[];
|
|
85
|
+
/** Active atomic leaves, sorted (one per parallel region). */
|
|
86
|
+
activeLeaves(): string[];
|
|
87
|
+
/** Hierarchical "state-in" predicate: `true` iff `id` is active. */
|
|
88
|
+
matches(id: string): boolean;
|
|
89
|
+
/** Run-to-completion transition. Returns `true` if any transition was taken,
|
|
90
|
+
* `false` if rejected (configuration unchanged, no actions fired). */
|
|
91
|
+
send(event: string, guards?: GuardMap): boolean;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A single transition, built in TypeScript. Equivalent to a JSON transition
|
|
96
|
+
* object; the typed builder path parallel to the declarative form.
|
|
97
|
+
*/
|
|
98
|
+
export class TransitionBuilder {
|
|
99
|
+
private constructor();
|
|
100
|
+
/** An external transition to `target` with no guard or actions. */
|
|
101
|
+
static to(target: string): TransitionBuilder;
|
|
102
|
+
/** Attach a named boolean guard (resolved at send time; absent → false). */
|
|
103
|
+
guard(name: string): TransitionBuilder;
|
|
104
|
+
/** Append a transition action name. */
|
|
105
|
+
action(name: string): TransitionBuilder;
|
|
106
|
+
/** Mark this transition internal (no exit/re-entry when target is the source
|
|
107
|
+
* or a proper descendant). */
|
|
108
|
+
internal(): TransitionBuilder;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A single chart state, built in TypeScript. Mirrors the JSON state object. */
|
|
112
|
+
export class StateBuilder {
|
|
113
|
+
private constructor();
|
|
114
|
+
/** An atomic leaf state. */
|
|
115
|
+
static atomic(id: string): StateBuilder;
|
|
116
|
+
/** A compound state with the given initial child. */
|
|
117
|
+
static compound(id: string, initial: string): StateBuilder;
|
|
118
|
+
/** A parallel (orthogonal) state; all child regions entered together. */
|
|
119
|
+
static parallel(id: string): StateBuilder;
|
|
120
|
+
/** A final leaf state (raises no completion event). */
|
|
121
|
+
static final(id: string): StateBuilder;
|
|
122
|
+
/** A shallow-history pseudostate for its parent region. */
|
|
123
|
+
static historyShallow(id: string): StateBuilder;
|
|
124
|
+
/** A deep-history pseudostate for its parent region. */
|
|
125
|
+
static historyDeep(id: string): StateBuilder;
|
|
126
|
+
/** Set the parent state id (omit only for the single chart root). */
|
|
127
|
+
parent(parent: string): StateBuilder;
|
|
128
|
+
/** Default target used on a history pseudostate's first entry. */
|
|
129
|
+
defaultChild(target: string): StateBuilder;
|
|
130
|
+
/** Append an entry action name. */
|
|
131
|
+
entry(action: string): StateBuilder;
|
|
132
|
+
/** Append an exit action name. */
|
|
133
|
+
exit(action: string): StateBuilder;
|
|
134
|
+
/** Add an unguarded external transition on `event` to `target`. */
|
|
135
|
+
on(event: string, target: string): StateBuilder;
|
|
136
|
+
/** Add a guarded external transition on `event` to `target`. */
|
|
137
|
+
onGuarded(event: string, target: string, guard: string): StateBuilder;
|
|
138
|
+
/** Add a fully-specified transition on `event`. */
|
|
139
|
+
onTransition(event: string, transition: TransitionBuilder): StateBuilder;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Fluent builder assembling a {@link ChartDef} from typed TS states — the
|
|
144
|
+
* definition path parallel to {@link ChartDef.fromChart}. State insertion order
|
|
145
|
+
* fixes deterministic parallel-region descent, exactly as JSON key order does.
|
|
146
|
+
*/
|
|
147
|
+
export class ChartBuilder {
|
|
148
|
+
constructor();
|
|
149
|
+
/** Add a state. The first parent-less state added becomes the root. */
|
|
150
|
+
state(state: StateBuilder): ChartBuilder;
|
|
151
|
+
/** Validate and assemble the {@link ChartDef}. Throws on a duplicate state id
|
|
152
|
+
* or on zero / more than one parent-less root. */
|
|
153
|
+
build(): ChartDef;
|
|
154
|
+
}
|