@bun-native/renderer 0.33.0 → 0.33.2
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/README.md +1 -1
- package/bun-native-source.json +18 -22
- package/package.json +2 -2
- package/src/event-properties.ts +3 -0
- package/src/index.ts +1 -1
- package/src/native-tree.ts +66 -49
- package/src/nodes.ts +45 -13
- package/src/root.ts +80 -34
- package/src/order.ts +0 -52
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
Native element tree and batching shared by Bun Native adapters.
|
|
4
4
|
|
|
5
|
-
Part of [Bun Native](https://github.com/0xPlayerOne/bun-native), an experimental developer preview. Version 0.33.
|
|
5
|
+
Part of [Bun Native](https://github.com/0xPlayerOne/bun-native), an experimental developer preview. Version 0.33.2 was built from [577f6fc5015b](https://github.com/0xPlayerOne/bun-native/tree/577f6fc5015b6eeef956efcc63f97e2a749d74ff) and published from GitHub Actions with npm provenance. Use the Bun revision pinned in that commit's toolchain.json. Start with the [getting-started guide](https://github.com/0xPlayerOne/bun-native/blob/577f6fc5015b6eeef956efcc63f97e2a749d74ff/docs/getting-started.md); `bun-native host install` fetches the matching release host. The license notice is in LICENSE.
|
package/bun-native-source.json
CHANGED
|
@@ -1,43 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
|
-
"commit": "
|
|
4
|
-
"tree": "
|
|
3
|
+
"commit": "577f6fc5015b6eeef956efcc63f97e2a749d74ff",
|
|
4
|
+
"tree": "c599e781af2ce0749d5e3a6d8700834690e872f4",
|
|
5
5
|
"files": {
|
|
6
6
|
"package.json": {
|
|
7
|
-
"bytes":
|
|
8
|
-
"sha256": "
|
|
7
|
+
"bytes": 2258,
|
|
8
|
+
"sha256": "38e2f0f89b5f6c4ba80b3baefcdf0806dd73c684294153df74ab470fda6bf3f9"
|
|
9
9
|
},
|
|
10
10
|
"packages/renderer/package.json": {
|
|
11
11
|
"bytes": 192,
|
|
12
12
|
"sha256": "720be385eb972eff6dac08b77948e09166ca11aa1db0100312cf6b7f36f53ae3"
|
|
13
13
|
},
|
|
14
14
|
"packages/renderer/src/event-properties.ts": {
|
|
15
|
-
"bytes":
|
|
16
|
-
"sha256": "
|
|
15
|
+
"bytes": 2407,
|
|
16
|
+
"sha256": "3f87af3950b787c5b8746d1743d516541e44d7eb6554b0653315a8a63cfb6520"
|
|
17
17
|
},
|
|
18
18
|
"packages/renderer/src/index.ts": {
|
|
19
|
-
"bytes":
|
|
20
|
-
"sha256": "
|
|
19
|
+
"bytes": 3197,
|
|
20
|
+
"sha256": "0d2e90764c87e734d896dc8bb3007340abe66a53d91fb05fe37bdc5d94b7bed2"
|
|
21
21
|
},
|
|
22
22
|
"packages/renderer/src/input-intent.ts": {
|
|
23
23
|
"bytes": 1698,
|
|
24
24
|
"sha256": "efe908fe1df1d14a7cbc1e3e30e8e76dc888fb865dca6b203bf91c18f3bb4d02"
|
|
25
25
|
},
|
|
26
26
|
"packages/renderer/src/native-tree.ts": {
|
|
27
|
-
"bytes":
|
|
28
|
-
"sha256": "
|
|
27
|
+
"bytes": 10697,
|
|
28
|
+
"sha256": "9bf0216b2adf0f2ac44d0d9bc0fd15f1b4501c333fc9b664c711e1e2960a4267"
|
|
29
29
|
},
|
|
30
30
|
"packages/renderer/src/nodes.ts": {
|
|
31
|
-
"bytes":
|
|
32
|
-
"sha256": "
|
|
33
|
-
},
|
|
34
|
-
"packages/renderer/src/order.ts": {
|
|
35
|
-
"bytes": 1994,
|
|
36
|
-
"sha256": "d115b5aafa2662509e7d600d23b00a596bf022f949f573ac9e8e585fb388af5e"
|
|
31
|
+
"bytes": 27579,
|
|
32
|
+
"sha256": "cb6acf41c522ee9512a43665db097a3ec17e8799699afefceba0c0e0b3bf8f62"
|
|
37
33
|
},
|
|
38
34
|
"packages/renderer/src/root.ts": {
|
|
39
|
-
"bytes":
|
|
40
|
-
"sha256": "
|
|
35
|
+
"bytes": 13836,
|
|
36
|
+
"sha256": "0a58f9d95198386079663393bf71f30f28454578500135b313a6aea63e507553"
|
|
41
37
|
},
|
|
42
38
|
"packages/renderer/src/window-lifetime.ts": {
|
|
43
39
|
"bytes": 1635,
|
|
@@ -49,11 +45,11 @@
|
|
|
49
45
|
}
|
|
50
46
|
},
|
|
51
47
|
"toolchain": {
|
|
52
|
-
"bytes":
|
|
53
|
-
"sha256": "
|
|
48
|
+
"bytes": 3234,
|
|
49
|
+
"sha256": "9f4d9362cb4cc38c0f4e705295789a0cbfec46ffdd007e0541b9441a7c3003a7"
|
|
54
50
|
},
|
|
55
51
|
"dependencyLock": {
|
|
56
|
-
"bytes":
|
|
57
|
-
"sha256": "
|
|
52
|
+
"bytes": 28430,
|
|
53
|
+
"sha256": "9d321f65b9224958ec99286628d652232a16e3242ea9536a396bceaf72892460"
|
|
58
54
|
}
|
|
59
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bun-native/renderer",
|
|
3
|
-
"version": "0.33.
|
|
3
|
+
"version": "0.33.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"exports": "./src/index.ts",
|
|
16
16
|
"description": "Native element tree and batching shared by Bun Native adapters.",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@bun-native/core": "0.33.
|
|
18
|
+
"@bun-native/core": "0.33.2"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
package/src/event-properties.ts
CHANGED
|
@@ -25,6 +25,9 @@ export const eventProperties = Object.freeze({
|
|
|
25
25
|
/** Requires `split_resize_v1`; only a split is ever the target. */
|
|
26
26
|
onSplitResize: binding('split_resize'),
|
|
27
27
|
onSplitResizeCapture: binding('split_resize', true),
|
|
28
|
+
/** A popup window overlay (`surface: 'window'`) lost its parent window's focus. */
|
|
29
|
+
onDismiss: binding('dismiss'),
|
|
30
|
+
onDismissCapture: binding('dismiss', true),
|
|
28
31
|
})
|
|
29
32
|
|
|
30
33
|
export type EventProperty = keyof typeof eventProperties
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
import { asNode, type RendererNode } from './nodes.ts'
|
|
11
11
|
|
|
12
12
|
export { createRenderRoot } from './root.ts'
|
|
13
|
-
export type { RenderContext, RenderOptions, RenderRoot } from './root.ts'
|
|
13
|
+
export type { RenderAdapter, RenderContext, RenderOptions, RenderRoot } from './root.ts'
|
|
14
14
|
export {
|
|
15
15
|
asNode,
|
|
16
16
|
createElement,
|
package/src/native-tree.ts
CHANGED
|
@@ -8,18 +8,35 @@ import {
|
|
|
8
8
|
type NativeWindow,
|
|
9
9
|
} from '@bun-native/core'
|
|
10
10
|
import type { Node, Snapshot } from './nodes.ts'
|
|
11
|
-
import { stableChildren } from '
|
|
11
|
+
import { stableChildren } from '@bun-native/core/order'
|
|
12
12
|
|
|
13
13
|
export type Counts = { create: number; set: number; insert: number; remove: number }
|
|
14
|
-
export type Committed = Snapshot & { handle: NativeNode }
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return
|
|
15
|
+
/**
|
|
16
|
+
* Whether two element values are equal as the wire sees them: plain data whose
|
|
17
|
+
* `undefined` properties are omitted. Elements hold only primitives and one
|
|
18
|
+
* level of style, so comparing in place is cheaper than serializing either.
|
|
19
|
+
*/
|
|
20
|
+
function same(left: unknown, right: unknown): boolean {
|
|
21
|
+
if (left === right) return true
|
|
22
|
+
if (
|
|
23
|
+
left === null ||
|
|
24
|
+
right === null ||
|
|
25
|
+
typeof left !== 'object' ||
|
|
26
|
+
typeof right !== 'object' ||
|
|
27
|
+
Array.isArray(left) !== Array.isArray(right)
|
|
28
|
+
)
|
|
29
|
+
return false
|
|
30
|
+
const a = left as Record<string, unknown>
|
|
31
|
+
const b = right as Record<string, unknown>
|
|
32
|
+
let fields = 0
|
|
33
|
+
for (const name in a) {
|
|
34
|
+
if (a[name] === undefined) continue
|
|
35
|
+
if (!same(a[name], b[name])) return false
|
|
36
|
+
fields += 1
|
|
37
|
+
}
|
|
38
|
+
for (const name in b) if (b[name] !== undefined) fields -= 1
|
|
39
|
+
return fields === 0
|
|
23
40
|
}
|
|
24
41
|
|
|
25
42
|
const noChildren: readonly Node[] = Object.freeze([])
|
|
@@ -27,7 +44,8 @@ const noChildren: readonly Node[] = Object.freeze([])
|
|
|
27
44
|
function updateInput(
|
|
28
45
|
window: NativeWindow,
|
|
29
46
|
batch: NativeBatch,
|
|
30
|
-
old:
|
|
47
|
+
old: Snapshot,
|
|
48
|
+
handle: NativeNode<'input'>,
|
|
31
49
|
state: Snapshot & { element: Extract<Element, { kind: 'input' }> }
|
|
32
50
|
): boolean {
|
|
33
51
|
if (old.element.kind !== 'input')
|
|
@@ -37,7 +55,7 @@ function updateInput(
|
|
|
37
55
|
const intent = state.inputIntent
|
|
38
56
|
if (intent && intent !== old.inputIntent) {
|
|
39
57
|
if (intent.observedNode) {
|
|
40
|
-
if (intent.observedNode !==
|
|
58
|
+
if (intent.observedNode !== handle)
|
|
41
59
|
throw new NativeStateError(
|
|
42
60
|
'invalid_handle',
|
|
43
61
|
'Native input observation belongs to another node'
|
|
@@ -45,61 +63,60 @@ function updateInput(
|
|
|
45
63
|
// Echo is an observation of a native edit, even if ingestion has advanced
|
|
46
64
|
// again before its callback runs. Never emit it as a replacement command.
|
|
47
65
|
} else {
|
|
48
|
-
batch.set(
|
|
66
|
+
batch.set(handle, state.element, intent.revision)
|
|
49
67
|
return true
|
|
50
68
|
}
|
|
51
69
|
}
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
})
|
|
59
|
-
|
|
70
|
+
const before = old.element
|
|
71
|
+
const after = state.element
|
|
72
|
+
if (
|
|
73
|
+
(before.placeholder ?? '') === (after.placeholder ?? '') &&
|
|
74
|
+
(before.disabled === true) === (after.disabled === true) &&
|
|
75
|
+
(before.read_only === true) === (after.read_only === true) &&
|
|
76
|
+
same(before.style ?? {}, after.style ?? {})
|
|
77
|
+
)
|
|
78
|
+
return false
|
|
60
79
|
// An appearance/state-only update preserves the current native value; this never upgrades
|
|
61
80
|
// an older application value intent to a newer revision.
|
|
62
|
-
const current = window.input(
|
|
63
|
-
batch.set(
|
|
64
|
-
old.handle as NativeNode<'input'>,
|
|
65
|
-
{ ...state.element, value: current.value },
|
|
66
|
-
current.revision
|
|
67
|
-
)
|
|
81
|
+
const current = window.input(handle)
|
|
82
|
+
batch.set(handle, { ...state.element, value: current.value }, current.revision)
|
|
68
83
|
return true
|
|
69
84
|
}
|
|
70
85
|
|
|
71
86
|
export type Commit = {
|
|
72
|
-
/**
|
|
73
|
-
|
|
87
|
+
/** Native handles of the nodes this batch created. */
|
|
88
|
+
created: Map<Node, NativeNode>
|
|
74
89
|
/** Previously committed nodes that left the tree, including removed descendants. */
|
|
75
90
|
removed: Node[]
|
|
76
91
|
counts: Counts
|
|
77
92
|
}
|
|
78
93
|
|
|
79
94
|
/**
|
|
80
|
-
* Framework-independent native diff from the changed captures alone.
|
|
81
|
-
*
|
|
82
|
-
*
|
|
95
|
+
* Framework-independent native diff from the changed captures alone. After
|
|
96
|
+
* atomic acknowledgement, the caller publishes every change as its committed
|
|
97
|
+
* record and each created handle; each node's `committed` and `handle` are
|
|
98
|
+
* read, never written, so a rejected batch leaves committed state untouched.
|
|
99
|
+
* A committed node's native handle never changes, so it is kept apart from
|
|
100
|
+
* its record.
|
|
83
101
|
*/
|
|
84
102
|
export async function commitNativeTree(
|
|
85
103
|
window: NativeWindow,
|
|
86
104
|
root: Node,
|
|
87
|
-
previous: ReadonlyMap<Node, Committed>,
|
|
88
105
|
changes: ReadonlyMap<Node, Snapshot>
|
|
89
106
|
): Promise<Commit> {
|
|
90
107
|
const counts: Counts = { create: 0, set: 0, insert: 0, remove: 0 }
|
|
91
|
-
const [
|
|
108
|
+
const [created, removed] = await window.batch((batch) => {
|
|
92
109
|
const removed: Node[] = []
|
|
93
|
-
// Working state overlays
|
|
110
|
+
// Working state overlays committed records and records only what this batch
|
|
94
111
|
// changes, instead of copying every committed handle, parent and child list.
|
|
95
112
|
const created = new Map<Node, NativeNode>()
|
|
96
113
|
const parents = new Map<Node, Node>()
|
|
97
114
|
const edited = new Map<Node, Node[]>()
|
|
98
|
-
const handle = (node: Node) => created.get(node) ??
|
|
115
|
+
const handle = (node: Node) => created.get(node) ?? node.handle!
|
|
99
116
|
const parentOf = (node: Node) =>
|
|
100
|
-
parents.has(node) ? parents.get(node) :
|
|
117
|
+
parents.has(node) ? parents.get(node) : node.committed?.parent
|
|
101
118
|
const childrenOf = (node: Node): readonly Node[] =>
|
|
102
|
-
edited.get(node) ??
|
|
119
|
+
edited.get(node) ?? node.committed?.children ?? noChildren
|
|
103
120
|
const editChildren = (node: Node): Node[] => {
|
|
104
121
|
let list = edited.get(node)
|
|
105
122
|
if (!list) edited.set(node, (list = [...childrenOf(node)]))
|
|
@@ -133,14 +150,14 @@ export async function commitNativeTree(
|
|
|
133
150
|
}
|
|
134
151
|
const forget = (node: Node): void => {
|
|
135
152
|
removed.push(node)
|
|
136
|
-
for (const child of
|
|
153
|
+
for (const child of node.committed!.children) if (!changes.has(child)) forget(child)
|
|
137
154
|
}
|
|
138
155
|
// Only a recaptured parent can have lost a committed child. A lost child
|
|
139
156
|
// still in the tree was reinserted elsewhere and is therefore recaptured.
|
|
140
157
|
// Lost children leave their working list in one pass per parent, so
|
|
141
158
|
// clearing a keyed region costs O(children) rather than O(children²).
|
|
142
159
|
for (const [parent, state] of changes) {
|
|
143
|
-
const old =
|
|
160
|
+
const old = parent.committed
|
|
144
161
|
if (!old?.children.length || old.children === state.children) continue
|
|
145
162
|
// Children shared by both lists' ends are retained; only the middles
|
|
146
163
|
// can differ, and keyed edits usually leave them short.
|
|
@@ -167,7 +184,7 @@ export async function commitNativeTree(
|
|
|
167
184
|
const node = before[index]!
|
|
168
185
|
if (changes.has(node) || retained.has(node)) continue
|
|
169
186
|
rescue(node)
|
|
170
|
-
batch.remove(
|
|
187
|
+
batch.remove(node.handle!)
|
|
171
188
|
counts.remove += 1
|
|
172
189
|
lost.add(node)
|
|
173
190
|
if (parent === root) unlisted += 1
|
|
@@ -180,9 +197,8 @@ export async function commitNativeTree(
|
|
|
180
197
|
)
|
|
181
198
|
if (parent === root) unlisted = 0
|
|
182
199
|
}
|
|
183
|
-
const updates = new Map<Node, Committed>()
|
|
184
200
|
for (const [node, state] of changes) {
|
|
185
|
-
const old =
|
|
201
|
+
const old = node.committed
|
|
186
202
|
if (!old) {
|
|
187
203
|
if (state.inputIntent?.observedNode)
|
|
188
204
|
throw new NativeStateError(
|
|
@@ -202,14 +218,15 @@ export async function commitNativeTree(
|
|
|
202
218
|
batch.append(window.root, nativeNode)
|
|
203
219
|
counts.insert += 1
|
|
204
220
|
}
|
|
205
|
-
updates.set(node, { ...state, handle: nativeNode })
|
|
206
221
|
continue
|
|
207
222
|
}
|
|
208
|
-
|
|
223
|
+
const nativeNode = node.handle!
|
|
209
224
|
if (state.element.kind === 'input') {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
225
|
+
const input = { ...state, element: state.element }
|
|
226
|
+
if (updateInput(window, batch, old, nativeNode as NativeNode<'input'>, input))
|
|
227
|
+
counts.set += 1
|
|
228
|
+
} else if (!same(old.element, state.element)) {
|
|
229
|
+
batch.set(nativeNode as NativeNode<Exclude<Element['kind'], 'input'>>, state.element)
|
|
213
230
|
counts.set += 1
|
|
214
231
|
}
|
|
215
232
|
}
|
|
@@ -242,7 +259,7 @@ export async function commitNativeTree(
|
|
|
242
259
|
before = at
|
|
243
260
|
}
|
|
244
261
|
}
|
|
245
|
-
return [
|
|
262
|
+
return [created, removed] as const
|
|
246
263
|
})
|
|
247
|
-
return {
|
|
264
|
+
return { created, removed, counts }
|
|
248
265
|
}
|
package/src/nodes.ts
CHANGED
|
@@ -26,6 +26,12 @@ export interface RendererNode {
|
|
|
26
26
|
|
|
27
27
|
export interface Owner {
|
|
28
28
|
readonly token: object
|
|
29
|
+
/**
|
|
30
|
+
* A handler property keeps its native registration while it stays defined:
|
|
31
|
+
* the registration calls the property's current value. Otherwise replacing
|
|
32
|
+
* a handler revokes the old registration once the replacement commits.
|
|
33
|
+
*/
|
|
34
|
+
readonly stableHandlers: boolean
|
|
29
35
|
changed(): void
|
|
30
36
|
detached(node: Node): void
|
|
31
37
|
input(node: Node): InputState
|
|
@@ -67,10 +73,17 @@ export type PropertyName = keyof Properties
|
|
|
67
73
|
|
|
68
74
|
const isContainer = (kind: Node['kind']) => kind === 'box' || kind === 'split' || kind === 'overlay'
|
|
69
75
|
|
|
70
|
-
|
|
76
|
+
/** Whether `value` was made by this module's `Node` constructor. */
|
|
77
|
+
let isStaged: (value: object) => boolean
|
|
71
78
|
|
|
72
79
|
export class Node implements RendererNode {
|
|
73
80
|
declare readonly [identity]: true
|
|
81
|
+
/** Unforgeable brand: checked with `in`, it costs no lookup in a side table. */
|
|
82
|
+
readonly #brand = true
|
|
83
|
+
static {
|
|
84
|
+
// Reading the brand after `in` is what marks it used for the linter.
|
|
85
|
+
isStaged = (value) => #brand in value && (value as Node).#brand
|
|
86
|
+
}
|
|
74
87
|
/**
|
|
75
88
|
* Staged children, where a detached child leaves an `undefined` hole until
|
|
76
89
|
* the list is next read: removing a child from a wide list then neither
|
|
@@ -91,9 +104,10 @@ export class Node implements RendererNode {
|
|
|
91
104
|
captured: Snapshot | undefined
|
|
92
105
|
/**
|
|
93
106
|
* 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.
|
|
107
|
+
* in a tree has dirty ancestors, so snapshot() walks only dirty paths. A new
|
|
108
|
+
* node has no capture yet.
|
|
95
109
|
*/
|
|
96
|
-
dirty =
|
|
110
|
+
dirty = true
|
|
97
111
|
/**
|
|
98
112
|
* Dirty children registered when they became dirty, so a parent whose own
|
|
99
113
|
* capture is current visits only them. A recaptured parent scans all of its
|
|
@@ -104,10 +118,17 @@ export class Node implements RendererNode {
|
|
|
104
118
|
index = 0
|
|
105
119
|
/** Children before this position have current `index` hints. */
|
|
106
120
|
indexed = 0
|
|
121
|
+
/**
|
|
122
|
+
* The owning render root's acknowledged record and native handle, kept on
|
|
123
|
+
* the node rather than in root-level maps. Only that root writes them: it
|
|
124
|
+
* publishes them after native acknowledgement and clears them when a commit
|
|
125
|
+
* removes the node or the root finishes, so nothing reachable from a
|
|
126
|
+
* removed or disposed node retains committed state.
|
|
127
|
+
*/
|
|
128
|
+
committed: Snapshot | undefined
|
|
129
|
+
handle: NativeNode | undefined
|
|
107
130
|
|
|
108
|
-
constructor(readonly kind: Element['kind'] | 'text-node') {
|
|
109
|
-
nodes.add(this)
|
|
110
|
-
}
|
|
131
|
+
constructor(readonly kind: Element['kind'] | 'text-node') {}
|
|
111
132
|
|
|
112
133
|
/** The staged children in order; adapters may truncate this list directly. */
|
|
113
134
|
get children(): Node[] {
|
|
@@ -117,7 +138,7 @@ export class Node implements RendererNode {
|
|
|
117
138
|
}
|
|
118
139
|
|
|
119
140
|
export function asNode(value: RendererNode): Node {
|
|
120
|
-
if (!value || typeof value !== 'object' || !
|
|
141
|
+
if (!value || typeof value !== 'object' || !isStaged(value))
|
|
121
142
|
throw new NativeStateError('invalid_handle', 'Expected an owned staged native node')
|
|
122
143
|
return value as Node
|
|
123
144
|
}
|
|
@@ -304,15 +325,18 @@ export function insertNode(parent: Node, node: Node, anchor?: Node): void {
|
|
|
304
325
|
if (owner && node.owner !== owner) checkScope(node, owner.token)
|
|
305
326
|
const previousOwner = node.owner
|
|
306
327
|
// 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
|
-
|
|
328
|
+
// clear it), so this subtree moves without ever leaving the live tree. An
|
|
329
|
+
// unowned subtree that stays unowned holds no capture an owner committed:
|
|
330
|
+
// leaving an owner cleared them, and the next owner adopts it in full. An
|
|
331
|
+
// adapter that builds detached trees bottom-up thus walks each node once.
|
|
332
|
+
const moved = previousOwner === owner && (owner === undefined || node.parent !== undefined)
|
|
309
333
|
if (previousOwner && previousOwner !== owner) previousOwner.detached(node)
|
|
310
334
|
if (node.parent) detachChild(node.parent, node)
|
|
311
335
|
invalidate(node.parent)
|
|
312
336
|
invalidate(parent)
|
|
313
337
|
node.parent = parent
|
|
314
338
|
attachChild(parent, node, anchor ? childIndex(parent, anchor) : parent.staged.length)
|
|
315
|
-
// Adapters may clear detached nodes directly, so a
|
|
339
|
+
// Adapters may clear detached nodes directly, so a subtree an owner takes is
|
|
316
340
|
// always captured again from its current state. A moved subtree's captures
|
|
317
341
|
// and dirty paths stay current below it: its root's capture names the old
|
|
318
342
|
// parent, so snapshot() recaptures that node alone, and the diff moves it
|
|
@@ -351,9 +375,12 @@ export function setProperty(node: Node, name: PropertyName, value: unknown): voi
|
|
|
351
375
|
if (isEventProperty(name)) {
|
|
352
376
|
if (node.kind === 'text-node')
|
|
353
377
|
throw new TypeError('Text literals cannot own native event handlers')
|
|
378
|
+
const replaced = props[name] !== undefined
|
|
354
379
|
if (value === undefined) delete props[name]
|
|
355
380
|
else if (typeof value === 'function') props[name] = value as EventListener
|
|
356
381
|
else throw new TypeError(`Native ${name} must be a function`)
|
|
382
|
+
// Only a defined/undefined change reaches a stable registration.
|
|
383
|
+
if (replaced && value !== undefined && node.owner?.stableHandlers) return
|
|
357
384
|
invalidate(node)
|
|
358
385
|
node.owner?.changed()
|
|
359
386
|
return
|
|
@@ -484,7 +511,12 @@ export type Snapshot = Readonly<{
|
|
|
484
511
|
inputIntent: InputIntent | undefined
|
|
485
512
|
}>
|
|
486
513
|
|
|
487
|
-
|
|
514
|
+
type LeafStyle = NonNullable<Extract<Element, { kind: 'text' }>['style']>
|
|
515
|
+
|
|
516
|
+
function leafStyle(style: Style): LeafStyle {
|
|
517
|
+
// The stored style is the renderer's own copy and is replaced, never
|
|
518
|
+
// mutated, so a style without scrolling members is shared as it is.
|
|
519
|
+
if (!('overflow' in style) && !('scroll_anchor' in style)) return style as LeafStyle
|
|
488
520
|
const { overflow, scroll_anchor, ...rest } = style
|
|
489
521
|
if (
|
|
490
522
|
scroll_anchor !== undefined ||
|
|
@@ -574,7 +606,7 @@ function build(node: Node, parent: Node | undefined): Snapshot {
|
|
|
574
606
|
...(props.accessibleSelected === undefined
|
|
575
607
|
? {}
|
|
576
608
|
: { accessible_selected: props.accessibleSelected }),
|
|
577
|
-
...(props.style ? { style:
|
|
609
|
+
...(props.style ? { style: props.style } : {}),
|
|
578
610
|
} as Element
|
|
579
611
|
else if (node.kind === 'split')
|
|
580
612
|
// Core validation rejects gap and alignment on a split.
|
|
@@ -596,7 +628,7 @@ function build(node: Node, parent: Node | undefined): Snapshot {
|
|
|
596
628
|
...(props.navigation === undefined ? {} : { navigation: props.navigation }),
|
|
597
629
|
...(props.surface === undefined ? {} : { surface: props.surface }),
|
|
598
630
|
...(props.accessibleLabel === undefined ? {} : { accessible_label: props.accessibleLabel }),
|
|
599
|
-
...(props.style ? { style:
|
|
631
|
+
...(props.style ? { style: props.style } : {}),
|
|
600
632
|
} as Element
|
|
601
633
|
else if (node.kind === 'input')
|
|
602
634
|
element = {
|
package/src/root.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
type NativeBatch,
|
|
6
6
|
type NativeDropTarget,
|
|
7
7
|
type NativeNode,
|
|
8
|
+
type NativeUIEvent,
|
|
8
9
|
type NativeWindow,
|
|
9
10
|
type Style,
|
|
10
11
|
} from '@bun-native/core'
|
|
@@ -17,7 +18,7 @@ import {
|
|
|
17
18
|
type Owner,
|
|
18
19
|
type Snapshot,
|
|
19
20
|
} from './nodes.ts'
|
|
20
|
-
import { commitNativeTree, type
|
|
21
|
+
import { commitNativeTree, type Counts } from './native-tree.ts'
|
|
21
22
|
import { observeInput } from './input-intent.ts'
|
|
22
23
|
import { watchWindow } from './window-lifetime.ts'
|
|
23
24
|
import {
|
|
@@ -32,6 +33,16 @@ type Registration = {
|
|
|
32
33
|
unsubscribe: () => void
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
/** Adapter behavior that is not a public render option. */
|
|
37
|
+
export interface RenderAdapter {
|
|
38
|
+
/**
|
|
39
|
+
* Run one native callback; React batches it as a discrete update. An
|
|
40
|
+
* adapter that supplies this keeps each handler's registration while the
|
|
41
|
+
* handler property stays defined (see `Owner.stableHandlers`).
|
|
42
|
+
*/
|
|
43
|
+
dispatch(listener: EventListener, event: NativeUIEvent): void | Promise<void>
|
|
44
|
+
}
|
|
45
|
+
|
|
35
46
|
export interface RenderOptions {
|
|
36
47
|
/** Initial native style of this root's ownership container; omitted preserves intrinsic sizing. */
|
|
37
48
|
style?: Style
|
|
@@ -72,7 +83,8 @@ class Root implements RenderRoot, Owner {
|
|
|
72
83
|
private rejectClosed!: (error: Error) => void
|
|
73
84
|
private disposeFramework: (() => void) | undefined
|
|
74
85
|
private unwatch: (() => void) | undefined
|
|
75
|
-
|
|
86
|
+
/** Committed nodes; each holds its own record and handle (see `Node.committed`). */
|
|
87
|
+
private nodes = 0
|
|
76
88
|
private readonly registrations = new Map<Node, Map<EventProperty, Registration>>()
|
|
77
89
|
private operation: Promise<void> | undefined
|
|
78
90
|
private version = 0
|
|
@@ -86,7 +98,8 @@ class Root implements RenderRoot, Owner {
|
|
|
86
98
|
constructor(
|
|
87
99
|
private readonly window: NativeWindow,
|
|
88
100
|
initialize: (context: RenderContext) => void,
|
|
89
|
-
style: Style | undefined
|
|
101
|
+
style: Style | undefined,
|
|
102
|
+
private readonly adapter: RenderAdapter | undefined
|
|
90
103
|
) {
|
|
91
104
|
// Copy before initialization or asynchronous commits, without publishing an owner yet.
|
|
92
105
|
setProperty(this.root, 'style', style)
|
|
@@ -138,7 +151,7 @@ class Root implements RenderRoot, Owner {
|
|
|
138
151
|
let listeners = 0
|
|
139
152
|
for (const registrations of this.registrations.values()) listeners += registrations.size
|
|
140
153
|
return Object.freeze({
|
|
141
|
-
nodes: this.
|
|
154
|
+
nodes: this.nodes,
|
|
142
155
|
listeners,
|
|
143
156
|
pending: this.operation !== undefined,
|
|
144
157
|
commits: this.commits,
|
|
@@ -146,6 +159,10 @@ class Root implements RenderRoot, Owner {
|
|
|
146
159
|
})
|
|
147
160
|
}
|
|
148
161
|
|
|
162
|
+
get stableHandlers(): boolean {
|
|
163
|
+
return this.adapter !== undefined
|
|
164
|
+
}
|
|
165
|
+
|
|
149
166
|
changed(): void {
|
|
150
167
|
if (this.phase !== 'active') return
|
|
151
168
|
this.version += 1
|
|
@@ -175,31 +192,31 @@ class Root implements RenderRoot, Owner {
|
|
|
175
192
|
node: Node,
|
|
176
193
|
listener: (event: DropTargetEvent) => void | Promise<void>
|
|
177
194
|
): Promise<NativeDropTarget> {
|
|
178
|
-
const
|
|
179
|
-
if (this.phase !== 'active' || !
|
|
195
|
+
const handle = node.handle
|
|
196
|
+
if (this.phase !== 'active' || !handle)
|
|
180
197
|
return Promise.reject(
|
|
181
198
|
new NativeStateError('invalid_handle', 'Node is unmounted or not yet committed')
|
|
182
199
|
)
|
|
183
200
|
// The native target is revoked when a later commit removes this node.
|
|
184
|
-
return this.window.registerDropTarget(
|
|
201
|
+
return this.window.registerDropTarget(handle, listener)
|
|
185
202
|
}
|
|
186
203
|
|
|
187
204
|
input(node: Node): InputState {
|
|
188
|
-
const
|
|
189
|
-
if (this.phase !== 'active' || !
|
|
205
|
+
const handle = node.handle
|
|
206
|
+
if (this.phase !== 'active' || !handle || handle.kind !== 'input')
|
|
190
207
|
throw new NativeStateError(
|
|
191
208
|
'invalid_handle',
|
|
192
209
|
'Input is unmounted or has no committed native handle'
|
|
193
210
|
)
|
|
194
|
-
return this.window.input(
|
|
211
|
+
return this.window.input(handle as NativeNode<'input'>)
|
|
195
212
|
}
|
|
196
213
|
|
|
197
214
|
async request(node: Node, send: (batch: NativeBatch, handle: NativeNode) => void): Promise<void> {
|
|
198
215
|
await this.flush()
|
|
199
|
-
const
|
|
200
|
-
if (this.phase !== 'active' || !
|
|
216
|
+
const handle = node.handle
|
|
217
|
+
if (this.phase !== 'active' || !handle)
|
|
201
218
|
throw new NativeStateError('invalid_handle', 'Node is unmounted or has no committed handle')
|
|
202
|
-
await this.window.batch((batch) => send(batch,
|
|
219
|
+
await this.window.batch((batch) => send(batch, handle))
|
|
203
220
|
}
|
|
204
221
|
|
|
205
222
|
private start(): void {
|
|
@@ -281,7 +298,8 @@ class Root implements RenderRoot, Owner {
|
|
|
281
298
|
this.phase = 'closed'
|
|
282
299
|
this.unwatch?.()
|
|
283
300
|
this.unwatch = undefined
|
|
284
|
-
this.
|
|
301
|
+
release(this.root)
|
|
302
|
+
this.nodes = 0
|
|
285
303
|
this.root.children.length = 0
|
|
286
304
|
invalidate(this.root)
|
|
287
305
|
if (this.failure) this.rejectClosed(this.failure)
|
|
@@ -289,9 +307,9 @@ class Root implements RenderRoot, Owner {
|
|
|
289
307
|
}
|
|
290
308
|
|
|
291
309
|
private async cleanup(): Promise<void> {
|
|
292
|
-
const root = this.
|
|
310
|
+
const root = this.root.handle
|
|
293
311
|
if (root && !this.windowEnded) {
|
|
294
|
-
await this.window.batch((batch) => batch.remove(root
|
|
312
|
+
await this.window.batch((batch) => batch.remove(root))
|
|
295
313
|
this.mutations.remove += 1
|
|
296
314
|
this.commits += 1
|
|
297
315
|
}
|
|
@@ -299,16 +317,16 @@ class Root implements RenderRoot, Owner {
|
|
|
299
317
|
}
|
|
300
318
|
|
|
301
319
|
private async commit(changes: ReadonlyMap<Node, Snapshot>): Promise<void> {
|
|
302
|
-
const {
|
|
303
|
-
this.window,
|
|
304
|
-
this.root,
|
|
305
|
-
this.committed,
|
|
306
|
-
changes
|
|
307
|
-
)
|
|
320
|
+
const { created, removed, counts } = await commitNativeTree(this.window, this.root, changes)
|
|
308
321
|
if (this.windowEnded) return
|
|
309
322
|
// Publish in place only after acknowledgement; unchanged records stay shared.
|
|
310
|
-
for (const node of removed)
|
|
311
|
-
|
|
323
|
+
for (const node of removed) {
|
|
324
|
+
node.committed = undefined
|
|
325
|
+
node.handle = undefined
|
|
326
|
+
}
|
|
327
|
+
for (const [node, state] of changes) node.committed = state
|
|
328
|
+
for (const [node, handle] of created) node.handle = handle
|
|
329
|
+
this.nodes += created.size - removed.length
|
|
312
330
|
if (Object.values(counts).some((count) => count > 0)) this.commits += 1
|
|
313
331
|
for (const field of ['create', 'set', 'insert', 'remove'] as const)
|
|
314
332
|
this.mutations[field] += counts[field]
|
|
@@ -318,23 +336,38 @@ class Root implements RenderRoot, Owner {
|
|
|
318
336
|
// and every unchanged record's registrations were reconciled when it was
|
|
319
337
|
// published, so only removed and changed nodes need reconciliation.
|
|
320
338
|
for (const node of removed) this.revoke(node)
|
|
321
|
-
for (const [node, state] of
|
|
339
|
+
for (const [node, state] of changes) {
|
|
322
340
|
if (node.owner !== this) {
|
|
323
341
|
this.revoke(node)
|
|
324
342
|
continue
|
|
325
343
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
344
|
+
const registrations = this.registrations.get(node)
|
|
345
|
+
if (registrations)
|
|
346
|
+
for (const [name, registration] of registrations)
|
|
347
|
+
if (
|
|
348
|
+
this.adapter
|
|
349
|
+
? state.events[name] === undefined
|
|
350
|
+
: state.events[name] !== registration.listener
|
|
351
|
+
)
|
|
352
|
+
this.revoke(node, name)
|
|
353
|
+
// Most changed nodes have no handlers: iterating allocates nothing then.
|
|
354
|
+
for (const name in state.events) {
|
|
355
|
+
if (!isEventProperty(name)) continue
|
|
356
|
+
const listener = state.events[name]
|
|
357
|
+
if (!listener || this.registrations.get(node)?.has(name)) continue
|
|
330
358
|
const binding = eventProperties[name]
|
|
331
359
|
const unsubscribe = this.window.on(
|
|
332
|
-
|
|
360
|
+
node.handle!,
|
|
333
361
|
binding.type,
|
|
334
362
|
async (event) => {
|
|
335
|
-
if (node.owner !== this
|
|
363
|
+
if (node.owner !== this) return
|
|
364
|
+
const adapter = this.adapter
|
|
365
|
+
const current = node.properties[name]
|
|
366
|
+
// A replaced handler is not called before its replacement commits,
|
|
367
|
+
// unless the adapter keeps the registration and calls the latest.
|
|
368
|
+
if (adapter ? !current : current !== listener) return
|
|
336
369
|
if (event.type === 'input') observeInput(event)
|
|
337
|
-
await listener(event)
|
|
370
|
+
await (adapter ? adapter.dispatch(current!, event) : listener(event))
|
|
338
371
|
await this.flush()
|
|
339
372
|
},
|
|
340
373
|
{ capture: binding.capture }
|
|
@@ -350,15 +383,28 @@ class Root implements RenderRoot, Owner {
|
|
|
350
383
|
}
|
|
351
384
|
}
|
|
352
385
|
|
|
386
|
+
/**
|
|
387
|
+
* Drop the committed record and handle of `node` and of every node its
|
|
388
|
+
* committed record reaches: on finishing, the committed tree is exactly the
|
|
389
|
+
* set of nodes holding them.
|
|
390
|
+
*/
|
|
391
|
+
function release(node: Node): void {
|
|
392
|
+
const record = node.committed
|
|
393
|
+
node.committed = undefined
|
|
394
|
+
node.handle = undefined
|
|
395
|
+
if (record) for (const child of record.children) release(child)
|
|
396
|
+
}
|
|
397
|
+
|
|
353
398
|
/** Own one framework's staged tree, atomic native commits and terminal cleanup. */
|
|
354
399
|
export function createRenderRoot(
|
|
355
400
|
window: NativeWindow,
|
|
356
401
|
initialize: (context: RenderContext) => void,
|
|
357
|
-
options: RenderOptions = {}
|
|
402
|
+
options: RenderOptions = {},
|
|
403
|
+
adapter?: RenderAdapter
|
|
358
404
|
): RenderRoot {
|
|
359
405
|
if (!options || typeof options !== 'object' || Array.isArray(options))
|
|
360
406
|
throw new TypeError('Native render options must be an object')
|
|
361
407
|
for (const name of Object.keys(options))
|
|
362
408
|
if (name !== 'style') throw new TypeError('Unknown native render option: ' + name)
|
|
363
|
-
return new Root(window, initialize, options.style)
|
|
409
|
+
return new Root(window, initialize, options.style, adapter)
|
|
364
410
|
}
|
package/src/order.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
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
|
-
}
|