@atom.io/spa 0.0.0-stage → 0.0.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 (106) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +401 -2
  3. package/README.mdx +150 -0
  4. package/dist/astro-application.d.ts +15 -0
  5. package/dist/astro-application.d.ts.map +1 -0
  6. package/dist/astro-application.js +62 -0
  7. package/dist/astro-application.js.map +1 -0
  8. package/dist/astro-client.d.ts +9 -0
  9. package/dist/astro-client.d.ts.map +1 -0
  10. package/dist/astro-client.js +98 -0
  11. package/dist/astro-client.js.map +1 -0
  12. package/dist/astro-context-OdlexHR4.js +79 -0
  13. package/dist/astro-context-OdlexHR4.js.map +1 -0
  14. package/dist/astro-middleware.d.ts +4 -0
  15. package/dist/astro-middleware.d.ts.map +1 -0
  16. package/dist/astro-middleware.js +49 -0
  17. package/dist/astro-middleware.js.map +1 -0
  18. package/dist/astro-request-DRKvDjsO.js +70 -0
  19. package/dist/astro-request-DRKvDjsO.js.map +1 -0
  20. package/dist/astro-server.d.ts +4 -0
  21. package/dist/astro-server.d.ts.map +1 -0
  22. package/dist/astro-server.js +30 -0
  23. package/dist/astro-server.js.map +1 -0
  24. package/dist/astro-shared-BT45auwR.js +29 -0
  25. package/dist/astro-shared-BT45auwR.js.map +1 -0
  26. package/dist/astro-shared-DkZaqX7L.d.ts +31 -0
  27. package/dist/astro-shared-DkZaqX7L.d.ts.map +1 -0
  28. package/dist/astro.d.ts +6 -0
  29. package/dist/astro.d.ts.map +1 -0
  30. package/dist/astro.js +26 -0
  31. package/dist/astro.js.map +1 -0
  32. package/dist/client.min.d.ts +1 -0
  33. package/dist/client.min.js +1 -0
  34. package/dist/core-BhiA9fFi.d.ts +44 -0
  35. package/dist/core-BhiA9fFi.d.ts.map +1 -0
  36. package/dist/core-Dvqm7w2y.js +231 -0
  37. package/dist/core-Dvqm7w2y.js.map +1 -0
  38. package/dist/development/astro-application.js +62 -0
  39. package/dist/development/astro-application.js.map +1 -0
  40. package/dist/development/astro-client.js +98 -0
  41. package/dist/development/astro-client.js.map +1 -0
  42. package/dist/development/astro-context-OdlexHR4.js +79 -0
  43. package/dist/development/astro-context-OdlexHR4.js.map +1 -0
  44. package/dist/development/astro-middleware.js +49 -0
  45. package/dist/development/astro-middleware.js.map +1 -0
  46. package/dist/development/astro-request-DRKvDjsO.js +70 -0
  47. package/dist/development/astro-request-DRKvDjsO.js.map +1 -0
  48. package/dist/development/astro-server.js +30 -0
  49. package/dist/development/astro-server.js.map +1 -0
  50. package/dist/development/astro-shared-BT45auwR.js +29 -0
  51. package/dist/development/astro-shared-BT45auwR.js.map +1 -0
  52. package/dist/development/astro.js +26 -0
  53. package/dist/development/astro.js.map +1 -0
  54. package/dist/development/core-DB3sQz4h.js +234 -0
  55. package/dist/development/core-DB3sQz4h.js.map +1 -0
  56. package/dist/development/dom-D9cY6l1R.js +194 -0
  57. package/dist/development/dom-D9cY6l1R.js.map +1 -0
  58. package/dist/development/index.js +40 -0
  59. package/dist/development/index.js.map +1 -0
  60. package/dist/development/jsx-runtime.js +25 -0
  61. package/dist/development/jsx-runtime.js.map +1 -0
  62. package/dist/development/server.js +78 -0
  63. package/dist/development/server.js.map +1 -0
  64. package/dist/development/vite-BadrFSIM.js +126 -0
  65. package/dist/development/vite-BadrFSIM.js.map +1 -0
  66. package/dist/development/vite.js +1 -0
  67. package/dist/dom-BR9o9wZW.js +188 -0
  68. package/dist/dom-BR9o9wZW.js.map +1 -0
  69. package/dist/index.d.ts +24 -0
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +40 -0
  72. package/dist/index.js.map +1 -0
  73. package/dist/jsx-runtime-BzGV18VO.d.ts +965 -0
  74. package/dist/jsx-runtime-BzGV18VO.d.ts.map +1 -0
  75. package/dist/jsx-runtime.d.ts +2 -0
  76. package/dist/jsx-runtime.js +25 -0
  77. package/dist/jsx-runtime.js.map +1 -0
  78. package/dist/server.d.ts +3 -0
  79. package/dist/server.d.ts.map +1 -0
  80. package/dist/server.js +78 -0
  81. package/dist/server.js.map +1 -0
  82. package/dist/size.json +30 -0
  83. package/dist/vite-BadrFSIM.js +126 -0
  84. package/dist/vite-BadrFSIM.js.map +1 -0
  85. package/dist/vite.d.ts +6 -0
  86. package/dist/vite.d.ts.map +1 -0
  87. package/dist/vite.js +1 -0
  88. package/package.json +131 -5
  89. package/src/astro-application.ts +71 -0
  90. package/src/astro-client.ts +136 -0
  91. package/src/astro-context.ts +102 -0
  92. package/src/astro-middleware.ts +48 -0
  93. package/src/astro-request.ts +79 -0
  94. package/src/astro-server.ts +43 -0
  95. package/src/astro-shared.ts +78 -0
  96. package/src/astro.ts +47 -0
  97. package/src/compiler.ts +189 -0
  98. package/src/core.ts +385 -0
  99. package/src/dom.ts +385 -0
  100. package/src/env.d.ts +1 -0
  101. package/src/index.ts +24 -0
  102. package/src/jsx-runtime.ts +63 -0
  103. package/src/jsx-types.d.ts +909 -0
  104. package/src/server.ts +150 -0
  105. package/src/use-loadable.ts +67 -0
  106. package/src/vite.ts +48 -0
@@ -0,0 +1,189 @@
1
+ import type { PluginObj } from "@babel/core"
2
+ import { transformSync, types as t } from "@babel/core"
3
+
4
+ export function compile(
5
+ code: string,
6
+ filename: string,
7
+ ): { code: string; map: any } {
8
+ const plugin = (): PluginObj => {
9
+ let create: t.Identifier
10
+ let merge: t.Identifier
11
+ const generated = new WeakSet<object>()
12
+ const components = new Set<string>()
13
+ function name(
14
+ node: t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName,
15
+ ): t.Expression {
16
+ if (t.isJSXMemberExpression(node))
17
+ return t.memberExpression(
18
+ t.isJSXIdentifier(node.object)
19
+ ? node.object.name === `this`
20
+ ? t.thisExpression()
21
+ : t.identifier(node.object.name)
22
+ : name(node.object),
23
+ t.identifier(node.property.name),
24
+ )
25
+ if (t.isJSXNamespacedName(node))
26
+ return t.stringLiteral(`${node.namespace.name}:${node.name.name}`)
27
+ return /^[a-z]/.test(node.name) || node.name.includes(`-`)
28
+ ? t.stringLiteral(node.name)
29
+ : t.identifier(node.name)
30
+ }
31
+ function literal(node: t.Node): boolean {
32
+ return (
33
+ t.isLiteral(node) &&
34
+ (!t.isTemplateLiteral(node) || node.expressions.length === 0)
35
+ )
36
+ }
37
+ function child(node: t.Expression): t.Expression {
38
+ return generated.has(node) || literal(node) || t.isFunction(node)
39
+ ? node
40
+ : t.arrowFunctionExpression([], node)
41
+ }
42
+ return {
43
+ visitor: {
44
+ ExportDefaultDeclaration(path) {
45
+ if (
46
+ t.isArrowFunctionExpression(path.node.declaration) ||
47
+ t.isFunctionExpression(path.node.declaration)
48
+ ) {
49
+ const id = path.scope.generateUidIdentifier(`Component`)
50
+ path.replaceWithMultiple([
51
+ t.variableDeclaration(`const`, [
52
+ t.variableDeclarator(id, path.node.declaration),
53
+ ]),
54
+ t.exportDefaultDeclaration(id),
55
+ ])
56
+ }
57
+ },
58
+ Program: {
59
+ enter(path) {
60
+ create = path.scope.generateUidIdentifier(`spa`)
61
+ merge = path.scope.generateUidIdentifier(`props`)
62
+ },
63
+ exit(path) {
64
+ path.unshiftContainer(
65
+ `body`,
66
+ t.importDeclaration(
67
+ [
68
+ t.importSpecifier(create, t.identifier(`jsx`)),
69
+ t.importSpecifier(merge, t.identifier(`merge`)),
70
+ ],
71
+ t.stringLiteral(`@atom.io/spa/jsx-runtime`),
72
+ ),
73
+ )
74
+ for (const component of components)
75
+ path.pushContainer(
76
+ `body`,
77
+ t.expressionStatement(
78
+ t.assignmentExpression(
79
+ `=`,
80
+ t.memberExpression(
81
+ t.identifier(component),
82
+ t.identifier(`spa`),
83
+ ),
84
+ t.booleanLiteral(true),
85
+ ),
86
+ ),
87
+ )
88
+ },
89
+ },
90
+ "JSXElement|JSXFragment": {
91
+ exit(path: any) {
92
+ const node = path.node as t.JSXElement | t.JSXFragment
93
+ const chunks: t.Expression[] = []
94
+ let properties: (t.ObjectProperty | t.ObjectMethod)[] = []
95
+ const flush = () => {
96
+ if (properties.length) {
97
+ chunks.push(t.objectExpression(properties))
98
+ properties = []
99
+ }
100
+ }
101
+ if (t.isJSXElement(node))
102
+ for (const attribute of node.openingElement.attributes) {
103
+ if (t.isJSXSpreadAttribute(attribute)) {
104
+ flush()
105
+ chunks.push(t.arrowFunctionExpression([], attribute.argument))
106
+ continue
107
+ }
108
+ const key = t.isJSXNamespacedName(attribute.name)
109
+ ? `${attribute.name.namespace.name}:${attribute.name.name.name}`
110
+ : attribute.name.name
111
+ const value =
112
+ attribute.value == null
113
+ ? t.booleanLiteral(true)
114
+ : t.isJSXExpressionContainer(attribute.value)
115
+ ? attribute.value.expression
116
+ : attribute.value
117
+ // The parser rejects empty attribute expressions.
118
+ const expression = value as t.Expression
119
+ properties.push(
120
+ key === `key` ||
121
+ literal(expression) ||
122
+ t.isFunction(expression)
123
+ ? t.objectProperty(t.stringLiteral(key), expression)
124
+ : t.objectMethod(
125
+ `get`,
126
+ t.stringLiteral(key),
127
+ [],
128
+ t.blockStatement([t.returnStatement(expression)]),
129
+ ),
130
+ )
131
+ }
132
+ const children = t.react
133
+ .buildChildren(node)
134
+ .map((value) => child(value as t.Expression))
135
+ if (children.length)
136
+ properties.push(
137
+ t.objectProperty(
138
+ t.identifier(`children`),
139
+ children.length === 1
140
+ ? children[0]
141
+ : t.arrayExpression(children),
142
+ ),
143
+ )
144
+ flush()
145
+ const props =
146
+ chunks.length > 1
147
+ ? t.callExpression(merge, chunks)
148
+ : (chunks[0] ?? t.objectExpression([]))
149
+ const result = t.callExpression(create, [
150
+ t.isJSXElement(node)
151
+ ? name(node.openingElement.name)
152
+ : t.nullLiteral(),
153
+ props,
154
+ ])
155
+ generated.add(result)
156
+ let fn = path.findParent((parent: any) => parent.isFunction())
157
+ while (fn?.findParent((parent: any) => parent.isFunction()))
158
+ fn = fn.findParent((parent: any) => parent.isFunction())
159
+ if (fn) {
160
+ if (fn.node.id) components.add(fn.node.id.name)
161
+ else if (
162
+ fn.parentPath.isVariableDeclarator() &&
163
+ t.isIdentifier(fn.parentPath.node.id)
164
+ )
165
+ components.add(fn.parentPath.node.id.name)
166
+ else if (
167
+ fn.parentPath.isExportDefaultDeclaration() &&
168
+ t.isFunctionDeclaration(fn.node)
169
+ ) {
170
+ fn.node.id = path.scope.generateUidIdentifier(`Component`)
171
+ components.add(fn.node.id.name)
172
+ }
173
+ }
174
+ path.replaceWith(result)
175
+ },
176
+ },
177
+ },
178
+ }
179
+ }
180
+ const result = transformSync(code, {
181
+ filename,
182
+ babelrc: false,
183
+ configFile: false,
184
+ sourceMaps: true,
185
+ parserOpts: { plugins: [`jsx`, `typescript`] },
186
+ plugins: [plugin],
187
+ })!
188
+ return { code: result.code!, map: result.map }
189
+ }
package/src/core.ts ADDED
@@ -0,0 +1,385 @@
1
+ import type {
2
+ ReadableFamilyToken,
3
+ ReadableToken,
4
+ RegularAtomFamilyToken,
5
+ RegularAtomToken,
6
+ Setter,
7
+ TimelineFamilyToken,
8
+ TimelineManageable,
9
+ TimelineToken,
10
+ TransactionToken,
11
+ ViewOf,
12
+ WritableFamilyToken,
13
+ WritableToken,
14
+ } from "atom.io"
15
+ import type { Canonical } from "atom.io/foundations/canonical"
16
+ import type { RootStore } from "atom.io/internal"
17
+ import {
18
+ actUponStore,
19
+ clearTimelineInStore,
20
+ findInStore,
21
+ findTimelineInStore,
22
+ getFromStore,
23
+ getTimelineTransactionAtHead,
24
+ IMPLICIT,
25
+ inspectTimelineInStore,
26
+ setIntoStore,
27
+ subscribeToState,
28
+ subscribeToTimeline,
29
+ timeTravel,
30
+ timeTravelTransactionGroupInStore,
31
+ } from "atom.io/internal"
32
+
33
+ export type Cleanup = () => void
34
+ // A callable interface permits the recursive Child union. A type alias does not.
35
+ /* oxlint-disable typescript/prefer-function-type -- A callable interface is required for recursive Child. */
36
+ export interface Accessor<T> {
37
+ (): T
38
+ }
39
+ /* oxlint-enable typescript/prefer-function-type */
40
+ export type Component<P = any> = (props: P) => Child
41
+ export interface VNode {
42
+ type: string | Component | null
43
+ props: Record<string, any>
44
+ }
45
+ export type Child =
46
+ | VNode
47
+ | string
48
+ | number
49
+ | bigint
50
+ | boolean
51
+ | null
52
+ | undefined
53
+ | readonly Child[]
54
+ | Accessor<Child>
55
+ export interface RootOptions {
56
+ store?: RootStore
57
+ idPrefix?: string
58
+ onError?: (error: unknown) => void
59
+ }
60
+ export interface Scope {
61
+ _root: Root
62
+ _parent?: Scope | undefined
63
+ _cleanups: Set<Cleanup | Scope>
64
+ _updates: Cleanup[]
65
+ _dead: boolean
66
+ }
67
+ export interface Root extends Scope {
68
+ _store: RootStore
69
+ _prefix: string
70
+ _id: number
71
+ _queue: Set<Cleanup>
72
+ _mounts: Cleanup[] | undefined
73
+ flush: Cleanup
74
+ error: (error: unknown) => void
75
+ }
76
+
77
+ export let owner: Scope | undefined
78
+ type Subscribe = (notify: Cleanup) => Cleanup
79
+ let tracking: ((subscribe: Subscribe) => void) | undefined
80
+ let serial = 0
81
+
82
+ export function within<T>(scope: Scope, run: () => T): T {
83
+ const previous = owner
84
+ owner = scope
85
+ try {
86
+ return run()
87
+ } finally {
88
+ owner = previous
89
+ }
90
+ }
91
+ export function untrack<T>(run: () => T): T {
92
+ const previous = tracking
93
+ tracking = undefined
94
+ try {
95
+ return run()
96
+ } finally {
97
+ tracking = previous
98
+ }
99
+ }
100
+ export function createScope(parent?: Scope): Scope {
101
+ const child: Scope = {
102
+ _root: parent?._root as Root,
103
+ _parent: parent,
104
+ _cleanups: new Set(),
105
+ _updates: [],
106
+ _dead: false,
107
+ }
108
+ parent?._cleanups.add(child)
109
+ return child
110
+ }
111
+ export function fail(scope: Scope, error: unknown): never {
112
+ try {
113
+ dispose(scope)
114
+ } catch (cleanup) {
115
+ throw new AggregateError([error, cleanup])
116
+ }
117
+ throw error
118
+ }
119
+ export function dispose(scope: Scope): void {
120
+ if (scope._dead) return
121
+ scope._dead = true
122
+ const errors: unknown[] = []
123
+ for (const cleanup of [...scope._cleanups].reverse()) {
124
+ try {
125
+ typeof cleanup === `function` ? cleanup() : dispose(cleanup)
126
+ } catch (error) {
127
+ errors.push(error)
128
+ }
129
+ }
130
+ scope._parent?._cleanups.delete(scope)
131
+ scope._cleanups.clear()
132
+ scope._updates.length = 0
133
+ if (errors.length) throw new AggregateError(errors)
134
+ }
135
+ export function invalidate(scope: Scope): void {
136
+ for (const run of scope._updates) enqueue(scope, run)
137
+ for (const child of scope._cleanups)
138
+ if (typeof child !== `function`) invalidate(child)
139
+ }
140
+ function enqueue(scope: Scope, run: Cleanup): void {
141
+ if (scope._dead) return
142
+ const root = scope._root
143
+ if (!root._queue.size)
144
+ queueMicrotask(() => {
145
+ try {
146
+ root.flush()
147
+ } catch (error) {
148
+ root.error(error)
149
+ }
150
+ })
151
+ root._queue.add(run)
152
+ }
153
+ export function createRoot(options: RootOptions = {}, server = false): Root {
154
+ const root = {
155
+ _store: options.store ?? IMPLICIT.STORE,
156
+ _prefix: options.idPrefix ?? `w${serial++}`,
157
+ _id: 0,
158
+ ...createScope(),
159
+ _queue: new Set(),
160
+ _mounts: server ? undefined : [],
161
+ } as unknown as Root
162
+ root._root = root
163
+ root.error =
164
+ options.onError ??
165
+ ((error) => {
166
+ throw error
167
+ })
168
+ root.flush = () => {
169
+ try {
170
+ let passes = 0
171
+ while (root._queue.size || root._mounts!.length) {
172
+ if (import.meta.env.DEV && ++passes > 100)
173
+ throw new Error(`spa: update loop`)
174
+ for (const run of [...root._queue]) if (root._queue.delete(run)) run()
175
+ for (const run of root._mounts!.splice(0)) run()
176
+ }
177
+ } catch (error) {
178
+ fail(root, error)
179
+ }
180
+ }
181
+ return root
182
+ }
183
+ /** Return the atom.io store selected by the current root. */
184
+ export function useStore(): RootStore {
185
+ return owner!._root._store
186
+ }
187
+ export function onCleanup(cleanup: Cleanup): void {
188
+ owner!._cleanups.add(cleanup)
189
+ }
190
+ export function onMount(run: Cleanup): void {
191
+ const scope = owner!
192
+ scope._root._mounts?.push(() => {
193
+ if (!scope._dead)
194
+ within(scope, () => {
195
+ untrack(run)
196
+ })
197
+ })
198
+ }
199
+ export function useId(): string {
200
+ const root = owner!._root
201
+ return `${root._prefix}-${root._id++}`
202
+ }
203
+
204
+ export function observe<T>(
205
+ scope: Scope,
206
+ read: Accessor<T>,
207
+ apply: (value: T) => void,
208
+ ): void {
209
+ const subscriptions = new Map<Subscribe, Cleanup>()
210
+ const run = () => {
211
+ if (scope._dead) return
212
+ const used = new Set<Subscribe>()
213
+ const previous = tracking
214
+ tracking = (subscribe) => {
215
+ used.add(subscribe)
216
+ if (!subscriptions.has(subscribe))
217
+ subscriptions.set(
218
+ subscribe,
219
+ subscribe(() => {
220
+ enqueue(scope, run)
221
+ }),
222
+ )
223
+ }
224
+ let value: T
225
+ try {
226
+ value = within(scope, read)
227
+ } finally {
228
+ tracking = previous
229
+ for (const [subscribe, unsubscribe] of subscriptions)
230
+ if (!used.has(subscribe)) {
231
+ unsubscribe()
232
+ subscriptions.delete(subscribe)
233
+ }
234
+ }
235
+ untrack(() => {
236
+ apply(value)
237
+ })
238
+ }
239
+ scope._updates.push(run)
240
+ scope._cleanups.add(() => {
241
+ scope._root._queue.delete(run)
242
+ for (const unsubscribe of subscriptions.values()) unsubscribe()
243
+ })
244
+ run()
245
+ }
246
+
247
+ /** Resolve a fixed family key in the current owner's store. */
248
+ export function resolveStateToken(token: any, key?: Canonical): any {
249
+ const store = owner!._root._store
250
+ return token.type.endsWith(`_family`) ? findInStore(store, token, key!) : token
251
+ }
252
+
253
+ /** Capture a token for this instance. Change the component key to change its token. */
254
+ export function useValue<T, E = never>(
255
+ token: ReadableToken<T, any, E>,
256
+ ): Accessor<ViewOf<T | E>>
257
+ export function useValue<T, K extends Canonical, E = never>(
258
+ family: ReadableFamilyToken<T, K, E>,
259
+ key: NoInfer<K>,
260
+ ): Accessor<ViewOf<T | E>>
261
+ export function useValue(token: any, key?: Canonical): Accessor<any> {
262
+ token = resolveStateToken(token, key)
263
+ const store = owner!._root._store
264
+ const subscribe: Subscribe = (notify) =>
265
+ subscribeToState(store, token, `spa:${serial++}`, notify)
266
+ return () => {
267
+ const value = getFromStore(store, token)
268
+ tracking?.(subscribe)
269
+ return value
270
+ }
271
+ }
272
+ export function useSetter<T>(
273
+ token: WritableToken<T, any, any>,
274
+ ): (value: T | Setter<T>) => void
275
+ export function useSetter<T, K extends Canonical>(
276
+ family: WritableFamilyToken<T, K, any>,
277
+ key: NoInfer<K>,
278
+ ): (value: T | Setter<T>) => void
279
+ export function useSetter(token: any, key?: Canonical): (value: any) => void {
280
+ token = resolveStateToken(token, key)
281
+ const store = owner!._root._store
282
+ return (value) => {
283
+ setIntoStore(store, token, value)
284
+ }
285
+ }
286
+ export function useTransaction<F extends (...args: any[]) => any>(
287
+ token: TransactionToken<F>,
288
+ ): (...args: Parameters<F>) => ReturnType<F> {
289
+ return actUponStore(owner!._root._store, token, `spa:${serial++}`)
290
+ }
291
+
292
+ const refs = new WeakMap<RootStore, Map<string, object>>()
293
+ export function useRef<E extends Element>(
294
+ token: RegularAtomToken<E | null>,
295
+ ): (element: E | null) => void
296
+ export function useRef<E extends Element, K extends Canonical>(
297
+ family: RegularAtomFamilyToken<E | null, K>,
298
+ key: NoInfer<K>,
299
+ ): (element: E | null) => void
300
+ export function useRef<E extends Element>(
301
+ token: any,
302
+ key?: Canonical,
303
+ ): (element: E | null) => void {
304
+ token = resolveStateToken(token, key)
305
+ const store = owner!._root._store
306
+ const owners = refs.get(store) ?? new Map<string, object>()
307
+ refs.set(store, owners)
308
+ const identity = {}
309
+ let held: E | null = null
310
+ return (element) => {
311
+ if (element) {
312
+ held = element
313
+ owners.set(token.key, identity)
314
+ setIntoStore(store, token, element)
315
+ } else if (owners.get(token.key) === identity) {
316
+ owners.delete(token.key)
317
+ if (getFromStore(store, token) === held) setIntoStore(store, token, null)
318
+ held = null
319
+ }
320
+ }
321
+ }
322
+ export interface TimelineValue {
323
+ at: number
324
+ length: number
325
+ undo(): void
326
+ redo(): void
327
+ clear(): void
328
+ undoTransaction?: Cleanup
329
+ redoTransaction?: Cleanup
330
+ }
331
+ export function useTimeline<M extends TimelineManageable>(
332
+ token: TimelineToken<M>,
333
+ ): Accessor<TimelineValue>
334
+ export function useTimeline<K extends Canonical, M extends TimelineManageable>(
335
+ family: TimelineFamilyToken<K, M>,
336
+ key: NoInfer<K>,
337
+ ): Accessor<TimelineValue>
338
+ export function useTimeline(
339
+ token: any,
340
+ key?: Canonical,
341
+ ): Accessor<TimelineValue> {
342
+ const store = owner!._root._store
343
+ if (token.type === `timeline_family`)
344
+ token = findTimelineInStore(store, token, key!)
345
+ const subscribe: Subscribe = (notify) =>
346
+ subscribeToTimeline(store, token, `spa:${serial++}`, notify)
347
+ const controls = {
348
+ undo: () => {
349
+ timeTravel(store, `undo`, token)
350
+ },
351
+ redo: () => {
352
+ timeTravel(store, `redo`, token)
353
+ },
354
+ clear: () => {
355
+ clearTimelineInStore(store, token)
356
+ },
357
+ }
358
+ return () => {
359
+ const value: TimelineValue = {
360
+ ...inspectTimelineInStore(store, token),
361
+ ...controls,
362
+ }
363
+ tracking?.(subscribe)
364
+ for (const direction of [`undo`, `redo`] as const) {
365
+ const group = getTimelineTransactionAtHead(
366
+ store.timelines.get(token.key)!,
367
+ direction,
368
+ )
369
+ if (group)
370
+ value[`${direction}Transaction`] = () =>
371
+ timeTravelTransactionGroupInStore(store, direction, group)
372
+ }
373
+ return value
374
+ }
375
+ }
376
+
377
+ export function plain(value: Child): string {
378
+ if (typeof value === `function`) return plain(value())
379
+ if (Array.isArray(value)) return value.map(plain).join(``)
380
+ if (value == null || typeof value === `boolean`) return ``
381
+ if (import.meta.env.DEV && typeof value === `object`)
382
+ throw new Error(`spa: text required`)
383
+ // oxlint-disable-next-line typescript/no-base-to-string -- Valid text children are primitives here; development rejects malformed object children.
384
+ return String(value)
385
+ }