@atom.io/spa 0.0.8 → 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 (151) 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 +3 -20
  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 +3 -20
  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/presence.ts +11 -23
  124. package/src/props.ts +55 -0
  125. package/src/renderer.ts +91 -71
  126. package/src/server-renderer.ts +18 -13
  127. package/src/three.ts +23 -24
  128. package/src/use-loadable.ts +2 -2
  129. package/src/vite.ts +44 -4
  130. package/dist/capabilities-DNcnGRv_.d.ts.map +0 -1
  131. package/dist/capabilities-nOIitQ-_.js +0 -65
  132. package/dist/capabilities-nOIitQ-_.js.map +0 -1
  133. package/dist/core-B4rJ0xdc.js +0 -268
  134. package/dist/core-B4rJ0xdc.js.map +0 -1
  135. package/dist/development/capabilities-nOIitQ-_.js +0 -65
  136. package/dist/development/capabilities-nOIitQ-_.js.map +0 -1
  137. package/dist/development/core-BHA7I1nf.js +0 -271
  138. package/dist/development/core-BHA7I1nf.js.map +0 -1
  139. package/dist/development/dom-DQ0M3rLL.js.map +0 -1
  140. package/dist/development/jsx-runtime.js.map +0 -1
  141. package/dist/development/server-renderer-DCLoMdOX.js.map +0 -1
  142. package/dist/development/src-DoQgCX-O.js.map +0 -1
  143. package/dist/development/vite-Bmyb6P5w.js +0 -265
  144. package/dist/development/vite-Bmyb6P5w.js.map +0 -1
  145. package/dist/dom-D8g3roX9.js.map +0 -1
  146. package/dist/jsx-runtime-DQnpyT_g.d.ts.map +0 -1
  147. package/dist/jsx-runtime.js.map +0 -1
  148. package/dist/server-renderer-D0rsFL_-.js.map +0 -1
  149. package/dist/src-Bme1kz2b.js.map +0 -1
  150. package/dist/vite-Bmyb6P5w.js +0 -265
  151. package/dist/vite-Bmyb6P5w.js.map +0 -1
@@ -0,0 +1,127 @@
1
+ import type { Scope } from "./core.ts"
2
+ import { enqueue, isPresent } from "./core.ts"
3
+
4
+ type Control = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement
5
+ const changes = new WeakMap<EventListener, EventListener>()
6
+ const requests = new WeakMap<Element, () => void>()
7
+ const events = [`input`, `change`, `click`]
8
+ const wake = () => {}
9
+
10
+ /** Keep native input events, using per-edit change semantics for text/range controls. */
11
+ export function changeHandler(handler: EventListener): EventListener {
12
+ let wrapped = changes.get(handler)
13
+ if (!wrapped) {
14
+ wrapped = (event) => {
15
+ const target = event.target as Control | null
16
+ const editing =
17
+ target?.localName === `textarea` ||
18
+ (target?.localName === `input` &&
19
+ !/^(checkbox|radio|file)$/.test(target.type))
20
+ if (event.type === (editing ? `input` : `change`))
21
+ handler.call(event.currentTarget, event)
22
+ }
23
+ changes.set(handler, wrapped)
24
+ }
25
+ return wrapped
26
+ }
27
+
28
+ export function controlValue(control: Control, value: string | number): void {
29
+ if (
30
+ control.localName === `input` &&
31
+ control.type === `number` &&
32
+ typeof value === `number` &&
33
+ control.value !== `` &&
34
+ Number(control.value) === value
35
+ )
36
+ return
37
+ if (control.value !== String(value)) control.value = String(value)
38
+ if (control.localName === `select`) {
39
+ const select = control as HTMLSelectElement
40
+ if (select.selectedIndex < 0) {
41
+ const first = [...select.options].find((option) => !option.disabled)
42
+ if (first) first.selected = true
43
+ }
44
+ }
45
+ }
46
+
47
+ function requestRestore(control: Control): void {
48
+ requests.get(control)?.()
49
+ if (control.localName !== `input` || control.type !== `radio`) return
50
+ const input = control as HTMLInputElement
51
+ for (const peer of (
52
+ input.getRootNode() as ParentNode
53
+ ).querySelectorAll<HTMLInputElement>(`input`)) {
54
+ if (
55
+ peer !== input &&
56
+ peer.type === `radio` &&
57
+ peer.name === input.name &&
58
+ peer.form === input.form
59
+ )
60
+ requests.get(peer)?.()
61
+ }
62
+ }
63
+
64
+ /** Restore controlled fields after event handlers and all queued option/prop updates. */
65
+ export function bindForm(
66
+ control: Control,
67
+ props: Record<string, any>,
68
+ scope: Scope,
69
+ ): void {
70
+ let active: Event | undefined
71
+ let timer: ReturnType<typeof setTimeout> | undefined
72
+ const restore = () => {
73
+ if (!isPresent(scope)) return true
74
+ const value = props[`value`]
75
+ const checked = control.localName === `input` ? props[`checked`] : undefined
76
+ if (active?.eventPhase) return value == null && checked == null
77
+ if (value != null) controlValue(control, value)
78
+ if (checked != null) {
79
+ const input = control as HTMLInputElement
80
+ if (input.checked !== Boolean(checked)) input.checked = Boolean(checked)
81
+ }
82
+ return true
83
+ }
84
+ const finish = () => {
85
+ active = undefined
86
+ clearTimeout(timer)
87
+ enqueue(scope, wake)
88
+ }
89
+ const capture = (event: Event) => {
90
+ if (event.type === `click` && !/^(checkbox|radio)$/.test(control.type))
91
+ return
92
+ active = event
93
+ clearTimeout(timer)
94
+ // A stopped native event never reaches the root's bubble listener.
95
+ timer = setTimeout(() => {
96
+ requestRestore(control)
97
+ }, 0)
98
+ }
99
+ const root = scope._root
100
+ if (!root._forms) {
101
+ root._forms = new Set()
102
+ const end = (event: Event) => {
103
+ const target = event.target as Control
104
+ // Checkbox/radio change follows click and input; its handlers must see the edit.
105
+ if (
106
+ event.type === `click` ||
107
+ (event.type === `input` && /^(checkbox|radio)$/.test(target.type))
108
+ )
109
+ return
110
+ requestRestore(target)
111
+ }
112
+ for (const event of events) root._host!.addEventListener(event, end)
113
+ root._cleanups.add(() => {
114
+ for (const event of events) root._host!.removeEventListener(event, end)
115
+ })
116
+ }
117
+ const forms = root._forms
118
+ forms.add(restore)
119
+ requests.set(control, finish)
120
+ for (const event of events) control.addEventListener(event, capture, true)
121
+ scope._cleanups.add(() => {
122
+ clearTimeout(timer)
123
+ forms.delete(restore)
124
+ requests.delete(control)
125
+ for (const event of events) control.removeEventListener(event, capture, true)
126
+ })
127
+ }
package/src/dom.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { Canonical } from "atom.io/foundations/canonical"
2
+
1
3
  import { capabilityProps } from "./capabilities.ts"
2
4
  import type {
3
5
  Accessor,
@@ -17,7 +19,12 @@ import {
17
19
  untrack,
18
20
  within,
19
21
  } from "./core.ts"
20
- import { Fragment, merge } from "./jsx-runtime.ts"
22
+ import { bindForm, changeHandler, controlValue } from "./dom-forms.ts"
23
+ import { hotRoot, trackRoot } from "./hot.ts"
24
+ import { identifyChildren, trackKeys } from "./identity.ts"
25
+ import { Fragment } from "./jsx-runtime.ts"
26
+ import type { PresenceCollection } from "./presence.ts"
27
+ import { instanceProps } from "./props.ts"
21
28
 
22
29
  const HTML = `http://www.w3.org/1999/xhtml`
23
30
  const SVG = `http://www.w3.org/2000/svg`
@@ -25,7 +32,20 @@ type Cursor = {
25
32
  node: Node | null
26
33
  report(expected: Node | null, found: Node | null): void
27
34
  }
28
- type RecordNode = { _value: Children; _scope: Scope; _start: Node; _end: Node }
35
+ type RecordNode = {
36
+ _value: Children
37
+ _scope: Scope
38
+ _start: Node
39
+ _end: Node
40
+ _index: number
41
+ }
42
+ function compatible(record: RecordNode, child: Children): boolean {
43
+ return (
44
+ typeof record._value === typeof child &&
45
+ (record._value as SpaElement | null)?.type ===
46
+ (child as SpaElement | null)?.type
47
+ )
48
+ }
29
49
 
30
50
  function finish(cursor?: Cursor): void {
31
51
  if (cursor?.node) {
@@ -106,11 +126,26 @@ function property(
106
126
  .slice(2, capture ? -7 : undefined)
107
127
  .toLowerCase()
108
128
  .replace(`doubleclick`, `dblclick`)
109
- if (previous) element.removeEventListener(event, previous, capture)
129
+ const events =
130
+ event === `change`
131
+ ? [`input`, `change`]
132
+ : [event === `focus` ? `focusin` : event === `blur` ? `focusout` : event]
133
+ if (previous)
134
+ for (const name of events)
135
+ element.removeEventListener(
136
+ name,
137
+ event === `change` ? changeHandler(previous) : previous,
138
+ capture,
139
+ )
110
140
  if (value != null) {
111
141
  if (import.meta.env.DEV && typeof value !== `function`)
112
142
  throw new Error(`spa: event handler`)
113
- element.addEventListener(event, value, capture)
143
+ for (const name of events)
144
+ element.addEventListener(
145
+ name,
146
+ event === `change` ? changeHandler(value) : value,
147
+ capture,
148
+ )
114
149
  }
115
150
  return
116
151
  }
@@ -123,13 +158,26 @@ function property(
123
158
  typeof value !== `string`
124
159
  )
125
160
  throw new Error(`spa: CSS string required`)
161
+ if (
162
+ element.namespaceURI === SVG &&
163
+ key === `focusable` &&
164
+ typeof value === `boolean`
165
+ )
166
+ value = String(value)
126
167
  if (
127
168
  element.namespaceURI === HTML &&
128
169
  key in element &&
129
170
  !/^(style|form|list)$/.test(key)
130
171
  ) {
131
172
  const target = element as any
132
- target[key] = value ?? (typeof target[key] === `boolean` ? false : ``)
173
+ if (
174
+ key === `value` &&
175
+ /^(input|textarea|select)$/.test(element.localName) &&
176
+ value != null
177
+ )
178
+ controlValue(target, value)
179
+ else target[key] = value ?? (typeof target[key] === `boolean` ? false : ``)
180
+ if (value == null) element.removeAttribute(key)
133
181
  } else if (value == null || (value === false && !/^(aria-|data-)/.test(key)))
134
182
  element.removeAttribute(key)
135
183
  else
@@ -147,18 +195,13 @@ function mount(
147
195
  namespace: string,
148
196
  cursor?: Cursor,
149
197
  source = () => value,
198
+ initialKey?: Canonical,
150
199
  ): void {
151
200
  if (typeof value === `function`) {
152
- comment(`{`, parent, before, owner, cursor)
201
+ const begin = comment(`{`, parent, before, owner, cursor)
153
202
  let end: Node | null = null
154
- let records = new Map<unknown, RecordNode>()
155
- const presence = owner._root._presence?.collection<RecordNode, Children>(
156
- (record) => record._scope,
157
- (record, child) =>
158
- typeof record._value === typeof child &&
159
- (record._value as SpaElement | null)?.type ===
160
- (child as SpaElement | null)?.type,
161
- )
203
+ let records = new Map<string | number, RecordNode>()
204
+ let presence: PresenceCollection<RecordNode, Children> | undefined
162
205
  const retire = (
163
206
  record: RecordNode,
164
207
  key: unknown,
@@ -171,68 +214,61 @@ function mount(
171
214
  let initial = cursor
172
215
  observe(
173
216
  owner,
174
- () => (source() as () => Children)(),
175
- (next) => {
176
- const values = ([next] as unknown[]).flat(Infinity) as Children[]
177
- const kept = new Map<unknown, RecordNode>()
217
+ () => identifyChildren((source() as () => Children)()),
218
+ (values) => {
219
+ presence ??= owner._root._presence?.collection(
220
+ (record: RecordNode) => record._scope,
221
+ compatible,
222
+ )
223
+ const kept = new Map<string | number, RecordNode>()
224
+ let previous: RecordNode | undefined
178
225
  const focus = parent.ownerDocument!.activeElement as HTMLElement | null
179
226
  for (let index = 0; index < values.length; index++) {
180
- const child = values[index]
181
- const vnode = child as SpaElement | null
182
- const explicit = vnode?.props?.[`key`]
183
- const key = explicit == null ? index : String(explicit)
184
- if (import.meta.env.DEV && kept.has(key))
185
- throw new Error(`spa: duplicate key`)
186
- let record = records.get(key)
187
- if (
188
- record &&
189
- (typeof record._value !== typeof child ||
190
- (record._value as SpaElement | null)?.type !== vnode?.type)
191
- ) {
227
+ const { value: child, key, id } = values[index]
228
+ let record = records.get(id)
229
+ if (record && !compatible(record, child)) {
192
230
  retire(
193
231
  record,
194
- key,
232
+ id,
195
233
  child == null || typeof child === `boolean`,
196
- [...records.keys()].indexOf(key),
234
+ record._index,
197
235
  )
198
236
  record = undefined
199
237
  }
200
- record ??= presence?.take(key, child)
238
+ record ??= presence?.take(id, child)
201
239
  if (!record) {
240
+ const at = previous ? previous._end.nextSibling : begin.nextSibling
202
241
  const childOwner = createScope(owner)
203
242
  record = {
243
+ _index: index,
204
244
  _value: child,
205
245
  _scope: childOwner,
206
- _start: comment(`[`, parent, end ?? before, childOwner, initial),
246
+ _start: comment(`[`, parent, at, childOwner, initial),
207
247
  _end: null!,
208
248
  }
209
249
  const held = record
210
250
  mount(
211
251
  child,
212
252
  parent,
213
- end ?? before,
253
+ at,
214
254
  childOwner,
215
255
  namespace,
216
256
  initial,
217
257
  () => held._value,
258
+ key,
218
259
  )
219
- record._end = comment(
220
- `]`,
221
- parent,
222
- end ?? before,
223
- childOwner,
224
- initial,
225
- )
260
+ record._end = comment(`]`, parent, at, childOwner, initial)
226
261
  } else {
227
262
  record._value = child
228
263
  owner._root._presence?.resume(record._scope)
229
264
  invalidate(record._scope)
230
265
  }
231
- kept.set(key, record)
266
+ record._index = index
267
+ previous = record
268
+ kept.set(id, record)
232
269
  }
233
270
  for (const [key, record] of records)
234
- if (!kept.has(key))
235
- retire(record, key, true, [...records.keys()].indexOf(key))
271
+ if (!kept.has(key)) retire(record, key, true, record._index)
236
272
  if (!initial) {
237
273
  let at = end ?? before
238
274
  const rendered = presence?.merge([...kept.values()]) ?? [
@@ -285,7 +321,7 @@ function mount(
285
321
  if (import.meta.env.DEV && (!(`type` in value) || !(`props` in value)))
286
322
  throw new Error(`spa: invalid child`)
287
323
  const vnode = value as SpaElement
288
- const props = merge(() => (source() as SpaElement).props)
324
+ const props = instanceProps(() => (source() as SpaElement).props, initialKey)
289
325
  if (!vnode.type || vnode.type === Fragment) {
290
326
  mount(() => props[`children`], parent, before, owner, namespace, cursor)
291
327
  return
@@ -295,7 +331,7 @@ function mount(
295
331
  const output = within(childOwner, () =>
296
332
  untrack(() => (vnode.type as Function)(props)),
297
333
  )
298
- mount(output, parent, before, childOwner, namespace, cursor)
334
+ mount(trackKeys(output), parent, before, childOwner, namespace, cursor)
299
335
  return
300
336
  }
301
337
  const ns = vnode.type === `svg` ? SVG : namespace
@@ -306,6 +342,13 @@ function mount(
306
342
  owner,
307
343
  cursor,
308
344
  ) as Element
345
+ if (ns === HTML && /^(input|textarea|select)$/.test(vnode.type))
346
+ bindForm(element as HTMLInputElement, props, owner)
347
+ if (ns === HTML && (props[`autoFocus`] || props[`autofocus`])) {
348
+ owner._root._mounts!.push(() => {
349
+ if (!owner._dead) (element as HTMLElement).focus()
350
+ })
351
+ }
309
352
  if (vnode.type === `astro-slot`) {
310
353
  if (!cursor) element.innerHTML = props[`innerHTML`] ?? ``
311
354
  } else if (ns === HTML && /^(textarea|title|style|script)$/.test(vnode.type)) {
@@ -373,7 +416,7 @@ function mount(
373
416
  owner._root._mounts!.unshift(() => {
374
417
  if (!owner._dead) {
375
418
  owner._cleanups.add(() => ref(null))
376
- ref(element)
419
+ within(owner, () => ref(element))
377
420
  }
378
421
  })
379
422
  }
@@ -388,6 +431,7 @@ function start(
388
431
  host: Element,
389
432
  options: RootOptions,
390
433
  hydrating: boolean,
434
+ before: Node | null = null,
391
435
  ): SpaRoot {
392
436
  let reported = false
393
437
  const cursor: Cursor | undefined = hydrating
@@ -411,21 +455,23 @@ function start(
411
455
  ? marker.nodeValue.slice(2)
412
456
  : undefined
413
457
  const root = createRoot(options)
458
+ root._host = host
414
459
  if (prefix !== undefined && options.idPrefix === undefined)
415
460
  root._prefix = prefix
461
+ if (import.meta.env.DEV) trackRoot(root)
416
462
  try {
417
- comment(`w:${root._prefix}`, host, null, root, cursor)
463
+ comment(`w:${root._prefix}`, host, before, root, cursor)
418
464
  within(root, () => {
419
465
  mount(
420
- untrack(setup),
466
+ trackKeys(untrack(setup)),
421
467
  host,
422
- null,
468
+ before,
423
469
  root,
424
470
  host.namespaceURI === SVG ? SVG : HTML,
425
471
  cursor,
426
472
  )
427
473
  })
428
- comment(`/w`, host, null, root, cursor)
474
+ comment(`/w`, host, before, root, cursor)
429
475
  finish(cursor)
430
476
  root.flush()
431
477
  } catch (error) {
@@ -443,6 +489,12 @@ export function render(
443
489
  host: Element,
444
490
  options: RootOptions = {},
445
491
  ): SpaRoot {
492
+ if (import.meta.env.DEV)
493
+ return hotRoot(
494
+ (next, before) => start(setup, host, next, false, before),
495
+ options,
496
+ host,
497
+ )
446
498
  return start(setup, host, options, false)
447
499
  }
448
500
  export function hydrate(
@@ -450,5 +502,17 @@ export function hydrate(
450
502
  host: Element,
451
503
  options: RootOptions = {},
452
504
  ): SpaRoot {
505
+ if (import.meta.env.DEV) {
506
+ let initial = true
507
+ return hotRoot(
508
+ (next, before) => {
509
+ const current = start(setup, host, next, initial, before)
510
+ initial = false
511
+ return current
512
+ },
513
+ options,
514
+ host,
515
+ )
516
+ }
453
517
  return start(setup, host, options, true)
454
518
  }
package/src/floating.ts CHANGED
@@ -13,7 +13,7 @@ import type {
13
13
  CapabilityTarget,
14
14
  } from "./capabilities.ts"
15
15
  import type { Scope } from "./core.ts"
16
- import { fail, onMount, within } from "./core.ts"
16
+ import { onMount, reportScopeError, within } from "./core.ts"
17
17
 
18
18
  export interface FloatingAttributes<Kind extends string = `dom`> {
19
19
  [`floating:reference`]?: Kind extends `dom` ? ReferenceElement | null : never
@@ -108,11 +108,7 @@ function attach(target: CapabilityTarget, scope?: Scope): CapabilityBinding {
108
108
  })
109
109
  return
110
110
  }
111
- try {
112
- fail(scope._root, error)
113
- } catch (failure) {
114
- scope._root.error(failure)
115
- }
111
+ reportScopeError(scope, error)
116
112
  }
117
113
  const updatePosition = () => {
118
114
  const anchor = reference
package/src/hot.ts ADDED
@@ -0,0 +1,190 @@
1
+ import { IMPLICIT } from "atom.io/internal"
2
+
3
+ import type { Component, Root, RootOptions } from "./core.ts"
4
+ import { onCleanup, owner } from "./core.ts"
5
+
6
+ interface Handle {
7
+ dispose(): void
8
+ flush(): void
9
+ }
10
+ type Factory = (options: RootOptions, before: Node | null) => Handle
11
+ interface View {
12
+ modules: Set<string>
13
+ options: RootOptions
14
+ refreshing: boolean
15
+ refresh(): void
16
+ report(error: unknown): void
17
+ }
18
+
19
+ function createHotRuntime() {
20
+ const components = new Map<string, { current: Component; proxy: Component }>()
21
+ const roots = new WeakMap<Root, View>()
22
+ const views = new Set<View>()
23
+ let mounting: View | undefined
24
+ function track(root: Root): void {
25
+ if (mounting) {
26
+ roots.set(root, mounting)
27
+ mounting.options.idPrefix ??= root._prefix
28
+ }
29
+ }
30
+ function onDispose(cleanup: () => void): void {
31
+ const view = roots.get(owner!._root)
32
+ onCleanup(() => {
33
+ if (!view?.refreshing) cleanup()
34
+ })
35
+ }
36
+ function register<T extends Component>(
37
+ id: string,
38
+ name: string,
39
+ implementation: T,
40
+ ): T {
41
+ const key = JSON.stringify([id, name])
42
+ let record = components.get(key)
43
+ if (record) record.current = implementation
44
+ else {
45
+ const created = {
46
+ current: implementation as Component,
47
+ proxy: null! as Component,
48
+ }
49
+ created.proxy = Object.assign(
50
+ (props: Record<string, any>) => {
51
+ if (owner) {
52
+ const root = owner._root
53
+ const view = roots.get(root) ?? mounting
54
+ if (view) {
55
+ roots.set(root, view)
56
+ view.options.idPrefix ??= root._prefix
57
+ view.modules.add(id)
58
+ }
59
+ }
60
+ return created.current(props)
61
+ },
62
+ { spa: true },
63
+ )
64
+ record = created
65
+ components.set(key, record)
66
+ }
67
+ return record.proxy as T
68
+ }
69
+ function manage(
70
+ factory: Factory,
71
+ options: RootOptions,
72
+ host?: Element,
73
+ ): Handle {
74
+ const settings = { ...options, store: options.store ?? IMPLICIT.STORE }
75
+ const anchor = host?.ownerDocument.createComment(`spa-hot`)
76
+ let current: Handle
77
+ let errorNode: HTMLElement | undefined
78
+ let disposed = false
79
+ const placeAnchor = () => {
80
+ if (!anchor || anchor.parentNode) return
81
+ let inside = false
82
+ for (const node of [...host!.childNodes]) {
83
+ if (node.nodeType === 8 && node.nodeValue === `w:${settings.idPrefix}`)
84
+ inside = true
85
+ if (inside && node.nodeType === 8 && node.nodeValue === `/w`) {
86
+ host!.insertBefore(anchor, node.nextSibling)
87
+ return
88
+ }
89
+ }
90
+ host!.append(anchor)
91
+ }
92
+ const boot = () => {
93
+ const previous = mounting
94
+ mounting = view
95
+ try {
96
+ current = factory(settings, anchor?.parentNode ? anchor : null)
97
+ } finally {
98
+ mounting = previous
99
+ }
100
+ }
101
+ const view: View = {
102
+ modules: new Set(),
103
+ options: settings,
104
+ refreshing: false,
105
+ refresh() {
106
+ view.refreshing = true
107
+ try {
108
+ placeAnchor()
109
+ current.dispose()
110
+ errorNode?.remove()
111
+ boot()
112
+ } finally {
113
+ view.refreshing = false
114
+ }
115
+ },
116
+ report(error) {
117
+ const target =
118
+ host ?? (typeof document === `undefined` ? undefined : document.body)
119
+ if (target) {
120
+ errorNode = target.ownerDocument.createElement(`pre`)
121
+ errorNode.setAttribute(`data-spa-hot-error`, ``)
122
+ errorNode.textContent = `SPA hot refresh failed: ${String(error)}`
123
+ target.insertBefore(errorNode, anchor?.parentNode ? anchor : null)
124
+ }
125
+ options.onError?.(error)
126
+ },
127
+ }
128
+ views.add(view)
129
+ try {
130
+ boot()
131
+ } catch (error) {
132
+ views.delete(view)
133
+ throw error
134
+ }
135
+ if (view.modules.size) placeAnchor()
136
+ return {
137
+ flush() {
138
+ current.flush()
139
+ },
140
+ dispose() {
141
+ if (disposed) return
142
+ disposed = true
143
+ views.delete(view)
144
+ errorNode?.remove()
145
+ anchor?.remove()
146
+ current.dispose()
147
+ },
148
+ }
149
+ }
150
+ function refreshViews(id: string): void {
151
+ for (const view of [...views]) {
152
+ if (!views.has(view) || !view.modules.has(id)) continue
153
+ try {
154
+ view.refresh()
155
+ } catch (error) {
156
+ view.report(error)
157
+ }
158
+ }
159
+ }
160
+ return { register, root: manage, refresh: refreshViews, track, onDispose }
161
+ }
162
+
163
+ const runtime = import.meta.env.DEV ? createHotRuntime() : undefined
164
+
165
+ /** Development protocol used by the Vite transform. */
166
+ export function registerComponent<T extends Component>(
167
+ id: string,
168
+ name: string,
169
+ implementation: T,
170
+ ): T {
171
+ return runtime ? runtime.register(id, name, implementation) : implementation
172
+ }
173
+ export function refresh(id: string): void {
174
+ runtime?.refresh(id)
175
+ }
176
+ export function trackRoot(root: Root): void {
177
+ runtime?.track(root)
178
+ }
179
+ /** Release root lifetime resources, retaining them through development remounts. */
180
+ export function onRootDispose(cleanup: () => void): void {
181
+ if (runtime) runtime.onDispose(cleanup)
182
+ else onCleanup(cleanup)
183
+ }
184
+ export function hotRoot(
185
+ factory: Factory,
186
+ options: RootOptions,
187
+ host?: Element,
188
+ ): Handle {
189
+ return runtime ? runtime.root(factory, options, host) : factory(options, null)
190
+ }
@@ -0,0 +1,39 @@
1
+ import type { Canonical } from "atom.io/foundations/canonical"
2
+ import { packCanonical } from "atom.io/foundations/canonical"
3
+
4
+ import type { Children, SpaElement } from "./core.ts"
5
+
6
+ type Props = SpaElement[`props`]
7
+ type Key = Canonical | undefined
8
+ type Identity = string | number
9
+
10
+ function childProps(value: Children): Props | undefined {
11
+ return (value as SpaElement | null)?.props
12
+ }
13
+
14
+ function hasKey(value: Children): boolean {
15
+ if (Array.isArray(value)) return value.some(hasKey)
16
+ const props = childProps(value)
17
+ return props !== undefined && Object.hasOwn(props, `key`)
18
+ }
19
+
20
+ /** Direct output opts into key tracking when it first mounts. */
21
+ export function trackKeys(value: Children): Children {
22
+ return hasKey(value) ? () => value : value
23
+ }
24
+
25
+ export function identifyChildren(
26
+ output: Children,
27
+ duplicate = `spa: duplicate key`,
28
+ ): { value: Children; key: Key; id: Identity }[] {
29
+ const values = ([output] as unknown[]).flat(Infinity) as Children[]
30
+ const seen = new Set<string>()
31
+ return values.map((value, index) => {
32
+ const key: Key = childProps(value)?.[`key`]
33
+ if (key === undefined) return { value, key, id: index }
34
+ const id = packCanonical(key)
35
+ if (seen.has(id)) throw new Error(duplicate)
36
+ seen.add(id)
37
+ return { value, key, id }
38
+ })
39
+ }
package/src/index.ts CHANGED
@@ -13,7 +13,9 @@ export type {
13
13
  } from "./core.ts"
14
14
  export {
15
15
  onCleanup,
16
+ onCommit,
16
17
  onMount,
18
+ RootBoundary as Root,
17
19
  useDisposal,
18
20
  useGetter,
19
21
  useId,