@bun-native/renderer 0.0.0-stage → 0.33.1
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 +55 -0
- package/package.json +29 -4
- package/src/event-properties.ts +57 -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 +653 -0
- package/src/root.ts +391 -0
- package/src/window-lifetime.ts +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bunly contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bun-native/renderer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Native element tree and batching shared by Bun Native adapters.
|
|
4
|
+
|
|
5
|
+
Part of [Bun Native](https://github.com/0xPlayerOne/bun-native), an experimental developer preview. Version 0.33.1 was built from [c56fdd675365](https://github.com/0xPlayerOne/bun-native/tree/c56fdd6753653a10144a4167c1d3dfb3c38a0647) 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/c56fdd6753653a10144a4167c1d3dfb3c38a0647/docs/getting-started.md); `bun-native host install` fetches the matching release host. The license notice is in LICENSE.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"commit": "c56fdd6753653a10144a4167c1d3dfb3c38a0647",
|
|
4
|
+
"tree": "c6e81b3f9d041e11dd0413b5fdd7a50910383854",
|
|
5
|
+
"files": {
|
|
6
|
+
"package.json": {
|
|
7
|
+
"bytes": 2115,
|
|
8
|
+
"sha256": "caf866d56f3cd7530d5735ecbf62512e9e22526bc44c75a4b48a18c26352c0b8"
|
|
9
|
+
},
|
|
10
|
+
"packages/renderer/package.json": {
|
|
11
|
+
"bytes": 192,
|
|
12
|
+
"sha256": "720be385eb972eff6dac08b77948e09166ca11aa1db0100312cf6b7f36f53ae3"
|
|
13
|
+
},
|
|
14
|
+
"packages/renderer/src/event-properties.ts": {
|
|
15
|
+
"bytes": 2407,
|
|
16
|
+
"sha256": "3f87af3950b787c5b8746d1743d516541e44d7eb6554b0653315a8a63cfb6520"
|
|
17
|
+
},
|
|
18
|
+
"packages/renderer/src/index.ts": {
|
|
19
|
+
"bytes": 3197,
|
|
20
|
+
"sha256": "0d2e90764c87e734d896dc8bb3007340abe66a53d91fb05fe37bdc5d94b7bed2"
|
|
21
|
+
},
|
|
22
|
+
"packages/renderer/src/input-intent.ts": {
|
|
23
|
+
"bytes": 1698,
|
|
24
|
+
"sha256": "efe908fe1df1d14a7cbc1e3e30e8e76dc888fb865dca6b203bf91c18f3bb4d02"
|
|
25
|
+
},
|
|
26
|
+
"packages/renderer/src/native-tree.ts": {
|
|
27
|
+
"bytes": 10353,
|
|
28
|
+
"sha256": "a0ee9a365500bc3cd66dcda5410fcf396681494daa2bac78370426036184ed4e"
|
|
29
|
+
},
|
|
30
|
+
"packages/renderer/src/nodes.ts": {
|
|
31
|
+
"bytes": 26306,
|
|
32
|
+
"sha256": "e24c747da4933cdfa2dda80bac54e602828a530c2f5920ce85119b7e78427548"
|
|
33
|
+
},
|
|
34
|
+
"packages/renderer/src/root.ts": {
|
|
35
|
+
"bytes": 13196,
|
|
36
|
+
"sha256": "20a18fb833f5f7a89180967c735efa875dc5a408826a17cee4fffc50fd2885d3"
|
|
37
|
+
},
|
|
38
|
+
"packages/renderer/src/window-lifetime.ts": {
|
|
39
|
+
"bytes": 1635,
|
|
40
|
+
"sha256": "e75cedfc7285f7b134bed4d1e861b125b29a5024b25188583eeb077ce545ad6b"
|
|
41
|
+
},
|
|
42
|
+
"LICENSE": {
|
|
43
|
+
"bytes": 1075,
|
|
44
|
+
"sha256": "86673fb485f12fa9aaa3941dfccc9f69a7f8322f8141d0d5c73db4323de10ac5"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"toolchain": {
|
|
48
|
+
"bytes": 3234,
|
|
49
|
+
"sha256": "9f4d9362cb4cc38c0f4e705295789a0cbfec46ffdd007e0541b9441a7c3003a7"
|
|
50
|
+
},
|
|
51
|
+
"dependencyLock": {
|
|
52
|
+
"bytes": 28357,
|
|
53
|
+
"sha256": "65e3dbd01be4a1889367690faf1a9c3f08fb31d22dbd273d9bb9f39dd6baa6fb"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bun-native/renderer",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.33.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"LICENSE",
|
|
9
|
+
"README.md",
|
|
10
|
+
"bun-native-source.json"
|
|
11
|
+
],
|
|
12
|
+
"engines": {
|
|
13
|
+
"bun": "1.4.4-canary.1"
|
|
14
|
+
},
|
|
15
|
+
"exports": "./src/index.ts",
|
|
16
|
+
"description": "Native element tree and batching shared by Bun Native adapters.",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@bun-native/core": "0.33.1"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/0xPlayerOne/bun-native.git",
|
|
23
|
+
"directory": "packages/renderer"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/0xPlayerOne/bun-native#readme",
|
|
26
|
+
"bugs": "https://github.com/0xPlayerOne/bun-native/issues",
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"provenance": true
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Listener, NativeEventMap, NativeEventType, NativeUIEvent } from '@bun-native/core'
|
|
2
|
+
|
|
3
|
+
function binding<T extends NativeEventType>(type: T, capture = false) {
|
|
4
|
+
return Object.freeze({ type, capture })
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** Both adapters use the public core's event types and propagation phases. */
|
|
8
|
+
export const eventProperties = Object.freeze({
|
|
9
|
+
onClick: binding('click'),
|
|
10
|
+
onClickCapture: binding('click', true),
|
|
11
|
+
onInput: binding('input'),
|
|
12
|
+
onInputCapture: binding('input', true),
|
|
13
|
+
onKeyDown: binding('key_down'),
|
|
14
|
+
onKeyDownCapture: binding('key_down', true),
|
|
15
|
+
onKeyUp: binding('key_up'),
|
|
16
|
+
onKeyUpCapture: binding('key_up', true),
|
|
17
|
+
onPointerDown: binding('pointer_down'),
|
|
18
|
+
onPointerDownCapture: binding('pointer_down', true),
|
|
19
|
+
onPointerUp: binding('pointer_up'),
|
|
20
|
+
onPointerUpCapture: binding('pointer_up', true),
|
|
21
|
+
onFocus: binding('focus'),
|
|
22
|
+
onFocusCapture: binding('focus', true),
|
|
23
|
+
onBlur: binding('blur'),
|
|
24
|
+
onBlurCapture: binding('blur', true),
|
|
25
|
+
/** Requires `split_resize_v1`; only a split is ever the target. */
|
|
26
|
+
onSplitResize: binding('split_resize'),
|
|
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),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
export type EventProperty = keyof typeof eventProperties
|
|
34
|
+
export type EventHandlers = {
|
|
35
|
+
[Property in EventProperty]?: Listener<NativeEventMap[(typeof eventProperties)[Property]['type']]>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isEventProperty(name: string): name is EventProperty {
|
|
39
|
+
return Object.hasOwn(eventProperties, name)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// The string-based universal renderer accepts unknown property values. After
|
|
43
|
+
// checking the property name and callable value, store one erased signature;
|
|
44
|
+
// the descriptor selects the matching core event, while JSX keeps its narrow type.
|
|
45
|
+
export type EventListener = Listener<NativeUIEvent>
|
|
46
|
+
export type EventListeners = Partial<Record<EventProperty, EventListener>>
|
|
47
|
+
const emptyListeners: Readonly<EventListeners> = Object.freeze({})
|
|
48
|
+
|
|
49
|
+
export function listeners(properties: EventListeners): Readonly<EventListeners> {
|
|
50
|
+
let result: EventListeners | undefined
|
|
51
|
+
for (const name of Object.keys(properties)) {
|
|
52
|
+
if (!isEventProperty(name)) continue
|
|
53
|
+
const listener = properties[name]
|
|
54
|
+
if (listener) (result ??= {})[name] = listener
|
|
55
|
+
}
|
|
56
|
+
return result ? Object.freeze(result) : emptyListeners
|
|
57
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NativeStateError,
|
|
3
|
+
type ContainerKind,
|
|
4
|
+
type DropTargetEvent,
|
|
5
|
+
type InputState,
|
|
6
|
+
type NativeDropTarget,
|
|
7
|
+
type NativeNode,
|
|
8
|
+
type ScrollOffset,
|
|
9
|
+
} from '@bun-native/core'
|
|
10
|
+
import { asNode, type RendererNode } from './nodes.ts'
|
|
11
|
+
|
|
12
|
+
export { createRenderRoot } from './root.ts'
|
|
13
|
+
export type { RenderAdapter, RenderContext, RenderOptions, RenderRoot } from './root.ts'
|
|
14
|
+
export {
|
|
15
|
+
asNode,
|
|
16
|
+
createElement,
|
|
17
|
+
createTextNode,
|
|
18
|
+
firstChild,
|
|
19
|
+
insertNode,
|
|
20
|
+
nextSibling,
|
|
21
|
+
removeNode,
|
|
22
|
+
replaceText,
|
|
23
|
+
setProperty,
|
|
24
|
+
} from './nodes.ts'
|
|
25
|
+
export type { ElementTag, Node, PropertyName, RendererNode } from './nodes.ts'
|
|
26
|
+
export { eventProperties, isEventProperty } from './event-properties.ts'
|
|
27
|
+
export type { EventHandlers, EventProperty } from './event-properties.ts'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Make a mounted, committed element an owned native drop target. The same
|
|
31
|
+
* core window method serves every adapter; removing the element revokes it.
|
|
32
|
+
*/
|
|
33
|
+
export function registerDropTarget(
|
|
34
|
+
reference: RendererNode,
|
|
35
|
+
listener: (event: DropTargetEvent) => void | Promise<void>
|
|
36
|
+
): Promise<NativeDropTarget> {
|
|
37
|
+
const node = asNode(reference)
|
|
38
|
+
if (node.kind === 'text-node' || !node.owner)
|
|
39
|
+
return Promise.reject(new NativeStateError('invalid_handle', 'Expected a mounted element'))
|
|
40
|
+
return node.owner.registerDropTarget(node, listener)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Observe current editing state without upgrading a submitted replacement intent. */
|
|
44
|
+
export function inputState(reference: RendererNode): InputState {
|
|
45
|
+
const node = asNode(reference)
|
|
46
|
+
if (node.kind !== 'input' || !node.owner)
|
|
47
|
+
throw new NativeStateError('invalid_handle', 'Expected a mounted native input')
|
|
48
|
+
return node.owner.input(node)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function mounted(reference: RendererNode) {
|
|
52
|
+
const node = asNode(reference)
|
|
53
|
+
if (!node.owner || node.kind === 'text-node')
|
|
54
|
+
throw new NativeStateError('invalid_handle', 'Expected a mounted native element')
|
|
55
|
+
return { node, owner: node.owner }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Focus a mounted button or input after staged changes commit, scrolling it
|
|
60
|
+
* into view. Requires the host's `focus_v1` capability.
|
|
61
|
+
*/
|
|
62
|
+
export function focusNode(reference: RendererNode): Promise<void> {
|
|
63
|
+
const { node, owner } = mounted(reference)
|
|
64
|
+
if (node.kind !== 'button' && node.kind !== 'input')
|
|
65
|
+
throw new NativeStateError('invalid_state', 'Only buttons and inputs can take focus')
|
|
66
|
+
return owner.request(node, (batch, handle) =>
|
|
67
|
+
batch.focus(handle as NativeNode<'button' | 'input'>)
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Set a mounted scrolling box or overlay's offset. Requires `scroll_control_v1`. */
|
|
72
|
+
export function scrollNodeTo(reference: RendererNode, offset: ScrollOffset): Promise<void> {
|
|
73
|
+
const { node, owner } = mounted(reference)
|
|
74
|
+
if (node.kind !== 'box' && node.kind !== 'overlay')
|
|
75
|
+
throw new NativeStateError('invalid_state', 'Only boxes and overlays scroll')
|
|
76
|
+
return owner.request(node, (batch, handle) =>
|
|
77
|
+
batch.scrollTo(handle as NativeNode<ContainerKind>, offset)
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Reveal a mounted element in its scrolling ancestors. Requires `scroll_control_v1`. */
|
|
82
|
+
export function scrollNodeIntoView(reference: RendererNode): Promise<void> {
|
|
83
|
+
const { node, owner } = mounted(reference)
|
|
84
|
+
return owner.request(node, (batch, handle) => batch.scrollIntoView(handle))
|
|
85
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { NativeStateError, type InputEvent, type NativeNode } from '@bun-native/core'
|
|
2
|
+
|
|
3
|
+
export type InputIntent = Readonly<{
|
|
4
|
+
value: string
|
|
5
|
+
revision: number
|
|
6
|
+
observedNode: NativeNode<'input'> | undefined
|
|
7
|
+
}>
|
|
8
|
+
|
|
9
|
+
// Only an actual dispatched native event is an observation. A copied InputState
|
|
10
|
+
// is an explicit replacement intent, whose expected revision must be preserved.
|
|
11
|
+
const observations = new WeakMap<object, InputIntent>()
|
|
12
|
+
|
|
13
|
+
export function observeInput(event: InputEvent): void {
|
|
14
|
+
observations.set(event, {
|
|
15
|
+
value: event.value,
|
|
16
|
+
revision: event.revision,
|
|
17
|
+
observedNode: event.node,
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function captureInput(value: unknown): string | InputIntent | undefined {
|
|
22
|
+
if (value === undefined || typeof value === 'string') return value
|
|
23
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
24
|
+
throw new TypeError(
|
|
25
|
+
'Native input value must be an initial string or { value, revision } intent'
|
|
26
|
+
)
|
|
27
|
+
const observed = observations.get(value)
|
|
28
|
+
if (observed) return Object.freeze({ ...observed })
|
|
29
|
+
if (Object.keys(value).some((field) => field !== 'value' && field !== 'revision'))
|
|
30
|
+
throw new TypeError('Explicit input intents accept only value and revision')
|
|
31
|
+
const data = value as { value?: unknown; revision?: unknown }
|
|
32
|
+
const text = data.value
|
|
33
|
+
const revision = data.revision
|
|
34
|
+
if (typeof text !== 'string') throw new TypeError('Input intent value must be a string')
|
|
35
|
+
if (typeof revision !== 'number' || !Number.isSafeInteger(revision) || revision < 0)
|
|
36
|
+
throw new NativeStateError(
|
|
37
|
+
'stale_revision',
|
|
38
|
+
'Input intent requires a nonnegative safe revision'
|
|
39
|
+
)
|
|
40
|
+
return Object.freeze({ value: text, revision, observedNode: undefined })
|
|
41
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NativeStateError,
|
|
3
|
+
UnsupportedFeatureError,
|
|
4
|
+
type NativeBatch,
|
|
5
|
+
type Element,
|
|
6
|
+
type ContainerKind,
|
|
7
|
+
type NativeNode,
|
|
8
|
+
type NativeWindow,
|
|
9
|
+
} from '@bun-native/core'
|
|
10
|
+
import type { Node, Snapshot } from './nodes.ts'
|
|
11
|
+
import { stableChildren } from '@bun-native/core/order'
|
|
12
|
+
|
|
13
|
+
export type Counts = { create: number; set: number; insert: number; remove: number }
|
|
14
|
+
export type Committed = Snapshot & { handle: NativeNode }
|
|
15
|
+
|
|
16
|
+
// Unchanged nodes share one immutable element across commits; memoize its
|
|
17
|
+
// serialization instead of restringifying the committed side every commit.
|
|
18
|
+
const keys = new WeakMap<Element, string>()
|
|
19
|
+
function key(element: Element): string {
|
|
20
|
+
let value = keys.get(element)
|
|
21
|
+
if (value === undefined) keys.set(element, (value = JSON.stringify(element)))
|
|
22
|
+
return value
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const noChildren: readonly Node[] = Object.freeze([])
|
|
26
|
+
|
|
27
|
+
function updateInput(
|
|
28
|
+
window: NativeWindow,
|
|
29
|
+
batch: NativeBatch,
|
|
30
|
+
old: Committed,
|
|
31
|
+
state: Snapshot & { element: Extract<Element, { kind: 'input' }> }
|
|
32
|
+
): boolean {
|
|
33
|
+
if (old.element.kind !== 'input')
|
|
34
|
+
throw new NativeStateError('invalid_handle', 'Input kind changed')
|
|
35
|
+
if (state.initialValue !== undefined && state.initialValue !== old.initialValue)
|
|
36
|
+
throw new UnsupportedFeatureError('input-value-requires-revision')
|
|
37
|
+
const intent = state.inputIntent
|
|
38
|
+
if (intent && intent !== old.inputIntent) {
|
|
39
|
+
if (intent.observedNode) {
|
|
40
|
+
if (intent.observedNode !== old.handle)
|
|
41
|
+
throw new NativeStateError(
|
|
42
|
+
'invalid_handle',
|
|
43
|
+
'Native input observation belongs to another node'
|
|
44
|
+
)
|
|
45
|
+
// Echo is an observation of a native edit, even if ingestion has advanced
|
|
46
|
+
// again before its callback runs. Never emit it as a replacement command.
|
|
47
|
+
} else {
|
|
48
|
+
batch.set(old.handle as NativeNode<'input'>, state.element, intent.revision)
|
|
49
|
+
return true
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const appearance = (element: Extract<Element, { kind: 'input' }>) =>
|
|
53
|
+
JSON.stringify({
|
|
54
|
+
placeholder: element.placeholder ?? '',
|
|
55
|
+
disabled: element.disabled === true,
|
|
56
|
+
read_only: element.read_only === true,
|
|
57
|
+
style: element.style ?? {},
|
|
58
|
+
})
|
|
59
|
+
if (appearance(old.element) === appearance(state.element)) return false
|
|
60
|
+
// An appearance/state-only update preserves the current native value; this never upgrades
|
|
61
|
+
// an older application value intent to a newer revision.
|
|
62
|
+
const current = window.input(old.handle as NativeNode<'input'>)
|
|
63
|
+
batch.set(
|
|
64
|
+
old.handle as NativeNode<'input'>,
|
|
65
|
+
{ ...state.element, value: current.value },
|
|
66
|
+
current.revision
|
|
67
|
+
)
|
|
68
|
+
return true
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type Commit = {
|
|
72
|
+
/** Committed records for every changed node, to publish over the previous ones. */
|
|
73
|
+
updates: Map<Node, Committed>
|
|
74
|
+
/** Previously committed nodes that left the tree, including removed descendants. */
|
|
75
|
+
removed: Node[]
|
|
76
|
+
counts: Counts
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Framework-independent native diff from the changed captures alone. Publish
|
|
81
|
+
* its records only after atomic acknowledgement; `previous` is read, never
|
|
82
|
+
* mutated, so a rejected batch leaves committed state untouched.
|
|
83
|
+
*/
|
|
84
|
+
export async function commitNativeTree(
|
|
85
|
+
window: NativeWindow,
|
|
86
|
+
root: Node,
|
|
87
|
+
previous: ReadonlyMap<Node, Committed>,
|
|
88
|
+
changes: ReadonlyMap<Node, Snapshot>
|
|
89
|
+
): Promise<Commit> {
|
|
90
|
+
const counts: Counts = { create: 0, set: 0, insert: 0, remove: 0 }
|
|
91
|
+
const [updates, removed] = await window.batch((batch) => {
|
|
92
|
+
const removed: Node[] = []
|
|
93
|
+
// Working state overlays `previous` and records only what this batch
|
|
94
|
+
// changes, instead of copying every committed handle, parent and child list.
|
|
95
|
+
const created = new Map<Node, NativeNode>()
|
|
96
|
+
const parents = new Map<Node, Node>()
|
|
97
|
+
const edited = new Map<Node, Node[]>()
|
|
98
|
+
const handle = (node: Node) => created.get(node) ?? previous.get(node)!.handle
|
|
99
|
+
const parentOf = (node: Node) =>
|
|
100
|
+
parents.has(node) ? parents.get(node) : previous.get(node)?.parent
|
|
101
|
+
const childrenOf = (node: Node): readonly Node[] =>
|
|
102
|
+
edited.get(node) ?? previous.get(node)?.children ?? noChildren
|
|
103
|
+
const editChildren = (node: Node): Node[] => {
|
|
104
|
+
let list = edited.get(node)
|
|
105
|
+
if (!list) edited.set(node, (list = [...childrenOf(node)]))
|
|
106
|
+
return list
|
|
107
|
+
}
|
|
108
|
+
// `from` is the child's known index among its current working siblings.
|
|
109
|
+
const attach = (parent: Node, child: Node, index: number, from?: number) => {
|
|
110
|
+
const oldParent = parentOf(child)
|
|
111
|
+
if (oldParent) {
|
|
112
|
+
const oldChildren = editChildren(oldParent)
|
|
113
|
+
oldChildren.splice(from ?? oldChildren.indexOf(child), 1)
|
|
114
|
+
}
|
|
115
|
+
editChildren(parent).splice(index, 0, child)
|
|
116
|
+
parents.set(child, parent)
|
|
117
|
+
batch.insert(handle(parent) as NativeNode<ContainerKind>, handle(child), index)
|
|
118
|
+
counts.insert += 1
|
|
119
|
+
}
|
|
120
|
+
// Rescue retained keyed descendants before deleting an obsolete ancestor.
|
|
121
|
+
// Moving them temporarily to this root also frees capacity before creations.
|
|
122
|
+
// A retained descendant was reinserted, so it was recaptured with its new
|
|
123
|
+
// parent; its own descendants keep their committed records and stay with it.
|
|
124
|
+
// Root children already removed natively but not yet dropped from its list.
|
|
125
|
+
let unlisted = 0
|
|
126
|
+
const rescue = (node: Node): void => {
|
|
127
|
+
// attach mutates the source sibling array, so traversal needs a snapshot.
|
|
128
|
+
const descendants = childrenOf(node).slice()
|
|
129
|
+
for (const child of descendants) {
|
|
130
|
+
if (changes.has(child)) attach(root, child, childrenOf(root).length - unlisted)
|
|
131
|
+
else rescue(child)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const forget = (node: Node): void => {
|
|
135
|
+
removed.push(node)
|
|
136
|
+
for (const child of previous.get(node)!.children) if (!changes.has(child)) forget(child)
|
|
137
|
+
}
|
|
138
|
+
// Only a recaptured parent can have lost a committed child. A lost child
|
|
139
|
+
// still in the tree was reinserted elsewhere and is therefore recaptured.
|
|
140
|
+
// Lost children leave their working list in one pass per parent, so
|
|
141
|
+
// clearing a keyed region costs O(children) rather than O(children²).
|
|
142
|
+
for (const [parent, state] of changes) {
|
|
143
|
+
const old = previous.get(parent)
|
|
144
|
+
if (!old?.children.length || old.children === state.children) continue
|
|
145
|
+
// Children shared by both lists' ends are retained; only the middles
|
|
146
|
+
// can differ, and keyed edits usually leave them short.
|
|
147
|
+
const before = old.children
|
|
148
|
+
const after = state.children
|
|
149
|
+
let start = 0
|
|
150
|
+
let end = before.length
|
|
151
|
+
let afterEnd = after.length
|
|
152
|
+
while (start < end && start < afterEnd && before[start] === after[start]) start += 1
|
|
153
|
+
while (end > start && afterEnd > start && before[end - 1] === after[afterEnd - 1]) {
|
|
154
|
+
end -= 1
|
|
155
|
+
afterEnd -= 1
|
|
156
|
+
}
|
|
157
|
+
// A child left uncaptured kept its committed parent, so it is in both
|
|
158
|
+
// middles; equal counts of them mean no committed child was lost.
|
|
159
|
+
let kept = 0
|
|
160
|
+
for (let index = start; index < afterEnd; index += 1)
|
|
161
|
+
if (!changes.has(after[index]!)) kept += 1
|
|
162
|
+
for (let index = start; index < end; index += 1) if (!changes.has(before[index]!)) kept -= 1
|
|
163
|
+
if (kept === 0) continue
|
|
164
|
+
const retained = new Set(after.slice(start, afterEnd))
|
|
165
|
+
const lost = new Set<Node>()
|
|
166
|
+
for (let index = start; index < end; index += 1) {
|
|
167
|
+
const node = before[index]!
|
|
168
|
+
if (changes.has(node) || retained.has(node)) continue
|
|
169
|
+
rescue(node)
|
|
170
|
+
batch.remove(previous.get(node)!.handle)
|
|
171
|
+
counts.remove += 1
|
|
172
|
+
lost.add(node)
|
|
173
|
+
if (parent === root) unlisted += 1
|
|
174
|
+
forget(node)
|
|
175
|
+
}
|
|
176
|
+
if (lost.size)
|
|
177
|
+
edited.set(
|
|
178
|
+
parent,
|
|
179
|
+
childrenOf(parent).filter((node) => !lost.has(node))
|
|
180
|
+
)
|
|
181
|
+
if (parent === root) unlisted = 0
|
|
182
|
+
}
|
|
183
|
+
const updates = new Map<Node, Committed>()
|
|
184
|
+
for (const [node, state] of changes) {
|
|
185
|
+
const old = previous.get(node)
|
|
186
|
+
if (!old) {
|
|
187
|
+
if (state.inputIntent?.observedNode)
|
|
188
|
+
throw new NativeStateError(
|
|
189
|
+
'invalid_handle',
|
|
190
|
+
'Native input observation belongs to another node'
|
|
191
|
+
)
|
|
192
|
+
if (state.inputIntent && state.inputIntent.revision !== 0)
|
|
193
|
+
throw new NativeStateError(
|
|
194
|
+
'stale_revision',
|
|
195
|
+
'New input value intent requires revision zero'
|
|
196
|
+
)
|
|
197
|
+
const nativeNode = batch.create(state.element)
|
|
198
|
+
created.set(node, nativeNode)
|
|
199
|
+
edited.set(node, [])
|
|
200
|
+
counts.create += 1
|
|
201
|
+
if (node === root) {
|
|
202
|
+
batch.append(window.root, nativeNode)
|
|
203
|
+
counts.insert += 1
|
|
204
|
+
}
|
|
205
|
+
updates.set(node, { ...state, handle: nativeNode })
|
|
206
|
+
continue
|
|
207
|
+
}
|
|
208
|
+
updates.set(node, { ...state, handle: old.handle })
|
|
209
|
+
if (state.element.kind === 'input') {
|
|
210
|
+
if (updateInput(window, batch, old, { ...state, element: state.element })) counts.set += 1
|
|
211
|
+
} else if (key(old.element) !== key(state.element)) {
|
|
212
|
+
batch.set(old.handle as NativeNode<Exclude<Element['kind'], 'input'>>, state.element)
|
|
213
|
+
counts.set += 1
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// A clean node's children are unchanged, so only recaptured lists can differ.
|
|
218
|
+
for (const [parent, state] of changes) {
|
|
219
|
+
const current = childrenOf(parent)
|
|
220
|
+
if (
|
|
221
|
+
current === state.children ||
|
|
222
|
+
(current.length === state.children.length &&
|
|
223
|
+
current.every((child, index) => child === state.children[index]))
|
|
224
|
+
)
|
|
225
|
+
continue
|
|
226
|
+
const siblings = editChildren(parent)
|
|
227
|
+
const stable = stableChildren(siblings, state.children)
|
|
228
|
+
// `before` tracks the anchor's working index from the end; it only moves
|
|
229
|
+
// left, so locating stable anchors costs O(children) for the whole list.
|
|
230
|
+
let before = siblings.length
|
|
231
|
+
for (let index = state.children.length - 1; index >= 0; index -= 1) {
|
|
232
|
+
const child = state.children[index]!
|
|
233
|
+
if (stable[index]) {
|
|
234
|
+
do if (--before < 0) throw new Error('Stable native child is not in its parent')
|
|
235
|
+
while (siblings[before] !== child)
|
|
236
|
+
continue
|
|
237
|
+
}
|
|
238
|
+
const old = parentOf(child) === parent ? siblings.indexOf(child) : -1
|
|
239
|
+
// Native insert indices are interpreted after detaching the child.
|
|
240
|
+
const at = before - (old >= 0 && old < before ? 1 : 0)
|
|
241
|
+
attach(parent, child, at, old >= 0 ? old : undefined)
|
|
242
|
+
before = at
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return [updates, removed] as const
|
|
246
|
+
})
|
|
247
|
+
return { updates, removed, counts }
|
|
248
|
+
}
|