@alecsibilia/ui-kit 0.0.0-stage → 0.1.0-alpha.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 (283) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +201 -0
  3. package/README.md +233 -2
  4. package/bin/ui-kit.js +12 -0
  5. package/dist/alchemy/app.d.ts +34 -0
  6. package/dist/alchemy/app.js +88 -0
  7. package/dist/alchemy/index.d.ts +11 -0
  8. package/dist/alchemy/index.js +11 -0
  9. package/dist/alchemy/live-rooms.d.ts +21 -0
  10. package/dist/alchemy/live-rooms.js +31 -0
  11. package/dist/alchemy/migrations.d.ts +28 -0
  12. package/dist/alchemy/migrations.js +56 -0
  13. package/dist/alchemy/stage.d.ts +20 -0
  14. package/dist/alchemy/stage.js +20 -0
  15. package/dist/alchemy/wrangler-config.d.ts +76 -0
  16. package/dist/alchemy/wrangler-config.js +82 -0
  17. package/dist/build/app-modules.d.ts +11 -0
  18. package/dist/build/app-modules.js +40 -0
  19. package/dist/build/check.d.ts +4 -0
  20. package/dist/build/check.js +170 -0
  21. package/dist/build/cli.d.ts +1 -0
  22. package/dist/build/cli.js +79 -0
  23. package/dist/build/feature.d.ts +8 -0
  24. package/dist/build/feature.js +24 -0
  25. package/dist/build/index.d.ts +13 -0
  26. package/dist/build/index.js +13 -0
  27. package/dist/build/measure-cpu.d.ts +2 -0
  28. package/dist/build/measure-cpu.js +196 -0
  29. package/dist/build/prerender.d.ts +5 -0
  30. package/dist/build/prerender.js +109 -0
  31. package/dist/build/preview.d.ts +7 -0
  32. package/dist/build/preview.js +41 -0
  33. package/dist/build/seo-files.d.ts +27 -0
  34. package/dist/build/seo-files.js +24 -0
  35. package/dist/client/head-swap.d.ts +45 -0
  36. package/dist/client/head-swap.js +183 -0
  37. package/dist/client/hydration-pass.d.ts +56 -0
  38. package/dist/client/hydration-pass.js +186 -0
  39. package/dist/client/hydration-reset.d.ts +82 -0
  40. package/dist/client/hydration-reset.js +154 -0
  41. package/dist/client/index.d.ts +37 -0
  42. package/dist/client/index.js +65 -0
  43. package/dist/client/nav-client.d.ts +35 -0
  44. package/dist/client/nav-client.js +371 -0
  45. package/dist/live/collab/doc-provider.d.ts +74 -0
  46. package/dist/live/collab/doc-provider.js +412 -0
  47. package/dist/live/collab/index.d.ts +51 -0
  48. package/dist/live/collab/index.jsx +142 -0
  49. package/dist/live/collab-status.d.ts +25 -0
  50. package/dist/live/collab-status.js +17 -0
  51. package/dist/live/cursors/cursor-engine.d.ts +57 -0
  52. package/dist/live/cursors/cursor-engine.js +243 -0
  53. package/dist/live/cursors/index.d.ts +46 -0
  54. package/dist/live/cursors/index.jsx +110 -0
  55. package/dist/live/declaration.d.ts +113 -0
  56. package/dist/live/declaration.js +94 -0
  57. package/dist/live/index.d.ts +8 -0
  58. package/dist/live/index.js +8 -0
  59. package/dist/live/protocol.d.ts +224 -0
  60. package/dist/live/protocol.js +272 -0
  61. package/dist/live/server/doc-store.d.ts +68 -0
  62. package/dist/live/server/doc-store.js +187 -0
  63. package/dist/live/server/index.d.ts +60 -0
  64. package/dist/live/server/index.js +77 -0
  65. package/dist/live/server/room-presence.d.ts +24 -0
  66. package/dist/live/server/room-presence.js +123 -0
  67. package/dist/live/server/room-sockets.d.ts +65 -0
  68. package/dist/live/server/room-sockets.js +97 -0
  69. package/dist/live/server/room.d.ts +20 -0
  70. package/dist/live/server/room.js +348 -0
  71. package/dist/live/server/route.d.ts +56 -0
  72. package/dist/live/server/route.js +159 -0
  73. package/dist/live/server/types.d.ts +163 -0
  74. package/dist/live/server/types.js +2 -0
  75. package/dist/logic/agent-actions.d.ts +133 -0
  76. package/dist/logic/agent-actions.js +150 -0
  77. package/dist/logic/command-types.d.ts +53 -0
  78. package/dist/logic/command-types.js +0 -0
  79. package/dist/logic/dev.d.ts +9 -0
  80. package/dist/logic/dev.js +10 -0
  81. package/dist/logic/draft-rebase.d.ts +59 -0
  82. package/dist/logic/draft-rebase.js +71 -0
  83. package/dist/logic/form-state.d.ts +105 -0
  84. package/dist/logic/form-state.js +93 -0
  85. package/dist/logic/head.d.ts +145 -0
  86. package/dist/logic/head.js +140 -0
  87. package/dist/logic/index.d.ts +27 -0
  88. package/dist/logic/index.js +25 -0
  89. package/dist/logic/island-def.d.ts +89 -0
  90. package/dist/logic/island-def.js +59 -0
  91. package/dist/logic/kit-commands.d.ts +104 -0
  92. package/dist/logic/kit-commands.js +72 -0
  93. package/dist/logic/kit-errors.d.ts +25 -0
  94. package/dist/logic/kit-errors.js +6 -0
  95. package/dist/logic/listen.d.ts +99 -0
  96. package/dist/logic/listen.js +92 -0
  97. package/dist/logic/mutation-def.d.ts +65 -0
  98. package/dist/logic/mutation-def.js +43 -0
  99. package/dist/logic/navigation-data.d.ts +50 -0
  100. package/dist/logic/navigation-data.js +55 -0
  101. package/dist/logic/navigation-policy.d.ts +74 -0
  102. package/dist/logic/navigation-policy.js +127 -0
  103. package/dist/logic/page-model-def.d.ts +108 -0
  104. package/dist/logic/page-model-def.js +31 -0
  105. package/dist/logic/path.d.ts +31 -0
  106. package/dist/logic/path.js +48 -0
  107. package/dist/logic/query-def.d.ts +56 -0
  108. package/dist/logic/query-def.js +37 -0
  109. package/dist/logic/query-key.d.ts +24 -0
  110. package/dist/logic/query-key.js +35 -0
  111. package/dist/logic/query-types.d.ts +28 -0
  112. package/dist/logic/query-types.js +2 -0
  113. package/dist/logic/resources.d.ts +163 -0
  114. package/dist/logic/resources.js +62 -0
  115. package/dist/logic/result.d.ts +30 -0
  116. package/dist/logic/result.js +23 -0
  117. package/dist/logic/route-model.d.ts +139 -0
  118. package/dist/logic/route-model.js +207 -0
  119. package/dist/logic/route-params.d.ts +80 -0
  120. package/dist/logic/route-params.js +114 -0
  121. package/dist/logic/route-table.d.ts +308 -0
  122. package/dist/logic/route-table.js +206 -0
  123. package/dist/logic/seo.d.ts +61 -0
  124. package/dist/logic/seo.js +101 -0
  125. package/dist/logic/site.d.ts +12 -0
  126. package/dist/logic/site.js +0 -0
  127. package/dist/logic/subscriptions.d.ts +177 -0
  128. package/dist/logic/subscriptions.js +152 -0
  129. package/dist/render/index.d.ts +32 -0
  130. package/dist/render/index.js +52 -0
  131. package/dist/runtime/island-wake.d.ts +38 -0
  132. package/dist/runtime/island-wake.js +147 -0
  133. package/dist/runtime/query-scope.d.ts +224 -0
  134. package/dist/runtime/query-scope.js +222 -0
  135. package/dist/runtime/room-hub.d.ts +107 -0
  136. package/dist/runtime/room-hub.js +307 -0
  137. package/dist/runtime/subscription-runner.d.ts +71 -0
  138. package/dist/runtime/subscription-runner.js +240 -0
  139. package/dist/server/bindings.d.ts +41 -0
  140. package/dist/server/bindings.js +74 -0
  141. package/dist/server/browser.d.ts +9 -0
  142. package/dist/server/browser.js +9 -0
  143. package/dist/server/edge-cache.d.ts +28 -0
  144. package/dist/server/edge-cache.js +35 -0
  145. package/dist/server/errors.d.ts +45 -0
  146. package/dist/server/errors.js +36 -0
  147. package/dist/server/form-actions-setup.d.ts +1 -0
  148. package/dist/server/form-actions-setup.js +15 -0
  149. package/dist/server/form-actions.d.ts +121 -0
  150. package/dist/server/form-actions.js +211 -0
  151. package/dist/server/index.d.ts +36 -0
  152. package/dist/server/index.js +35 -0
  153. package/dist/server/inputs.d.ts +45 -0
  154. package/dist/server/inputs.js +77 -0
  155. package/dist/server/rate-limit.d.ts +29 -0
  156. package/dist/server/rate-limit.js +32 -0
  157. package/dist/server/read-cache.d.ts +74 -0
  158. package/dist/server/read-cache.js +58 -0
  159. package/dist/server/request-info.d.ts +17 -0
  160. package/dist/server/request-info.js +11 -0
  161. package/dist/server/request.d.ts +52 -0
  162. package/dist/server/request.js +73 -0
  163. package/dist/server/response-cookies.d.ts +23 -0
  164. package/dist/server/response-cookies.js +24 -0
  165. package/dist/server/run-effect.d.ts +42 -0
  166. package/dist/server/run-effect.js +92 -0
  167. package/dist/server/run.d.ts +63 -0
  168. package/dist/server/run.js +66 -0
  169. package/dist/server/session.d.ts +125 -0
  170. package/dist/server/session.js +247 -0
  171. package/dist/test/fake-bindings.d.ts +32 -0
  172. package/dist/test/fake-bindings.js +108 -0
  173. package/dist/test/fake-room.d.ts +66 -0
  174. package/dist/test/fake-room.js +216 -0
  175. package/dist/test/index.d.ts +19 -0
  176. package/dist/test/index.js +44 -0
  177. package/dist/test/probe.d.ts +11 -0
  178. package/dist/test/probe.js +11 -0
  179. package/dist/test/solid-jsx.d.ts +23 -0
  180. package/dist/test/solid-jsx.js +28 -0
  181. package/dist/test/solid-preload.d.ts +1 -0
  182. package/dist/test/solid-preload.js +29 -0
  183. package/dist/ui/code-editor/index.d.ts +21 -0
  184. package/dist/ui/code-editor/index.jsx +286 -0
  185. package/dist/ui/collab-text/index.d.ts +29 -0
  186. package/dist/ui/collab-text/index.jsx +96 -0
  187. package/dist/ui/components/controls.d.ts +69 -0
  188. package/dist/ui/components/controls.jsx +81 -0
  189. package/dist/ui/components/feedback.d.ts +26 -0
  190. package/dist/ui/components/feedback.jsx +80 -0
  191. package/dist/ui/components/layout.d.ts +74 -0
  192. package/dist/ui/components/layout.jsx +146 -0
  193. package/dist/ui/components/lists.d.ts +69 -0
  194. package/dist/ui/components/lists.jsx +133 -0
  195. package/dist/ui/components/navigation.d.ts +73 -0
  196. package/dist/ui/components/navigation.jsx +112 -0
  197. package/dist/ui/components/text.d.ts +34 -0
  198. package/dist/ui/components/text.jsx +163 -0
  199. package/dist/ui/index.d.ts +7 -0
  200. package/dist/ui/index.js +7 -0
  201. package/dist/ui/jsx-runtime.d.ts +19 -0
  202. package/dist/ui/jsx-runtime.js +0 -0
  203. package/dist/ui/list-keys.d.ts +31 -0
  204. package/dist/ui/list-keys.js +45 -0
  205. package/dist/ui/styles/base.css +38 -0
  206. package/dist/ui/styles/components.css +608 -0
  207. package/dist/ui/styles/tokens.css +27 -0
  208. package/dist/ui/styles/transitions.css +23 -0
  209. package/dist/ui/styles/workbench.css +428 -0
  210. package/dist/ui/styles.css +9 -0
  211. package/dist/view/agent-actions-view.d.ts +30 -0
  212. package/dist/view/agent-actions-view.js +27 -0
  213. package/dist/view/agent-surface.d.ts +42 -0
  214. package/dist/view/agent-surface.js +123 -0
  215. package/dist/view/app-scope.d.ts +51 -0
  216. package/dist/view/app-scope.js +75 -0
  217. package/dist/view/boundary.d.ts +18 -0
  218. package/dist/view/boundary.jsx +26 -0
  219. package/dist/view/command-runner.d.ts +16 -0
  220. package/dist/view/command-runner.js +245 -0
  221. package/dist/view/form.d.ts +82 -0
  222. package/dist/view/form.jsx +109 -0
  223. package/dist/view/head-view.d.ts +31 -0
  224. package/dist/view/head-view.js +46 -0
  225. package/dist/view/index.d.ts +41 -0
  226. package/dist/view/index.js +40 -0
  227. package/dist/view/island-root.d.ts +30 -0
  228. package/dist/view/island-root.js +36 -0
  229. package/dist/view/island.d.ts +102 -0
  230. package/dist/view/island.jsx +154 -0
  231. package/dist/view/match.d.ts +41 -0
  232. package/dist/view/match.js +111 -0
  233. package/dist/view/model.d.ts +92 -0
  234. package/dist/view/model.js +162 -0
  235. package/dist/view/navigation-head.d.ts +29 -0
  236. package/dist/view/navigation-head.jsx +38 -0
  237. package/dist/view/page-model.d.ts +46 -0
  238. package/dist/view/page-model.js +146 -0
  239. package/dist/view/query-effects.d.ts +87 -0
  240. package/dist/view/query-effects.js +147 -0
  241. package/dist/view/query-ok.d.ts +45 -0
  242. package/dist/view/query-ok.js +96 -0
  243. package/dist/view/query.d.ts +115 -0
  244. package/dist/view/query.jsx +179 -0
  245. package/dist/view/result-view.d.ts +14 -0
  246. package/dist/view/result-view.jsx +15 -0
  247. package/dist/view/route-pages.d.ts +43 -0
  248. package/dist/view/route-pages.js +59 -0
  249. package/dist/view/router.d.ts +44 -0
  250. package/dist/view/router.js +123 -0
  251. package/dist/vite/dev-middleware.d.ts +12 -0
  252. package/dist/vite/dev-middleware.js +39 -0
  253. package/dist/vite/dev-objects.d.ts +25 -0
  254. package/dist/vite/dev-objects.js +127 -0
  255. package/dist/vite/dev-platform.d.ts +19 -0
  256. package/dist/vite/dev-platform.js +77 -0
  257. package/dist/vite/dev-seo-files.d.ts +11 -0
  258. package/dist/vite/dev-seo-files.js +40 -0
  259. package/dist/vite/index.d.ts +53 -0
  260. package/dist/vite/index.js +148 -0
  261. package/dist/vite/local-config.d.ts +34 -0
  262. package/dist/vite/local-config.js +72 -0
  263. package/dist/vite/worker-build.d.ts +24 -0
  264. package/dist/vite/worker-build.js +84 -0
  265. package/dist/worker/create-worker.d.ts +52 -0
  266. package/dist/worker/create-worker.js +90 -0
  267. package/dist/worker/index.d.ts +12 -0
  268. package/dist/worker/index.js +12 -0
  269. package/dist/worker/page-routes.d.ts +88 -0
  270. package/dist/worker/page-routes.js +100 -0
  271. package/dist/worker/page-status.d.ts +41 -0
  272. package/dist/worker/page-status.js +23 -0
  273. package/dist/worker/request-prelude.d.ts +69 -0
  274. package/dist/worker/request-prelude.js +107 -0
  275. package/dist/worker/security.d.ts +76 -0
  276. package/dist/worker/security.js +137 -0
  277. package/dist/worker/server-app.d.ts +54 -0
  278. package/dist/worker/server-app.js +76 -0
  279. package/guards/browser-only.js +5 -0
  280. package/guards/node-only.js +6 -0
  281. package/guards/server-only.js +5 -0
  282. package/migrations/0001-sessions.sql +22 -0
  283. package/package.json +233 -5
@@ -0,0 +1,59 @@
1
+ /**
2
+ * The island table: every island root a page may render, by name, with how
3
+ * it wakes. Plain data (lazy imports are thunks), so the server and the
4
+ * island client entry both import it without loading any island.
5
+ *
6
+ * - `wake: "load"`: **hydrated** at page load. The server renders the
7
+ * island's real component (inside `<Hydration id>`), and the browser
8
+ * hydrates every such root in one pass, in one task, right after the
9
+ * page loads (solidjs/solid#3845: a root hydrated later silently replaces
10
+ * its server DOM).
11
+ * - `wake: "visible" | "interaction" | "idle"`: **client-only**. The server
12
+ * renders the use site's `fallback` (real, semantic HTML); when the wake
13
+ * trigger fires, the browser renders the island fresh over it. Never a
14
+ * late hydrate. Its module may be client-only code (`*-island.lazy.tsx`,
15
+ * CodeMirror): the server never loads it.
16
+ *
17
+ * The wake is part of the table entry, not the use site, so a hydrated
18
+ * island cannot be woken late and a lazy one cannot be hydrated: the API
19
+ * has no way to ask for a late hydrate.
20
+ */
21
+ /**
22
+ * Declares the app's island table (`src/islands.ts`). Read by pages
23
+ * (`<Island of={islands.planner} props={...} />`) and by the island client
24
+ * entry (`startIslands(islands, site)`), which finds each root's module by
25
+ * its name. Like the route table, every lazy import is a thunk: each island
26
+ * is its own client chunk, loaded only on pages that render it.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * export const islands = defineIslands({
31
+ * planner: { wake: "load", load: () => import("./features/planner/planner-island.tsx") },
32
+ * dpsChart: { wake: "interaction", load: () => import("./islands/live-demo-dps-chart-island.lazy.tsx") },
33
+ * });
34
+ * ```
35
+ */
36
+ export function defineIslands(specs) {
37
+ const table = {};
38
+ for (const [name, spec] of Object.entries(specs)) {
39
+ table[name] = {
40
+ _tag: 'IslandDef',
41
+ name,
42
+ wake: spec.wake,
43
+ load: spec.load,
44
+ };
45
+ }
46
+ return table;
47
+ }
48
+ /** Island root element attributes (server markup, read by the client entry). */
49
+ export const ISLAND_ATTR = {
50
+ name: 'data-kit-island',
51
+ id: 'data-kit-island-id',
52
+ props: 'data-kit-props',
53
+ wake: 'data-kit-wake',
54
+ state: 'data-island-state',
55
+ /** `<Island persist>`: the key (island name + JSON props) a client navigation keeps it by. */
56
+ persist: 'data-kit-persist',
57
+ };
58
+ /** The class every island root element carries (`display: contents` in `@alecsibilia/ui-kit/ui`). */
59
+ export const ISLAND_CLASS = 'kit-island';
@@ -0,0 +1,104 @@
1
+ import type { AnyQueryRegistry, RegistryIdOf, RegistryInputOf } from './query-def.ts';
2
+ /**
3
+ * Commands the kit runs itself (no app handler). Import-free, so a
4
+ * `.model.ts` returns them from `update` like any other Command.
5
+ */
6
+ /**
7
+ * Refetch a registry query in the page's `<AppScope>`: every `<Query>` of it
8
+ * (every input, or only `inputKey`) moves to one fresh execution and keeps
9
+ * showing its current value in place (`meta.isPending()` is true meanwhile).
10
+ * Retry after an error is the same Command. Replaces hand-made
11
+ * `revision` / `requestSeq` counter fields.
12
+ */
13
+ export interface RefetchCommand {
14
+ readonly _tag: 'Refetch';
15
+ readonly queryId: string;
16
+ /** Only this input (`inputKeyOf`); absent = every input. */
17
+ readonly inputKey?: string;
18
+ }
19
+ /**
20
+ * Reads browser storage (`localStorage`) and sends `toMessage(values)` with
21
+ * the keys that hold a value (missing keys are absent). Runs only in the
22
+ * browser; blocked storage (private mode, quota) reads as empty.
23
+ */
24
+ export interface StorageReadCommand<Msg> {
25
+ readonly _tag: 'StorageRead';
26
+ readonly keys: readonly string[];
27
+ /** Also read every stored key starting with one of these. */
28
+ readonly prefixes?: readonly string[];
29
+ readonly toMessage: (values: Readonly<Record<string, string>>) => Msg;
30
+ }
31
+ /** Writes one browser storage key; `value: null` removes it. Browser only. */
32
+ export interface StorageWriteCommand {
33
+ readonly _tag: 'StorageWrite';
34
+ readonly key: string;
35
+ readonly value: string | null;
36
+ }
37
+ /** Scrolls the window to the top (instant). Browser only. */
38
+ export interface ScrollToTopCommand {
39
+ readonly _tag: 'ScrollToTop';
40
+ }
41
+ /**
42
+ * Goes to `href` (built with `hrefFor` from the route table): a history
43
+ * entry (or a replace), scroll to top, and every `urlChanged` model (the
44
+ * route model) hears `UrlChanged`. Browser only. Elm's `Nav.pushUrl`.
45
+ */
46
+ export interface NavigateCommand {
47
+ readonly _tag: 'Navigate';
48
+ readonly href: string;
49
+ readonly replace?: boolean;
50
+ }
51
+ /**
52
+ * Every Command the kit's runner handles itself. App Commands (with
53
+ * handlers) only run registered mutations and timers; any other effect the
54
+ * app needs is one of these, so reads and writes cannot bypass the query
55
+ * and mutation registries.
56
+ */
57
+ export type KitCommand = RefetchCommand | StorageReadCommand<any> | StorageWriteCommand | ScrollToTopCommand | NavigateCommand;
58
+ /** Tags of `KitCommand`s (the runner checks these before app handlers). */
59
+ export declare const KIT_COMMAND_TAGS: ReadonlySet<string>;
60
+ /**
61
+ * `StorageRead` Command: read `keys`, then send `toMessage(values)`.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const draftsLoaded = (values: Readonly<Record<string, string>>): Msg => ({ _tag: "DraftsLoaded", values });
66
+ * init: () => [initialModel, [readStorage(["app:notes"], draftsLoaded)]],
67
+ * ```
68
+ */
69
+ export declare function readStorage<Msg>(keys: readonly string[], toMessage: (values: Readonly<Record<string, string>>) => Msg, prefixes?: readonly string[]): StorageReadCommand<Msg>;
70
+ /** `StorageWrite` Command; `null` removes the key. */
71
+ export declare function writeStorage(key: string, value: string | null): StorageWriteCommand;
72
+ /**
73
+ * `Navigate` Command.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * // in update, after a sign-in succeeded:
78
+ * Ok: () => [model, [navigate(hrefFor(paths, { _tag: "Account", params: {}, search: {} }))]]
79
+ * ```
80
+ */
81
+ export declare function navigate(href: string, options?: {
82
+ readonly replace?: boolean;
83
+ }): NavigateCommand;
84
+ /** `ScrollToTop` Command. */
85
+ export declare function scrollToTop(): ScrollToTopCommand;
86
+ /**
87
+ * Builds `Refetch` Commands for one registry. Curried like `listen`: the
88
+ * registry type is explicit (`import type`), the id string and query name
89
+ * are checked against it.
90
+ *
91
+ * @param registry - The registry id given to `createQueryRegistry(id, ...)`.
92
+ * @returns `(name, input?) => RefetchCommand`; without `input`, every input
93
+ * of the query refetches.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * import type { plannerQueries } from "../../server/planner.queries.ts";
98
+ * const plannerRefetch = refetch<typeof plannerQueries>("planner");
99
+ *
100
+ * // in update:
101
+ * RetryRequested: () => [model, [plannerRefetch("simStats")]],
102
+ * ```
103
+ */
104
+ export declare function refetch<R extends AnyQueryRegistry>(registry: RegistryIdOf<R>): <K extends keyof R & string>(name: K, ...input: [] | [RegistryInputOf<R, K>]) => RefetchCommand;
@@ -0,0 +1,72 @@
1
+ import { inputKeyOf, queryIdOf } from './query-key.js';
2
+ /** Tags of `KitCommand`s (the runner checks these before app handlers). */
3
+ export const KIT_COMMAND_TAGS = new Set([
4
+ 'Refetch',
5
+ 'StorageRead',
6
+ 'StorageWrite',
7
+ 'ScrollToTop',
8
+ 'Navigate',
9
+ ]);
10
+ /**
11
+ * `StorageRead` Command: read `keys`, then send `toMessage(values)`.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const draftsLoaded = (values: Readonly<Record<string, string>>): Msg => ({ _tag: "DraftsLoaded", values });
16
+ * init: () => [initialModel, [readStorage(["app:notes"], draftsLoaded)]],
17
+ * ```
18
+ */
19
+ export function readStorage(keys, toMessage, prefixes) {
20
+ return prefixes && prefixes.length > 0
21
+ ? { _tag: 'StorageRead', keys, prefixes, toMessage }
22
+ : { _tag: 'StorageRead', keys, toMessage };
23
+ }
24
+ /** `StorageWrite` Command; `null` removes the key. */
25
+ export function writeStorage(key, value) {
26
+ return { _tag: 'StorageWrite', key, value };
27
+ }
28
+ /**
29
+ * `Navigate` Command.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * // in update, after a sign-in succeeded:
34
+ * Ok: () => [model, [navigate(hrefFor(paths, { _tag: "Account", params: {}, search: {} }))]]
35
+ * ```
36
+ */
37
+ export function navigate(href, options) {
38
+ return options?.replace
39
+ ? { _tag: 'Navigate', href, replace: true }
40
+ : { _tag: 'Navigate', href };
41
+ }
42
+ /** `ScrollToTop` Command. */
43
+ export function scrollToTop() {
44
+ return { _tag: 'ScrollToTop' };
45
+ }
46
+ /**
47
+ * Builds `Refetch` Commands for one registry. Curried like `listen`: the
48
+ * registry type is explicit (`import type`), the id string and query name
49
+ * are checked against it.
50
+ *
51
+ * @param registry - The registry id given to `createQueryRegistry(id, ...)`.
52
+ * @returns `(name, input?) => RefetchCommand`; without `input`, every input
53
+ * of the query refetches.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * import type { plannerQueries } from "../../server/planner.queries.ts";
58
+ * const plannerRefetch = refetch<typeof plannerQueries>("planner");
59
+ *
60
+ * // in update:
61
+ * RetryRequested: () => [model, [plannerRefetch("simStats")]],
62
+ * ```
63
+ */
64
+ export function refetch(registry) {
65
+ return (name, ...input) => input.length === 0
66
+ ? { _tag: 'Refetch', queryId: queryIdOf(registry, name) }
67
+ : {
68
+ _tag: 'Refetch',
69
+ queryId: queryIdOf(registry, name),
70
+ inputKey: inputKeyOf(input[0]),
71
+ };
72
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The plain (wire) shapes of the kit's tagged errors, for browser code and
3
+ * view cases. The server classes are in `@alecsibilia/ui-kit/server`
4
+ * (`ValidationFailed`, `Unauthorized`, `RateLimited`); a test keeps the two
5
+ * in step.
6
+ */
7
+ /** One field's problem, as `ValidationFailed.field_errors` carries it. */
8
+ export interface FieldErrorShape {
9
+ readonly field: string;
10
+ readonly message: string;
11
+ }
12
+ export interface ValidationFailedError {
13
+ readonly _tag: 'ValidationFailed';
14
+ readonly message: string;
15
+ readonly field_errors: readonly FieldErrorShape[];
16
+ }
17
+ export interface UnauthorizedError {
18
+ readonly _tag: 'Unauthorized';
19
+ readonly message: string;
20
+ }
21
+ export interface RateLimitedError {
22
+ readonly _tag: 'RateLimited';
23
+ readonly message: string;
24
+ readonly retry_after_sec: number;
25
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The plain (wire) shapes of the kit's tagged errors, for browser code and
3
+ * view cases. The server classes are in `@alecsibilia/ui-kit/server`
4
+ * (`ValidationFailed`, `Unauthorized`, `RateLimited`); a test keeps the two
5
+ * in step.
6
+ */
@@ -0,0 +1,99 @@
1
+ import type { AnyQueryRegistry, QueryDef, RegistryIdOf, RegistryInputOf } from './query-def.ts';
2
+ import type { QueryError } from './query-types.ts';
3
+ import type { Tagged } from './result.ts';
4
+ import type { QueryOutcome } from '../runtime/query-scope.ts';
5
+ export type { AnyQueryRegistry } from './query-def.ts';
6
+ /** Per-query Message mapping: which Message to send when it fails / succeeds. */
7
+ export interface QueryListener<Msg, T, E extends Tagged, Input = unknown> {
8
+ /** Receives the typed failure or `{ _tag: "Unexpected" }`. */
9
+ readonly failed?: (error: QueryError<E>) => Msg;
10
+ readonly succeeded?: (value: T) => Msg;
11
+ /**
12
+ * Only hear this input. Omitted: every input of the query (each input's
13
+ * outcome is delivered separately; none overwrites another).
14
+ */
15
+ readonly input?: Input;
16
+ }
17
+ /** Listener object accepted by `listen<Msg, typeof registry>(id)(...)`. */
18
+ export type ListenSpec<Msg, R extends AnyQueryRegistry> = {
19
+ readonly [K in keyof R & string]?: R[K] extends QueryDef<any, infer T, infer E> ? QueryListener<Msg, T, E, RegistryInputOf<R, K>> : never;
20
+ };
21
+ declare const LISTENS_BRAND: unique symbol;
22
+ interface CompiledListener<Msg> {
23
+ readonly failed?: (error: Tagged) => Msg;
24
+ readonly succeeded?: (value: unknown) => Msg;
25
+ /** `undefined` = every input. */
26
+ readonly inputKey: string | undefined;
27
+ }
28
+ /**
29
+ * Type-erased, branded listener table produced only by `listen(...)` and
30
+ * consumed by `createModel({ ..., listens })`. Keyed by query id
31
+ * (`"<registry>/<name>"`).
32
+ */
33
+ export interface Listens<Msg> {
34
+ readonly [LISTENS_BRAND]: Msg;
35
+ readonly byQueryId: ReadonlyMap<string, CompiledListener<Msg>>;
36
+ /**
37
+ * Heard for a failure of ANY query (after its own listener, if any, gave
38
+ * no Message). Kit-internal: the route model uses it to hear
39
+ * `Unauthorized` from every read (`routerListens`).
40
+ */
41
+ readonly anyFailed?: (error: Tagged) => Msg | undefined;
42
+ }
43
+ /**
44
+ * Kit-internal: a `Listens` table from compiled parts (`routerListens`).
45
+ */
46
+ export declare function compiledListens<Msg>(byQueryId: ReadonlyMap<string, CompiledListener<Msg>>, anyFailed?: (error: Tagged) => Msg | undefined): Listens<Msg>;
47
+ /** Kit-internal: one compiled listener (`routerListens`). */
48
+ export type { CompiledListener };
49
+ /**
50
+ * Declares, as plain data in a `.model.ts` file, which Messages a model wants
51
+ * when registry queries fail or succeed anywhere on the page ("Way 2":
52
+ * query outcomes become Messages to models).
53
+ *
54
+ * Curried so the Message and registry types are given explicitly while the
55
+ * object is checked. Import the registry with `import type` so the model
56
+ * file stays pure; the registry id string is checked against that type.
57
+ *
58
+ * Delivery: every `<Query>` inside `<AppScope>` publishes its outcome keyed
59
+ * by query id + input. Models created with `createModel({ listens })`
60
+ * receive the mapped Messages on the client only, after hydration settles
61
+ * (never during SSR). The latest outcome per query + input is replayed when
62
+ * a model subscribes, so a model that mounts after the query settled still
63
+ * hears it. Another registry's query of the same name never matches.
64
+ *
65
+ * @param registry - The registry id given to `createQueryRegistry(id, ...)`.
66
+ * @returns A function taking the listener object (`{ queryName: { failed?, succeeded?, input? } }`).
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import type { plannerQueries } from "../../server/planner.queries.ts";
71
+ *
72
+ * export const plannerListens = listen<PlanMessage, typeof plannerQueries>("planner")({
73
+ * character: {
74
+ * failed: (e) => ({ _tag: "CharacterUnavailable", reason: e._tag, detail: e.message }),
75
+ * succeeded: () => ({ _tag: "CharacterAvailable" }),
76
+ * },
77
+ * plan: { input: "voltar-main", succeeded: (plan) => ({ _tag: "PlanRefetched", plan }) },
78
+ * });
79
+ * ```
80
+ */
81
+ export declare function listen<Msg, R extends AnyQueryRegistry = AnyQueryRegistry>(registry: RegistryIdOf<R>): (spec: ListenSpec<Msg, R>) => Listens<Msg>;
82
+ /**
83
+ * Maps one published outcome to the listening model's Message, or
84
+ * `undefined` when the model does not listen to that query / input /
85
+ * outcome. Pure; used by `createModel` and unit tests.
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * messageForOutcome(plannerListens, {
90
+ * queryId: "planner/character", inputKey: '"voltar-main"',
91
+ * outcome: { _tag: "Failed", error: { _tag: "CharacterNotFound", message: "..." } },
92
+ * }); // { _tag: "CharacterUnavailable", ... }
93
+ * ```
94
+ */
95
+ export declare function messageForOutcome<Msg>(listens: Listens<Msg> | readonly Listens<Msg>[], event: {
96
+ readonly queryId: string;
97
+ readonly inputKey: string;
98
+ readonly outcome: QueryOutcome;
99
+ }): Msg | undefined;
@@ -0,0 +1,92 @@
1
+ import { inputKeyOf, queryIdOf } from './query-key.js';
2
+ /**
3
+ * Kit-internal: a `Listens` table from compiled parts (`routerListens`).
4
+ */
5
+ export function compiledListens(byQueryId, anyFailed) {
6
+ return { byQueryId, anyFailed };
7
+ }
8
+ /**
9
+ * Declares, as plain data in a `.model.ts` file, which Messages a model wants
10
+ * when registry queries fail or succeed anywhere on the page ("Way 2":
11
+ * query outcomes become Messages to models).
12
+ *
13
+ * Curried so the Message and registry types are given explicitly while the
14
+ * object is checked. Import the registry with `import type` so the model
15
+ * file stays pure; the registry id string is checked against that type.
16
+ *
17
+ * Delivery: every `<Query>` inside `<AppScope>` publishes its outcome keyed
18
+ * by query id + input. Models created with `createModel({ listens })`
19
+ * receive the mapped Messages on the client only, after hydration settles
20
+ * (never during SSR). The latest outcome per query + input is replayed when
21
+ * a model subscribes, so a model that mounts after the query settled still
22
+ * hears it. Another registry's query of the same name never matches.
23
+ *
24
+ * @param registry - The registry id given to `createQueryRegistry(id, ...)`.
25
+ * @returns A function taking the listener object (`{ queryName: { failed?, succeeded?, input? } }`).
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import type { plannerQueries } from "../../server/planner.queries.ts";
30
+ *
31
+ * export const plannerListens = listen<PlanMessage, typeof plannerQueries>("planner")({
32
+ * character: {
33
+ * failed: (e) => ({ _tag: "CharacterUnavailable", reason: e._tag, detail: e.message }),
34
+ * succeeded: () => ({ _tag: "CharacterAvailable" }),
35
+ * },
36
+ * plan: { input: "voltar-main", succeeded: (plan) => ({ _tag: "PlanRefetched", plan }) },
37
+ * });
38
+ * ```
39
+ */
40
+ export function listen(registry) {
41
+ return (spec) => {
42
+ const byQueryId = new Map();
43
+ for (const [name, raw] of Object.entries(spec)) {
44
+ if (!raw)
45
+ continue;
46
+ const listener = raw;
47
+ byQueryId.set(queryIdOf(registry, name), {
48
+ failed: listener.failed,
49
+ succeeded: listener.succeeded,
50
+ inputKey: 'input' in listener
51
+ ? inputKeyOf(listener.input)
52
+ : undefined,
53
+ });
54
+ }
55
+ return { byQueryId };
56
+ };
57
+ }
58
+ /**
59
+ * Maps one published outcome to the listening model's Message, or
60
+ * `undefined` when the model does not listen to that query / input /
61
+ * outcome. Pure; used by `createModel` and unit tests.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * messageForOutcome(plannerListens, {
66
+ * queryId: "planner/character", inputKey: '"voltar-main"',
67
+ * outcome: { _tag: "Failed", error: { _tag: "CharacterNotFound", message: "..." } },
68
+ * }); // { _tag: "CharacterUnavailable", ... }
69
+ * ```
70
+ */
71
+ export function messageForOutcome(listens, event) {
72
+ const tables = Array.isArray(listens) ? listens : [listens];
73
+ for (const table of tables) {
74
+ const handlers = table.byQueryId.get(event.queryId);
75
+ const heard = handlers !== undefined &&
76
+ (handlers.inputKey === undefined ||
77
+ handlers.inputKey === event.inputKey);
78
+ const msg = !heard
79
+ ? undefined
80
+ : event.outcome._tag === 'Failed'
81
+ ? handlers.failed?.(event.outcome.error)
82
+ : handlers.succeeded?.(event.outcome.value);
83
+ if (msg !== undefined)
84
+ return msg;
85
+ if (event.outcome._tag === 'Failed' && table.anyFailed) {
86
+ const any = table.anyFailed(event.outcome.error);
87
+ if (any !== undefined)
88
+ return any;
89
+ }
90
+ }
91
+ return undefined;
92
+ }
@@ -0,0 +1,65 @@
1
+ import type { QueryDef } from './query-def.ts';
2
+ import type { Result as ResultValue, Tagged } from './result.ts';
3
+ declare const MUTATION_BRAND: unique symbol;
4
+ /**
5
+ * Branded write definition produced only by `createMutationRegistry(...)`.
6
+ * Command handlers run it with `ctx.mutate(def, input, toMessage)`; on
7
+ * success the kit refetches every query in `invalidates`.
8
+ */
9
+ export interface MutationDef<Input, T, E extends Tagged, Id extends string = string> {
10
+ readonly [MUTATION_BRAND]: true;
11
+ /** `"<registry id>/<name>"`, e.g. `"planner/saveDraft"`. */
12
+ readonly id: Id;
13
+ readonly name: string;
14
+ readonly run: (input: Input) => Promise<ResultValue<T, E>> | ResultValue<T, E>;
15
+ /**
16
+ * The server function's plain-HTTP form address (Solid's reference
17
+ * `url`): where `<Form mutation={...}>` posts without JS. `""` when the
18
+ * function is not a server function (tests).
19
+ */
20
+ readonly url: string;
21
+ /** Queries refetched (every input) after an `Ok` result. */
22
+ readonly invalidates: readonly QueryDef<any, any, any>[];
23
+ /**
24
+ * After an `Ok` result, who is looking or what they may do changed
25
+ * (sign-in, sign-out, rename, a membership change by the actor): the kit
26
+ * reopens the page's live room sockets and tells this origin's other tabs
27
+ * to reopen theirs, so rooms see the new identity and re-run
28
+ * `authorize(...)`.
29
+ */
30
+ readonly changesAccess: boolean;
31
+ }
32
+ type AnyMutationFn = (input: any) => Promise<ResultValue<any, any>> | ResultValue<any, any>;
33
+ /** One entry of `createMutationRegistry`: the server function and what it makes stale. */
34
+ export interface MutationSpec<Fn extends AnyMutationFn> {
35
+ readonly run: Fn;
36
+ readonly invalidates: readonly QueryDef<any, any, any>[];
37
+ /** Sign-in / sign-out / rename / membership: reopen live rooms after `Ok` (see `MutationDef.changesAccess`). */
38
+ readonly changesAccess?: true;
39
+ }
40
+ type ResultOf<Fn extends AnyMutationFn> = Awaited<ReturnType<Fn>>;
41
+ export type MutationRegistry<Id extends string, Specs extends Record<string, MutationSpec<AnyMutationFn>>> = {
42
+ readonly [K in keyof Specs & string]: MutationDef<Parameters<Specs[K]['run']>[0], ResultOf<Specs[K]['run']> extends ResultValue<infer T, any> ? T : never, ResultOf<Specs[K]['run']> extends ResultValue<any, infer E> ? E : never, `${Id}/${K}`>;
43
+ };
44
+ /**
45
+ * Declares a feature's writes next to its reads (the mirror of
46
+ * `createQueryRegistry`). Each entry is a `"use server"` function and the
47
+ * queries it makes stale. Command handlers call `ctx.mutate(...)`; the
48
+ * result returns as a Message and, after `Ok`, the kit refetches the
49
+ * invalidated queries in the page's `<AppScope>` (like a Remix action
50
+ * revalidating loaders).
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * export const plannerMutations = createMutationRegistry("planner", {
55
+ * saveDraft: { run: saveDraft, invalidates: [plannerQueries.plan] },
56
+ * });
57
+ *
58
+ * // planner.commands.ts
59
+ * PersistDraft: (cmd, ctx) =>
60
+ * ctx.mutate(plannerMutations.saveDraft, { planId, version, slots }, (result) =>
61
+ * result._tag === "Ok" ? { _tag: "SaveSucceeded", ... } : { _tag: "SaveFailed", ... }),
62
+ * ```
63
+ */
64
+ export declare function createMutationRegistry<const Id extends string, const Specs extends Record<string, MutationSpec<AnyMutationFn>>>(id: Id, specs: Specs): MutationRegistry<Id, Specs>;
65
+ export {};
@@ -0,0 +1,43 @@
1
+ import { queryIdOf } from './query-key.js';
2
+ const MUTATION_BRAND = Symbol('our.kit.mutation');
3
+ /** A server function reference's form address (`fn.url`), or `""`. */
4
+ function formUrlOf(fn) {
5
+ const url = fn.url;
6
+ return typeof url === 'string' ? url : '';
7
+ }
8
+ /**
9
+ * Declares a feature's writes next to its reads (the mirror of
10
+ * `createQueryRegistry`). Each entry is a `"use server"` function and the
11
+ * queries it makes stale. Command handlers call `ctx.mutate(...)`; the
12
+ * result returns as a Message and, after `Ok`, the kit refetches the
13
+ * invalidated queries in the page's `<AppScope>` (like a Remix action
14
+ * revalidating loaders).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * export const plannerMutations = createMutationRegistry("planner", {
19
+ * saveDraft: { run: saveDraft, invalidates: [plannerQueries.plan] },
20
+ * });
21
+ *
22
+ * // planner.commands.ts
23
+ * PersistDraft: (cmd, ctx) =>
24
+ * ctx.mutate(plannerMutations.saveDraft, { planId, version, slots }, (result) =>
25
+ * result._tag === "Ok" ? { _tag: "SaveSucceeded", ... } : { _tag: "SaveFailed", ... }),
26
+ * ```
27
+ */
28
+ export function createMutationRegistry(id, specs) {
29
+ const registry = {};
30
+ for (const name of Object.keys(specs)) {
31
+ const spec = specs[name];
32
+ registry[name] = {
33
+ [MUTATION_BRAND]: true,
34
+ id: queryIdOf(id, name),
35
+ name,
36
+ run: spec.run,
37
+ url: formUrlOf(spec.run),
38
+ invalidates: spec.invalidates,
39
+ changesAccess: spec.changesAccess === true,
40
+ };
41
+ }
42
+ return registry;
43
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The head's client navigation data block (`<PageNavigation>` writes it,
3
+ * the island client entry and the navigation runtime read it). Tiny and
4
+ * import-free on purpose: the island entry of every page imports it, so it
5
+ * must not pull the route table into the shared client chunks.
6
+ */
7
+ /** The `type` of the head's client navigation data block. */
8
+ export declare const CLIENT_NAV_DATA_TYPE = "application/kit-nav+json";
9
+ /**
10
+ * What the head says about client navigation on one page: its named
11
+ * section and that section's route patterns only (a page never learns the
12
+ * other sections' routes; a link there is a plain full load).
13
+ */
14
+ export interface ClientNavigationData {
15
+ /** The page's client-navigation section (`"app"`). */
16
+ readonly section: string;
17
+ /** Route patterns of this section, reachable by client navigation from here (`/`, `/plans/:planId`). */
18
+ readonly client: readonly string[];
19
+ }
20
+ /**
21
+ * Reads the head's client navigation data block (the runtime's side).
22
+ * Anything malformed reads as `undefined` (no client navigation).
23
+ */
24
+ export declare function parseClientNavigationData(text: string | null | undefined): ClientNavigationData | undefined;
25
+ /** Why a client navigation becomes a full load instead (`navigationDecision`). */
26
+ export type FullLoadReason = 'outside the section' | 'the page has no client navigation' | 'another section';
27
+ /**
28
+ * Whether a fetched page may be swapped into this document (pure; the
29
+ * runtime's last check before the swap). Only a page of the **same named
30
+ * section** is swapped: anything else is a full load, which is what
31
+ * resets persisted islands, page models and held room sockets across
32
+ * sections.
33
+ *
34
+ * @param here - This document's data block.
35
+ * @param finalPath - The fetched page's normalized pathname (after redirects).
36
+ * @param next - The fetched page's data block (`undefined`: none).
37
+ * @param matches - `matchPattern`-style test of a pattern against a path.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * navigationDecision({ section: "app", client: ["/"] }, "/", { section: "app", client: ["/"] }, m); // { swap: true }
42
+ * navigationDecision({ section: "app", client: ["/"] }, "/account", undefined, m); // { swap: false, reason: "outside the section" }
43
+ * ```
44
+ */
45
+ export declare function navigationDecision(here: ClientNavigationData, finalPath: string, next: ClientNavigationData | undefined, matches: (pattern: string, path: string) => boolean): {
46
+ readonly swap: true;
47
+ } | {
48
+ readonly swap: false;
49
+ readonly reason: FullLoadReason;
50
+ };