@overmvp/kumi 0.0.0-stage → 0.3.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 (319) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -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 +387 -0
  13. package/builder/css-v1.test.mjs +361 -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 +127 -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 +435 -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 +2336 -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 +381 -0
  48. package/builder/src/native_ir.rs +65 -0
  49. package/builder/src/native_signal.rs +83 -0
  50. package/builder/src/numbers.lua +76 -0
  51. package/builder/src/project-front.mjs +3682 -0
  52. package/builder/src/records.lua +75 -0
  53. package/builder/src/routes.rs +124 -0
  54. package/builder/src/setup-store.lua +18 -0
  55. package/builder/src/signal-kernel.fingerprint +1 -0
  56. package/builder/src/strings.lua +103 -0
  57. package/builder/src/ts-lower.mjs +68 -0
  58. package/builder/src/wow-reads.lua +22 -0
  59. package/builder/src/wow_client.rs +253 -0
  60. package/builder/src/wow_types.rs +564 -0
  61. package/builder/style-modules.mjs +93 -0
  62. package/builder/style-states.mjs +100 -0
  63. package/builder/style-states.test.mjs +103 -0
  64. package/builder/vite-assets.mjs +53 -0
  65. package/builder/vite-project.mjs +10 -0
  66. package/config.ts +59 -0
  67. package/eslint.mjs +74 -0
  68. package/form/index.ts +526 -0
  69. package/form/package.json +13 -0
  70. package/motion/index.ts +2 -0
  71. package/motion/package.json +10 -0
  72. package/package.json +26 -4
  73. package/router/index.ts +50 -0
  74. package/router/navigation.ts +22 -0
  75. package/router/package.json +12 -0
  76. package/router/router-view.ts +134 -0
  77. package/router/routes.ts +64 -0
  78. package/scripts/addon-build.mjs +364 -0
  79. package/scripts/addon-project.mjs +78 -0
  80. package/scripts/kumi-cli.mjs +105 -0
  81. package/scripts/link-addons.mjs +61 -0
  82. package/src/bindings.ts +35 -0
  83. package/src/context.ts +15 -0
  84. package/src/css-tokens.ts +239 -0
  85. package/src/css-v1.ts +68 -0
  86. package/src/cvars.ts +91 -0
  87. package/src/element-node.ts +32 -0
  88. package/src/elements.ts +505 -0
  89. package/src/feature-scope.ts +82 -0
  90. package/src/flex-layout.ts +807 -0
  91. package/src/frame-scheduler.ts +336 -0
  92. package/src/grid-tracks.ts +165 -0
  93. package/src/image-imports.d.ts +24 -0
  94. package/src/images.ts +79 -0
  95. package/src/index.ts +29 -0
  96. package/src/jsx-dev-runtime.ts +2 -0
  97. package/src/jsx-runtime.ts +29 -0
  98. package/src/jsx.d.ts +12 -0
  99. package/src/keyed-list.ts +366 -0
  100. package/src/media-query.ts +144 -0
  101. package/src/module-ui.ts +147 -0
  102. package/src/module.ts +311 -0
  103. package/src/node-selectors.ts +114 -0
  104. package/src/owned-collections.ts +52 -0
  105. package/src/providers.ts +241 -0
  106. package/src/reactive-scope.ts +34 -0
  107. package/src/reactivity.ts +275 -0
  108. package/src/result.ts +64 -0
  109. package/src/runtime.ts +52 -0
  110. package/src/scheduler.ts +185 -0
  111. package/src/signal-kernel.ts +63 -0
  112. package/src/style-classes.ts +51 -0
  113. package/src/style-states.ts +134 -0
  114. package/src/style-variables.ts +53 -0
  115. package/src/text-children.ts +42 -0
  116. package/src/theme.ts +50 -0
  117. package/src/transition.ts +18 -0
  118. package/state/index.ts +9 -0
  119. package/state/package.json +12 -0
  120. package/state/setup-store-persistence.ts +206 -0
  121. package/state/setup-store-view.ts +10 -0
  122. package/state/setup-store.ts +342 -0
  123. package/state/store-bindings.ts +25 -0
  124. package/state/store.ts +84 -0
  125. package/styles/_layers.scss +14 -0
  126. package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
  127. package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
  128. package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
  129. package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
  130. package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
  131. package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
  132. package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
  133. package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
  134. package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
  135. package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
  136. package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
  137. package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
  138. package/templates/addon/AGENTS.md +34 -0
  139. package/templates/addon/Assets/Art/Corner10.tga +0 -0
  140. package/templates/addon/Assets/Art/Corner11.tga +0 -0
  141. package/templates/addon/Assets/Art/Corner12.tga +0 -0
  142. package/templates/addon/Assets/Art/Corner13.tga +0 -0
  143. package/templates/addon/Assets/Art/Corner14.tga +0 -0
  144. package/templates/addon/Assets/Art/Corner15.tga +0 -0
  145. package/templates/addon/Assets/Art/Corner16.tga +0 -0
  146. package/templates/addon/Assets/Art/Corner2.tga +0 -0
  147. package/templates/addon/Assets/Art/Corner3.tga +0 -0
  148. package/templates/addon/Assets/Art/Corner4.tga +0 -0
  149. package/templates/addon/Assets/Art/Corner5.tga +0 -0
  150. package/templates/addon/Assets/Art/Corner6.tga +0 -0
  151. package/templates/addon/Assets/Art/Corner7.tga +0 -0
  152. package/templates/addon/Assets/Art/Corner8.tga +0 -0
  153. package/templates/addon/Assets/Art/Corner9.tga +0 -0
  154. package/templates/addon/Assets/Art/Dot.tga +0 -0
  155. package/templates/addon/Assets/Art/Gear.tga +0 -0
  156. package/templates/addon/Assets/Art/Grip.tga +0 -0
  157. package/templates/addon/Assets/Art/Outline10.tga +0 -0
  158. package/templates/addon/Assets/Art/Outline12.tga +0 -0
  159. package/templates/addon/Assets/Art/Outline13.tga +0 -0
  160. package/templates/addon/Assets/Art/Outline14.tga +0 -0
  161. package/templates/addon/Assets/Art/Outline15.tga +0 -0
  162. package/templates/addon/Assets/Art/Outline2.tga +0 -0
  163. package/templates/addon/Assets/Art/Outline3.tga +0 -0
  164. package/templates/addon/Assets/Art/Outline5.tga +0 -0
  165. package/templates/addon/Assets/Art/Outline7.tga +0 -0
  166. package/templates/addon/Assets/Art/Outline9.tga +0 -0
  167. package/templates/addon/Assets/Art/Round11.tga +0 -0
  168. package/templates/addon/Assets/Art/Round11Border.tga +0 -0
  169. package/templates/addon/Assets/Art/Round4.tga +0 -0
  170. package/templates/addon/Assets/Art/Round4Border.tga +0 -0
  171. package/templates/addon/Assets/Art/Round6.tga +0 -0
  172. package/templates/addon/Assets/Art/Round6Border.tga +0 -0
  173. package/templates/addon/Assets/Art/Round8.tga +0 -0
  174. package/templates/addon/Assets/Art/Round8Border.tga +0 -0
  175. package/templates/addon/Assets/Art/Search.tga +0 -0
  176. package/templates/addon/CLAUDE.md +1 -0
  177. package/templates/addon/README.md +49 -0
  178. package/templates/addon/eslint.config.mjs +6 -0
  179. package/templates/addon/gitignore +9 -0
  180. package/templates/addon/kumi.config.ts +17 -0
  181. package/templates/addon/package.json +20 -0
  182. package/templates/addon/src/app/app.module.ts +18 -0
  183. package/templates/addon/src/app/commands.store.ts +11 -0
  184. package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
  185. package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
  186. package/templates/addon/src/app/main.ts +5 -0
  187. package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
  188. package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
  189. package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
  190. package/templates/addon/src/app/theme.ts +6 -0
  191. package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
  192. package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
  193. package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
  194. package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
  195. package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
  196. package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
  197. package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
  198. package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
  199. package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
  200. package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
  201. package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
  202. package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
  203. package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
  204. package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
  205. package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
  206. package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
  207. package/templates/addon/src/shared/api/native-art.ts +28 -0
  208. package/templates/addon/src/shared/assets/close.png +0 -0
  209. package/templates/addon/src/shared/lib/format.ts +20 -0
  210. package/templates/addon/src/shared/styles/_tokens.scss +12 -0
  211. package/templates/addon/src/shared/ui/Button.tsx +29 -0
  212. package/templates/addon/src/shared/ui/Choice.tsx +34 -0
  213. package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
  214. package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
  215. package/templates/addon/src/shared/ui/Slider.tsx +25 -0
  216. package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
  217. package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
  218. package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
  219. package/templates/addon/tsconfig.json +63 -0
  220. package/translate/index.ts +14 -0
  221. package/translate/package.json +9 -0
  222. package/utils/colors.ts +76 -0
  223. package/utils/format.ts +37 -0
  224. package/utils/index.ts +4 -0
  225. package/utils/number.ts +17 -0
  226. package/utils/package.json +7 -0
  227. package/utils/text.ts +6 -0
  228. package/validate/index.ts +119 -0
  229. package/validate/package.json +9 -0
  230. package/wow/absolute-position.ts +142 -0
  231. package/wow/action-bar-layout.ts +108 -0
  232. package/wow/action-button.ts +219 -0
  233. package/wow/action-catalog.ts +98 -0
  234. package/wow/addon-manifest.ts +36 -0
  235. package/wow/animation.ts +428 -0
  236. package/wow/app-ui.ts +221 -0
  237. package/wow/aura-button.ts +76 -0
  238. package/wow/aura-container.ts +591 -0
  239. package/wow/aura-countdown.ts +201 -0
  240. package/wow/aura-slot.ts +153 -0
  241. package/wow/bar-shade.ts +93 -0
  242. package/wow/borrowed-slot.ts +216 -0
  243. package/wow/button-drag.ts +48 -0
  244. package/wow/cast-clock.ts +153 -0
  245. package/wow/cast-text.ts +83 -0
  246. package/wow/class-colors.ts +126 -0
  247. package/wow/clock.ts +7 -0
  248. package/wow/color-picker.ts +128 -0
  249. package/wow/commands.ts +17 -0
  250. package/wow/contained-frame.ts +128 -0
  251. package/wow/contract.ts +42 -0
  252. package/wow/cooldown-timing.ts +24 -0
  253. package/wow/css-box.ts +144 -0
  254. package/wow/css-color.ts +12 -0
  255. package/wow/css-style.ts +109 -0
  256. package/wow/css-surfaces.ts +603 -0
  257. package/wow/css-text.ts +399 -0
  258. package/wow/cursor.ts +45 -0
  259. package/wow/cvar-schema.ts +32 -0
  260. package/wow/cvars.ts +98 -0
  261. package/wow/dropdown-art.ts +39 -0
  262. package/wow/element-click.ts +20 -0
  263. package/wow/element-event-coordinator.ts +219 -0
  264. package/wow/element-events.ts +317 -0
  265. package/wow/event-dispatcher.ts +151 -0
  266. package/wow/events.ts +50 -0
  267. package/wow/font-cache.ts +92 -0
  268. package/wow/frame-access.ts +53 -0
  269. package/wow/frame-layer.ts +5 -0
  270. package/wow/frame-timing.ts +13 -0
  271. package/wow/frames.ts +236 -0
  272. package/wow/heal-prediction.ts +305 -0
  273. package/wow/hooks.ts +133 -0
  274. package/wow/image-layers.ts +55 -0
  275. package/wow/index.ts +111 -0
  276. package/wow/layout-view.ts +663 -0
  277. package/wow/locale-font.ts +15 -0
  278. package/wow/lua-string.ts +18 -0
  279. package/wow/lua-table.ts +18 -0
  280. package/wow/macro-book.ts +84 -0
  281. package/wow/map-position.ts +27 -0
  282. package/wow/media-query.ts +65 -0
  283. package/wow/module-events.ts +11 -0
  284. package/wow/native-hooks.ts +168 -0
  285. package/wow/native-placement.ts +88 -0
  286. package/wow/native.ts +153 -0
  287. package/wow/number-format.ts +8 -0
  288. package/wow/number-input.ts +10 -0
  289. package/wow/panel-host.ts +97 -0
  290. package/wow/pixel.ts +35 -0
  291. package/wow/plugins.ts +246 -0
  292. package/wow/protect.ts +48 -0
  293. package/wow/protected-layout.ts +133 -0
  294. package/wow/protected-view.ts +103 -0
  295. package/wow/range-art.ts +81 -0
  296. package/wow/readable-api.ts +120 -0
  297. package/wow/reparent-frame.ts +99 -0
  298. package/wow/rounded-texture.ts +183 -0
  299. package/wow/saved-variables.ts +109 -0
  300. package/wow/scroll-view.ts +64 -0
  301. package/wow/secret-ref.ts +14 -0
  302. package/wow/secure-bindings.ts +89 -0
  303. package/wow/secure-clicks.ts +432 -0
  304. package/wow/secure-snippet.ts +235 -0
  305. package/wow/secure-state.ts +130 -0
  306. package/wow/secure-target-visibility.ts +89 -0
  307. package/wow/secure-transaction.ts +331 -0
  308. package/wow/special-window.ts +269 -0
  309. package/wow/spell-cooldown.ts +36 -0
  310. package/wow/start-addon.ts +122 -0
  311. package/wow/state-paint.ts +256 -0
  312. package/wow/transition.ts +86 -0
  313. package/wow/unit-button.ts +39 -0
  314. package/wow/unit-cast.ts +92 -0
  315. package/wow/unit-health-text.ts +47 -0
  316. package/wow/view-nodes.ts +304 -0
  317. package/wow/view.ts +1864 -0
  318. package/wow/visible-frame-job.ts +34 -0
  319. package/wow/width-reserve.ts +108 -0
@@ -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,63 @@
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
+ ): {
9
+ create<Value>(this: void, initial: Value, equals?: Equality<Value>): Ref<Value>;
10
+ owns(this: void, value: unknown): boolean;
11
+ guard<Value>(this: void, signal: Ref<Value>, before: (this: void, next: Value) => void):
12
+ { peek(this: void): Value; detach(this: void): void } | undefined;
13
+ } {
14
+ class Signal<Value> implements Ref<Value> {
15
+ private readonly observers = makeDependency();
16
+
17
+ constructor(private current: Value, private readonly equals: Equality<Value> | true = true) {}
18
+
19
+ get value(): Value {
20
+ track(this.observers);
21
+ return this.current;
22
+ }
23
+
24
+ set value(next: Value) {
25
+ const { current, equals } = this;
26
+ if (equals === true) {
27
+ if (next === current) return;
28
+ } else if (equals !== false && equals(current, next)) return;
29
+ this.current = next;
30
+ notify(this.observers);
31
+ }
32
+
33
+ guard(before: (this: void, next: Value) => void) {
34
+ if (Object.getOwnPropertyDescriptor(this, "value") !== undefined) return undefined;
35
+ Object.defineProperty(this, "value", {
36
+ configurable: true,
37
+ get: () => {
38
+ track(this.observers);
39
+ return this.current;
40
+ },
41
+ set: (next: Value) => {
42
+ const { current, equals } = this;
43
+ if (equals === true) {
44
+ if (next === current) return;
45
+ } else if (equals !== false && equals(current, next)) return;
46
+ before(next);
47
+ this.current = next;
48
+ notify(this.observers);
49
+ },
50
+ });
51
+ return {
52
+ peek: () => this.current,
53
+ detach: () => { delete (this as { value?: Value }).value; },
54
+ };
55
+ }
56
+ }
57
+
58
+ return {
59
+ create: (initial, equals) => new Signal(initial, equals),
60
+ owns: value => value instanceof Signal,
61
+ guard: (signal, before) => signal instanceof Signal ? signal.guard(before) : undefined,
62
+ };
63
+ }
@@ -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
+ }
@@ -0,0 +1,134 @@
1
+ import type { CssTokenRecipe } from "./css-v1";
2
+ import { mergeCssTokens } from "./css-tokens";
3
+ import type { OwnedArray, OwnedReadonlyArray } from "./owned-collections";
4
+
5
+ export type StyleState = "hover" | "active" | "focus" | "disabled" | "checked";
6
+
7
+ export interface StatePaint {
8
+ color?: string;
9
+ backgroundColor?: string;
10
+ borderColor?: string;
11
+ opacity?: number;
12
+ backgroundImage?: Readonly<{ path: string } | { atlas: string }>;
13
+ styleTokens?: readonly CssTokenRecipe[];
14
+ }
15
+
16
+ export interface StateStyleVariant {
17
+ readonly when: readonly StyleState[];
18
+ readonly paint: Readonly<StatePaint>;
19
+ }
20
+
21
+ export interface PreparedStyleStates {
22
+ /** Complete cached paint for the current flags. Treat the returned snapshot as immutable. */
23
+ current(this: void): Readonly<StatePaint>;
24
+ /** Whether a state appears in any variant and needs to be observed by the host. */
25
+ needs(this: void, state: StyleState): boolean;
26
+ isActive(this: void, state: StyleState): boolean;
27
+ /** Update a flag and return the new cached snapshot only if its paint changed. */
28
+ set(this: void, state: StyleState, on: boolean): Readonly<StatePaint> | undefined;
29
+ }
30
+
31
+ interface StyleRule {
32
+ readonly mask: number;
33
+ readonly paint: Readonly<StatePaint>;
34
+ readonly order: number;
35
+ readonly priority: number;
36
+ }
37
+
38
+ const bits: Readonly<Record<StyleState, number>> = { hover: 1, active: 2, focus: 4, disabled: 8, checked: 16 };
39
+
40
+ function has(mask: number, bit: number) {
41
+ // Use arithmetic masks on Lua 5.1.
42
+ return mask % (bit * 2) >= bit;
43
+ }
44
+
45
+ function matches(required: number, active: number) {
46
+ while (required !== 0) {
47
+ if (required % 2 === 1 && active % 2 === 0) return false;
48
+ required = Math.floor(required / 2);
49
+ active = Math.floor(active / 2);
50
+ }
51
+ return true;
52
+ }
53
+
54
+ function sameImage(left: StatePaint["backgroundImage"], right: StatePaint["backgroundImage"]) {
55
+ if (left === right) return true;
56
+ if (left === undefined || right === undefined) return false;
57
+ return "path" in left
58
+ ? "path" in right && left.path === right.path
59
+ : "atlas" in right && left.atlas === right.atlas;
60
+ }
61
+
62
+ function sameTokens(left: StatePaint["styleTokens"], right: StatePaint["styleTokens"]) {
63
+ if (left === right) return true;
64
+ if (left === undefined || right === undefined) return false;
65
+ const before = left as OwnedReadonlyArray<CssTokenRecipe>;
66
+ const after = right as OwnedReadonlyArray<CssTokenRecipe>;
67
+ return before.length === after.length && before.every((recipe, index) => recipe === after[index]);
68
+ }
69
+
70
+ /** Compile state conditions once. More conditions win, then disabled, then author order. */
71
+ export function prepareStyleStates(this: void, base: Readonly<StatePaint>, variants: readonly StateStyleVariant[]): PreparedStyleStates {
72
+ let needed = 0;
73
+ const rules = (variants as OwnedReadonlyArray<StateStyleVariant>).map((variant, order) => {
74
+ const { when, paint } = variant;
75
+ let mask = 0;
76
+ let count = 0;
77
+ for (const state of when as OwnedReadonlyArray<StyleState>) {
78
+ const bit = bits[state];
79
+ if (!has(mask, bit)) { mask += bit; count++; }
80
+ if (!has(needed, bit)) needed += bit;
81
+ }
82
+ return { mask, paint, order, priority: count * 2 + (has(mask, bits.disabled) ? 1 : 0) };
83
+ }) as OwnedArray<StyleRule>;
84
+ rules.sort((left, right) => left.priority === right.priority ? left.order - right.order : left.priority - right.priority);
85
+
86
+ const paints: Partial<Record<number, Readonly<StatePaint>>> = {};
87
+ const snapshots: Readonly<StatePaint>[] = [base];
88
+
89
+ function resolve(mask: number) {
90
+ let { color, backgroundColor, borderColor, opacity, backgroundImage, styleTokens } = base;
91
+ for (const rule of rules) {
92
+ if (!matches(rule.mask, mask)) continue;
93
+ const { paint } = rule;
94
+ color = paint.color ?? color;
95
+ backgroundColor = paint.backgroundColor ?? backgroundColor;
96
+ borderColor = paint.borderColor ?? borderColor;
97
+ opacity = paint.opacity ?? opacity;
98
+ backgroundImage = paint.backgroundImage ?? backgroundImage;
99
+ if (styleTokens !== undefined || paint.styleTokens !== undefined) styleTokens = mergeCssTokens(styleTokens, paint);
100
+ }
101
+ for (const paint of snapshots) {
102
+ if (paint.color === color && paint.backgroundColor === backgroundColor && paint.borderColor === borderColor && paint.opacity === opacity
103
+ && sameImage(paint.backgroundImage, backgroundImage) && sameTokens(paint.styleTokens, styleTokens)) return paint;
104
+ }
105
+ const paint: StatePaint = {};
106
+ if (color !== undefined) paint.color = color;
107
+ if (backgroundColor !== undefined) paint.backgroundColor = backgroundColor;
108
+ if (borderColor !== undefined) paint.borderColor = borderColor;
109
+ if (opacity !== undefined) paint.opacity = opacity;
110
+ if (backgroundImage !== undefined) paint.backgroundImage = backgroundImage;
111
+ if (styleTokens !== undefined) paint.styleTokens = styleTokens;
112
+ snapshots.push(paint);
113
+ return paint;
114
+ }
115
+
116
+ for (let mask = 0; mask <= needed; mask++) {
117
+ if (matches(mask, needed)) paints[mask] = resolve(mask);
118
+ }
119
+
120
+ let active = 0;
121
+ return {
122
+ current: () => paints[active]!,
123
+ needs: state => has(needed, bits[state]),
124
+ isActive: state => has(active, bits[state]),
125
+ set: (state, on) => {
126
+ const bit = bits[state];
127
+ if (!has(needed, bit) || has(active, bit) === on) return;
128
+ const previous = paints[active];
129
+ active += on ? bit : -bit;
130
+ const next = paints[active];
131
+ return next === previous ? undefined : next;
132
+ },
133
+ };
134
+ }
@@ -0,0 +1,53 @@
1
+ import type { Binding } from "./elements";
2
+ import { ownedSet, type OwnedSet } from "./owned-collections";
3
+ import { ok, type Result } from "./result";
4
+
5
+ export type StyleVariable = `--${string}`;
6
+ export type StyleVariables = Readonly<Partial<Record<StyleVariable, Binding<string>>>>;
7
+ export type StyleEnvironment = (this: void, name: StyleVariable) => Result<string | undefined>;
8
+
9
+ type RestrictedValue = (this: void, value: unknown) => boolean;
10
+
11
+ export function styleValue(value: Binding<string>, restricted: RestrictedValue): Result<string | undefined> {
12
+ if (restricted(value)) return ok(undefined);
13
+ if (typeof value === "string") return ok(value);
14
+ const next = value.value;
15
+ if (restricted(next)) return ok(undefined);
16
+ if (typeof next === "string") return ok(next);
17
+ return next.ok && restricted(next.value) ? ok(undefined) : next;
18
+ }
19
+
20
+ /** Resolve a color literal or a CSS variable with an optional fallback. */
21
+ export function resolveStyleValue(value: string, lookup?: StyleEnvironment): Result<string | undefined> {
22
+ if (!value.startsWith("var(") || !value.endsWith(")")) return ok(value);
23
+ const argument = value.slice(4, -1);
24
+ const comma = argument.indexOf(",");
25
+ const name = (comma < 0 ? argument : argument.slice(0, comma)).trim();
26
+ if (!name.startsWith("--")) return ok(undefined);
27
+ const resolved = lookup?.(name as StyleVariable);
28
+ if (resolved && (!resolved.ok || resolved.value !== undefined)) return resolved;
29
+ return comma < 0 ? ok(undefined) : resolveStyleValue(argument.slice(comma + 1).trim(), lookup);
30
+ }
31
+
32
+ /** Extend the inherited variable lookup for a single element and its descendants. */
33
+ export function extendStyleEnvironment(parent: StyleEnvironment | undefined, variables: StyleVariables | undefined,
34
+ restricted: RestrictedValue): StyleEnvironment | undefined {
35
+ if (variables === undefined) return parent;
36
+
37
+ function read(name: StyleVariable, resolving?: OwnedSet<StyleVariable>): Result<string | undefined> {
38
+ const binding = variables?.[name];
39
+ if (restricted(binding)) return ok(undefined);
40
+ if (binding === undefined) return parent?.(name) ?? ok(undefined);
41
+ if (resolving !== undefined && resolving.has(name)) return ok(undefined);
42
+ const value = styleValue(binding, restricted);
43
+ if (!value.ok || value.value === undefined) return value;
44
+ if (!value.value.startsWith("var(") || !value.value.endsWith(")")) return value;
45
+ const chain = resolving ?? ownedSet<StyleVariable>();
46
+ chain.add(name);
47
+ const result = resolveStyleValue(value.value, next => read(next, chain));
48
+ chain.delete(name);
49
+ return result;
50
+ }
51
+
52
+ return name => read(name);
53
+ }
@@ -0,0 +1,42 @@
1
+ import type { Binding } from "./elements";
2
+ import type { ReadonlyRef } from "./reactivity";
3
+ import type { OwnedReadonlyArray } from "./owned-collections";
4
+ import { ok, type Result } from "./result";
5
+
6
+ type Text = string | number;
7
+ export type TextRef = ReadonlyRef<Text> | ReadonlyRef<Result<Text>>;
8
+ type TextPart = string | TextRef;
9
+
10
+ /** Defer reads to the mounted value binding so all fragments share its subscription. */
11
+ export class TextBinding implements ReadonlyRef<Result<string>> {
12
+ constructor(private readonly parts: readonly TextPart[]) {}
13
+
14
+ static join(values: readonly Binding<string>[]): Binding<string> {
15
+ const source = values as OwnedReadonlyArray<Binding<string>>;
16
+ if (source.length === 1) return source[0];
17
+
18
+ const parts: TextPart[] = [];
19
+ for (const value of source) {
20
+ if (value instanceof TextBinding) {
21
+ for (const part of value.parts as OwnedReadonlyArray<TextPart>) parts.push(part);
22
+ } else parts.push(value);
23
+ }
24
+ return new TextBinding(parts);
25
+ }
26
+
27
+ get value(): Result<string> {
28
+ let text = "";
29
+ for (const part of this.parts as OwnedReadonlyArray<TextPart>) {
30
+ if (typeof part === "string") {
31
+ text += part;
32
+ continue;
33
+ }
34
+ const current = part.value;
35
+ if (typeof current === "object") {
36
+ if (!current.ok) return current;
37
+ text += String(current.value);
38
+ } else text += String(current);
39
+ }
40
+ return ok(text);
41
+ }
42
+ }
package/src/theme.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { OwnedMap, OwnedRecord } from "./owned-collections";
2
+ import { ref, type ReadonlyRef } from "./reactivity";
3
+ import { fail, ok, type Failure, type Result } from "./result";
4
+
5
+ type Token = string | number | boolean;
6
+ type Tokens = Record<string, Token>;
7
+ type Resolved<T extends Tokens> = {
8
+ readonly [K in keyof T]: T[K] extends string ? string : T[K] extends number ? number : boolean;
9
+ };
10
+
11
+ export interface Theme<T extends Tokens, Name extends string> {
12
+ name: ReadonlyRef<Name | "Default">;
13
+ tokens: ReadonlyRef<Resolved<T>>;
14
+ select: (this: void, name: Name | "Default") => Result<void>;
15
+ }
16
+
17
+ export class ThemeBuilder<T extends Tokens, Name extends string = never> {
18
+ private readonly variants = new Map<string, Resolved<T>>() as OwnedMap<string, Resolved<T>>;
19
+ private readonly defaults: OwnedRecord<T>;
20
+ private failure?: Failure;
21
+
22
+ constructor(defaults: T) {
23
+ this.defaults = { ...defaults as OwnedRecord<T> };
24
+ }
25
+
26
+ add<Next extends string, Patch extends Partial<Resolved<T>>>(name: Next extends "Default" ? never : Next,
27
+ tokens: Patch & Record<Exclude<keyof Patch, keyof T>, never>): ThemeBuilder<T, Name | Next> {
28
+ if (name === "Default" || this.variants.has(name)) this.failure ??= fail(`Duplicate theme: ${name}`);
29
+ else this.variants.set(name, { ...this.defaults, ...tokens as unknown as OwnedRecord<Patch> } as Resolved<T>);
30
+ return this as ThemeBuilder<T, Name | Next>;
31
+ }
32
+
33
+ build(): Result<Theme<T, Name>> {
34
+ if (this.failure) return this.failure;
35
+ const variants = new Map(this.variants);
36
+ const defaults = { ...this.defaults } as Resolved<T>;
37
+ const active = ref<Name | "Default">("Default");
38
+ const tokens = ref(defaults);
39
+ return ok({
40
+ name: active,
41
+ tokens,
42
+ select: name => {
43
+ if (name !== "Default" && !variants.has(name)) return fail(`Unknown theme: ${name}`);
44
+ tokens.value = variants.get(name) ?? defaults;
45
+ active.value = name;
46
+ return ok(undefined);
47
+ },
48
+ });
49
+ }
50
+ }
@@ -0,0 +1,18 @@
1
+ export type Timing = "linear" | "ease-in" | "ease-out" | "ease-in-out" | ((this: void, progress: number) => number);
2
+
3
+ export interface TransitionOptions {
4
+ duration?: number;
5
+ enter?: "fade-in" | "none";
6
+ exit?: "fade-out" | "none";
7
+ enterOffset?: number;
8
+ exitOffset?: number;
9
+ timing?: Timing;
10
+ }
11
+
12
+ export function transitionProgress(timing: Timing | undefined, progress: number) {
13
+ if (typeof timing === "function") return timing(progress);
14
+ if (timing === "ease-in") return progress * progress;
15
+ if (timing === "ease-in-out") return progress * progress * (3 - 2 * progress);
16
+ if (timing === "linear") return progress;
17
+ return 1 - (1 - progress) * (1 - progress);
18
+ }
package/state/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { computed, computedResult, watch, watchEffect, withReactiveScope } from "../src/reactive-scope";
2
+ export { ref, type Equality, type ReadonlyRef, type Ref, type RefOptions, type WatchOptions } from "../src/reactivity";
3
+ export { createStateStore, createStore, type StateStore, type StoreStorage } from "./store";
4
+ export { batch } from "./setup-store-persistence";
5
+ export {
6
+ defineStore, inject, readonly, storeToRefs,
7
+ type AnyStoreFactory, type SetupStore, type SetupStoreOptions, type SetupStoreRefs, type StoreApi, type StoreFactory, type StoreReadonly,
8
+ } from "./setup-store";
9
+ export type { ComputedRef, SignalRef } from "../src/reactivity";
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@kumi/state",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./index.ts"
8
+ },
9
+ "peerDependencies": {
10
+ "@kumi/core": "0.1.x"
11
+ }
12
+ }