@bun-native/core 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 +43 -35
- package/package.json +7 -4
- package/src/application.ts +655 -925
- package/src/batches.ts +195 -0
- package/src/context-menus.ts +12 -4
- package/src/drops.ts +7 -23
- package/src/events.ts +19 -6
- package/src/files.ts +6 -17
- package/src/message-dialogs.ts +12 -10
- package/src/notifications.ts +12 -29
- package/src/order.ts +50 -0
- package/src/processes.ts +6 -23
- package/src/registration.ts +39 -0
- package/src/shortcuts.ts +5 -26
- package/src/tree.ts +128 -67
- package/src/types.ts +1 -1
- package/src/commit-plan.ts +0 -233
package/src/shortcuts.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
type GlobalShortcutKey,
|
|
4
4
|
type ShortcutModifiers,
|
|
5
5
|
} from '@bun-native/protocol'
|
|
6
|
+
import { Registration } from './registration.ts'
|
|
6
7
|
import type { Listener } from './types.ts'
|
|
7
8
|
|
|
8
9
|
/** Layout-level key names; the native backend grabs the unshifted key that produces them. */
|
|
@@ -74,42 +75,20 @@ export function shortcutRequest(options: GlobalShortcutOptions): {
|
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
/** Internal registry entry. Consumers only receive its frozen handle. */
|
|
77
|
-
export class ShortcutRegistration {
|
|
78
|
+
export class ShortcutRegistration extends Registration<GlobalShortcutEvent> {
|
|
78
79
|
readonly handle: NativeGlobalShortcut
|
|
79
|
-
readonly controller = new AbortController()
|
|
80
|
-
listener: Listener<GlobalShortcutEvent> | undefined
|
|
81
|
-
private released = false
|
|
82
|
-
private releasing: Promise<void> | undefined
|
|
83
80
|
|
|
84
81
|
constructor(
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
id: number,
|
|
83
|
+
windowId: number,
|
|
87
84
|
key: GlobalShortcutKey,
|
|
88
85
|
modifiers: ShortcutModifiers,
|
|
89
86
|
listener: Listener<GlobalShortcutEvent>,
|
|
90
87
|
release: (registration: ShortcutRegistration) => Promise<void>
|
|
91
88
|
) {
|
|
92
|
-
|
|
89
|
+
super(id, windowId, listener)
|
|
93
90
|
this.handle = publicHandle(this, key, modifiers, release)
|
|
94
91
|
}
|
|
95
|
-
|
|
96
|
-
/** Shared by the public handle; one release per registration. */
|
|
97
|
-
release(release: (registration: ShortcutRegistration) => Promise<void>): Promise<void> {
|
|
98
|
-
this.releasing ??= this.released ? Promise.resolve() : release(this)
|
|
99
|
-
return this.releasing
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
get live(): boolean {
|
|
103
|
-
return !this.released
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Synchronous local revocation; the native grab is released separately. */
|
|
107
|
-
revoke(): void {
|
|
108
|
-
if (this.released) return
|
|
109
|
-
this.released = true
|
|
110
|
-
this.listener = undefined
|
|
111
|
-
this.controller.abort()
|
|
112
|
-
}
|
|
113
92
|
}
|
|
114
93
|
|
|
115
94
|
function publicHandle(
|
package/src/tree.ts
CHANGED
|
@@ -2,11 +2,11 @@ import {
|
|
|
2
2
|
DECORATION_FIELDS,
|
|
3
3
|
MAX_OVERLAY_DEPTH,
|
|
4
4
|
MAX_SCROLL_OFFSET,
|
|
5
|
+
OperationList,
|
|
5
6
|
parseElement,
|
|
6
7
|
type Capability,
|
|
7
8
|
type Element,
|
|
8
9
|
type Limits,
|
|
9
|
-
type Operation,
|
|
10
10
|
type Style,
|
|
11
11
|
} from '@bun-native/protocol'
|
|
12
12
|
import {
|
|
@@ -19,8 +19,28 @@ import {
|
|
|
19
19
|
type ScrollOffset,
|
|
20
20
|
} from './types.ts'
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
/** Node identity of a handle this module made, or undefined for any other value. */
|
|
23
|
+
let identity: (handle: object) => number | undefined
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A node handle: its only public member is `kind`. The identity is a private
|
|
27
|
+
* field, so no other value can carry one and reading it costs no side table.
|
|
28
|
+
*/
|
|
29
|
+
class Handle {
|
|
30
|
+
readonly #id: number
|
|
31
|
+
static {
|
|
32
|
+
identity = (handle) => (#id in handle ? (handle as Handle).#id : undefined)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
constructor(
|
|
36
|
+
readonly kind: ElementKind,
|
|
37
|
+
id: number
|
|
38
|
+
) {
|
|
39
|
+
this.#id = id
|
|
40
|
+
Object.freeze(this)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
24
44
|
// Normalized elements are replaced, never mutated, so their size is stable.
|
|
25
45
|
const textSizes = new WeakMap<Element, readonly number[]>()
|
|
26
46
|
const elementKinds: readonly string[] = ['box', 'text', 'button', 'input', 'split', 'overlay']
|
|
@@ -124,8 +144,7 @@ export function makeNode<E extends Element>(
|
|
|
124
144
|
id: number,
|
|
125
145
|
element: E
|
|
126
146
|
): NodeState & { handle: NativeNode<E['kind']> } {
|
|
127
|
-
const handle =
|
|
128
|
-
identities.set(handle, id)
|
|
147
|
+
const handle = new Handle(element.kind, id) as unknown as NativeNode<E['kind']>
|
|
129
148
|
return {
|
|
130
149
|
id,
|
|
131
150
|
handle,
|
|
@@ -136,16 +155,8 @@ export function makeNode<E extends Element>(
|
|
|
136
155
|
}
|
|
137
156
|
}
|
|
138
157
|
|
|
139
|
-
/**
|
|
140
|
-
* One logical transaction may exceed a native batch (see commit-plan.ts), but
|
|
141
|
-
* it stays bounded: a full-window mount needs a create and an insert per node.
|
|
142
|
-
*/
|
|
143
|
-
function logicalOperationLimit(limits: Readonly<Limits>): number {
|
|
144
|
-
return 2 * limits.max_nodes_per_window + limits.max_operations
|
|
145
|
-
}
|
|
146
|
-
|
|
147
158
|
export function own(nodes: ReadonlyMap<number, NodeState>, handle: NativeNode): NodeState {
|
|
148
|
-
const id =
|
|
159
|
+
const id = handle !== null && typeof handle === 'object' ? identity(handle) : undefined
|
|
149
160
|
const node = id === undefined ? undefined : nodes.get(id)
|
|
150
161
|
if (!node || node.handle !== handle)
|
|
151
162
|
throw new NativeStateError(
|
|
@@ -166,7 +177,8 @@ function textSizesOf(element: Element): readonly number[] {
|
|
|
166
177
|
: 'accessible_label' in element && element.accessible_label !== undefined
|
|
167
178
|
? [element.accessible_label]
|
|
168
179
|
: []
|
|
169
|
-
|
|
180
|
+
// Counted without encoding; texts are well formed, as parseElement checked.
|
|
181
|
+
sizes = texts.map((text) => Buffer.byteLength(text, 'utf8'))
|
|
170
182
|
textSizes.set(element, sizes)
|
|
171
183
|
}
|
|
172
184
|
return sizes
|
|
@@ -202,19 +214,28 @@ export class CommittedText {
|
|
|
202
214
|
}
|
|
203
215
|
}
|
|
204
216
|
|
|
217
|
+
/** The window text a transaction starts from: committed totals, or an earlier candidate's. */
|
|
218
|
+
interface TextBase {
|
|
219
|
+
readonly bytes: number
|
|
220
|
+
/** Nodes above the per-text limit; a validated candidate has none. */
|
|
221
|
+
readonly oversized: ReadonlySet<number>
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const NO_NODES: ReadonlySet<number> = new Set()
|
|
225
|
+
|
|
205
226
|
/**
|
|
206
227
|
* The candidate graph: this transaction's own node copies and removals over
|
|
207
|
-
* the committed map
|
|
208
|
-
* per window node.
|
|
228
|
+
* the committed map (or an earlier transaction's candidate), which it never
|
|
229
|
+
* writes, so opening a batch costs nothing per window node.
|
|
209
230
|
*/
|
|
210
231
|
class Overlay implements ReadonlyMap<number, NodeState> {
|
|
211
232
|
/** Nodes this transaction created or wrote; each is its own copy. */
|
|
212
233
|
readonly writes = new Map<number, NodeState>()
|
|
213
|
-
/**
|
|
234
|
+
/** Base nodes this transaction removed. */
|
|
214
235
|
readonly deletes = new Set<number>()
|
|
215
236
|
size: number
|
|
216
237
|
|
|
217
|
-
constructor(
|
|
238
|
+
constructor(public base: ReadonlyMap<number, NodeState>) {
|
|
218
239
|
this.size = base.size
|
|
219
240
|
}
|
|
220
241
|
|
|
@@ -267,14 +288,29 @@ class Overlay implements ReadonlyMap<number, NodeState> {
|
|
|
267
288
|
}
|
|
268
289
|
}
|
|
269
290
|
|
|
291
|
+
/**
|
|
292
|
+
* Upper bound on an operation's JSON bytes beyond its text values: its keys,
|
|
293
|
+
* identifiers and every element field and style value at full width.
|
|
294
|
+
*/
|
|
295
|
+
const OPERATION_BYTES = 2_048
|
|
296
|
+
/** Upper bound on a batch command's envelope: id, type, window, revision and brackets. */
|
|
297
|
+
export const BATCH_ENVELOPE_BYTES = 256
|
|
298
|
+
|
|
270
299
|
/**
|
|
271
300
|
* A bounded candidate graph; native preflight remains authoritative. Nodes are
|
|
272
301
|
* shared with the committed graph until this transaction first writes one.
|
|
302
|
+
*
|
|
303
|
+
* A transaction opened with `next` continues an earlier one that is not yet
|
|
304
|
+
* acknowledged: it reads that candidate, and both can be sent as one native
|
|
305
|
+
* batch. It commits only after `rebase` onto the committed graph the earlier
|
|
306
|
+
* one was committed to.
|
|
273
307
|
*/
|
|
274
308
|
export class Transaction implements NativeBatch {
|
|
275
309
|
readonly nodes: Overlay
|
|
276
|
-
readonly operations
|
|
310
|
+
readonly operations = new OperationList()
|
|
277
311
|
private readonly requests: Request[] = []
|
|
312
|
+
/** Requests of earlier transactions sent in the same native batch, checked on this candidate. */
|
|
313
|
+
private prior: readonly Request[] = []
|
|
278
314
|
/** Created and moved nodes: the only places a batch can break ownership or depth. */
|
|
279
315
|
private readonly attached: number[] = []
|
|
280
316
|
private active = true
|
|
@@ -286,11 +322,62 @@ export class Transaction implements NativeBatch {
|
|
|
286
322
|
private readonly allocateId: () => number,
|
|
287
323
|
private readonly limits: Readonly<Limits>,
|
|
288
324
|
private readonly capabilities: readonly Capability[],
|
|
289
|
-
private
|
|
325
|
+
private text: TextBase
|
|
290
326
|
) {
|
|
291
327
|
this.nodes = new Overlay(current)
|
|
292
328
|
}
|
|
293
329
|
|
|
330
|
+
/** A transaction over this validated candidate, to send in the same native batch. */
|
|
331
|
+
next(): Transaction {
|
|
332
|
+
const next = new Transaction(
|
|
333
|
+
this.nodes,
|
|
334
|
+
this.rootId,
|
|
335
|
+
this.allocateId,
|
|
336
|
+
this.limits,
|
|
337
|
+
this.capabilities,
|
|
338
|
+
{ bytes: this.textBytes(), oversized: NO_NODES }
|
|
339
|
+
)
|
|
340
|
+
next.prior = this.prior.length ? [...this.prior, ...this.requests] : this.requests
|
|
341
|
+
return next
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Continue from `nodes` and `text` once the transaction this one continued
|
|
346
|
+
* is committed to them; it is then sent, or committed, on its own.
|
|
347
|
+
*/
|
|
348
|
+
rebase(nodes: Map<number, NodeState>, text: CommittedText): void {
|
|
349
|
+
this.nodes.base = nodes
|
|
350
|
+
this.text = text
|
|
351
|
+
this.prior = []
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** Start a native batch of its own: the batch it continued was sent without it. */
|
|
355
|
+
detach(): void {
|
|
356
|
+
this.prior = []
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Whether the earlier requests of this native batch still hold on this candidate. */
|
|
360
|
+
keepsPrior(): boolean {
|
|
361
|
+
try {
|
|
362
|
+
this.validateRequests(this.prior)
|
|
363
|
+
return true
|
|
364
|
+
} catch {
|
|
365
|
+
return false
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** An upper bound on this transaction's operations as JSON, without serializing them. */
|
|
370
|
+
bytesBound(): number {
|
|
371
|
+
let bytes = 0
|
|
372
|
+
for (const operation of this.operations) {
|
|
373
|
+
bytes += OPERATION_BYTES
|
|
374
|
+
// A JSON escape is at most six bytes per UTF-8 byte (`\u001f` for one).
|
|
375
|
+
if (operation.type === 'create' || operation.type === 'set')
|
|
376
|
+
bytes += 6 * textBytesOf(operation.element)
|
|
377
|
+
}
|
|
378
|
+
return bytes
|
|
379
|
+
}
|
|
380
|
+
|
|
294
381
|
/** The candidate's own copy of a node, made on its first write. */
|
|
295
382
|
private writable(node: NodeState): NodeState {
|
|
296
383
|
const written = this.nodes.writes.get(node.id)
|
|
@@ -303,7 +390,7 @@ export class Transaction implements NativeBatch {
|
|
|
303
390
|
private check(): void {
|
|
304
391
|
if (!this.active)
|
|
305
392
|
throw new NativeStateError('invalid_state', 'Batch callback has already returned')
|
|
306
|
-
if (this.operations.length >=
|
|
393
|
+
if (this.operations.length >= this.limits.max_operations)
|
|
307
394
|
throw new NativeStateError('limit_exceeded', 'Batch operation limit exceeded')
|
|
308
395
|
}
|
|
309
396
|
|
|
@@ -319,7 +406,7 @@ export class Transaction implements NativeBatch {
|
|
|
319
406
|
const node = makeNode(this.allocateId(), normalized)
|
|
320
407
|
this.nodes.write(node)
|
|
321
408
|
this.attached.push(node.id)
|
|
322
|
-
this.operations.
|
|
409
|
+
this.operations.create(node.id, node.element)
|
|
323
410
|
return node.handle as NativeNode<E['kind']>
|
|
324
411
|
}
|
|
325
412
|
|
|
@@ -352,17 +439,12 @@ export class Transaction implements NativeBatch {
|
|
|
352
439
|
)
|
|
353
440
|
if (node.inputRevision === Number.MAX_SAFE_INTEGER)
|
|
354
441
|
throw new NativeStateError('limit_exceeded', 'Input revision exhausted')
|
|
355
|
-
this.operations.
|
|
356
|
-
type: 'set',
|
|
357
|
-
node_id: node.id,
|
|
358
|
-
element,
|
|
359
|
-
expected_input_revision: node.inputRevision,
|
|
360
|
-
})
|
|
442
|
+
this.operations.set(node.id, element, node.inputRevision)
|
|
361
443
|
node.inputRevision += 1
|
|
362
444
|
} else {
|
|
363
445
|
if (expectedInputRevision !== undefined)
|
|
364
446
|
throw new NativeStateError('invalid_state', 'Only an input accepts an edit revision')
|
|
365
|
-
this.operations.
|
|
447
|
+
this.operations.set(node.id, element)
|
|
366
448
|
}
|
|
367
449
|
node.element = element
|
|
368
450
|
// An overlay's surface decides what its subtree may hold.
|
|
@@ -371,8 +453,10 @@ export class Transaction implements NativeBatch {
|
|
|
371
453
|
|
|
372
454
|
insert(parentHandle: NativeNode<ContainerKind>, childHandle: NativeNode, index: number): void {
|
|
373
455
|
this.check()
|
|
374
|
-
|
|
375
|
-
|
|
456
|
+
this.insertNodes(own(this.nodes, parentHandle), own(this.nodes, childHandle), index)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
private insertNodes(parent: NodeState, child: NodeState, index: number): void {
|
|
376
460
|
if (!isContainer(parent.element) || child.id === this.rootId)
|
|
377
461
|
throw new NativeStateError(
|
|
378
462
|
'invalid_state',
|
|
@@ -396,17 +480,14 @@ export class Transaction implements NativeBatch {
|
|
|
396
480
|
parent.children.splice(index, 0, child.id)
|
|
397
481
|
child.parent = parent.id
|
|
398
482
|
this.attached.push(child.id)
|
|
399
|
-
this.operations.
|
|
483
|
+
this.operations.insert(parent.id, child.id, index)
|
|
400
484
|
}
|
|
401
485
|
|
|
402
486
|
append(parentHandle: NativeNode<ContainerKind>, childHandle: NativeNode): void {
|
|
403
487
|
const parent = own(this.nodes, parentHandle)
|
|
404
488
|
const child = own(this.nodes, childHandle)
|
|
405
|
-
this.
|
|
406
|
-
|
|
407
|
-
childHandle,
|
|
408
|
-
parent.children.length - (child.parent === parent.id ? 1 : 0)
|
|
409
|
-
)
|
|
489
|
+
this.check()
|
|
490
|
+
this.insertNodes(parent, child, parent.children.length - (child.parent === parent.id ? 1 : 0))
|
|
410
491
|
}
|
|
411
492
|
|
|
412
493
|
remove(handle: NativeNode): void {
|
|
@@ -416,7 +497,8 @@ export class Transaction implements NativeBatch {
|
|
|
416
497
|
throw new NativeStateError('invalid_state', 'Close the window to dispose its root')
|
|
417
498
|
if (node.parent !== undefined) {
|
|
418
499
|
const parent = this.writable(this.nodes.get(node.parent)!)
|
|
419
|
-
parent.children
|
|
500
|
+
if (parent.children[parent.children.length - 1] === node.id) parent.children.pop()
|
|
501
|
+
else parent.children.splice(parent.children.indexOf(node.id), 1)
|
|
420
502
|
}
|
|
421
503
|
const pending = [node.id]
|
|
422
504
|
for (const id of pending) {
|
|
@@ -424,7 +506,7 @@ export class Transaction implements NativeBatch {
|
|
|
424
506
|
pending.push(...child.children)
|
|
425
507
|
this.nodes.delete(id)
|
|
426
508
|
}
|
|
427
|
-
this.operations.
|
|
509
|
+
this.operations.delete(node.id)
|
|
428
510
|
}
|
|
429
511
|
|
|
430
512
|
focus(handle: NativeNode<'button' | 'input'>): void {
|
|
@@ -432,7 +514,7 @@ export class Transaction implements NativeBatch {
|
|
|
432
514
|
requireCapability(this.capabilities, 'focus_v1', 'operation:focus')
|
|
433
515
|
const node = own(this.nodes, handle)
|
|
434
516
|
this.requests.push({ type: 'focus', id: node.id })
|
|
435
|
-
this.operations.
|
|
517
|
+
this.operations.focus(node.id)
|
|
436
518
|
}
|
|
437
519
|
|
|
438
520
|
scrollTo(handle: NativeNode<ContainerKind>, offset: ScrollOffset): void {
|
|
@@ -457,7 +539,7 @@ export class Transaction implements NativeBatch {
|
|
|
457
539
|
if (axes.x === undefined && axes.y === undefined)
|
|
458
540
|
throw new TypeError('A scroll offset requires x or y')
|
|
459
541
|
this.requests.push({ type: 'scroll_to', id: node.id, ...axes })
|
|
460
|
-
this.operations.
|
|
542
|
+
this.operations.scrollTo(node.id, axes)
|
|
461
543
|
}
|
|
462
544
|
|
|
463
545
|
scrollIntoView(handle: NativeNode): void {
|
|
@@ -465,7 +547,7 @@ export class Transaction implements NativeBatch {
|
|
|
465
547
|
requireCapability(this.capabilities, 'scroll_control_v1', 'operation:scroll_into_view')
|
|
466
548
|
const node = own(this.nodes, handle)
|
|
467
549
|
this.requests.push({ type: 'scroll_into_view', id: node.id })
|
|
468
|
-
this.operations.
|
|
550
|
+
this.operations.scrollIntoView(node.id)
|
|
469
551
|
}
|
|
470
552
|
|
|
471
553
|
/** The modal overlay highest in paint order: deepest, then later in tree order. */
|
|
@@ -486,8 +568,8 @@ export class Transaction implements NativeBatch {
|
|
|
486
568
|
return active?.id
|
|
487
569
|
}
|
|
488
570
|
|
|
489
|
-
private validateRequests(): void {
|
|
490
|
-
for (const request of
|
|
571
|
+
private validateRequests(requests: readonly Request[] = this.requests): void {
|
|
572
|
+
for (const request of requests) {
|
|
491
573
|
const node = this.nodes.get(request.id)
|
|
492
574
|
if (!node)
|
|
493
575
|
throw new NativeStateError('invalid_handle', 'Request target is removed by this batch')
|
|
@@ -520,28 +602,6 @@ export class Transaction implements NativeBatch {
|
|
|
520
602
|
}
|
|
521
603
|
}
|
|
522
604
|
|
|
523
|
-
/**
|
|
524
|
-
* Give created nodes the identities in `identity` (old to new) before a
|
|
525
|
-
* staged commit (see commit-plan.ts). Their handles were never committed,
|
|
526
|
-
* and only this transaction's own copies can name a created node.
|
|
527
|
-
*/
|
|
528
|
-
renumber(identity: ReadonlyMap<number, number>): void {
|
|
529
|
-
const { writes } = this.nodes
|
|
530
|
-
const map = (id: number) => identity.get(id) ?? id
|
|
531
|
-
const written = [...writes.values()]
|
|
532
|
-
for (const id of identity.keys()) writes.delete(id)
|
|
533
|
-
for (const node of written) {
|
|
534
|
-
const next = map(node.id)
|
|
535
|
-
if (next !== node.id) {
|
|
536
|
-
identities.set(node.handle, next)
|
|
537
|
-
node.id = next
|
|
538
|
-
}
|
|
539
|
-
if (node.parent !== undefined) node.parent = map(node.parent)
|
|
540
|
-
node.children = node.children.map(map)
|
|
541
|
-
writes.set(next, node)
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
605
|
/** The candidate's window text bytes, from the committed total and this batch's writes. */
|
|
546
606
|
private textBytes(): number {
|
|
547
607
|
const { base, writes, deletes } = this.nodes
|
|
@@ -643,6 +703,7 @@ export class Transaction implements NativeBatch {
|
|
|
643
703
|
* totals it was opened over, in time proportional to the batch.
|
|
644
704
|
*/
|
|
645
705
|
commit(nodes: Map<number, NodeState>, text: CommittedText): void {
|
|
706
|
+
// A continuation commits only after `rebase` onto the committed graph.
|
|
646
707
|
if (nodes !== this.nodes.base || text !== this.text)
|
|
647
708
|
throw new NativeStateError(
|
|
648
709
|
'invalid_state',
|
package/src/types.ts
CHANGED
|
@@ -181,7 +181,7 @@ export interface NativeBatch {
|
|
|
181
181
|
scrollTo(node: NativeNode<ContainerKind>, offset: ScrollOffset): void
|
|
182
182
|
/**
|
|
183
183
|
* Requires `scroll_control_v1`. Scroll every scrolling ancestor inside the
|
|
184
|
-
* node's own layer
|
|
184
|
+
* node's own layer, by the least amount, so the node is visible in it.
|
|
185
185
|
*/
|
|
186
186
|
scrollIntoView(node: NativeNode): void
|
|
187
187
|
}
|
package/src/commit-plan.ts
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import type { Element, Limits, Operation } from '@bun-native/protocol'
|
|
2
|
-
import type { NodeState, Transaction } from './tree.ts'
|
|
3
|
-
import { NativeStateError } from './types.ts'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A logical transaction larger than one native batch.
|
|
7
|
-
*
|
|
8
|
-
* The native host validates and paints each batch atomically, rejects a batch
|
|
9
|
-
* above `max_operations`, and rejects any batch that leaves a node detached.
|
|
10
|
-
* Splitting the application's operation sequence would therefore expose (and
|
|
11
|
-
* be unable to roll back) its intermediate states. Instead, new nodes are
|
|
12
|
-
* first built under a zero-size, clipped staging box appended to the window
|
|
13
|
-
* root. Those staging batches touch only nodes the application has not yet
|
|
14
|
-
* committed, so they change nothing it or the user can see. One final batch
|
|
15
|
-
* then applies every change to committed nodes at once: sets, moves of new
|
|
16
|
-
* subtrees and retained nodes into place, deletions, and the staging box's
|
|
17
|
-
* removal. A failure before or in that batch removes the staging box, which
|
|
18
|
-
* restores the committed tree exactly. The wire protocol is unchanged.
|
|
19
|
-
*
|
|
20
|
-
* New nodes receive fresh identities in pre-order so that each parent exists
|
|
21
|
-
* before its children while native identities keep increasing. The
|
|
22
|
-
* transaction renumbers its own copies and their handles, none of which was
|
|
23
|
-
* committed, and later commits copy-on-write as a single batch does.
|
|
24
|
-
*/
|
|
25
|
-
export interface StagedCommit {
|
|
26
|
-
readonly staging: readonly (readonly Operation[])[]
|
|
27
|
-
readonly visible: readonly Operation[]
|
|
28
|
-
readonly discard: Operation
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const STAGING: Element = {
|
|
32
|
-
kind: 'box',
|
|
33
|
-
style: { width: 0, height: 0, max_width: 0, max_height: 0, flex_shrink: 0, overflow: 'hidden' },
|
|
34
|
-
}
|
|
35
|
-
// The command envelope (id, type, window and revision) and the array brackets.
|
|
36
|
-
const FRAME_OVERHEAD = 256
|
|
37
|
-
|
|
38
|
-
function bytes(operation: Operation): number {
|
|
39
|
-
return Buffer.byteLength(JSON.stringify(operation)) + 1
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Split units that must stay together into frames within both native limits. */
|
|
43
|
-
function frames(units: readonly Operation[][], limits: Readonly<Limits>): Operation[][] {
|
|
44
|
-
const budget = limits.max_frame_bytes - FRAME_OVERHEAD
|
|
45
|
-
const result: Operation[][] = []
|
|
46
|
-
let current: Operation[] = []
|
|
47
|
-
let size = 0
|
|
48
|
-
for (const unit of units) {
|
|
49
|
-
const unitSize = unit.reduce((total, operation) => total + bytes(operation), 0)
|
|
50
|
-
if (
|
|
51
|
-
current.length &&
|
|
52
|
-
(current.length + unit.length > limits.max_operations || size + unitSize > budget)
|
|
53
|
-
) {
|
|
54
|
-
result.push(current)
|
|
55
|
-
current = []
|
|
56
|
-
size = 0
|
|
57
|
-
}
|
|
58
|
-
current.push(...unit)
|
|
59
|
-
size += unitSize
|
|
60
|
-
}
|
|
61
|
-
if (current.length) result.push(current)
|
|
62
|
-
return result
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** The longest subsequence of `target` already in `current` order. */
|
|
66
|
-
function stableChildren(current: readonly number[], target: readonly number[]): Set<number> {
|
|
67
|
-
const positions = new Map(current.map((id, index) => [id, index]))
|
|
68
|
-
const tails: { id: number; position: number }[] = []
|
|
69
|
-
const preceding = new Map<number, number | undefined>()
|
|
70
|
-
for (const id of target) {
|
|
71
|
-
const position = positions.get(id)
|
|
72
|
-
if (position === undefined) continue
|
|
73
|
-
let start = 0
|
|
74
|
-
let end = tails.length
|
|
75
|
-
while (start < end) {
|
|
76
|
-
const middle = (start + end) >>> 1
|
|
77
|
-
if (tails[middle]!.position < position) start = middle + 1
|
|
78
|
-
else end = middle
|
|
79
|
-
}
|
|
80
|
-
preceding.set(id, tails[start - 1]?.id)
|
|
81
|
-
tails[start] = { id, position }
|
|
82
|
-
}
|
|
83
|
-
const stable = new Set<number>()
|
|
84
|
-
for (let id = tails.at(-1)?.id; id !== undefined; id = preceding.get(id)) stable.add(id)
|
|
85
|
-
return stable
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function planStagedCommit(
|
|
89
|
-
committed: ReadonlyMap<number, NodeState>,
|
|
90
|
-
transaction: Transaction,
|
|
91
|
-
rootId: number,
|
|
92
|
-
allocate: () => number,
|
|
93
|
-
limits: Readonly<Limits>
|
|
94
|
-
): StagedCommit {
|
|
95
|
-
const { nodes, operations } = transaction
|
|
96
|
-
const preorder: number[] = []
|
|
97
|
-
for (const pending = [rootId]; pending.length;) {
|
|
98
|
-
const id = pending.pop()!
|
|
99
|
-
preorder.push(id)
|
|
100
|
-
pending.push(...[...nodes.get(id)!.children].reverse())
|
|
101
|
-
}
|
|
102
|
-
const staging = allocate()
|
|
103
|
-
const identity = new Map<number, number>()
|
|
104
|
-
for (const id of preorder) if (!committed.has(id)) identity.set(id, allocate())
|
|
105
|
-
transaction.renumber(identity)
|
|
106
|
-
const order = preorder.map((id) => identity.get(id) ?? id)
|
|
107
|
-
const fresh = new Set(identity.values())
|
|
108
|
-
|
|
109
|
-
const units: Operation[][] = [
|
|
110
|
-
[
|
|
111
|
-
{ type: 'create', node_id: staging, element: STAGING },
|
|
112
|
-
{
|
|
113
|
-
type: 'insert',
|
|
114
|
-
parent_id: rootId,
|
|
115
|
-
child_id: staging,
|
|
116
|
-
index: committed.get(rootId)!.children.length,
|
|
117
|
-
},
|
|
118
|
-
],
|
|
119
|
-
]
|
|
120
|
-
// The simulated native children after staging, for placing nodes below.
|
|
121
|
-
const children = new Map<number, number[]>()
|
|
122
|
-
const parents = new Map<number, number>()
|
|
123
|
-
for (const [id, node] of committed) {
|
|
124
|
-
children.set(id, [...node.children])
|
|
125
|
-
if (node.parent !== undefined) parents.set(id, node.parent)
|
|
126
|
-
}
|
|
127
|
-
children.get(rootId)!.push(staging)
|
|
128
|
-
children.set(staging, [])
|
|
129
|
-
parents.set(staging, rootId)
|
|
130
|
-
for (const next of order) {
|
|
131
|
-
if (!fresh.has(next)) continue
|
|
132
|
-
const node = nodes.get(next)!
|
|
133
|
-
// Pre-order: a new parent is already staged; otherwise wait in the box.
|
|
134
|
-
const parent = fresh.has(node.parent!) ? node.parent! : staging
|
|
135
|
-
const siblings = children.get(parent)!
|
|
136
|
-
units.push([
|
|
137
|
-
{ type: 'create', node_id: next, element: node.element },
|
|
138
|
-
{ type: 'insert', parent_id: parent, child_id: next, index: siblings.length },
|
|
139
|
-
])
|
|
140
|
-
siblings.push(next)
|
|
141
|
-
children.set(next, [])
|
|
142
|
-
parents.set(next, parent)
|
|
143
|
-
// A new input's edit revision counts its sets in this transaction.
|
|
144
|
-
if (node.element.kind === 'input')
|
|
145
|
-
for (let revision = 0; revision < node.inputRevision; revision += 1)
|
|
146
|
-
units.push([
|
|
147
|
-
{
|
|
148
|
-
type: 'set',
|
|
149
|
-
node_id: next,
|
|
150
|
-
element: node.element,
|
|
151
|
-
expected_input_revision: revision,
|
|
152
|
-
},
|
|
153
|
-
])
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const visible: Operation[] = operations.filter(
|
|
157
|
-
(operation) =>
|
|
158
|
-
operation.type === 'set' && committed.has(operation.node_id) && nodes.has(operation.node_id)
|
|
159
|
-
)
|
|
160
|
-
for (const next of order) {
|
|
161
|
-
const target = nodes.get(next)!.children
|
|
162
|
-
const current = children.get(next)!
|
|
163
|
-
// Keep a longest already ordered run and move only the other children,
|
|
164
|
-
// last first, each before its placed successor. Nodes that are not in the
|
|
165
|
-
// target stay behind until they move to a later parent or are deleted.
|
|
166
|
-
const stable = stableChildren(current, target)
|
|
167
|
-
let anchor: number | undefined
|
|
168
|
-
for (let index = target.length - 1; index >= 0; index -= 1) {
|
|
169
|
-
const child = target[index]!
|
|
170
|
-
if (!stable.has(child)) {
|
|
171
|
-
const previous = children.get(parents.get(child)!)!
|
|
172
|
-
previous.splice(previous.indexOf(child), 1)
|
|
173
|
-
const before = anchor === undefined ? current.length : current.indexOf(anchor)
|
|
174
|
-
current.splice(before, 0, child)
|
|
175
|
-
parents.set(child, next)
|
|
176
|
-
visible.push({ type: 'insert', parent_id: next, child_id: child, index: before })
|
|
177
|
-
}
|
|
178
|
-
anchor = child
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
for (const [id, node] of committed)
|
|
182
|
-
if (!nodes.has(id) && node.parent !== undefined && nodes.has(node.parent))
|
|
183
|
-
visible.push({ type: 'delete', node_id: id })
|
|
184
|
-
visible.push({ type: 'delete', node_id: staging })
|
|
185
|
-
// Focus and scroll requests are applied natively after the tree changes, so
|
|
186
|
-
// they ride the final batch, addressed by the renumbered identities.
|
|
187
|
-
for (const operation of operations)
|
|
188
|
-
if (
|
|
189
|
-
operation.type === 'focus' ||
|
|
190
|
-
operation.type === 'scroll_to' ||
|
|
191
|
-
operation.type === 'scroll_into_view'
|
|
192
|
-
)
|
|
193
|
-
visible.push({ ...operation, node_id: identity.get(operation.node_id) ?? operation.node_id })
|
|
194
|
-
if (visible.length > limits.max_operations)
|
|
195
|
-
throw new NativeStateError(
|
|
196
|
-
'limit_exceeded',
|
|
197
|
-
'Batch operation limit exceeded by changes to committed nodes'
|
|
198
|
-
)
|
|
199
|
-
return {
|
|
200
|
-
staging: frames(units, limits),
|
|
201
|
-
visible,
|
|
202
|
-
discard: { type: 'delete', node_id: staging },
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Send a staged commit. `acknowledge` runs in each reply's own continuation,
|
|
208
|
-
* so a native event after that batch never sees an older revision.
|
|
209
|
-
*/
|
|
210
|
-
export async function sendStagedCommit<Reply>(
|
|
211
|
-
plan: StagedCommit,
|
|
212
|
-
send: (operations: readonly Operation[]) => Promise<Reply>,
|
|
213
|
-
acknowledge: (reply: Reply, final: boolean) => void
|
|
214
|
-
): Promise<void> {
|
|
215
|
-
let staged = false
|
|
216
|
-
try {
|
|
217
|
-
for (const operations of plan.staging) {
|
|
218
|
-
acknowledge(await send(operations), false)
|
|
219
|
-
staged = true
|
|
220
|
-
}
|
|
221
|
-
acknowledge(await send(plan.visible), true)
|
|
222
|
-
} catch (error) {
|
|
223
|
-
// Removing the box (and every new node still in it) restores the
|
|
224
|
-
// committed tree; the original rejection is the transaction's result.
|
|
225
|
-
if (staged)
|
|
226
|
-
try {
|
|
227
|
-
acknowledge(await send([plan.discard]), false)
|
|
228
|
-
} catch {
|
|
229
|
-
// A closed window or failed host already ended this tree's lifetime.
|
|
230
|
-
}
|
|
231
|
-
throw error
|
|
232
|
-
}
|
|
233
|
-
}
|