@overmvp/kumi 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.
Files changed (205) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +14 -2
  3. package/animation/color-transition.ts +196 -0
  4. package/animation/cycle.ts +151 -0
  5. package/animation/ease.ts +29 -0
  6. package/animation/index.ts +13 -0
  7. package/animation/package.json +12 -0
  8. package/animation/presets.ts +376 -0
  9. package/animation/spec.ts +197 -0
  10. package/builder/Cargo.lock +192 -0
  11. package/builder/Cargo.toml +7 -0
  12. package/builder/css-v1.mjs +334 -0
  13. package/builder/css-v1.test.mjs +321 -0
  14. package/builder/generated-output.mjs +61 -0
  15. package/builder/generated-output.test.mjs +132 -0
  16. package/builder/jsx-form.mjs +93 -0
  17. package/builder/jsx-form.test.mjs +48 -0
  18. package/builder/jsx-lists.mjs +209 -0
  19. package/builder/link-addon-package.mjs +86 -0
  20. package/builder/lua-assets.mjs +74 -0
  21. package/builder/lua-builtin-values.mjs +57 -0
  22. package/builder/lua-builtin-values.test.mjs +155 -0
  23. package/builder/lua-loop-closures.mjs +183 -0
  24. package/builder/lua-loop-closures.test.mjs +89 -0
  25. package/builder/lua-truthiness.mjs +61 -0
  26. package/builder/lua-truthiness.test.mjs +113 -0
  27. package/builder/module-declarations.mjs +594 -0
  28. package/builder/module-declarations.test.mjs +444 -0
  29. package/builder/native-kernels.mjs +60 -0
  30. package/builder/native-lua.d.ts +57 -0
  31. package/builder/native-routes.mjs +51 -0
  32. package/builder/no-static-ref-read.mjs +93 -0
  33. package/builder/no-static-ref-read.test.mjs +181 -0
  34. package/builder/secure-snippet.mjs +317 -0
  35. package/builder/secure-snippet.test.mjs +101 -0
  36. package/builder/shared-lua-runtime.mjs +150 -0
  37. package/builder/src/addon-link.lua +29 -0
  38. package/builder/src/arrays.lua +260 -0
  39. package/builder/src/assets.rs +177 -0
  40. package/builder/src/classes.lua +75 -0
  41. package/builder/src/collections.lua +190 -0
  42. package/builder/src/compiler.rs +2164 -0
  43. package/builder/src/config.rs +42 -0
  44. package/builder/src/entries.rs +166 -0
  45. package/builder/src/errors.lua +15 -0
  46. package/builder/src/lua_data.rs +231 -0
  47. package/builder/src/main.rs +367 -0
  48. package/builder/src/native_ir.rs +65 -0
  49. package/builder/src/native_signal.rs +47 -0
  50. package/builder/src/numbers.lua +62 -0
  51. package/builder/src/project-front.mjs +3331 -0
  52. package/builder/src/records.lua +75 -0
  53. package/builder/src/routes.rs +127 -0
  54. package/builder/src/signal-kernel.fingerprint +1 -0
  55. package/builder/src/strings.lua +103 -0
  56. package/builder/src/ts-lower.mjs +68 -0
  57. package/builder/src/wow_client.rs +253 -0
  58. package/builder/src/wow_types.rs +564 -0
  59. package/builder/style-states.mjs +100 -0
  60. package/builder/style-states.test.mjs +103 -0
  61. package/builder/vite-assets.mjs +53 -0
  62. package/builder/vite-project.mjs +10 -0
  63. package/config.ts +17 -0
  64. package/form/index.ts +522 -0
  65. package/form/package.json +13 -0
  66. package/motion/index.ts +2 -0
  67. package/motion/package.json +10 -0
  68. package/package.json +22 -4
  69. package/router/index.ts +43 -0
  70. package/router/package.json +12 -0
  71. package/router/routes.ts +64 -0
  72. package/scripts/kumi-cli.mjs +70 -0
  73. package/scripts/link-addons.mjs +51 -0
  74. package/src/bindings.ts +35 -0
  75. package/src/context.ts +15 -0
  76. package/src/css-tokens.ts +234 -0
  77. package/src/css-v1.ts +58 -0
  78. package/src/cvars.ts +91 -0
  79. package/src/element-node.ts +32 -0
  80. package/src/elements.ts +479 -0
  81. package/src/feature-scope.ts +82 -0
  82. package/src/flex-layout.ts +807 -0
  83. package/src/frame-scheduler.ts +334 -0
  84. package/src/grid-tracks.ts +165 -0
  85. package/src/image-imports.d.ts +24 -0
  86. package/src/images.ts +79 -0
  87. package/src/index.ts +28 -0
  88. package/src/jsx-dev-runtime.ts +2 -0
  89. package/src/jsx-runtime.ts +22 -0
  90. package/src/jsx.d.ts +12 -0
  91. package/src/keyed-list.ts +363 -0
  92. package/src/media-query.ts +144 -0
  93. package/src/module.ts +277 -0
  94. package/src/node-selectors.ts +114 -0
  95. package/src/owned-collections.ts +44 -0
  96. package/src/providers.ts +235 -0
  97. package/src/reactive-scope.ts +34 -0
  98. package/src/reactivity.ts +254 -0
  99. package/src/result.ts +58 -0
  100. package/src/runtime.ts +52 -0
  101. package/src/scheduler.ts +185 -0
  102. package/src/signal-kernel.ts +30 -0
  103. package/src/style-classes.ts +51 -0
  104. package/src/style-states.ts +134 -0
  105. package/src/style-variables.ts +53 -0
  106. package/src/text-children.ts +42 -0
  107. package/src/theme.ts +50 -0
  108. package/src/transition.ts +18 -0
  109. package/state/index.ts +3 -0
  110. package/state/package.json +12 -0
  111. package/state/store-bindings.ts +25 -0
  112. package/state/store.ts +82 -0
  113. package/styles/_layers.scss +14 -0
  114. package/templates/addon/README.md +14 -0
  115. package/templates/addon/__ADDON_NAME__.toc +4 -0
  116. package/templates/addon/eslint.config.mjs +12 -0
  117. package/templates/addon/gitignore +8 -0
  118. package/templates/addon/package.json +21 -0
  119. package/templates/addon/scripts/build.mjs +25 -0
  120. package/templates/addon/src/app/main.tsx +13 -0
  121. package/templates/addon/tsconfig.json +24 -0
  122. package/translate/index.ts +14 -0
  123. package/translate/package.json +9 -0
  124. package/utils/colors.ts +76 -0
  125. package/utils/format.ts +37 -0
  126. package/utils/index.ts +4 -0
  127. package/utils/number.ts +17 -0
  128. package/utils/package.json +7 -0
  129. package/utils/text.ts +6 -0
  130. package/validate/index.ts +119 -0
  131. package/validate/package.json +9 -0
  132. package/wow/absolute-position.ts +142 -0
  133. package/wow/animation.ts +428 -0
  134. package/wow/aura-button.ts +76 -0
  135. package/wow/aura-container.ts +591 -0
  136. package/wow/aura-countdown.ts +201 -0
  137. package/wow/aura-slot.ts +153 -0
  138. package/wow/bar-shade.ts +93 -0
  139. package/wow/borrowed-slot.ts +216 -0
  140. package/wow/button-drag.ts +48 -0
  141. package/wow/cast-clock.ts +153 -0
  142. package/wow/cast-text.ts +83 -0
  143. package/wow/class-colors.ts +126 -0
  144. package/wow/clock.ts +7 -0
  145. package/wow/color-picker.ts +128 -0
  146. package/wow/commands.ts +17 -0
  147. package/wow/contained-frame.ts +128 -0
  148. package/wow/contract.ts +42 -0
  149. package/wow/css-box.ts +110 -0
  150. package/wow/css-color.ts +12 -0
  151. package/wow/css-style.ts +109 -0
  152. package/wow/css-surfaces.ts +398 -0
  153. package/wow/css-text.ts +399 -0
  154. package/wow/cvar-schema.ts +32 -0
  155. package/wow/cvars.ts +98 -0
  156. package/wow/dropdown-art.ts +39 -0
  157. package/wow/element-click.ts +20 -0
  158. package/wow/element-event-coordinator.ts +219 -0
  159. package/wow/element-events.ts +317 -0
  160. package/wow/event-dispatcher.ts +151 -0
  161. package/wow/events.ts +50 -0
  162. package/wow/font-cache.ts +92 -0
  163. package/wow/frame-access.ts +31 -0
  164. package/wow/frame-layer.ts +5 -0
  165. package/wow/frames.ts +236 -0
  166. package/wow/heal-prediction.ts +305 -0
  167. package/wow/image-layers.ts +55 -0
  168. package/wow/index.ts +97 -0
  169. package/wow/layout-view.ts +651 -0
  170. package/wow/lua-string.ts +18 -0
  171. package/wow/lua-table.ts +18 -0
  172. package/wow/macro-book.ts +84 -0
  173. package/wow/map-position.ts +27 -0
  174. package/wow/media-query.ts +65 -0
  175. package/wow/module-events.ts +11 -0
  176. package/wow/native-hooks.ts +168 -0
  177. package/wow/native-placement.ts +88 -0
  178. package/wow/native.ts +135 -0
  179. package/wow/number-format.ts +8 -0
  180. package/wow/number-input.ts +10 -0
  181. package/wow/protect.ts +24 -0
  182. package/wow/protected-layout.ts +133 -0
  183. package/wow/range-art.ts +74 -0
  184. package/wow/reparent-frame.ts +99 -0
  185. package/wow/rounded-texture.ts +156 -0
  186. package/wow/saved-variables.ts +60 -0
  187. package/wow/scroll-view.ts +64 -0
  188. package/wow/secret-ref.ts +14 -0
  189. package/wow/secure-bindings.ts +89 -0
  190. package/wow/secure-clicks.ts +432 -0
  191. package/wow/secure-snippet.ts +235 -0
  192. package/wow/secure-state.ts +130 -0
  193. package/wow/secure-target-visibility.ts +89 -0
  194. package/wow/secure-transaction.ts +331 -0
  195. package/wow/special-window.ts +269 -0
  196. package/wow/start-addon.ts +99 -0
  197. package/wow/state-paint.ts +256 -0
  198. package/wow/transition.ts +86 -0
  199. package/wow/unit-button.ts +39 -0
  200. package/wow/unit-cast.ts +92 -0
  201. package/wow/unit-health-text.ts +47 -0
  202. package/wow/view-nodes.ts +304 -0
  203. package/wow/view.ts +1746 -0
  204. package/wow/visible-frame-job.ts +34 -0
  205. package/wow/width-reserve.ts +108 -0
@@ -0,0 +1,34 @@
1
+ import { createContext } from "./context";
2
+ import { ref } from "./reactivity";
3
+ import { fail, type Result } from "./result";
4
+ import type { RuntimeScope } from "./runtime";
5
+
6
+ const context = createContext<RuntimeScope>();
7
+ const missingScope = fail("Reactive subscriptions require an active scope");
8
+
9
+ export function withReactiveScope<Output>(scope: RuntimeScope, setup: (this: void) => Output) {
10
+ return context.run(scope, setup, scope.protect);
11
+ }
12
+
13
+ /** The scope of the component being set up, if any. Lets APIs bind their lifetime without an explicit scope. */
14
+ export function currentScope(): RuntimeScope | undefined {
15
+ return context.use();
16
+ }
17
+
18
+ function missingComputationScope(operation: "computed" | "computedResult", name?: string) {
19
+ return name === undefined || name === "" ? missingScope : fail(`${operation} ${name}: ${missingScope.reason}`);
20
+ }
21
+
22
+ export function computed<Value>(read: (this: void) => Value, name?: string) {
23
+ return context.use()?.computed(read, name) ?? ref<Result<Value>>(missingComputationScope("computed", name));
24
+ }
25
+
26
+ export function computedResult<Value>(read: (this: void) => Result<Value>, name?: string) {
27
+ return context.use()?.computedResult(read, name) ?? ref<Result<Value>>(missingComputationScope("computedResult", name));
28
+ }
29
+
30
+ export const watch: RuntimeScope["watch"] = (read, change, options) =>
31
+ context.use()?.watch(read, change, options) ?? missingScope;
32
+
33
+ export const watchEffect: RuntimeScope["watchEffect"] = effect =>
34
+ context.use()?.watchEffect(effect) ?? missingScope;
@@ -0,0 +1,254 @@
1
+ import type { Cancel, Job, Scheduler } from "./scheduler";
2
+ import { fail, ok, type Failure, type Protect, type Result } from "./result";
3
+ import { createSignalFactory } from "./signal-kernel";
4
+ import { ownedArray, ownedSet, type OwnedArray, type OwnedSet } from "./owned-collections";
5
+
6
+ export interface ReadonlyRef<T> {
7
+ readonly value: T;
8
+ }
9
+
10
+ export interface Ref<T> extends ReadonlyRef<T> {
11
+ value: T;
12
+ }
13
+
14
+ /** A pure comparison, or false to invalidate on every write or tracked read. */
15
+ export type Equality<T> = false | ((this: void, previous: T, next: T) => boolean);
16
+
17
+ export interface RefOptions<T> {
18
+ equals?: Equality<T>;
19
+ }
20
+
21
+ export interface WatchOptions<T> {
22
+ immediate?: boolean;
23
+ equals?: Equality<T>;
24
+ }
25
+
26
+ interface Dependency {
27
+ readonly id: number;
28
+ readonly observers: OwnedSet<Observer>;
29
+ }
30
+ type Cleanup = (this: void) => unknown;
31
+ interface Observer {
32
+ dependencies: OwnedArray<Dependency>;
33
+ seen: Record<number, number | undefined>;
34
+ revision: number;
35
+ invalidate: Job;
36
+ }
37
+
38
+ let active: Observer | undefined;
39
+ let lastDependencyId = 0;
40
+ const computations = ownedArray<{ readonly name?: string }>();
41
+ const closed = fail("The reactive scope has been disposed");
42
+
43
+ function createDependency() {
44
+ return { id: ++lastDependencyId, observers: ownedSet<Observer>() };
45
+ }
46
+
47
+ function circularDependency(target: { readonly name?: string }): Failure {
48
+ let path = "Circular computed dependency: ";
49
+ let inCycle = false;
50
+ for (const computation of computations) {
51
+ if (computation === target) inCycle = true;
52
+ if (inCycle) path = path + (computation.name ?? "<computed>") + " -> ";
53
+ }
54
+ return fail(path + (target.name ?? "<computed>"));
55
+ }
56
+
57
+ function createObserver(invalidate: Job): Observer {
58
+ return { dependencies: ownedArray(), seen: {}, revision: 0, invalidate };
59
+ }
60
+
61
+ function track(dependency: Dependency) {
62
+ const observer = active;
63
+ if (!observer) return;
64
+ const { seen, revision } = observer;
65
+ const id = dependency.id;
66
+ const visited = seen[id];
67
+ if (visited === revision) return;
68
+ if (visited === undefined) {
69
+ dependency.observers.add(observer);
70
+ observer.dependencies.push(dependency);
71
+ }
72
+ seen[id] = revision;
73
+ }
74
+
75
+ function notify(dependency: Dependency) {
76
+ // Invalidation marks computed values dirty and queues observers. Reads collect dependencies later.
77
+ for (const observer of dependency.observers) observer.invalidate();
78
+ }
79
+
80
+ function unlink(observer: Observer) {
81
+ for (const dependency of observer.dependencies) dependency.observers.delete(observer);
82
+ observer.dependencies.length = 0;
83
+ observer.seen = {};
84
+ }
85
+
86
+ function collect<T>(observer: Observer, read: (this: void) => T, protect: Protect): Result<T> {
87
+ const previous = active;
88
+ active = observer;
89
+ const revision = ++observer.revision;
90
+ const result = protect(read);
91
+ active = previous;
92
+ // Keep stable subscriptions and remove only dependencies the latest run stopped reading.
93
+ const { dependencies, seen } = observer;
94
+ let retained = 0;
95
+ for (const dependency of dependencies) {
96
+ const id = dependency.id;
97
+ if (seen[id] === revision) dependencies[retained++] = dependency;
98
+ else {
99
+ dependency.observers.delete(observer);
100
+ delete seen[id];
101
+ }
102
+ }
103
+ dependencies.length = retained;
104
+ return result;
105
+ }
106
+
107
+ const newSignal = createSignalFactory(track, notify, createDependency);
108
+
109
+ export function ref<T>(value: T, options?: RefOptions<T>): Ref<T> {
110
+ return newSignal(value, options?.equals);
111
+ }
112
+
113
+ class Computed<T> implements ReadonlyRef<Result<T>> {
114
+ private readonly observers = createDependency();
115
+ private readonly observer = createObserver(() => {
116
+ if (this.dirty || this.stopped) return;
117
+ this.dirty = true;
118
+ notify(this.observers);
119
+ });
120
+ private dirty = true;
121
+ private evaluating = false;
122
+ private stopped = false;
123
+ private cached: Result<T> = closed;
124
+
125
+ constructor(private readonly read: (this: void) => Result<T>, private readonly protect: Protect, readonly name?: string) {}
126
+
127
+ get value(): Result<T> {
128
+ // Keep even the closing edge so either side of a failed cycle can invalidate it.
129
+ if (this.stopped) return closed;
130
+ track(this.observers);
131
+ if (this.evaluating) return circularDependency(this);
132
+ if (this.dirty) this.evaluate();
133
+ return this.cached;
134
+ }
135
+
136
+ private evaluate() {
137
+ this.dirty = false;
138
+ this.evaluating = true;
139
+ computations.push(this);
140
+ const result = collect(this.observer, this.read, this.protect);
141
+ this.cached = this.stopped ? closed : result.ok ? result.value : result;
142
+ computations.pop();
143
+ this.evaluating = false;
144
+ if (this.stopped) unlink(this.observer);
145
+ }
146
+
147
+ stop() {
148
+ if (this.stopped) return;
149
+ this.stopped = true;
150
+ this.cached = closed;
151
+ unlink(this.observer);
152
+ notify(this.observers);
153
+ this.observers.observers.clear();
154
+ }
155
+ }
156
+
157
+ function cleanupFailure(value: unknown): Failure | undefined {
158
+ if (value == null || typeof value !== "object") return;
159
+ const candidate = value as Partial<Failure>;
160
+ if (candidate.ok !== false || typeof candidate.reason !== "string") return;
161
+ return candidate as Failure;
162
+ }
163
+
164
+ export function createScope(scheduler: Scheduler) {
165
+ const { protect } = scheduler;
166
+ const cleanup = ownedSet<Cleanup>();
167
+ let disposed = false;
168
+
169
+ function onDispose(stop: Cleanup) {
170
+ if (disposed) return closed;
171
+ cleanup.add(stop);
172
+ return ok(() => cleanup.delete(stop));
173
+ }
174
+
175
+ function computedResult<T>(read: (this: void) => Result<T>, name?: string): ReadonlyRef<Result<T>> {
176
+ if (disposed) return ref<Result<T>>(closed);
177
+ const value = new Computed(read, protect, name);
178
+ cleanup.add(() => value.stop());
179
+ return value;
180
+ }
181
+
182
+ function computed<T>(read: (this: void) => T, name?: string): ReadonlyRef<Result<T>> {
183
+ return computedResult(() => ok(read()), name);
184
+ }
185
+
186
+ function watchEffect(effect: Job): Result<Cancel> {
187
+ if (disposed) return closed;
188
+ let stopped = false;
189
+ let observer: Observer;
190
+
191
+ function run() {
192
+ if (stopped) return;
193
+ const result = collect(observer, effect, protect);
194
+ if (stopped) unlink(observer);
195
+ return result.ok ? result.value : result;
196
+ }
197
+
198
+ function stop() {
199
+ stopped = true;
200
+ scheduler.cancel(run);
201
+ unlink(observer);
202
+ cleanup.delete(stop);
203
+ }
204
+
205
+ observer = createObserver(() => scheduler.queue(run));
206
+ cleanup.add(stop);
207
+ const result = run();
208
+ if (result) {
209
+ stop();
210
+ return result;
211
+ }
212
+ return ok(stop);
213
+ }
214
+
215
+ function dispose(): Result<void> {
216
+ if (disposed) return ok(undefined);
217
+ disposed = true;
218
+ let failure: Failure | undefined;
219
+ for (const stop of [...cleanup]) {
220
+ const result = protect(stop);
221
+ failure ??= result.ok ? cleanupFailure(result.value) : result;
222
+ }
223
+ cleanup.clear();
224
+ return failure ?? ok(undefined);
225
+ }
226
+
227
+ function watch<T>(read: (this: void) => T, change: (this: void, value: T, previous: T | undefined) => void | Failure,
228
+ options: boolean | WatchOptions<T> = false): Result<Cancel> {
229
+ const immediate = typeof options === "boolean" ? options : options.immediate;
230
+ const equals = typeof options === "boolean" ? undefined : options.equals;
231
+ let initialized = false;
232
+ let previous: T | undefined;
233
+ return watchEffect(() => {
234
+ const value = read();
235
+ if (initialized) {
236
+ if (equals === undefined) {
237
+ if (value === previous) return;
238
+ } else if (equals !== false && equals(previous as T, value)) return;
239
+ }
240
+ const notifyChange = initialized || immediate;
241
+ const before = previous;
242
+ previous = value;
243
+ initialized = true;
244
+ if (!notifyChange) return;
245
+ const observer = active;
246
+ active = undefined;
247
+ const result = protect(() => change(value, before));
248
+ active = observer;
249
+ return result.ok ? result.value : result;
250
+ });
251
+ }
252
+
253
+ return { computed, computedResult, watchEffect, watch, onDispose, dispose, protect, isActive: () => !disposed };
254
+ }
package/src/result.ts ADDED
@@ -0,0 +1,58 @@
1
+ export interface Failure {
2
+ readonly ok: false;
3
+ readonly reason: string;
4
+ }
5
+
6
+ export interface Success<Value> {
7
+ readonly ok: true;
8
+ readonly value: Value;
9
+ }
10
+
11
+ export type Result<Value> = Success<Value> | Failure;
12
+
13
+ export function ok<Value>(value: Value): Success<Value> {
14
+ return { ok: true, value };
15
+ }
16
+
17
+ export function fail(reason: string): Failure {
18
+ return { ok: false, reason };
19
+ }
20
+
21
+ export type Protect = <Value>(this: void, run: (this: void) => Value) => Result<Value>;
22
+
23
+ /** For callbacks which already report errors as values. Hosts may inject a protected boundary. */
24
+ export function direct<Value>(run: (this: void) => Value): Result<Value> {
25
+ return ok(run());
26
+ }
27
+
28
+ export function chain<A, B>(next: (this: void, value: A) => Result<B>) {
29
+ return (result: Result<A>): Result<B> => result.ok ? next(result.value) : result;
30
+ }
31
+
32
+ export function map<A, B>(transform: (this: void, value: A) => B) {
33
+ return (result: Result<A>): Result<B> => result.ok ? ok(transform(result.value)) : result;
34
+ }
35
+
36
+ export function fold<A, B, C>(onFailure: (this: void, reason: string) => B, onSuccess: (this: void, value: A) => C) {
37
+ return (result: Result<A>): B | C => result.ok ? onSuccess(result.value) : onFailure(result.reason);
38
+ }
39
+
40
+ type Step<A, B> = (this: void, value: A) => B;
41
+
42
+ export function pipe<A>(value: A): A;
43
+ export function pipe<A, B>(value: A, first: Step<A, B>): B;
44
+ export function pipe<A, B, C>(value: A, first: Step<A, B>, second: Step<B, C>): C;
45
+ export function pipe<A, B, C, D>(value: A, first: Step<A, B>, second: Step<B, C>, third: Step<C, D>): D;
46
+ export function pipe<A, B, C, D, E>(value: A, first: Step<A, B>, second: Step<B, C>, third: Step<C, D>,
47
+ fourth: Step<D, E>): E;
48
+ export function pipe<A, B, C, D, E, F>(value: A, first: Step<A, B>, second: Step<B, C>, third: Step<C, D>,
49
+ fourth: Step<D, E>, fifth: Step<E, F>): F;
50
+ export function pipe(value: unknown, first?: Step<never, unknown>, second?: Step<never, unknown>,
51
+ third?: Step<never, unknown>, fourth?: Step<never, unknown>, fifth?: Step<never, unknown>): unknown {
52
+ if (first) value = first(value as never);
53
+ if (second) value = second(value as never);
54
+ if (third) value = third(value as never);
55
+ if (fourth) value = fourth(value as never);
56
+ if (fifth) value = fifth(value as never);
57
+ return value;
58
+ }
package/src/runtime.ts ADDED
@@ -0,0 +1,52 @@
1
+ import type { FrameScheduler } from "./frame-scheduler";
2
+ import { createScope, ref, type ReadonlyRef } from "./reactivity";
3
+ import { direct, fail, type Failure, type Protect, type Result } from "./result";
4
+ import { createRoutine, createScheduler, type Clock, type Job, type RoutineStep, type SchedulerOptions } from "./scheduler";
5
+
6
+ export interface RuntimeScope extends ReturnType<typeof createScope> {
7
+ /** @internal The owning frame budget for scoped platform integrations. */
8
+ readonly frameJobs: FrameScheduler;
9
+ fork(this: void): RuntimeScope;
10
+ nextTick(this: void, job: Job): void;
11
+ createRoutine(this: void, steps: readonly RoutineStep[]): {
12
+ start(this: void): Result<void>;
13
+ cancel(this: void): void;
14
+ };
15
+ }
16
+
17
+ export function createRuntime(clock: Clock, onError?: (this: void, failure: Failure) => void, protect: Protect = direct,
18
+ options?: SchedulerOptions) {
19
+ const errors = ref<Failure | undefined>(undefined);
20
+ const scheduler = createScheduler(clock, failure => {
21
+ errors.value = failure;
22
+ onError?.(failure);
23
+ }, protect, options);
24
+
25
+ function scope(): RuntimeScope {
26
+ const reactive = createScope(scheduler);
27
+ return {
28
+ ...reactive,
29
+ frameJobs: scheduler.frames,
30
+ fork: () => {
31
+ const child = scope();
32
+ const linked = reactive.onDispose(child.dispose);
33
+ if (linked.ok) child.onDispose(linked.value);
34
+ else child.dispose();
35
+ return child;
36
+ },
37
+ nextTick: (job: Job) => scheduler.nextTick(() => { if (reactive.isActive()) return job(); }),
38
+ createRoutine: (steps: readonly RoutineStep[]) => {
39
+ const routine = createRoutine(scheduler, steps);
40
+ reactive.onDispose(routine.cancel);
41
+ return {
42
+ start: () => reactive.isActive() ? routine.start() : fail("The reactive scope has been disposed"),
43
+ cancel: routine.cancel,
44
+ };
45
+ },
46
+ };
47
+ }
48
+
49
+ return { scope, frameJobs: scheduler.frames, nextTick: scheduler.nextTick, errors: errors as ReadonlyRef<Failure | undefined> };
50
+ }
51
+
52
+ export type Runtime = ReturnType<typeof createRuntime>;
@@ -0,0 +1,185 @@
1
+ import { direct, fail, ok, type Failure, type Protect, type Result } from "./result";
2
+ import { createFrameDriver, createFrameScheduler, type FrameDriver, type FrameLimits, type FrameScheduler } from "./frame-scheduler";
3
+ import type { OwnedReadonlyArray } from "./owned-collections";
4
+
5
+ export type Cancel = (this: void) => void;
6
+ export type Job = (this: void) => void | Failure;
7
+ export type Clock = (this: void, seconds: number, job: Job) => Cancel;
8
+
9
+ export interface Scheduler {
10
+ after: Clock;
11
+ protect: Protect;
12
+ frames: FrameScheduler;
13
+ queue: (this: void, job: Job) => void;
14
+ cancel: (this: void, job: Job) => void;
15
+ /** Runs after pending reactive work and the updates it schedules. */
16
+ nextTick: (this: void, job: Job) => void;
17
+ }
18
+
19
+ export interface SchedulerOptions extends FrameLimits {
20
+ driver?: FrameDriver;
21
+ }
22
+
23
+ interface ReactiveJob {
24
+ readonly run: Job;
25
+ queued?: ReactiveRun;
26
+ }
27
+ interface ReactiveCause {
28
+ readonly effect: ReactiveJob;
29
+ readonly previous?: ReactiveCause;
30
+ readonly attempts: number;
31
+ reported?: true;
32
+ }
33
+ interface ReactiveRun {
34
+ callback: Job;
35
+ cause: ReactiveCause;
36
+ batch: ReactiveBatch;
37
+ }
38
+ interface ReactiveBatch {
39
+ readonly order: number;
40
+ pending: number;
41
+ closed?: { next: ReactiveBatch; callback: Job };
42
+ }
43
+
44
+ const reaction = { label: "Reactive update" };
45
+ const tick = { label: "Next tick" };
46
+
47
+ export function createScheduler(clock: Clock, onError?: (this: void, error: Failure) => void, protect: Protect = direct,
48
+ options: SchedulerOptions = {}): Scheduler {
49
+ const frames = createFrameScheduler(options.driver ?? createFrameDriver(clock), options, onError, protect);
50
+ const effects = new WeakMap<Job, ReactiveJob>();
51
+ let openBatch: ReactiveBatch = { order: 0, pending: 0 };
52
+ let firstBatch = openBatch;
53
+ let active: ReactiveRun | undefined;
54
+
55
+ function releaseCallbacks() {
56
+ while (firstBatch.pending === 0 && firstBatch.closed) {
57
+ const closed = firstBatch.closed;
58
+ firstBatch = closed.next;
59
+ frames.queue(closed, closed.callback, tick);
60
+ }
61
+ }
62
+
63
+ function queue(job: Job) {
64
+ let effect = effects.get(job);
65
+ if (!effect) {
66
+ let entry: ReactiveJob;
67
+ function run(this: void) {
68
+ const queued = entry.queued;
69
+ if (!queued) return;
70
+ entry.queued = undefined;
71
+ const previous = active;
72
+ active = queued;
73
+ const result = protect(queued.callback);
74
+ active = previous;
75
+ queued.batch.pending--;
76
+ releaseCallbacks();
77
+ return result.ok ? result.value : result;
78
+ }
79
+ entry = { run };
80
+ effects.set(job, entry);
81
+ effect = entry;
82
+ }
83
+ const batch = active?.batch ?? openBatch;
84
+ if (effect.queued) {
85
+ // An external invalidation refreshes the cause without moving the queued job.
86
+ if (!active && effect.queued.cause.previous) effect.queued.cause = { effect, attempts: 1 };
87
+ if (batch.order < effect.queued.batch.order) {
88
+ // An older batch now also depends on this already queued job.
89
+ effect.queued.batch.pending--;
90
+ effect.queued.batch = batch;
91
+ batch.pending++;
92
+ }
93
+ return;
94
+ }
95
+ // Count returns to this effect along the causal chain, including across frames.
96
+ let previous = active?.cause;
97
+ while (previous && previous.effect !== effect) previous = previous.previous;
98
+ if (previous?.attempts === 100) {
99
+ if (!previous.reported) {
100
+ previous.reported = true;
101
+ onError?.(fail("Reactive updates did not settle"));
102
+ }
103
+ return;
104
+ }
105
+ effect.queued = { callback: job, cause: { effect, previous: active?.cause, attempts: (previous?.attempts ?? 0) + 1 }, batch };
106
+ batch.pending++;
107
+ frames.queue(effect.run, effect.run, reaction);
108
+ }
109
+
110
+ function cancel(job: Job) {
111
+ const effect = effects.get(job);
112
+ effects.delete(job);
113
+ if (!effect?.queued) return;
114
+ effect.queued.batch.pending--;
115
+ effect.queued = undefined;
116
+ frames.cancel(effect.run);
117
+ releaseCallbacks();
118
+ }
119
+
120
+ function after(seconds: number, job: Job) {
121
+ const key = {};
122
+ const timer = clock(seconds, () => { frames.queue(key, job); });
123
+ return () => {
124
+ timer();
125
+ frames.cancel(key);
126
+ };
127
+ }
128
+
129
+ function nextTick(job: Job) {
130
+ const closed = openBatch;
131
+ openBatch = { order: closed.order + 1, pending: 0 };
132
+ closed.closed = { next: openBatch, callback: job };
133
+ releaseCallbacks();
134
+ }
135
+
136
+ return { after, protect, frames, queue, cancel, nextTick };
137
+ }
138
+
139
+ export type RoutineStep = Job | { wait: number };
140
+
141
+ export function wait(seconds: number): RoutineStep {
142
+ return { wait: seconds };
143
+ }
144
+
145
+ export function createRoutine(scheduler: Scheduler, steps: readonly RoutineStep[]) {
146
+ const sequence = steps as OwnedReadonlyArray<RoutineStep>;
147
+ let cancelWait: Cancel | undefined;
148
+ let generation = 0;
149
+
150
+ function cancel() {
151
+ generation++;
152
+ cancelWait?.();
153
+ cancelWait = undefined;
154
+ }
155
+
156
+ function start(): Result<void> {
157
+ cancel();
158
+ const run = generation;
159
+ let index = 0;
160
+
161
+ function advance(): void | Failure {
162
+ if (run !== generation) return;
163
+ cancelWait = undefined;
164
+ while (run === generation && index < sequence.length) {
165
+ const step = sequence[index++];
166
+ if (typeof step === "function") {
167
+ const result = step();
168
+ if (result) return result;
169
+ } else {
170
+ cancelWait = scheduler.after(step.wait, advance);
171
+ return;
172
+ }
173
+ }
174
+ }
175
+
176
+ const result = scheduler.protect(advance);
177
+ if (!result.ok) {
178
+ cancel();
179
+ return result;
180
+ }
181
+ return result.value ?? ok(undefined);
182
+ }
183
+
184
+ return { start, cancel };
185
+ }
@@ -0,0 +1,30 @@
1
+ import type { Equality, Ref } from "./reactivity";
2
+
3
+ /** Internal Lua codegen boundary. The owner supplies the reactive graph operations. */
4
+ export function createSignalFactory<Dependency>(
5
+ track: (this: void, dependency: Dependency) => void,
6
+ notify: (this: void, dependency: Dependency) => void,
7
+ makeDependency: (this: void) => Dependency,
8
+ ): <Value>(this: void, initial: Value, equals?: Equality<Value>) => Ref<Value> {
9
+ class Signal<Value> implements Ref<Value> {
10
+ private readonly observers = makeDependency();
11
+
12
+ constructor(private current: Value, private readonly equals: Equality<Value> | true = true) {}
13
+
14
+ get value(): Value {
15
+ track(this.observers);
16
+ return this.current;
17
+ }
18
+
19
+ set value(next: Value) {
20
+ const { current, equals } = this;
21
+ if (equals === true) {
22
+ if (next === current) return;
23
+ } else if (equals !== false && equals(current, next)) return;
24
+ this.current = next;
25
+ notify(this.observers);
26
+ }
27
+ }
28
+
29
+ return (initial, equals) => new Signal(initial, equals);
30
+ }
@@ -0,0 +1,51 @@
1
+ import type { MediaStyleVariant, NativeStyle } from "./elements";
2
+ import { mergeCssTokens } from "./css-tokens";
3
+ import { ownedArray, type OwnedArray, type OwnedReadonlyArray, type OwnedRecord } from "./owned-collections";
4
+ import type { StateStyleVariant } from "./style-states";
5
+
6
+ export interface StyleClass extends NativeStyle {
7
+ readonly className: string;
8
+ }
9
+
10
+ export type ClassNames = string | StyleClass | readonly (string | StyleClass)[];
11
+
12
+ export function resolveStyleClasses(this: void, classes: Exclude<ClassNames, string>, inline?: NativeStyle) {
13
+ const list: readonly (string | StyleClass)[] = Array.isArray(classes) ? classes : [classes as StyleClass];
14
+ const entries = list as OwnedReadonlyArray<string | StyleClass>;
15
+ const names: string[] = [];
16
+ const style: NativeStyle = {};
17
+ const states: StateStyleVariant[] = [];
18
+ let media: OwnedArray<MediaStyleVariant> | undefined;
19
+ for (const entry of entries) {
20
+ if (typeof entry === "string") {
21
+ if (entry !== "") names.push(entry);
22
+ continue;
23
+ }
24
+ const { className, stateStyles, mediaStyles, ...base } = entry as OwnedRecord<StyleClass>;
25
+ names.push(className);
26
+ const tokens = mergeCssTokens(style.styleTokens, base);
27
+ Object.assign(style, base);
28
+ style.styleTokens = tokens;
29
+ if (stateStyles !== undefined) for (const state of stateStyles as OwnedReadonlyArray<StateStyleVariant>) states.push(state);
30
+ if (mediaStyles !== undefined) {
31
+ media ??= ownedArray<MediaStyleVariant>();
32
+ for (const variant of mediaStyles as OwnedReadonlyArray<MediaStyleVariant>) media.push(variant);
33
+ }
34
+ }
35
+ if (inline !== undefined) {
36
+ const tokens = mergeCssTokens(style.styleTokens, inline);
37
+ Object.assign(style, inline as OwnedRecord<NativeStyle>);
38
+ style.styleTokens = tokens;
39
+ }
40
+ if (inline?.stateStyles !== undefined) {
41
+ for (const state of inline.stateStyles as OwnedReadonlyArray<StateStyleVariant>) states.push(state);
42
+ }
43
+ if (states.length > 0) style.stateStyles = states;
44
+ if (media !== undefined) {
45
+ if (inline?.mediaStyles !== undefined) {
46
+ for (const variant of inline.mediaStyles as OwnedReadonlyArray<MediaStyleVariant>) media.push(variant);
47
+ }
48
+ style.mediaStyles = media;
49
+ }
50
+ return { className: names.join(" "), style };
51
+ }