@bun-native/renderer 0.0.0-stage → 0.33.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.
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/bun-native-source.json +59 -0
- package/package.json +29 -4
- package/src/event-properties.ts +54 -0
- package/src/index.ts +85 -0
- package/src/input-intent.ts +41 -0
- package/src/native-tree.ts +248 -0
- package/src/nodes.ts +644 -0
- package/src/order.ts +52 -0
- package/src/root.ts +364 -0
- package/src/window-lifetime.ts +42 -0
package/src/nodes.ts
ADDED
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NativeStateError,
|
|
3
|
+
UnsupportedFeatureError,
|
|
4
|
+
type DropTargetEvent,
|
|
5
|
+
type Element,
|
|
6
|
+
type InputState,
|
|
7
|
+
type NativeBatch,
|
|
8
|
+
type NativeDropTarget,
|
|
9
|
+
type NativeNode,
|
|
10
|
+
type Style,
|
|
11
|
+
} from '@bun-native/core'
|
|
12
|
+
import { captureInput, type InputIntent } from './input-intent.ts'
|
|
13
|
+
import {
|
|
14
|
+
isEventProperty,
|
|
15
|
+
listeners,
|
|
16
|
+
type EventListener,
|
|
17
|
+
type EventListeners,
|
|
18
|
+
} from './event-properties.ts'
|
|
19
|
+
|
|
20
|
+
declare const identity: unique symbol
|
|
21
|
+
|
|
22
|
+
/** An adapter-owned JSX reference, not a DOM node or a native handle. */
|
|
23
|
+
export interface RendererNode {
|
|
24
|
+
readonly [identity]: true
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Owner {
|
|
28
|
+
readonly token: object
|
|
29
|
+
changed(): void
|
|
30
|
+
detached(node: Node): void
|
|
31
|
+
input(node: Node): InputState
|
|
32
|
+
/** Commit staged changes, then send one request batch for the committed handle. */
|
|
33
|
+
request(node: Node, send: (batch: NativeBatch, handle: NativeNode) => void): Promise<void>
|
|
34
|
+
registerDropTarget(
|
|
35
|
+
node: Node,
|
|
36
|
+
listener: (event: DropTargetEvent) => void | Promise<void>
|
|
37
|
+
): Promise<NativeDropTarget>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type Properties = EventListeners & {
|
|
41
|
+
style?: Style
|
|
42
|
+
text?: string
|
|
43
|
+
value?: string | InputIntent
|
|
44
|
+
placeholder?: string
|
|
45
|
+
disabled?: boolean
|
|
46
|
+
ratio?: number
|
|
47
|
+
min_size?: number
|
|
48
|
+
anchor?: 'window' | 'parent'
|
|
49
|
+
modal?: boolean
|
|
50
|
+
autofocus?: boolean
|
|
51
|
+
navigation?: 'tab' | 'menu'
|
|
52
|
+
surface?: 'layer' | 'window'
|
|
53
|
+
readOnly?: boolean
|
|
54
|
+
accessibleLabel?: string
|
|
55
|
+
accessibleRole?: 'list' | 'list_item'
|
|
56
|
+
accessibleSetSize?: number
|
|
57
|
+
accessiblePosition?: number
|
|
58
|
+
accessibleSelected?: boolean
|
|
59
|
+
accessibleLive?: 'polite' | 'assertive'
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Every native element tag the renderer accepts. */
|
|
63
|
+
export type ElementTag = Element['kind']
|
|
64
|
+
|
|
65
|
+
/** Every property name `setProperty` accepts: element props and event handlers. */
|
|
66
|
+
export type PropertyName = keyof Properties
|
|
67
|
+
|
|
68
|
+
const isContainer = (kind: Node['kind']) => kind === 'box' || kind === 'split' || kind === 'overlay'
|
|
69
|
+
|
|
70
|
+
const nodes = new WeakSet<object>()
|
|
71
|
+
|
|
72
|
+
export class Node implements RendererNode {
|
|
73
|
+
declare readonly [identity]: true
|
|
74
|
+
/**
|
|
75
|
+
* Staged children, where a detached child leaves an `undefined` hole until
|
|
76
|
+
* the list is next read: removing a child from a wide list then neither
|
|
77
|
+
* searches nor shifts it. Positions in this list are the `index` hints.
|
|
78
|
+
*/
|
|
79
|
+
readonly staged: (Node | undefined)[] = []
|
|
80
|
+
/** Holes in `staged`, the first and last of them. */
|
|
81
|
+
holes = 0
|
|
82
|
+
firstHole = 0
|
|
83
|
+
lastHole = 0
|
|
84
|
+
/** Read-only to adapters: setProperty validates, stores and invalidates captures. */
|
|
85
|
+
readonly properties: Readonly<Properties> = {}
|
|
86
|
+
parent: Node | undefined
|
|
87
|
+
owner: Owner | undefined
|
|
88
|
+
scope: object | undefined
|
|
89
|
+
data = ''
|
|
90
|
+
/** Last immutable capture; cleared by every mutation that can change it. */
|
|
91
|
+
captured: Snapshot | undefined
|
|
92
|
+
/**
|
|
93
|
+
* This node or a staged descendant lacks a current capture. Every dirty node
|
|
94
|
+
* in a tree has dirty ancestors, so snapshot() walks only dirty paths.
|
|
95
|
+
*/
|
|
96
|
+
dirty = false
|
|
97
|
+
/**
|
|
98
|
+
* Dirty children registered when they became dirty, so a parent whose own
|
|
99
|
+
* capture is current visits only them. A recaptured parent scans all of its
|
|
100
|
+
* children instead, which covers a child that arrived already dirty.
|
|
101
|
+
*/
|
|
102
|
+
dirtyChildren: Set<Node> | undefined
|
|
103
|
+
/** Position hint in the parent's children; trusted only once verified. */
|
|
104
|
+
index = 0
|
|
105
|
+
/** Children before this position have current `index` hints. */
|
|
106
|
+
indexed = 0
|
|
107
|
+
|
|
108
|
+
constructor(readonly kind: Element['kind'] | 'text-node') {
|
|
109
|
+
nodes.add(this)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The staged children in order; adapters may truncate this list directly. */
|
|
113
|
+
get children(): Node[] {
|
|
114
|
+
if (this.holes) compact(this)
|
|
115
|
+
return this.staged as Node[]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function asNode(value: RendererNode): Node {
|
|
120
|
+
if (!value || typeof value !== 'object' || !nodes.has(value))
|
|
121
|
+
throw new NativeStateError('invalid_handle', 'Expected an owned staged native node')
|
|
122
|
+
return value as Node
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function createElement(tag: ElementTag): Node {
|
|
126
|
+
if (
|
|
127
|
+
tag !== 'box' &&
|
|
128
|
+
tag !== 'text' &&
|
|
129
|
+
tag !== 'button' &&
|
|
130
|
+
tag !== 'input' &&
|
|
131
|
+
tag !== 'split' &&
|
|
132
|
+
tag !== 'overlay'
|
|
133
|
+
)
|
|
134
|
+
throw new UnsupportedFeatureError('native-element:' + tag)
|
|
135
|
+
return new Node(tag)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function createTextNode(value: string): Node {
|
|
139
|
+
const node = new Node('text-node')
|
|
140
|
+
node.data = String(value)
|
|
141
|
+
return node
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Discard a cached capture after changing this node's own snapshot inputs. */
|
|
145
|
+
export function invalidate(node: Node | undefined): void {
|
|
146
|
+
if (!node) return
|
|
147
|
+
node.captured = undefined
|
|
148
|
+
// An already dirty ancestor's own ancestors are dirty too.
|
|
149
|
+
for (let path: Node | undefined = node; path && !path.dirty; path = path.parent) {
|
|
150
|
+
path.dirty = true
|
|
151
|
+
if (path.parent) (path.parent.dirtyChildren ??= new Set()).add(path)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Gap of unverified siblings worth re-indexing to extend the valid prefix. */
|
|
156
|
+
const reindexLimit = 64
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A child's position in its parent's `staged` list. Hints are verified before
|
|
160
|
+
* use, so a list an adapter truncated directly stays correct. A removal leaves
|
|
161
|
+
* every hint valid and an insertion only shortens the valid prefix to its
|
|
162
|
+
* position, so regions edited in order are found without a search.
|
|
163
|
+
*/
|
|
164
|
+
function childIndex(parent: Node, child: Node): number {
|
|
165
|
+
const list = parent.staged
|
|
166
|
+
const hint = child.index
|
|
167
|
+
if (list[hint] === child) return hint
|
|
168
|
+
const last = list.length - 1
|
|
169
|
+
if (list[last] === child) return (child.index = last)
|
|
170
|
+
const start = Math.min(parent.indexed, list.length)
|
|
171
|
+
let found = list.indexOf(child, start)
|
|
172
|
+
if (found < 0) found = list.indexOf(child)
|
|
173
|
+
else if (found - start <= reindexLimit) {
|
|
174
|
+
for (let index = start; index < found; index += 1) {
|
|
175
|
+
const sibling = list[index]
|
|
176
|
+
if (sibling) sibling.index = index
|
|
177
|
+
}
|
|
178
|
+
parent.indexed = found + 1
|
|
179
|
+
}
|
|
180
|
+
if (found >= 0) child.index = found
|
|
181
|
+
return found
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function contiguousHoles(parent: Node): boolean {
|
|
185
|
+
return parent.lastHole - parent.firstHole + 1 === parent.holes
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Drop the holes in one pass, or one native splice when they are adjacent. */
|
|
189
|
+
function compact(parent: Node): void {
|
|
190
|
+
const list = parent.staged
|
|
191
|
+
const first = parent.firstHole
|
|
192
|
+
const adjacent = contiguousHoles(parent)
|
|
193
|
+
if (adjacent) list.splice(first, parent.holes)
|
|
194
|
+
else {
|
|
195
|
+
let write = first
|
|
196
|
+
for (let read = first; read < list.length; read += 1) {
|
|
197
|
+
const child = list[read]
|
|
198
|
+
if (!child) continue
|
|
199
|
+
child.index = write
|
|
200
|
+
list[write++] = child
|
|
201
|
+
}
|
|
202
|
+
list.length = write
|
|
203
|
+
}
|
|
204
|
+
// Hints from the first hole on moved, unless the pass above rewrote them.
|
|
205
|
+
if (parent.indexed >= first) parent.indexed = adjacent ? first : list.length
|
|
206
|
+
parent.holes = 0
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function detachChild(parent: Node, child: Node): void {
|
|
210
|
+
parent.dirtyChildren?.delete(child)
|
|
211
|
+
const list = parent.staged
|
|
212
|
+
const index = childIndex(parent, child)
|
|
213
|
+
if (index < 0) return
|
|
214
|
+
// Shifting the remaining siblings would cost O(children) per removal.
|
|
215
|
+
// Never shift(): JavaScriptCore then keeps the list in a much slower mode.
|
|
216
|
+
if (index === list.length - 1) {
|
|
217
|
+
list.pop()
|
|
218
|
+
if (index < parent.indexed) parent.indexed = index
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
list[index] = undefined
|
|
222
|
+
if (!parent.holes) parent.firstHole = parent.lastHole = index
|
|
223
|
+
else if (index < parent.firstHole) parent.firstHole = index
|
|
224
|
+
else if (index > parent.lastHole) parent.lastHole = index
|
|
225
|
+
parent.holes += 1
|
|
226
|
+
// Bound the holes of a list that is edited but never read, such as one
|
|
227
|
+
// outside a render root; compacting at half keeps removals amortized O(1).
|
|
228
|
+
if (parent.holes * 2 > list.length) compact(parent)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function attachChild(parent: Node, child: Node, index: number): void {
|
|
232
|
+
const list = parent.staged
|
|
233
|
+
if (index === list.length) list.push(child)
|
|
234
|
+
else list.splice(index, 0, child)
|
|
235
|
+
if (parent.holes) {
|
|
236
|
+
if (index <= parent.firstHole) parent.firstHole += 1
|
|
237
|
+
if (index <= parent.lastHole) parent.lastHole += 1
|
|
238
|
+
}
|
|
239
|
+
if (index < parent.indexed) parent.indexed = index
|
|
240
|
+
child.index = index
|
|
241
|
+
if (parent.indexed === index) parent.indexed = index + 1
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** The first staged child, without compacting a run of leading holes. */
|
|
245
|
+
export function firstChild(node: Node): Node | undefined {
|
|
246
|
+
if (node.holes && node.firstHole === 0 && contiguousHoles(node))
|
|
247
|
+
return node.staged[node.lastHole + 1]
|
|
248
|
+
return node.children[0]
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The staged sibling after `node`, for adapters that walk sibling chains. */
|
|
252
|
+
export function nextSibling(node: Node): Node | undefined {
|
|
253
|
+
const parent = node.parent
|
|
254
|
+
if (!parent) return undefined
|
|
255
|
+
const list = parent.staged
|
|
256
|
+
let index = childIndex(parent, node) + 1
|
|
257
|
+
if (parent.holes && index === parent.firstHole && contiguousHoles(parent))
|
|
258
|
+
index = parent.lastHole + 1
|
|
259
|
+
const start = index
|
|
260
|
+
while (index < list.length && !list[index]) index += 1
|
|
261
|
+
const sibling = list[index]
|
|
262
|
+
if (index - start > reindexLimit) compact(parent)
|
|
263
|
+
return sibling
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function visit(node: Node, fn: (node: Node) => void): void {
|
|
267
|
+
fn(node)
|
|
268
|
+
for (const child of node.children) visit(child, fn)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function checkScope(node: Node, token: object): void {
|
|
272
|
+
if (node.scope && node.scope !== token)
|
|
273
|
+
throw new NativeStateError('invalid_handle', 'Renderer nodes cannot move between render roots')
|
|
274
|
+
for (const child of node.children) checkScope(child, token)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Invalidate a subtree whose parent was just invalidated and hand it to
|
|
279
|
+
* `owner`, giving each node the owner's scope. Each node's parent already lacks
|
|
280
|
+
* a capture, so `snapshot()` scans all of its children and no node registers
|
|
281
|
+
* with its parent's `dirtyChildren`, which only a current capture reads.
|
|
282
|
+
*/
|
|
283
|
+
function adopt(node: Node, owner: Owner | undefined): void {
|
|
284
|
+
node.captured = undefined
|
|
285
|
+
node.dirty = true
|
|
286
|
+
node.owner = owner
|
|
287
|
+
if (owner) node.scope = owner.token
|
|
288
|
+
for (const child of node.children) adopt(child, owner)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function insertNode(parent: Node, node: Node, anchor?: Node): void {
|
|
292
|
+
if (parent.kind === 'input' || parent.kind === 'text-node')
|
|
293
|
+
throw new NativeStateError('invalid_state', 'This native element cannot have children')
|
|
294
|
+
if (!isContainer(parent.kind) && node.kind !== 'text-node')
|
|
295
|
+
throw new NativeStateError('invalid_state', 'Text and button children must be text')
|
|
296
|
+
if (anchor && anchor.parent !== parent)
|
|
297
|
+
throw new NativeStateError('invalid_handle', 'Insertion anchor belongs to another parent')
|
|
298
|
+
if (anchor === node) return
|
|
299
|
+
for (let ancestor: Node | undefined = parent; ancestor; ancestor = ancestor.parent)
|
|
300
|
+
if (ancestor === node) throw new NativeStateError('invalid_state', 'Native tree cycle')
|
|
301
|
+
const owner = parent.owner
|
|
302
|
+
// Every node an owner holds was given its scope with it (see adopt), so a
|
|
303
|
+
// subtree already owned by this root needs no check.
|
|
304
|
+
if (owner && node.owner !== owner) checkScope(node, owner.token)
|
|
305
|
+
const previousOwner = node.owner
|
|
306
|
+
// An owner only holds nodes attached under its root (removal and disposal
|
|
307
|
+
// clear it), so this subtree moves without ever leaving the live tree.
|
|
308
|
+
const moved = owner !== undefined && previousOwner === owner && node.parent !== undefined
|
|
309
|
+
if (previousOwner && previousOwner !== owner) previousOwner.detached(node)
|
|
310
|
+
if (node.parent) detachChild(node.parent, node)
|
|
311
|
+
invalidate(node.parent)
|
|
312
|
+
invalidate(parent)
|
|
313
|
+
node.parent = parent
|
|
314
|
+
attachChild(parent, node, anchor ? childIndex(parent, anchor) : parent.staged.length)
|
|
315
|
+
// Adapters may clear detached nodes directly, so a reinserted subtree is
|
|
316
|
+
// always captured again from its current state. A moved subtree's captures
|
|
317
|
+
// and dirty paths stay current below it: its root's capture names the old
|
|
318
|
+
// parent, so snapshot() recaptures that node alone, and the diff moves it
|
|
319
|
+
// natively while every descendant keeps its committed record.
|
|
320
|
+
if (!moved) adopt(node, owner)
|
|
321
|
+
previousOwner?.changed()
|
|
322
|
+
owner?.changed()
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function removeNode(parent: Node, node: Node): void {
|
|
326
|
+
if (node.parent !== parent)
|
|
327
|
+
throw new NativeStateError('invalid_handle', 'Removal target belongs to another parent')
|
|
328
|
+
const owner = parent.owner
|
|
329
|
+
owner?.detached(node)
|
|
330
|
+
detachChild(parent, node)
|
|
331
|
+
invalidate(parent)
|
|
332
|
+
node.parent = undefined
|
|
333
|
+
// A detached capture would otherwise retain listeners an adapter releases.
|
|
334
|
+
adopt(node, undefined)
|
|
335
|
+
owner?.changed()
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export function replaceText(node: Node, value: string): void {
|
|
339
|
+
if (node.kind !== 'text-node')
|
|
340
|
+
throw new NativeStateError('invalid_state', 'Text replacement requires a text node')
|
|
341
|
+
if (node.data === value) return
|
|
342
|
+
node.data = value
|
|
343
|
+
invalidate(node)
|
|
344
|
+
// Text and button elements join their text-node children.
|
|
345
|
+
invalidate(node.parent)
|
|
346
|
+
node.owner?.changed()
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function setProperty(node: Node, name: PropertyName, value: unknown): void {
|
|
350
|
+
const props = node.properties as Properties
|
|
351
|
+
if (isEventProperty(name)) {
|
|
352
|
+
if (node.kind === 'text-node')
|
|
353
|
+
throw new TypeError('Text literals cannot own native event handlers')
|
|
354
|
+
if (value === undefined) delete props[name]
|
|
355
|
+
else if (typeof value === 'function') props[name] = value as EventListener
|
|
356
|
+
else throw new TypeError(`Native ${name} must be a function`)
|
|
357
|
+
invalidate(node)
|
|
358
|
+
node.owner?.changed()
|
|
359
|
+
return
|
|
360
|
+
}
|
|
361
|
+
switch (name) {
|
|
362
|
+
case 'style':
|
|
363
|
+
if (value === undefined) delete props.style
|
|
364
|
+
else if (value && typeof value === 'object' && !Array.isArray(value))
|
|
365
|
+
props.style = { ...value }
|
|
366
|
+
else throw new TypeError('Native style must be an object')
|
|
367
|
+
break
|
|
368
|
+
case 'text':
|
|
369
|
+
if (node.kind !== 'text' && node.kind !== 'button')
|
|
370
|
+
throw new TypeError('Only text and button elements accept text')
|
|
371
|
+
if (value === undefined) delete props.text
|
|
372
|
+
else if (typeof value === 'string') props.text = value
|
|
373
|
+
else throw new TypeError('Native text must be a string')
|
|
374
|
+
break
|
|
375
|
+
case 'value': {
|
|
376
|
+
if (node.kind !== 'input') throw new TypeError('Only inputs accept value')
|
|
377
|
+
const captured = captureInput(value)
|
|
378
|
+
if (captured === undefined) delete props.value
|
|
379
|
+
else props.value = captured
|
|
380
|
+
break
|
|
381
|
+
}
|
|
382
|
+
case 'placeholder':
|
|
383
|
+
if (node.kind !== 'input') throw new TypeError('Only inputs accept placeholder')
|
|
384
|
+
if (value === undefined) delete props.placeholder
|
|
385
|
+
else if (typeof value === 'string') props.placeholder = value
|
|
386
|
+
else throw new TypeError('Native input placeholder must be a string')
|
|
387
|
+
break
|
|
388
|
+
case 'disabled':
|
|
389
|
+
if (node.kind !== 'button' && node.kind !== 'input')
|
|
390
|
+
throw new TypeError('Only buttons and inputs accept disabled')
|
|
391
|
+
if (value === undefined) delete props.disabled
|
|
392
|
+
else if (typeof value === 'boolean') props.disabled = value
|
|
393
|
+
else throw new TypeError('Native disabled must be boolean')
|
|
394
|
+
break
|
|
395
|
+
case 'ratio':
|
|
396
|
+
case 'min_size':
|
|
397
|
+
if (node.kind !== 'split') throw new TypeError('Only splits accept ' + name)
|
|
398
|
+
if (value === undefined) delete props[name]
|
|
399
|
+
else if (typeof value === 'number') props[name] = value
|
|
400
|
+
else throw new TypeError(`Native split ${name} must be a number`)
|
|
401
|
+
break
|
|
402
|
+
case 'anchor':
|
|
403
|
+
if (node.kind !== 'overlay') throw new TypeError('Only overlays accept anchor')
|
|
404
|
+
if (value === undefined) delete props.anchor
|
|
405
|
+
else if (value === 'window' || value === 'parent') props.anchor = value
|
|
406
|
+
else throw new TypeError('Native overlay anchor must be window or parent')
|
|
407
|
+
break
|
|
408
|
+
case 'modal':
|
|
409
|
+
if (node.kind !== 'overlay') throw new TypeError('Only overlays accept modal')
|
|
410
|
+
if (value === undefined) delete props.modal
|
|
411
|
+
else if (typeof value === 'boolean') props.modal = value
|
|
412
|
+
else throw new TypeError('Native modal must be boolean')
|
|
413
|
+
break
|
|
414
|
+
case 'autofocus':
|
|
415
|
+
if (node.kind !== 'overlay') throw new TypeError('Only overlays accept autofocus')
|
|
416
|
+
if (value === undefined) delete props.autofocus
|
|
417
|
+
else if (typeof value === 'boolean') props.autofocus = value
|
|
418
|
+
else throw new TypeError('Native autofocus must be boolean')
|
|
419
|
+
break
|
|
420
|
+
case 'navigation':
|
|
421
|
+
if (node.kind !== 'overlay') throw new TypeError('Only overlays accept navigation')
|
|
422
|
+
if (value === undefined) delete props.navigation
|
|
423
|
+
else if (value === 'tab' || value === 'menu') props.navigation = value
|
|
424
|
+
else throw new TypeError('Native overlay navigation must be tab or menu')
|
|
425
|
+
break
|
|
426
|
+
case 'surface':
|
|
427
|
+
if (node.kind !== 'overlay') throw new TypeError('Only overlays accept surface')
|
|
428
|
+
if (value === undefined) delete props.surface
|
|
429
|
+
else if (value === 'layer' || value === 'window') props.surface = value
|
|
430
|
+
else throw new TypeError('Native overlay surface must be layer or window')
|
|
431
|
+
break
|
|
432
|
+
case 'accessibleLabel':
|
|
433
|
+
if (node.kind !== 'split' && node.kind !== 'overlay' && node.kind !== 'box')
|
|
434
|
+
throw new TypeError('Only boxes, splits and overlays accept accessibleLabel')
|
|
435
|
+
if (value === undefined) delete props.accessibleLabel
|
|
436
|
+
else if (typeof value === 'string') props.accessibleLabel = value
|
|
437
|
+
else throw new TypeError('Native accessibleLabel must be a string')
|
|
438
|
+
break
|
|
439
|
+
case 'accessibleRole':
|
|
440
|
+
if (node.kind !== 'box') throw new TypeError('Only boxes accept accessibleRole')
|
|
441
|
+
if (value === undefined) delete props.accessibleRole
|
|
442
|
+
else if (value === 'list' || value === 'list_item') props.accessibleRole = value
|
|
443
|
+
else throw new TypeError('Native accessibleRole must be list or list_item')
|
|
444
|
+
break
|
|
445
|
+
case 'accessibleSetSize':
|
|
446
|
+
case 'accessiblePosition':
|
|
447
|
+
if (node.kind !== 'box') throw new TypeError('Only boxes accept ' + name)
|
|
448
|
+
if (value === undefined) delete props[name]
|
|
449
|
+
else if (typeof value === 'number' && Number.isSafeInteger(value)) props[name] = value
|
|
450
|
+
else throw new TypeError(`Native ${name} must be an integer`)
|
|
451
|
+
break
|
|
452
|
+
case 'accessibleSelected':
|
|
453
|
+
if (node.kind !== 'box') throw new TypeError('Only boxes accept accessibleSelected')
|
|
454
|
+
if (value === undefined) delete props.accessibleSelected
|
|
455
|
+
else if (typeof value === 'boolean') props.accessibleSelected = value
|
|
456
|
+
else throw new TypeError('Native accessibleSelected must be boolean')
|
|
457
|
+
break
|
|
458
|
+
case 'accessibleLive':
|
|
459
|
+
if (node.kind !== 'text') throw new TypeError('Only text accepts accessibleLive')
|
|
460
|
+
if (value === undefined) delete props.accessibleLive
|
|
461
|
+
else if (value === 'polite' || value === 'assertive') props.accessibleLive = value
|
|
462
|
+
else throw new TypeError('Native accessibleLive must be polite or assertive')
|
|
463
|
+
break
|
|
464
|
+
case 'readOnly':
|
|
465
|
+
if (node.kind !== 'input') throw new TypeError('Only inputs accept readOnly')
|
|
466
|
+
if (value === undefined) delete props.readOnly
|
|
467
|
+
else if (typeof value === 'boolean') props.readOnly = value
|
|
468
|
+
else throw new TypeError('Native readOnly must be boolean')
|
|
469
|
+
break
|
|
470
|
+
default:
|
|
471
|
+
throw new UnsupportedFeatureError('native-property:' + name)
|
|
472
|
+
}
|
|
473
|
+
invalidate(node)
|
|
474
|
+
node.owner?.changed()
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** Immutable once captured: unchanged nodes share it across commits. */
|
|
478
|
+
export type Snapshot = Readonly<{
|
|
479
|
+
element: Element
|
|
480
|
+
children: readonly Node[]
|
|
481
|
+
parent: Node | undefined
|
|
482
|
+
events: Readonly<EventListeners>
|
|
483
|
+
initialValue: string | undefined
|
|
484
|
+
inputIntent: InputIntent | undefined
|
|
485
|
+
}>
|
|
486
|
+
|
|
487
|
+
function leafStyle(style: Style): NonNullable<Extract<Element, { kind: 'text' }>['style']> {
|
|
488
|
+
const { overflow, scroll_anchor, ...rest } = style
|
|
489
|
+
if (
|
|
490
|
+
scroll_anchor !== undefined ||
|
|
491
|
+
(overflow !== undefined && overflow !== 'visible' && overflow !== 'hidden')
|
|
492
|
+
)
|
|
493
|
+
throw new TypeError('Only native boxes and overlays support scrolling')
|
|
494
|
+
return { ...rest, ...(overflow === undefined ? {} : { overflow }) }
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Capture values before awaiting the core queue; later effects form another batch.
|
|
499
|
+
*
|
|
500
|
+
* Returns only nodes captured anew: every other staged node keeps the immutable
|
|
501
|
+
* capture its acknowledged committed record already holds. The walk descends
|
|
502
|
+
* only dirty paths, so a clean subtree costs nothing. A node can leave its
|
|
503
|
+
* committed position only through a mutation that invalidates its old parent
|
|
504
|
+
* and leaves its own capture naming another parent (or none), so the diff
|
|
505
|
+
* finds removals and moves from the old and new child lists of recaptured
|
|
506
|
+
* nodes alone.
|
|
507
|
+
*/
|
|
508
|
+
export function snapshot(root: Node): Map<Node, Snapshot> {
|
|
509
|
+
const result = new Map<Node, Snapshot>()
|
|
510
|
+
const current = (node: Node, parent: Node | undefined): boolean =>
|
|
511
|
+
!node.dirty && node.captured !== undefined && node.captured.parent === parent
|
|
512
|
+
const capture = (node: Node, parent: Node | undefined): void => {
|
|
513
|
+
node.dirty = false
|
|
514
|
+
const dirty = node.dirtyChildren
|
|
515
|
+
node.dirtyChildren = undefined
|
|
516
|
+
let captured = node.captured
|
|
517
|
+
if (!captured || captured.parent !== parent) {
|
|
518
|
+
captured = build(node, parent)
|
|
519
|
+
node.captured = captured
|
|
520
|
+
result.set(node, captured)
|
|
521
|
+
} else if (dirty && dirty.size * 8 < captured.children.length) {
|
|
522
|
+
// A current capture's children are this node's children, and each one
|
|
523
|
+
// that became dirty registered itself, so only those are visited, in
|
|
524
|
+
// child order: the diff emits native operations in snapshot order.
|
|
525
|
+
for (const child of inChildOrder(node, dirty)) if (!current(child, node)) capture(child, node)
|
|
526
|
+
return
|
|
527
|
+
}
|
|
528
|
+
for (const child of captured.children) if (!current(child, node)) capture(child, node)
|
|
529
|
+
}
|
|
530
|
+
if (!current(root, undefined)) capture(root, undefined)
|
|
531
|
+
return result
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** Registered children still under `parent`, in staged child order. */
|
|
535
|
+
function inChildOrder(parent: Node, children: ReadonlySet<Node>): Node[] {
|
|
536
|
+
const positioned: [number, Node][] = []
|
|
537
|
+
for (const child of children)
|
|
538
|
+
if (child.parent === parent) positioned.push([childIndex(parent, child), child])
|
|
539
|
+
if (positioned.length > 1) positioned.sort((left, right) => left[0] - right[0])
|
|
540
|
+
return positioned.map((entry) => entry[1])
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* The staged children as an immutable list, sharing the previous capture's
|
|
545
|
+
* list when they are unchanged (a moved node), so the diff skips it by identity.
|
|
546
|
+
*/
|
|
547
|
+
function childList(node: Node): readonly Node[] {
|
|
548
|
+
const children = node.children
|
|
549
|
+
const previous = node.captured?.children
|
|
550
|
+
if (
|
|
551
|
+
previous?.length === children.length &&
|
|
552
|
+
previous.every((child, index) => child === children[index])
|
|
553
|
+
)
|
|
554
|
+
return previous
|
|
555
|
+
return [...children]
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function build(node: Node, parent: Node | undefined): Snapshot {
|
|
559
|
+
const props = node.properties
|
|
560
|
+
const style = props.style && !isContainer(node.kind) ? { style: leafStyle(props.style) } : {}
|
|
561
|
+
let element: Element
|
|
562
|
+
if (node.kind === 'box')
|
|
563
|
+
// Core validation rejects members that do not fit the role.
|
|
564
|
+
element = {
|
|
565
|
+
kind: 'box',
|
|
566
|
+
...(props.accessibleRole === undefined ? {} : { accessible_role: props.accessibleRole }),
|
|
567
|
+
...(props.accessibleLabel === undefined ? {} : { accessible_label: props.accessibleLabel }),
|
|
568
|
+
...(props.accessibleSetSize === undefined
|
|
569
|
+
? {}
|
|
570
|
+
: { accessible_set_size: props.accessibleSetSize }),
|
|
571
|
+
...(props.accessiblePosition === undefined
|
|
572
|
+
? {}
|
|
573
|
+
: { accessible_position: props.accessiblePosition }),
|
|
574
|
+
...(props.accessibleSelected === undefined
|
|
575
|
+
? {}
|
|
576
|
+
: { accessible_selected: props.accessibleSelected }),
|
|
577
|
+
...(props.style ? { style: { ...props.style } } : {}),
|
|
578
|
+
} as Element
|
|
579
|
+
else if (node.kind === 'split')
|
|
580
|
+
// Core validation rejects gap and alignment on a split.
|
|
581
|
+
element = {
|
|
582
|
+
kind: 'split',
|
|
583
|
+
...(props.ratio === undefined ? {} : { ratio: props.ratio }),
|
|
584
|
+
...(props.min_size === undefined ? {} : { min_size: props.min_size }),
|
|
585
|
+
...(props.accessibleLabel === undefined ? {} : { accessible_label: props.accessibleLabel }),
|
|
586
|
+
...(props.style ? { style: leafStyle(props.style) } : {}),
|
|
587
|
+
}
|
|
588
|
+
else if (node.kind === 'overlay')
|
|
589
|
+
// Core validation rejects a modal parent-anchored overlay and an
|
|
590
|
+
// unsized or window-anchored popup surface.
|
|
591
|
+
element = {
|
|
592
|
+
kind: 'overlay',
|
|
593
|
+
...(props.anchor === undefined ? {} : { anchor: props.anchor }),
|
|
594
|
+
...(props.modal === undefined ? {} : { modal: props.modal }),
|
|
595
|
+
...(props.autofocus === undefined ? {} : { autofocus: props.autofocus }),
|
|
596
|
+
...(props.navigation === undefined ? {} : { navigation: props.navigation }),
|
|
597
|
+
...(props.surface === undefined ? {} : { surface: props.surface }),
|
|
598
|
+
...(props.accessibleLabel === undefined ? {} : { accessible_label: props.accessibleLabel }),
|
|
599
|
+
...(props.style ? { style: { ...props.style } } : {}),
|
|
600
|
+
} as Element
|
|
601
|
+
else if (node.kind === 'input')
|
|
602
|
+
element = {
|
|
603
|
+
kind: 'input',
|
|
604
|
+
value: typeof props.value === 'string' ? props.value : (props.value?.value ?? ''),
|
|
605
|
+
...(props.placeholder === undefined ? {} : { placeholder: props.placeholder }),
|
|
606
|
+
...(props.disabled ? { disabled: true } : {}),
|
|
607
|
+
...(props.readOnly ? { read_only: true } : {}),
|
|
608
|
+
...style,
|
|
609
|
+
}
|
|
610
|
+
else {
|
|
611
|
+
if (props.text !== undefined && node.children.length)
|
|
612
|
+
throw new TypeError('Choose text properties or text children, not both')
|
|
613
|
+
// Joined text-node children are part of this capture, not native nodes.
|
|
614
|
+
for (const child of node.children) child.dirty = false
|
|
615
|
+
const text =
|
|
616
|
+
node.kind === 'text-node'
|
|
617
|
+
? node.data
|
|
618
|
+
: (props.text ?? node.children.map((child) => child.data).join(''))
|
|
619
|
+
element =
|
|
620
|
+
node.kind === 'button'
|
|
621
|
+
? {
|
|
622
|
+
kind: 'button',
|
|
623
|
+
text,
|
|
624
|
+
...(props.disabled === undefined ? {} : { disabled: props.disabled }),
|
|
625
|
+
...style,
|
|
626
|
+
}
|
|
627
|
+
: {
|
|
628
|
+
kind: 'text',
|
|
629
|
+
text,
|
|
630
|
+
...(props.accessibleLive === undefined
|
|
631
|
+
? {}
|
|
632
|
+
: { accessible_live: props.accessibleLive }),
|
|
633
|
+
...style,
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return {
|
|
637
|
+
element,
|
|
638
|
+
children: isContainer(node.kind) ? childList(node) : [],
|
|
639
|
+
parent,
|
|
640
|
+
events: listeners(props),
|
|
641
|
+
initialValue: typeof props.value === 'string' ? props.value : undefined,
|
|
642
|
+
inputIntent: typeof props.value === 'object' ? props.value : undefined,
|
|
643
|
+
}
|
|
644
|
+
}
|
package/src/order.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flag, by index in `desired`, a longest existing sibling subsequence; every
|
|
3
|
+
* other desired child needs a move. Children are distinct within each list.
|
|
4
|
+
*/
|
|
5
|
+
export function stableChildren<T extends object>(
|
|
6
|
+
current: readonly T[],
|
|
7
|
+
desired: readonly T[]
|
|
8
|
+
): Uint8Array {
|
|
9
|
+
const stable = new Uint8Array(desired.length)
|
|
10
|
+
// A common prefix and suffix extend some longest subsequence, so only the
|
|
11
|
+
// middle needs the search; keyed edits usually leave both ends in place.
|
|
12
|
+
let start = 0
|
|
13
|
+
const shorter = Math.min(current.length, desired.length)
|
|
14
|
+
while (start < shorter && current[start] === desired[start]) stable[start++] = 1
|
|
15
|
+
let currentEnd = current.length
|
|
16
|
+
let desiredEnd = desired.length
|
|
17
|
+
while (
|
|
18
|
+
currentEnd > start &&
|
|
19
|
+
desiredEnd > start &&
|
|
20
|
+
current[currentEnd - 1] === desired[desiredEnd - 1]
|
|
21
|
+
) {
|
|
22
|
+
currentEnd -= 1
|
|
23
|
+
stable[--desiredEnd] = 1
|
|
24
|
+
}
|
|
25
|
+
if (start === currentEnd || start === desiredEnd) return stable
|
|
26
|
+
const positions = new Map<T, number>()
|
|
27
|
+
for (let index = start; index < currentEnd; index += 1) positions.set(current[index]!, index)
|
|
28
|
+
// tails[length - 1]: desired index ending the lowest-positioned run of that length.
|
|
29
|
+
const size = desiredEnd - start
|
|
30
|
+
const tails = new Int32Array(size)
|
|
31
|
+
const tailPositions = new Int32Array(size)
|
|
32
|
+
const preceding = new Int32Array(size)
|
|
33
|
+
let length = 0
|
|
34
|
+
for (let index = start; index < desiredEnd; index += 1) {
|
|
35
|
+
const position = positions.get(desired[index]!)
|
|
36
|
+
if (position === undefined) continue
|
|
37
|
+
let low = 0
|
|
38
|
+
let high = length
|
|
39
|
+
while (low < high) {
|
|
40
|
+
const middle = (low + high) >>> 1
|
|
41
|
+
if (tailPositions[middle]! < position) low = middle + 1
|
|
42
|
+
else high = middle
|
|
43
|
+
}
|
|
44
|
+
preceding[index - start] = low > 0 ? tails[low - 1]! : -1
|
|
45
|
+
tails[low] = index
|
|
46
|
+
tailPositions[low] = position
|
|
47
|
+
if (low === length) length += 1
|
|
48
|
+
}
|
|
49
|
+
for (let index = length ? tails[length - 1]! : -1; index >= 0; index = preceding[index - start]!)
|
|
50
|
+
stable[index] = 1
|
|
51
|
+
return stable
|
|
52
|
+
}
|