@atom.io/spa 0.0.7 → 0.0.9

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 (152) hide show
  1. package/README.md +169 -25
  2. package/README.mdx +62 -15
  3. package/dist/astro-application.d.ts +1 -1
  4. package/dist/astro-client.d.ts +1 -1
  5. package/dist/astro-client.d.ts.map +1 -1
  6. package/dist/astro-client.js +5 -4
  7. package/dist/astro-client.js.map +1 -1
  8. package/dist/astro-resumability-client.d.ts +1 -1
  9. package/dist/astro-server.js +4 -4
  10. package/dist/astro-server.js.map +1 -1
  11. package/dist/{astro-shared-83hxw7-M.js → astro-shared-DBwINQav.js} +2 -2
  12. package/dist/{astro-shared-83hxw7-M.js.map → astro-shared-DBwINQav.js.map} +1 -1
  13. package/dist/{astro-shared-DZp1x8RQ.d.ts → astro-shared-KxK9OUSj.d.ts} +2 -2
  14. package/dist/{astro-shared-DZp1x8RQ.d.ts.map → astro-shared-KxK9OUSj.d.ts.map} +1 -1
  15. package/dist/astro.d.ts +1 -1
  16. package/dist/astro.js +1 -1
  17. package/dist/{capabilities-DNcnGRv_.d.ts → capabilities-ChgSs6bp.d.ts} +34 -6
  18. package/dist/capabilities-ChgSs6bp.d.ts.map +1 -0
  19. package/dist/client.min.js +1 -1
  20. package/dist/core-DD2giR6E.js +451 -0
  21. package/dist/core-DD2giR6E.js.map +1 -0
  22. package/dist/development/astro-client.js +5 -4
  23. package/dist/development/astro-client.js.map +1 -1
  24. package/dist/development/astro-server.js +4 -4
  25. package/dist/development/astro-server.js.map +1 -1
  26. package/dist/development/{astro-shared-83hxw7-M.js → astro-shared-DBwINQav.js} +2 -2
  27. package/dist/development/{astro-shared-83hxw7-M.js.map → astro-shared-DBwINQav.js.map} +1 -1
  28. package/dist/development/astro.js +1 -1
  29. package/dist/development/core-DrUd8x25.js +465 -0
  30. package/dist/development/core-DrUd8x25.js.map +1 -0
  31. package/dist/development/{dom-DQ0M3rLL.js → dom-BRFv7FP_.js} +150 -35
  32. package/dist/development/dom-BRFv7FP_.js.map +1 -0
  33. package/dist/development/floating.js +2 -6
  34. package/dist/development/floating.js.map +1 -1
  35. package/dist/development/hot.js +164 -0
  36. package/dist/development/hot.js.map +1 -0
  37. package/dist/development/identity-DwB8MTM1.js +121 -0
  38. package/dist/development/identity-DwB8MTM1.js.map +1 -0
  39. package/dist/development/index.js +5 -5
  40. package/dist/development/jsx-runtime-BGDPK50B.js +49 -0
  41. package/dist/development/jsx-runtime-BGDPK50B.js.map +1 -0
  42. package/dist/development/jsx-runtime.js +1 -25
  43. package/dist/development/konva.js +12 -5
  44. package/dist/development/konva.js.map +1 -1
  45. package/dist/development/motion.js +4 -21
  46. package/dist/development/motion.js.map +1 -1
  47. package/dist/development/renderer.js +41 -40
  48. package/dist/development/renderer.js.map +1 -1
  49. package/dist/development/{server-renderer-DCLoMdOX.js → server-renderer-u1-c5PGd.js} +8 -18
  50. package/dist/development/server-renderer-u1-c5PGd.js.map +1 -0
  51. package/dist/development/server.js +1 -1
  52. package/dist/{src-Bme1kz2b.js → development/src-CB7ruwRs.js} +3 -3
  53. package/dist/development/src-CB7ruwRs.js.map +1 -0
  54. package/dist/development/three.js +20 -19
  55. package/dist/development/three.js.map +1 -1
  56. package/dist/development/vite-WUiz1ju6.js +545 -0
  57. package/dist/development/vite-WUiz1ju6.js.map +1 -0
  58. package/dist/development/vite.js +1 -1
  59. package/dist/{dom-D8g3roX9.js → dom--HZvmgqd.js} +139 -32
  60. package/dist/dom--HZvmgqd.js.map +1 -0
  61. package/dist/floating.d.ts +1 -1
  62. package/dist/floating.d.ts.map +1 -1
  63. package/dist/floating.js +2 -6
  64. package/dist/floating.js.map +1 -1
  65. package/dist/hot.d.ts +15 -0
  66. package/dist/hot.d.ts.map +1 -0
  67. package/dist/hot.js +19 -0
  68. package/dist/hot.js.map +1 -0
  69. package/dist/identity-oFU5p-Qj.js +121 -0
  70. package/dist/identity-oFU5p-Qj.js.map +1 -0
  71. package/dist/{index-BFnXIfOj.d.ts → index-DdA39aMv.d.ts} +4 -4
  72. package/dist/{index-BFnXIfOj.d.ts.map → index-DdA39aMv.d.ts.map} +1 -1
  73. package/dist/index.d.ts +4 -4
  74. package/dist/index.js +5 -5
  75. package/dist/jsx-runtime-BGDPK50B.js +49 -0
  76. package/dist/jsx-runtime-BGDPK50B.js.map +1 -0
  77. package/dist/{jsx-runtime-DQnpyT_g.d.ts → jsx-runtime-CCvcEosg.d.ts} +14 -9
  78. package/dist/jsx-runtime-CCvcEosg.d.ts.map +1 -0
  79. package/dist/jsx-runtime.d.ts +1 -1
  80. package/dist/jsx-runtime.js +1 -25
  81. package/dist/konva.d.ts +46 -9
  82. package/dist/konva.d.ts.map +1 -1
  83. package/dist/konva.js +12 -5
  84. package/dist/konva.js.map +1 -1
  85. package/dist/motion.d.ts +1 -1
  86. package/dist/motion.js +4 -21
  87. package/dist/motion.js.map +1 -1
  88. package/dist/renderer.d.ts +1 -1
  89. package/dist/renderer.d.ts.map +1 -1
  90. package/dist/renderer.js +39 -40
  91. package/dist/renderer.js.map +1 -1
  92. package/dist/{server-renderer-D0rsFL_-.js → server-renderer-BYFDmqnM.js} +8 -18
  93. package/dist/server-renderer-BYFDmqnM.js.map +1 -0
  94. package/dist/server.d.ts +1 -1
  95. package/dist/server.js +1 -1
  96. package/dist/size.json +12 -12
  97. package/dist/{development/src-DoQgCX-O.js → src-B2W_TA_X.js} +3 -3
  98. package/dist/src-B2W_TA_X.js.map +1 -0
  99. package/dist/three.d.ts +3 -3
  100. package/dist/three.d.ts.map +1 -1
  101. package/dist/three.js +20 -19
  102. package/dist/three.js.map +1 -1
  103. package/dist/vite-WUiz1ju6.js +545 -0
  104. package/dist/vite-WUiz1ju6.js.map +1 -0
  105. package/dist/vite.d.ts.map +1 -1
  106. package/dist/vite.js +1 -1
  107. package/package.json +16 -9
  108. package/src/astro-client.ts +3 -2
  109. package/src/astro-server.ts +2 -2
  110. package/src/capabilities.ts +48 -21
  111. package/src/compiler-hmr.ts +297 -0
  112. package/src/compiler.ts +127 -25
  113. package/src/core.ts +282 -33
  114. package/src/dom-forms.ts +127 -0
  115. package/src/dom.ts +116 -52
  116. package/src/floating.ts +2 -6
  117. package/src/hot.ts +190 -0
  118. package/src/identity.ts +39 -0
  119. package/src/index.ts +2 -0
  120. package/src/jsx-runtime.ts +6 -26
  121. package/src/jsx-types.d.ts +8 -3
  122. package/src/konva.ts +66 -10
  123. package/src/motion.ts +3 -1
  124. package/src/presence.ts +11 -23
  125. package/src/props.ts +55 -0
  126. package/src/renderer.ts +91 -71
  127. package/src/server-renderer.ts +18 -13
  128. package/src/three.ts +23 -24
  129. package/src/use-loadable.ts +2 -2
  130. package/src/vite.ts +44 -4
  131. package/dist/capabilities-DNcnGRv_.d.ts.map +0 -1
  132. package/dist/capabilities-nOIitQ-_.js +0 -65
  133. package/dist/capabilities-nOIitQ-_.js.map +0 -1
  134. package/dist/core-B4rJ0xdc.js +0 -268
  135. package/dist/core-B4rJ0xdc.js.map +0 -1
  136. package/dist/development/capabilities-nOIitQ-_.js +0 -65
  137. package/dist/development/capabilities-nOIitQ-_.js.map +0 -1
  138. package/dist/development/core-BHA7I1nf.js +0 -271
  139. package/dist/development/core-BHA7I1nf.js.map +0 -1
  140. package/dist/development/dom-DQ0M3rLL.js.map +0 -1
  141. package/dist/development/jsx-runtime.js.map +0 -1
  142. package/dist/development/server-renderer-DCLoMdOX.js.map +0 -1
  143. package/dist/development/src-DoQgCX-O.js.map +0 -1
  144. package/dist/development/vite-Bmyb6P5w.js +0 -265
  145. package/dist/development/vite-Bmyb6P5w.js.map +0 -1
  146. package/dist/dom-D8g3roX9.js.map +0 -1
  147. package/dist/jsx-runtime-DQnpyT_g.d.ts.map +0 -1
  148. package/dist/jsx-runtime.js.map +0 -1
  149. package/dist/server-renderer-D0rsFL_-.js.map +0 -1
  150. package/dist/src-Bme1kz2b.js.map +0 -1
  151. package/dist/vite-Bmyb6P5w.js +0 -265
  152. package/dist/vite-Bmyb6P5w.js.map +0 -1
@@ -1,5 +1,8 @@
1
+ import type { Canonical } from "atom.io/foundations/canonical"
2
+
1
3
  import type { Children, Component, SpaElement } from "./core.ts"
2
4
  import type { HTMLAttributes, NativeElements } from "./jsx-types.d.ts"
5
+ import { merge } from "./props.ts"
3
6
 
4
7
  export type {
5
8
  AriaAttributes,
@@ -11,6 +14,7 @@ export type {
11
14
  SVGAttributes,
12
15
  TargetedEvent,
13
16
  } from "./jsx-types.d.ts"
17
+ export { merge } from "./props.ts"
14
18
 
15
19
  /** Optional integrations augment attributes without changing element identity. */
16
20
  // oxlint-disable-next-line typescript/no-empty-interface -- Optional integrations augment this declaration.
@@ -21,7 +25,7 @@ export const Fragment = (props: { children?: Children }): Children =>
21
25
  export function jsx(
22
26
  type: SpaElement[`type`],
23
27
  props: SpaElement[`props`] | null,
24
- key?: unknown,
28
+ key?: Canonical,
25
29
  ): SpaElement {
26
30
  return {
27
31
  type,
@@ -31,30 +35,6 @@ export function jsx(
31
35
  export const jsxs = jsx
32
36
  export const jsxDEV = jsx
33
37
 
34
- /** Preserve lazy props and spread precedence, including keys added by later updates. */
35
- export function merge(
36
- ...sources: (Record<string, any> | (() => Record<string, any>))[]
37
- ): Record<string, any> {
38
- const values = () =>
39
- sources.map(
40
- (source) => (typeof source === `function` ? source() : source) ?? {},
41
- )
42
- return new Proxy(
43
- {},
44
- {
45
- get: (_, key) =>
46
- values()
47
- .reverse()
48
- .find((source) => Object.hasOwn(source, key))?.[key],
49
- ownKeys: () => [...new Set(values().flatMap(Object.keys))],
50
- getOwnPropertyDescriptor: (_, key) =>
51
- values().some((source) => Object.hasOwn(source, key))
52
- ? { enumerable: true, configurable: true }
53
- : undefined,
54
- },
55
- )
56
- }
57
-
58
38
  export namespace JSX {
59
39
  export type Element = SpaElement
60
40
  export type ElementType = keyof IntrinsicElements | Component
@@ -62,7 +42,7 @@ export namespace JSX {
62
42
  children: {}
63
43
  }
64
44
  export interface IntrinsicAttributes {
65
- key?: string | number
45
+ key?: Canonical | undefined
66
46
  }
67
47
  /** Augment this interface to declare attributes for registered custom elements. */
68
48
  export interface IntrinsicElements extends NativeElements {
@@ -1,11 +1,15 @@
1
+ import type { Canonical } from "atom.io/foundations/canonical"
2
+
1
3
  import type { Children } from "./core.ts"
2
4
  import type { CapabilityAttributes } from "./jsx-runtime.ts"
3
5
 
4
- /** Native events retain their target; only currentTarget is the attached element. */
6
+ /** Native targets are preserved; currentTarget identifies the attached element. */
5
7
  export type TargetedEvent<E extends Element, T extends Event = Event> = Omit<
6
8
  T,
7
9
  `currentTarget`
8
- > & { readonly currentTarget: E }
10
+ > & { readonly currentTarget: E } & (T extends FocusEvent | MouseEvent
11
+ ? { readonly relatedTarget: (EventTarget & Element) | null }
12
+ : {})
9
13
  export type EventHandler<E extends Element, T extends Event = Event> = (
10
14
  event: TargetedEvent<E, T>,
11
15
  ) => void
@@ -201,7 +205,7 @@ export type DOMAttributes<E extends Element> = CapabilityAttributes<`dom`> &
201
205
  AriaAttributes &
202
206
  Attributes<{
203
207
  children: Children
204
- key: string | number
208
+ key: Canonical
205
209
  ref: Ref<E>
206
210
  id: string
207
211
  class: string
@@ -585,6 +589,7 @@ export type HTMLIntrinsicElements = {
585
589
  /** SVG uses literal attribute names: e.g. stroke-width, not strokeWidth. */
586
590
  export type SVGAttributes<E extends SVGElement = SVGElement> = DOMAttributes<E> &
587
591
  Attributes<{
592
+ focusable: Booleanish | `auto`
588
593
  lang: string
589
594
  [`xml:lang`]: string
590
595
  [`xml:space`]: `default` | `preserve`
package/src/konva.ts CHANGED
@@ -8,14 +8,58 @@ import type { CapabilityAttributes } from "./jsx-runtime.ts"
8
8
  import { jsx } from "./jsx-runtime.ts"
9
9
  import { bindScene, createSceneRenderer, sceneComponent } from "./renderer.ts"
10
10
 
11
- type Props<C, N> = C &
12
- CapabilityAttributes<`konva`> & {
11
+ type InteractionEvent = MouseEvent | TouchEvent | PointerEvent
12
+ interface Events {
13
+ onClick: MouseEvent
14
+ onDblClick: MouseEvent
15
+ onMouseDown: MouseEvent
16
+ onMouseUp: MouseEvent
17
+ onMouseMove: MouseEvent
18
+ onMouseEnter: MouseEvent
19
+ onMouseLeave: MouseEvent
20
+ onMouseOver: MouseEvent
21
+ onMouseOut: MouseEvent
22
+ onContextMenu: PointerEvent
23
+ onWheel: WheelEvent
24
+ onPointerDown: PointerEvent
25
+ onPointerUp: PointerEvent
26
+ onPointerMove: PointerEvent
27
+ onPointerEnter: PointerEvent
28
+ onPointerLeave: PointerEvent
29
+ onPointerOver: PointerEvent
30
+ onPointerOut: PointerEvent
31
+ onPointerCancel: PointerEvent
32
+ onPointerClick: PointerEvent
33
+ onPointerDblClick: PointerEvent
34
+ onGotPointerCapture: PointerEvent
35
+ onLostPointerCapture: PointerEvent
36
+ onTouchStart: TouchEvent
37
+ onTouchMove: TouchEvent
38
+ onTouchEnd: TouchEvent
39
+ onTouchCancel: TouchEvent
40
+ onTap: TouchEvent
41
+ onDblTap: TouchEvent
42
+ onDragStart: InteractionEvent
43
+ onDragMove: InteractionEvent
44
+ onDragEnd: InteractionEvent
45
+ onTransformStart: InteractionEvent
46
+ onTransform: InteractionEvent
47
+ onTransformEnd: InteractionEvent | undefined
48
+ }
49
+ type Config<C> = {
50
+ [K in keyof C as string extends K ? never : number extends K ? never : K]: C[K]
51
+ }
52
+ type Props<C, N extends Konva.Node> = Config<C> & {
53
+ [K in keyof Events]?:
54
+ | ((
55
+ event: Omit<Konva.KonvaEventObject<Events[K]>, `currentTarget`> & {
56
+ readonly currentTarget: N
57
+ },
58
+ ) => void)
59
+ | undefined
60
+ } & CapabilityAttributes<`konva`> & {
13
61
  children?: Children
14
62
  ref?: (node: N | null) => void
15
- onClick?: (event: Konva.KonvaEventObject<MouseEvent>) => void
16
- onPointerDown?: (event: Konva.KonvaEventObject<PointerEvent>) => void
17
- onDragMove?: (event: Konva.KonvaEventObject<DragEvent>) => void
18
- onDragEnd?: (event: Konva.KonvaEventObject<DragEvent>) => void
19
63
  }
20
64
  const constructors = {
21
65
  Layer: Konva.Layer,
@@ -86,12 +130,24 @@ const renderer = createSceneRenderer<Konva.Node>({
86
130
  if (children.length && !(parent instanceof Konva.Container))
87
131
  throw new Error(`spa/konva: shapes cannot contain children`)
88
132
  if (parent instanceof Konva.Container) {
89
- for (const child of [...parent.getChildren()])
90
- if (!children.includes(child)) child.remove()
133
+ const current = parent.getChildren()
134
+ if (
135
+ current.length === children.length &&
136
+ children.every((child, index) => child === current[index])
137
+ )
138
+ return
139
+ const wanted = new Set(children)
140
+ for (const child of [...current]) if (!wanted.has(child)) child.remove()
91
141
  children.forEach((child, index) => {
92
142
  if (child.getParent() !== parent) parent.add(child as any)
93
- child.zIndex(index)
143
+ // Layer zIndex also updates the Stage's canvas order.
144
+ if (parent instanceof Konva.Stage && child.zIndex() !== index)
145
+ child.zIndex(index)
94
146
  })
147
+ if (!(parent instanceof Konva.Stage)) {
148
+ parent.children = [...children]
149
+ parent._setChildrenIndices()
150
+ }
95
151
  }
96
152
  },
97
153
  destroy(node) {
@@ -102,7 +158,7 @@ const renderer = createSceneRenderer<Konva.Node>({
102
158
 
103
159
  /** A DOM boundary owning a Konva stage and its scene subtree. */
104
160
  export function Stage(
105
- props: Props<Omit<Konva.StageConfig, `container`>, Konva.Stage> & {
161
+ props: Props<Omit<Config<Konva.StageConfig>, `container`>, Konva.Stage> & {
106
162
  width?: number
107
163
  height?: number
108
164
  },
package/src/motion.ts CHANGED
@@ -648,7 +648,9 @@ export function motionCapability(
648
648
  const run = ({ timestamp }: { timestamp: number }) => {
649
649
  callback(timestamp)
650
650
  }
651
- frame.render(run, true)
651
+ // Layout measurement flushes update/preRender/render outside animation frames.
652
+ // Run application scene work after rendering, only in the full frame batch.
653
+ frame.postRender(run, true)
652
654
  return () => {
653
655
  cancelFrame(run)
654
656
  }
package/src/presence.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  import type { CapabilityBinding } from "./capabilities.ts"
2
2
  import type { Cleanup, Root, Scope } from "./core.ts"
3
- import { dispose, fail, invalidate, isPresent, untrack } from "./core.ts"
3
+ import {
4
+ dispose,
5
+ invalidate,
6
+ isPresent,
7
+ reportScopeError,
8
+ untrack,
9
+ } from "./core.ts"
4
10
 
5
11
  export interface ExitHandle {
6
12
  finished: Promise<void>
@@ -15,7 +21,7 @@ export interface PresenceTarget {
15
21
  node: unknown
16
22
  suspend?(): Cleanup
17
23
  }
18
- interface PresenceCollection<T, V> {
24
+ export interface PresenceCollection<T, V> {
19
25
  take(key: unknown, value: V): T | undefined
20
26
  remove(
21
27
  record: T,
@@ -39,7 +45,6 @@ export interface PresenceCoordinator {
39
45
  namespace: string,
40
46
  ): void
41
47
  unbind(binding: CapabilityBinding): void
42
- event(scope: Scope, handler: Function): Function
43
48
  remove(scope: Scope, done: Cleanup): boolean
44
49
  resume(scope: Scope): void
45
50
  forget(scope: Scope): void
@@ -60,7 +65,6 @@ interface Group {
60
65
  /** Optional coordinator: only imported by integrations that support retention. */
61
66
  export function createPresence(root: Root): PresenceCoordinator {
62
67
  const releases = new Map<CapabilityBinding, Cleanup>()
63
- const handlers = new WeakMap<Scope, WeakMap<Function, Function>>()
64
68
  // Groups own absent ranges; sessions can be shared when a parent adopts a child exit.
65
69
  const groups = new Map<Scope, Group>()
66
70
  const sessions = new Set<Session>()
@@ -103,14 +107,6 @@ export function createPresence(root: Root): PresenceCoordinator {
103
107
  if (at === parent) return true
104
108
  return false
105
109
  }
106
- const report = (error: unknown) => {
107
- if (root._dead) return
108
- try {
109
- fail(root, error)
110
- } catch (failure) {
111
- root.error(failure)
112
- }
113
- }
114
110
  const finish = () => {
115
111
  for (const group of [...groups.values()]) {
116
112
  if (group.scope._dead || group.sessions.some((session) => !session.done))
@@ -125,7 +121,7 @@ export function createPresence(root: Root): PresenceCoordinator {
125
121
  try {
126
122
  group.done()
127
123
  } catch (error) {
128
- report(error)
124
+ if (!root._dead) reportScopeError(group.scope, error)
129
125
  }
130
126
  }
131
127
  }
@@ -169,15 +165,6 @@ export function createPresence(root: Root): PresenceCoordinator {
169
165
  unbind(binding) {
170
166
  releases.get(binding)?.()
171
167
  },
172
- event(scope, handler) {
173
- const cached = handlers.get(scope) ?? new WeakMap<Function, Function>()
174
- handlers.set(scope, cached)
175
- if (!cached.has(handler))
176
- cached.set(handler, function (this: unknown, ...args: unknown[]) {
177
- if (isPresent(scope)) return handler.apply(this, args)
178
- })
179
- return cached.get(handler)!
180
- },
181
168
  collection<T, V>(
182
169
  scopeOf: (record: T) => Scope,
183
170
  compatible: (record: T, value: V) => boolean,
@@ -257,7 +244,8 @@ export function createPresence(root: Root): PresenceCoordinator {
257
244
  finish()
258
245
  },
259
246
  (error) => {
260
- if (!held.cancelled) report(error)
247
+ if (!held.cancelled && !root._dead)
248
+ reportScopeError(held.scope, error)
261
249
  },
262
250
  )
263
251
  }
package/src/props.ts ADDED
@@ -0,0 +1,55 @@
1
+ import type { Canonical } from "atom.io/foundations/canonical"
2
+
3
+ import type { SpaElement } from "./core.ts"
4
+
5
+ type Props = SpaElement[`props`]
6
+ type FixedKey = { value: Canonical | undefined; present: boolean }
7
+
8
+ function liveProps(values: () => Props[], fixedKey?: FixedKey): Props {
9
+ const source = (name: PropertyKey) =>
10
+ values()
11
+ .reverse()
12
+ .find((props) => Object.hasOwn(props, name))
13
+ const has = (_: Props, name: PropertyKey): boolean =>
14
+ fixedKey && name === `key` ? fixedKey.present : source(name) !== undefined
15
+ return new Proxy(
16
+ {},
17
+ {
18
+ has,
19
+ get(_, name) {
20
+ if (fixedKey && name === `key`) return fixedKey.value
21
+ const props = source(name)
22
+ return props && Reflect.get(props, name)
23
+ },
24
+ ownKeys() {
25
+ const keys = new Set(values().flatMap(Object.keys))
26
+ if (fixedKey?.present) keys.add(`key`)
27
+ else if (fixedKey) keys.delete(`key`)
28
+ return [...keys]
29
+ },
30
+ getOwnPropertyDescriptor(_, name) {
31
+ if (has(_, name)) return { enumerable: true, configurable: true }
32
+ },
33
+ },
34
+ )
35
+ }
36
+
37
+ /** Preserve lazy props and spread precedence, including keys added by later updates. */
38
+ export function merge(...sources: (Props | (() => Props))[]): Props {
39
+ return liveProps(() =>
40
+ sources.map(
41
+ (source) => (typeof source === `function` ? source() : source) ?? {},
42
+ ),
43
+ )
44
+ }
45
+
46
+ /** Expose the first key while leaving data props live, including through spreads. */
47
+ export function instanceProps(
48
+ read: () => Props,
49
+ key: Canonical | undefined,
50
+ ): Props {
51
+ return liveProps(() => [read()], {
52
+ value: key,
53
+ present: key !== undefined || Object.hasOwn(read(), `key`),
54
+ })
55
+ }
package/src/renderer.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { Canonical } from "atom.io/foundations/canonical"
2
+
1
3
  import type { CapabilityTarget } from "./capabilities.ts"
2
4
  import { capabilityProps } from "./capabilities.ts"
3
5
  import type {
@@ -16,10 +18,15 @@ import {
16
18
  isPresent,
17
19
  observe,
18
20
  owner,
21
+ reportScopeError,
22
+ scopeCapabilities,
19
23
  untrack,
20
24
  within,
21
25
  } from "./core.ts"
22
- import { Fragment, merge } from "./jsx-runtime.ts"
26
+ import { hotRoot, trackRoot } from "./hot.ts"
27
+ import { identifyChildren } from "./identity.ts"
28
+ import { Fragment } from "./jsx-runtime.ts"
29
+ import { instanceProps } from "./props.ts"
23
30
 
24
31
  export interface SceneHost<N> {
25
32
  target?(node: N): CapabilityTarget
@@ -35,6 +42,7 @@ export interface SceneHost<N> {
35
42
  type SceneNodes<N> = (activeOnly?: boolean) => N[]
36
43
 
37
44
  interface Entry<N> {
45
+ id: string | number
38
46
  scope: Scope
39
47
  value: Children
40
48
  nodes: SceneNodes<N>
@@ -57,13 +65,19 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
57
65
  changed: () => void,
58
66
  ): SceneNodes<N> {
59
67
  let entries: Entry<N>[] = []
60
- const presence = scope._root._presence?.collection<Entry<N>, Children>(
61
- (entry) => entry.scope,
62
- (entry, value) => same(entry.value, value),
63
- )
68
+ let reconciling = false
69
+ const report = () => {
70
+ if (!reconciling) changed()
71
+ }
72
+ const collection = () =>
73
+ scope._root._presence?.collection<Entry<N>, Children>(
74
+ (entry) => entry.scope,
75
+ (entry, value) => same(entry.value, value),
76
+ )
77
+ let presence: ReturnType<typeof collection>
64
78
  const retire = (
65
79
  entry: Entry<N>,
66
- id: string,
80
+ id: string | number,
67
81
  eligible: boolean,
68
82
  index: number,
69
83
  ) => {
@@ -73,46 +87,53 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
73
87
  })
74
88
  else dispose(entry.scope)
75
89
  }
76
- observe(scope, source, (next) => {
77
- const values = (
78
- Array.isArray(next) ? next.flat(Infinity) : [next]
79
- ) as Children[]
80
- const previous = entries
81
- const old = new Map(
82
- entries.map((entry, i) => [key(entry.value, i), entry]),
83
- )
84
- const seen = new Set<string>()
85
- entries = values.map((value, i) => {
86
- const id = key(value, i)
87
- if (seen.has(id)) throw new Error(`spa: duplicate scene key`)
88
- seen.add(id)
89
- let entry = old.get(id)
90
- old.delete(id)
91
- if (entry && !same(entry.value, value)) {
92
- retire(
93
- entry,
90
+ observe(
91
+ scope,
92
+ () => identifyChildren(source(), `spa: duplicate scene key`),
93
+ (values) => {
94
+ presence ??= collection()
95
+ reconciling = true
96
+ const previous = entries
97
+ const old = new Map(entries.map((entry) => [entry.id, entry]))
98
+ entries = values.map(({ value, key, id }) => {
99
+ let entry = old.get(id)
100
+ old.delete(id)
101
+ if (entry && !same(entry.value, value)) {
102
+ retire(
103
+ entry,
104
+ id,
105
+ value == null || typeof value === `boolean`,
106
+ previous.indexOf(entry),
107
+ )
108
+ entry = undefined
109
+ }
110
+ entry ??= presence?.take(id, value)
111
+ if (entry) {
112
+ entry.value = value
113
+ scope._root._presence?.resume(entry.scope)
114
+ invalidate(entry.scope)
115
+ return entry
116
+ }
117
+ const childScope = createScope(scope)
118
+ const created: Entry<N> = {
94
119
  id,
95
- value == null || typeof value === `boolean`,
96
- previous.indexOf(entry),
97
- )
98
- entry = undefined
99
- }
100
- entry ??= presence?.take(id, value)
101
- if (entry) {
102
- entry.value = value
103
- scope._root._presence?.resume(entry.scope)
104
- invalidate(entry.scope)
105
- return entry
106
- }
107
- const childScope = createScope(scope)
108
- const created: Entry<N> = { scope: childScope, value, nodes: null! }
109
- created.nodes = build(() => created.value, childScope, changed)
110
- return created
111
- })
112
- for (const [id, entry] of old)
113
- retire(entry, id, true, previous.indexOf(entry))
114
- changed()
115
- })
120
+ scope: childScope,
121
+ value,
122
+ nodes: null!,
123
+ }
124
+ created.nodes = build(() => created.value, childScope, report, key)
125
+ return created
126
+ })
127
+ for (const [id, entry] of old)
128
+ retire(entry, id, true, previous.indexOf(entry))
129
+ reconciling = false
130
+ if (
131
+ entries.length !== previous.length ||
132
+ entries.some((entry, i) => entry !== previous[i])
133
+ )
134
+ changed()
135
+ },
136
+ )
116
137
  return (activeOnly = false) => {
117
138
  const rendered = activeOnly
118
139
  ? entries
@@ -120,10 +141,6 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
120
141
  return rendered.flatMap((entry) => entry.nodes(activeOnly))
121
142
  }
122
143
  }
123
- function key(value: Children, i: number): string {
124
- const explicit = (value as SpaElement | null)?.props?.[`key`]
125
- return explicit == null ? `index:${i}` : `key:${explicit}`
126
- }
127
144
  function same(a: Children, b: Children): boolean {
128
145
  return (
129
146
  typeof a === typeof b &&
@@ -134,6 +151,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
134
151
  source: () => Children,
135
152
  scope: Scope,
136
153
  changed: () => void,
154
+ key: Canonical | undefined,
137
155
  ): SceneNodes<N> {
138
156
  const value = untrack(source)
139
157
  if (typeof value === `function`)
@@ -144,7 +162,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
144
162
  if (!(`type` in value) || !(`props` in value))
145
163
  throw new Error(`spa: invalid scene child`)
146
164
  const vnode = value
147
- const props = merge(() => (source() as SpaElement).props)
165
+ const props = instanceProps(() => (source() as SpaElement).props, key)
148
166
  if (!vnode.type || vnode.type === Fragment)
149
167
  return mount(() => props[`children`], scope, changed)
150
168
  if (typeof vnode.type === `function`) {
@@ -186,7 +204,7 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
186
204
  scope._root._mounts!.unshift(() => {
187
205
  if (!scope._dead) {
188
206
  scope._cleanups.add(() => ref(null))
189
- ref(node)
207
+ within(scope, () => ref(node))
190
208
  }
191
209
  })
192
210
  return () => [node]
@@ -214,21 +232,27 @@ export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
214
232
  }
215
233
  },
216
234
  render(setup: () => Children, parent: N, options: RootOptions = {}) {
217
- const root = createRoot(options)
218
- try {
219
- within(root, () => {
220
- attach(setup, parent, root)
221
- })
222
- root.flush()
223
- } catch (error) {
224
- fail(root, error)
225
- }
226
- return {
227
- flush: root.flush,
228
- dispose: () => {
229
- dispose(root)
230
- },
235
+ const mountRoot = (next: RootOptions) => {
236
+ const root = createRoot(next)
237
+ if (import.meta.env.DEV) trackRoot(root)
238
+ try {
239
+ within(root, () => {
240
+ attach(setup, parent, root)
241
+ })
242
+ root.flush()
243
+ } catch (error) {
244
+ fail(root, error)
245
+ }
246
+ return {
247
+ flush: root.flush,
248
+ dispose: () => {
249
+ dispose(root)
250
+ },
251
+ }
231
252
  }
253
+ return import.meta.env.DEV
254
+ ? hotRoot(mountRoot, options)
255
+ : mountRoot(options)
232
256
  },
233
257
  }
234
258
  }
@@ -249,7 +273,7 @@ export function onSceneFrame(
249
273
  infrastructure = false,
250
274
  ): void {
251
275
  const scope = owner!
252
- const clocks = [...scope._root._capabilities.values()].filter(
276
+ const clocks = [...scopeCapabilities(scope).values()].filter(
253
277
  (capability) => capability.scheduleFrame,
254
278
  )
255
279
  if (clocks.length > 1)
@@ -259,11 +283,7 @@ export function onSceneFrame(
259
283
  try {
260
284
  callback(time)
261
285
  } catch (error) {
262
- try {
263
- fail(scope._root, error)
264
- } catch (failure) {
265
- scope._root.error(failure)
266
- }
286
+ reportScopeError(scope, error)
267
287
  }
268
288
  }
269
289
  let stop: () => void
@@ -1,7 +1,9 @@
1
1
  import type { ReadableToken } from "atom.io"
2
+ import type { Canonical } from "atom.io/foundations/canonical"
3
+ import type { RootStore } from "atom.io/internal"
2
4
 
3
5
  import { capabilityNamespace, validateCapabilities } from "./capabilities.ts"
4
- import type { Children, RootOptions, Scope, SpaElement } from "./core.ts"
6
+ import type { Children, RootOptions, Scope } from "./core.ts"
5
7
  import {
6
8
  createRoot,
7
9
  createScope,
@@ -10,7 +12,9 @@ import {
10
12
  untrack,
11
13
  within,
12
14
  } from "./core.ts"
15
+ import { identifyChildren, trackKeys } from "./identity.ts"
13
16
  import { Fragment } from "./jsx-runtime.ts"
17
+ import { instanceProps } from "./props.ts"
14
18
 
15
19
  const escape = (value: unknown): string =>
16
20
  String(value).replace(
@@ -32,20 +36,17 @@ function stringify(
32
36
  owner: Scope,
33
37
  namespace = `html`,
34
38
  selectValue?: string | number,
39
+ initialKey?: Canonical,
35
40
  ): string {
36
41
  if (typeof value === `function`) {
37
- const values = ([untrack(value)] as unknown[]).flat(Infinity) as Children[]
38
- const keys = new Set<unknown>()
42
+ const values = within(owner, () => untrack(() => identifyChildren(value())))
39
43
  return (
40
44
  `<!--{-->` +
41
45
  values
42
- .map((child, index) => {
43
- const explicit = (child as SpaElement | null)?.props?.[`key`]
44
- const key = explicit == null ? index : String(explicit)
45
- if (keys.has(key)) throw new Error(`spa: duplicate key`)
46
- keys.add(key)
47
- return `<!--[-->${within(owner, () => stringify(child, createScope(owner), namespace, selectValue))}<!--]-->`
48
- })
46
+ .map(
47
+ ({ value: child, key }) =>
48
+ `<!--[-->${within(owner, () => stringify(child, createScope(owner), namespace, selectValue, key))}<!--]-->`,
49
+ )
49
50
  .join(``) +
50
51
  `<!--}-->`
51
52
  )
@@ -65,7 +66,7 @@ function stringify(
65
66
  const childOwner = createScope(owner)
66
67
  return within(childOwner, () =>
67
68
  stringify(
68
- untrack(() => type(props)),
69
+ trackKeys(untrack(() => type(instanceProps(() => props, initialKey)))),
69
70
  childOwner,
70
71
  namespace,
71
72
  selectValue,
@@ -149,13 +150,17 @@ function stringify(
149
150
  export function renderToStringWithReads(
150
151
  setup: () => Children,
151
152
  options: RootOptions = {},
152
- onRead?: (value: unknown, token: ReadableToken<unknown>) => void,
153
+ onRead?: (
154
+ value: unknown,
155
+ token: ReadableToken<unknown>,
156
+ store: RootStore,
157
+ ) => void,
153
158
  ): string {
154
159
  const root = createRoot(options, true)
155
160
  root._onRead = onRead
156
161
  if (/--|[<>]/.test(root._prefix)) throw new Error(`spa: invalid idPrefix`)
157
162
  try {
158
- return `<!--w:${root._prefix}-->${within(root, () => stringify(untrack(setup), root))}<!--/w-->`
163
+ return `<!--w:${root._prefix}-->${within(root, () => stringify(trackKeys(untrack(setup)), root))}<!--/w-->`
159
164
  } finally {
160
165
  dispose(root)
161
166
  }