@bun-native/core 0.33.1 → 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 -14
- package/package.json +3 -3
- package/src/application.ts +93 -53
- package/src/batches.ts +195 -0
- package/src/context-menus.ts +12 -4
- package/src/tree.ts +127 -36
- package/src/types.ts +1 -1
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
The public TypeScript API of the Bun Native desktop framework.
|
|
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,11 +1,11 @@
|
|
|
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/core/package.json": {
|
|
11
11
|
"bytes": 282,
|
|
@@ -16,12 +16,16 @@
|
|
|
16
16
|
"sha256": "be41f76ea03059acb892700a172a8df3f07821af79f16332173399aaf6db7731"
|
|
17
17
|
},
|
|
18
18
|
"packages/core/src/application.ts": {
|
|
19
|
-
"bytes":
|
|
20
|
-
"sha256": "
|
|
19
|
+
"bytes": 67089,
|
|
20
|
+
"sha256": "bf60c3fca0f0a08a1a9f9d9db79a28674515bf3ccdc66baf8b8a2fe9942c1b71"
|
|
21
|
+
},
|
|
22
|
+
"packages/core/src/batches.ts": {
|
|
23
|
+
"bytes": 6731,
|
|
24
|
+
"sha256": "60725d766db72490aff5008c87d012d9992a2a1a66254781b67ee9cf72493c61"
|
|
21
25
|
},
|
|
22
26
|
"packages/core/src/context-menus.ts": {
|
|
23
|
-
"bytes":
|
|
24
|
-
"sha256": "
|
|
27
|
+
"bytes": 2592,
|
|
28
|
+
"sha256": "4163f9e8d6280e7d52d04c5365e8babef43329e9198230ea8d64a75f66d2a4cb"
|
|
25
29
|
},
|
|
26
30
|
"packages/core/src/dialogs.ts": {
|
|
27
31
|
"bytes": 5546,
|
|
@@ -88,12 +92,12 @@
|
|
|
88
92
|
"sha256": "c460c4e1a2e9d337e79294bce333e53896381529af8f7e35b23e207c6e132b85"
|
|
89
93
|
},
|
|
90
94
|
"packages/core/src/tree.ts": {
|
|
91
|
-
"bytes":
|
|
92
|
-
"sha256": "
|
|
95
|
+
"bytes": 26849,
|
|
96
|
+
"sha256": "625288cead96ed1705dcadbb7e88382c4b1e9db5999ec26550521095939bfc0f"
|
|
93
97
|
},
|
|
94
98
|
"packages/core/src/types.ts": {
|
|
95
|
-
"bytes":
|
|
96
|
-
"sha256": "
|
|
99
|
+
"bytes": 14315,
|
|
100
|
+
"sha256": "5d8196d284e81c064c4c677f4c0c362434fa4e32555607dd3ea27f61453c037d"
|
|
97
101
|
},
|
|
98
102
|
"LICENSE": {
|
|
99
103
|
"bytes": 1075,
|
|
@@ -105,7 +109,7 @@
|
|
|
105
109
|
"sha256": "9f4d9362cb4cc38c0f4e705295789a0cbfec46ffdd007e0541b9441a7c3003a7"
|
|
106
110
|
},
|
|
107
111
|
"dependencyLock": {
|
|
108
|
-
"bytes":
|
|
109
|
-
"sha256": "
|
|
112
|
+
"bytes": 28430,
|
|
113
|
+
"sha256": "9d321f65b9224958ec99286628d652232a16e3242ea9536a396bceaf72892460"
|
|
110
114
|
}
|
|
111
115
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bun-native/core",
|
|
3
|
-
"version": "0.33.
|
|
3
|
+
"version": "0.33.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"description": "The public TypeScript API of the Bun Native desktop framework.",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@bun-native/protocol": "0.33.
|
|
22
|
-
"@bun-native/runtime": "0.33.
|
|
21
|
+
"@bun-native/protocol": "0.33.2",
|
|
22
|
+
"@bun-native/runtime": "0.33.2"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
package/src/application.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
OperationList,
|
|
2
3
|
parseRootId,
|
|
3
4
|
ProtocolError,
|
|
4
5
|
type ApplicationEvent,
|
|
@@ -95,6 +96,7 @@ import {
|
|
|
95
96
|
type MessageDialogOptions,
|
|
96
97
|
type MessageDialogResult,
|
|
97
98
|
} from './message-dialogs.ts'
|
|
99
|
+
import { runBatches, type BatchEntry } from './batches.ts'
|
|
98
100
|
import { CommittedText, makeNode, own, Transaction, type NodeState } from './tree.ts'
|
|
99
101
|
import {
|
|
100
102
|
NativeStateError,
|
|
@@ -190,6 +192,8 @@ class Application implements NativeApplication {
|
|
|
190
192
|
private menuBar: MenuBarRegistration | undefined
|
|
191
193
|
private tail: Promise<void> = Promise.resolve()
|
|
192
194
|
private queued = 0
|
|
195
|
+
/** The last queued operation while it is a batch group that has not started. */
|
|
196
|
+
private openGroup: { window: Window; entries: BatchEntry[] } | undefined
|
|
193
197
|
private readonly services = new Set<Promise<void>>()
|
|
194
198
|
private callbacks: Callback[] = []
|
|
195
199
|
private dispatching: Promise<void> | undefined
|
|
@@ -288,16 +292,59 @@ class Application implements NativeApplication {
|
|
|
288
292
|
}
|
|
289
293
|
}
|
|
290
294
|
|
|
291
|
-
|
|
295
|
+
/** Why a new queued operation is refused, if it is. */
|
|
296
|
+
private refusal(closingWindow: boolean): Error | undefined {
|
|
292
297
|
if (this.phase !== 'running')
|
|
293
|
-
return
|
|
294
|
-
this.failure ?? new NativeStateError('invalid_state', 'Application is shutting down')
|
|
295
|
-
)
|
|
298
|
+
return this.failure ?? new NativeStateError('invalid_state', 'Application is shutting down')
|
|
296
299
|
// At most one close per live window may use the reserved cleanup capacity.
|
|
297
300
|
if (!closingWindow && this.queued >= MAX_QUEUED_TRANSACTIONS)
|
|
298
|
-
return
|
|
299
|
-
|
|
300
|
-
|
|
301
|
+
return new NativeStateError('limit_exceeded', 'Application transaction queue is full')
|
|
302
|
+
return undefined
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Queue a window batch. Batches queued back to back for one window, while
|
|
307
|
+
* another operation is in flight, run as one group and share native batches
|
|
308
|
+
* (`runBatches`); each still counts against the queue limit.
|
|
309
|
+
*/
|
|
310
|
+
batch<T>(window: Window, update: (batch: NativeBatch) => T): Promise<T> {
|
|
311
|
+
const refused = this.refusal(false)
|
|
312
|
+
if (refused) return Promise.reject(refused)
|
|
313
|
+
let entry!: BatchEntry
|
|
314
|
+
const result = new Promise<T>((resolve, reject) => {
|
|
315
|
+
entry = { update, resolve: resolve as (value: unknown) => void, reject }
|
|
316
|
+
})
|
|
317
|
+
const open = this.openGroup
|
|
318
|
+
if (open?.window === window) {
|
|
319
|
+
this.queued += 1
|
|
320
|
+
open.entries.push(entry)
|
|
321
|
+
return result
|
|
322
|
+
}
|
|
323
|
+
const group = { window, entries: [entry] }
|
|
324
|
+
const run = this.enqueue(() => {
|
|
325
|
+
if (this.openGroup === group) this.openGroup = undefined
|
|
326
|
+
return runBatches(window, group.entries, this.limits)
|
|
327
|
+
})
|
|
328
|
+
// The group holds one queue slot and its other entries one each. Its last
|
|
329
|
+
// callers settle once the queue has moved on, as a single batch's would.
|
|
330
|
+
run.then(
|
|
331
|
+
(release) => {
|
|
332
|
+
this.queued -= group.entries.length - 1
|
|
333
|
+
release()
|
|
334
|
+
},
|
|
335
|
+
(error: unknown) => {
|
|
336
|
+
this.queued -= group.entries.length - 1
|
|
337
|
+
for (const queued of group.entries) queued.reject(error)
|
|
338
|
+
}
|
|
339
|
+
)
|
|
340
|
+
this.openGroup = group
|
|
341
|
+
return result
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
enqueue<T>(operation: () => Promise<T>, closingWindow = false): Promise<T> {
|
|
345
|
+
const refused = this.refusal(closingWindow)
|
|
346
|
+
if (refused) return Promise.reject(refused)
|
|
347
|
+
this.openGroup = undefined
|
|
301
348
|
this.queued += 1
|
|
302
349
|
const result = this.tail
|
|
303
350
|
.then(() => {
|
|
@@ -658,54 +705,47 @@ class Window implements NativeWindow {
|
|
|
658
705
|
}
|
|
659
706
|
|
|
660
707
|
batch<T>(update: (batch: NativeBatch) => T): Promise<T> {
|
|
661
|
-
return this.application.
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
window_id: this.id,
|
|
693
|
-
base_revision: this.revision,
|
|
694
|
-
operations: transaction.operations,
|
|
695
|
-
})
|
|
696
|
-
// Publish in the reply's own continuation, before any later native event.
|
|
697
|
-
this.assertActive()
|
|
708
|
+
return this.application.batch(this, update)
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
transaction(): Transaction {
|
|
712
|
+
this.assertActive()
|
|
713
|
+
return new Transaction(
|
|
714
|
+
this.nodes,
|
|
715
|
+
this.rootId,
|
|
716
|
+
() => this.application.ids.node.allocate(),
|
|
717
|
+
this.application.limits,
|
|
718
|
+
this.application.capabilities,
|
|
719
|
+
this.text
|
|
720
|
+
)
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
async apply(transactions: readonly Transaction[]): Promise<void> {
|
|
724
|
+
// One native batch applies every transaction atomically.
|
|
725
|
+
const reply = await this.application.host.send({
|
|
726
|
+
type: 'batch',
|
|
727
|
+
window_id: this.id,
|
|
728
|
+
base_revision: this.revision,
|
|
729
|
+
operations:
|
|
730
|
+
transactions.length === 1
|
|
731
|
+
? transactions[0]!.operations
|
|
732
|
+
: OperationList.join(transactions.map((transaction) => transaction.operations)),
|
|
733
|
+
})
|
|
734
|
+
// Publish in the reply's own continuation, before any later native event.
|
|
735
|
+
this.assertActive()
|
|
736
|
+
for (const transaction of transactions) {
|
|
737
|
+
// Each continues the one before it, which is now committed.
|
|
738
|
+
if (transaction.nodes.base !== this.nodes) transaction.rebase(this.nodes, this.text)
|
|
698
739
|
transaction.commit(this.nodes, this.text)
|
|
699
|
-
this.revision = reply.tree_revision
|
|
700
740
|
this.listeners.forget(transaction.nodes.deletes)
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
741
|
+
}
|
|
742
|
+
this.revision = reply.tree_revision
|
|
743
|
+
// The host revokes a target whose node a committed tree removed.
|
|
744
|
+
for (const registration of this.application.dropTargets.values())
|
|
745
|
+
if (registration.windowId === this.id && !this.nodes.has(registration.nodeId)) {
|
|
746
|
+
registration.revoke()
|
|
747
|
+
this.application.dropTargets.delete(registration.id)
|
|
748
|
+
}
|
|
709
749
|
}
|
|
710
750
|
|
|
711
751
|
on<T extends NativeEventType>(
|
package/src/batches.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type { Limits } from '@bun-native/protocol'
|
|
2
|
+
import { NativeCommandError } from '@bun-native/runtime'
|
|
3
|
+
import { BATCH_ENVELOPE_BYTES, type Transaction } from './tree.ts'
|
|
4
|
+
import { NativeStateError, type NativeBatch } from './types.ts'
|
|
5
|
+
|
|
6
|
+
/** One caller's batch callback, queued behind the transaction in flight. */
|
|
7
|
+
export interface BatchEntry {
|
|
8
|
+
readonly update: (batch: NativeBatch) => unknown
|
|
9
|
+
resolve(value: unknown): void
|
|
10
|
+
reject(error: unknown): void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The window a group of batches commits to. */
|
|
14
|
+
export interface BatchWindow {
|
|
15
|
+
/** A transaction over the committed graph; throws once the window is closed. */
|
|
16
|
+
transaction(): Transaction
|
|
17
|
+
/**
|
|
18
|
+
* Send `transactions`, each continuing the one before it, as one native
|
|
19
|
+
* batch, and commit them in order after its acknowledgement.
|
|
20
|
+
*/
|
|
21
|
+
apply(transactions: readonly Transaction[]): Promise<void>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A callback's outcome. Only one with operations takes part in a native
|
|
26
|
+
* batch; the others settle in their place in the order.
|
|
27
|
+
*/
|
|
28
|
+
type Member = { entry: BatchEntry; transaction?: Transaction } & (
|
|
29
|
+
| { failed: false; value: unknown }
|
|
30
|
+
| { failed: true; error: unknown }
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
function settle(member: Member): void {
|
|
34
|
+
if (member.failed) member.entry.reject(member.error)
|
|
35
|
+
else member.entry.resolve(member.value)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isThenable(value: unknown): boolean {
|
|
39
|
+
return (
|
|
40
|
+
value !== null &&
|
|
41
|
+
(typeof value === 'object' || typeof value === 'function') &&
|
|
42
|
+
typeof (value as { then?: unknown }).then === 'function'
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Run one callback in its own transaction; a failure is that caller's alone. */
|
|
47
|
+
function build(window: BatchWindow, entry: BatchEntry, base: Transaction | undefined): Member {
|
|
48
|
+
let transaction: Transaction | undefined
|
|
49
|
+
try {
|
|
50
|
+
transaction = base ? base.next() : window.transaction()
|
|
51
|
+
const value = entry.update(transaction)
|
|
52
|
+
if (isThenable(value)) {
|
|
53
|
+
// Reject the async contract while consuming a later rejection from
|
|
54
|
+
// the already-started callback. Its escaped transaction is revoked.
|
|
55
|
+
void Promise.resolve(value).catch(() => {})
|
|
56
|
+
throw new NativeStateError('invalid_state', 'Batch callbacks must be synchronous')
|
|
57
|
+
}
|
|
58
|
+
transaction.validate()
|
|
59
|
+
return transaction.operations.length
|
|
60
|
+
? { entry, transaction, failed: false, value }
|
|
61
|
+
: { entry, failed: false, value }
|
|
62
|
+
} catch (error) {
|
|
63
|
+
return { entry, failed: true, error }
|
|
64
|
+
} finally {
|
|
65
|
+
transaction?.finish()
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const dependent = () =>
|
|
70
|
+
new NativeStateError(
|
|
71
|
+
'stale_revision',
|
|
72
|
+
'Batch was built on a coalesced batch that the native host rejected'
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Send one native batch for `frame` and record its callers' outcomes in
|
|
77
|
+
* order. When the host rejects a merged batch, its transactions are retried
|
|
78
|
+
* one at a time so only the first rejected one and those built on it fail.
|
|
79
|
+
* Returns whether every transaction committed.
|
|
80
|
+
*/
|
|
81
|
+
async function flush(
|
|
82
|
+
window: BatchWindow,
|
|
83
|
+
frame: readonly Member[],
|
|
84
|
+
outcomes: Member[]
|
|
85
|
+
): Promise<boolean> {
|
|
86
|
+
const sent = frame.flatMap((member) => (member.transaction ? [member.transaction] : []))
|
|
87
|
+
let failure: { error: unknown } | undefined
|
|
88
|
+
try {
|
|
89
|
+
await window.apply(sent)
|
|
90
|
+
} catch (error) {
|
|
91
|
+
failure = { error }
|
|
92
|
+
}
|
|
93
|
+
if (!failure || sent.length === 1 || !(failure.error instanceof NativeCommandError)) {
|
|
94
|
+
// A callback without operations neither depends on nor changes the batch.
|
|
95
|
+
for (const member of frame)
|
|
96
|
+
outcomes.push(
|
|
97
|
+
failure && member.transaction
|
|
98
|
+
? { entry: member.entry, failed: true, error: failure.error }
|
|
99
|
+
: member
|
|
100
|
+
)
|
|
101
|
+
return !failure
|
|
102
|
+
}
|
|
103
|
+
let rejected = false
|
|
104
|
+
for (const member of frame) {
|
|
105
|
+
if (!member.transaction) outcomes.push(member)
|
|
106
|
+
else if (rejected) outcomes.push({ entry: member.entry, failed: true, error: dependent() })
|
|
107
|
+
else
|
|
108
|
+
try {
|
|
109
|
+
await window.apply([member.transaction])
|
|
110
|
+
outcomes.push(member)
|
|
111
|
+
} catch (error) {
|
|
112
|
+
rejected = true
|
|
113
|
+
outcomes.push({ entry: member.entry, failed: true, error })
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return !rejected
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Run batch callbacks queued behind the transaction in flight, in order, and
|
|
121
|
+
* send them as few native batches as the negotiated limits allow. Each
|
|
122
|
+
* callback runs in its own transaction over the candidate before it, exactly
|
|
123
|
+
* as if the earlier ones had been acknowledged, so a throwing or invalid
|
|
124
|
+
* callback rejects only its own promise. A transaction joins the open batch
|
|
125
|
+
* only while the batch stays within the operation limit, a conservative
|
|
126
|
+
* frame size bound and the focus and scroll requests already in it;
|
|
127
|
+
* otherwise the open batch is sent first.
|
|
128
|
+
*
|
|
129
|
+
* Callers settle in order, each after the acknowledgement of the batch that
|
|
130
|
+
* carried its operations. The returned function settles the last ones; the
|
|
131
|
+
* queue calls it once it has released the next queued operation.
|
|
132
|
+
*/
|
|
133
|
+
export async function runBatches(
|
|
134
|
+
window: BatchWindow,
|
|
135
|
+
entries: readonly BatchEntry[],
|
|
136
|
+
limits: Readonly<Limits>
|
|
137
|
+
): Promise<() => void> {
|
|
138
|
+
const outcomes: Member[] = []
|
|
139
|
+
const release = () => {
|
|
140
|
+
for (const member of outcomes.splice(0)) settle(member)
|
|
141
|
+
}
|
|
142
|
+
if (entries.length === 1) {
|
|
143
|
+
// Nothing to merge: one transaction is one batch, whatever its size.
|
|
144
|
+
const member = build(window, entries[0]!, undefined)
|
|
145
|
+
if (member.transaction) await flush(window, [member], outcomes)
|
|
146
|
+
else outcomes.push(member)
|
|
147
|
+
return release
|
|
148
|
+
}
|
|
149
|
+
const budget = limits.max_frame_bytes - BATCH_ENVELOPE_BYTES
|
|
150
|
+
let frame: Member[] = []
|
|
151
|
+
let tip: Transaction | undefined
|
|
152
|
+
let operations = 0
|
|
153
|
+
let bytes = 0
|
|
154
|
+
const send = async () => {
|
|
155
|
+
release()
|
|
156
|
+
const committed = await flush(window, frame, outcomes)
|
|
157
|
+
frame = []
|
|
158
|
+
tip = undefined
|
|
159
|
+
operations = 0
|
|
160
|
+
bytes = 0
|
|
161
|
+
return committed
|
|
162
|
+
}
|
|
163
|
+
for (const entry of entries) {
|
|
164
|
+
const member = build(window, entry, tip)
|
|
165
|
+
const transaction = member.transaction
|
|
166
|
+
if (!transaction) {
|
|
167
|
+
;(frame.length ? frame : outcomes).push(member)
|
|
168
|
+
continue
|
|
169
|
+
}
|
|
170
|
+
const count = transaction.operations.length
|
|
171
|
+
const size = transaction.bytesBound()
|
|
172
|
+
if (
|
|
173
|
+
tip &&
|
|
174
|
+
(operations + count > limits.max_operations ||
|
|
175
|
+
bytes + size > budget ||
|
|
176
|
+
!transaction.keepsPrior())
|
|
177
|
+
) {
|
|
178
|
+
if (!(await send())) {
|
|
179
|
+
// It read the candidate of a batch that did not commit.
|
|
180
|
+
outcomes.push({ entry, failed: true, error: dependent() })
|
|
181
|
+
continue
|
|
182
|
+
}
|
|
183
|
+
transaction.detach()
|
|
184
|
+
}
|
|
185
|
+
frame.push(member)
|
|
186
|
+
tip = transaction
|
|
187
|
+
operations += count
|
|
188
|
+
bytes += size
|
|
189
|
+
// A transaction that may not fit one frame alone is sent before any other
|
|
190
|
+
// callback builds on it, so its failure is its own.
|
|
191
|
+
if (bytes > budget) await send()
|
|
192
|
+
}
|
|
193
|
+
if (frame.length) await send()
|
|
194
|
+
return release
|
|
195
|
+
}
|
package/src/context-menus.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
MAX_POINTER_COORDINATE,
|
|
3
|
+
parseContextMenuItems,
|
|
4
|
+
ProtocolError,
|
|
5
|
+
type AppMenuEntry,
|
|
6
|
+
} from '@bun-native/protocol'
|
|
2
7
|
import { menuEntriesRequest, type MenuEntryOptions } from './app-menu.ts'
|
|
3
8
|
|
|
4
9
|
/** Where an OS context menu opens, in logical pixels of the window's content. */
|
|
@@ -7,8 +12,6 @@ export interface ContextMenuPosition {
|
|
|
7
12
|
readonly y: number
|
|
8
13
|
}
|
|
9
14
|
|
|
10
|
-
const MAX_COORDINATE = 1_048_576
|
|
11
|
-
|
|
12
15
|
/** Copies consumer items into the closed wire shape and validates it with the shared parser. */
|
|
13
16
|
export function contextMenuRequest(
|
|
14
17
|
items: readonly MenuEntryOptions[],
|
|
@@ -18,7 +21,12 @@ export function contextMenuRequest(
|
|
|
18
21
|
throw new TypeError('Context menu position must be an object')
|
|
19
22
|
const { x, y } = position
|
|
20
23
|
for (const value of [x, y])
|
|
21
|
-
if (
|
|
24
|
+
if (
|
|
25
|
+
typeof value !== 'number' ||
|
|
26
|
+
!Number.isFinite(value) ||
|
|
27
|
+
value < 0 ||
|
|
28
|
+
value > MAX_POINTER_COORDINATE
|
|
29
|
+
)
|
|
22
30
|
throw new TypeError('Context menu position must be finite, nonnegative logical pixels')
|
|
23
31
|
try {
|
|
24
32
|
return { items: parseContextMenuItems(menuEntriesRequest(items)), x, y }
|
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,
|
|
@@ -137,7 +156,7 @@ export function makeNode<E extends Element>(
|
|
|
137
156
|
}
|
|
138
157
|
|
|
139
158
|
export function own(nodes: ReadonlyMap<number, NodeState>, handle: NativeNode): NodeState {
|
|
140
|
-
const id =
|
|
159
|
+
const id = handle !== null && typeof handle === 'object' ? identity(handle) : undefined
|
|
141
160
|
const node = id === undefined ? undefined : nodes.get(id)
|
|
142
161
|
if (!node || node.handle !== handle)
|
|
143
162
|
throw new NativeStateError(
|
|
@@ -158,7 +177,8 @@ function textSizesOf(element: Element): readonly number[] {
|
|
|
158
177
|
: 'accessible_label' in element && element.accessible_label !== undefined
|
|
159
178
|
? [element.accessible_label]
|
|
160
179
|
: []
|
|
161
|
-
|
|
180
|
+
// Counted without encoding; texts are well formed, as parseElement checked.
|
|
181
|
+
sizes = texts.map((text) => Buffer.byteLength(text, 'utf8'))
|
|
162
182
|
textSizes.set(element, sizes)
|
|
163
183
|
}
|
|
164
184
|
return sizes
|
|
@@ -194,19 +214,28 @@ export class CommittedText {
|
|
|
194
214
|
}
|
|
195
215
|
}
|
|
196
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
|
+
|
|
197
226
|
/**
|
|
198
227
|
* The candidate graph: this transaction's own node copies and removals over
|
|
199
|
-
* the committed map
|
|
200
|
-
* 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.
|
|
201
230
|
*/
|
|
202
231
|
class Overlay implements ReadonlyMap<number, NodeState> {
|
|
203
232
|
/** Nodes this transaction created or wrote; each is its own copy. */
|
|
204
233
|
readonly writes = new Map<number, NodeState>()
|
|
205
|
-
/**
|
|
234
|
+
/** Base nodes this transaction removed. */
|
|
206
235
|
readonly deletes = new Set<number>()
|
|
207
236
|
size: number
|
|
208
237
|
|
|
209
|
-
constructor(
|
|
238
|
+
constructor(public base: ReadonlyMap<number, NodeState>) {
|
|
210
239
|
this.size = base.size
|
|
211
240
|
}
|
|
212
241
|
|
|
@@ -259,14 +288,29 @@ class Overlay implements ReadonlyMap<number, NodeState> {
|
|
|
259
288
|
}
|
|
260
289
|
}
|
|
261
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
|
+
|
|
262
299
|
/**
|
|
263
300
|
* A bounded candidate graph; native preflight remains authoritative. Nodes are
|
|
264
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.
|
|
265
307
|
*/
|
|
266
308
|
export class Transaction implements NativeBatch {
|
|
267
309
|
readonly nodes: Overlay
|
|
268
|
-
readonly operations
|
|
310
|
+
readonly operations = new OperationList()
|
|
269
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[] = []
|
|
270
314
|
/** Created and moved nodes: the only places a batch can break ownership or depth. */
|
|
271
315
|
private readonly attached: number[] = []
|
|
272
316
|
private active = true
|
|
@@ -278,11 +322,62 @@ export class Transaction implements NativeBatch {
|
|
|
278
322
|
private readonly allocateId: () => number,
|
|
279
323
|
private readonly limits: Readonly<Limits>,
|
|
280
324
|
private readonly capabilities: readonly Capability[],
|
|
281
|
-
private
|
|
325
|
+
private text: TextBase
|
|
282
326
|
) {
|
|
283
327
|
this.nodes = new Overlay(current)
|
|
284
328
|
}
|
|
285
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
|
+
|
|
286
381
|
/** The candidate's own copy of a node, made on its first write. */
|
|
287
382
|
private writable(node: NodeState): NodeState {
|
|
288
383
|
const written = this.nodes.writes.get(node.id)
|
|
@@ -311,7 +406,7 @@ export class Transaction implements NativeBatch {
|
|
|
311
406
|
const node = makeNode(this.allocateId(), normalized)
|
|
312
407
|
this.nodes.write(node)
|
|
313
408
|
this.attached.push(node.id)
|
|
314
|
-
this.operations.
|
|
409
|
+
this.operations.create(node.id, node.element)
|
|
315
410
|
return node.handle as NativeNode<E['kind']>
|
|
316
411
|
}
|
|
317
412
|
|
|
@@ -344,17 +439,12 @@ export class Transaction implements NativeBatch {
|
|
|
344
439
|
)
|
|
345
440
|
if (node.inputRevision === Number.MAX_SAFE_INTEGER)
|
|
346
441
|
throw new NativeStateError('limit_exceeded', 'Input revision exhausted')
|
|
347
|
-
this.operations.
|
|
348
|
-
type: 'set',
|
|
349
|
-
node_id: node.id,
|
|
350
|
-
element,
|
|
351
|
-
expected_input_revision: node.inputRevision,
|
|
352
|
-
})
|
|
442
|
+
this.operations.set(node.id, element, node.inputRevision)
|
|
353
443
|
node.inputRevision += 1
|
|
354
444
|
} else {
|
|
355
445
|
if (expectedInputRevision !== undefined)
|
|
356
446
|
throw new NativeStateError('invalid_state', 'Only an input accepts an edit revision')
|
|
357
|
-
this.operations.
|
|
447
|
+
this.operations.set(node.id, element)
|
|
358
448
|
}
|
|
359
449
|
node.element = element
|
|
360
450
|
// An overlay's surface decides what its subtree may hold.
|
|
@@ -363,8 +453,10 @@ export class Transaction implements NativeBatch {
|
|
|
363
453
|
|
|
364
454
|
insert(parentHandle: NativeNode<ContainerKind>, childHandle: NativeNode, index: number): void {
|
|
365
455
|
this.check()
|
|
366
|
-
|
|
367
|
-
|
|
456
|
+
this.insertNodes(own(this.nodes, parentHandle), own(this.nodes, childHandle), index)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
private insertNodes(parent: NodeState, child: NodeState, index: number): void {
|
|
368
460
|
if (!isContainer(parent.element) || child.id === this.rootId)
|
|
369
461
|
throw new NativeStateError(
|
|
370
462
|
'invalid_state',
|
|
@@ -388,17 +480,14 @@ export class Transaction implements NativeBatch {
|
|
|
388
480
|
parent.children.splice(index, 0, child.id)
|
|
389
481
|
child.parent = parent.id
|
|
390
482
|
this.attached.push(child.id)
|
|
391
|
-
this.operations.
|
|
483
|
+
this.operations.insert(parent.id, child.id, index)
|
|
392
484
|
}
|
|
393
485
|
|
|
394
486
|
append(parentHandle: NativeNode<ContainerKind>, childHandle: NativeNode): void {
|
|
395
487
|
const parent = own(this.nodes, parentHandle)
|
|
396
488
|
const child = own(this.nodes, childHandle)
|
|
397
|
-
this.
|
|
398
|
-
|
|
399
|
-
childHandle,
|
|
400
|
-
parent.children.length - (child.parent === parent.id ? 1 : 0)
|
|
401
|
-
)
|
|
489
|
+
this.check()
|
|
490
|
+
this.insertNodes(parent, child, parent.children.length - (child.parent === parent.id ? 1 : 0))
|
|
402
491
|
}
|
|
403
492
|
|
|
404
493
|
remove(handle: NativeNode): void {
|
|
@@ -408,7 +497,8 @@ export class Transaction implements NativeBatch {
|
|
|
408
497
|
throw new NativeStateError('invalid_state', 'Close the window to dispose its root')
|
|
409
498
|
if (node.parent !== undefined) {
|
|
410
499
|
const parent = this.writable(this.nodes.get(node.parent)!)
|
|
411
|
-
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)
|
|
412
502
|
}
|
|
413
503
|
const pending = [node.id]
|
|
414
504
|
for (const id of pending) {
|
|
@@ -416,7 +506,7 @@ export class Transaction implements NativeBatch {
|
|
|
416
506
|
pending.push(...child.children)
|
|
417
507
|
this.nodes.delete(id)
|
|
418
508
|
}
|
|
419
|
-
this.operations.
|
|
509
|
+
this.operations.delete(node.id)
|
|
420
510
|
}
|
|
421
511
|
|
|
422
512
|
focus(handle: NativeNode<'button' | 'input'>): void {
|
|
@@ -424,7 +514,7 @@ export class Transaction implements NativeBatch {
|
|
|
424
514
|
requireCapability(this.capabilities, 'focus_v1', 'operation:focus')
|
|
425
515
|
const node = own(this.nodes, handle)
|
|
426
516
|
this.requests.push({ type: 'focus', id: node.id })
|
|
427
|
-
this.operations.
|
|
517
|
+
this.operations.focus(node.id)
|
|
428
518
|
}
|
|
429
519
|
|
|
430
520
|
scrollTo(handle: NativeNode<ContainerKind>, offset: ScrollOffset): void {
|
|
@@ -449,7 +539,7 @@ export class Transaction implements NativeBatch {
|
|
|
449
539
|
if (axes.x === undefined && axes.y === undefined)
|
|
450
540
|
throw new TypeError('A scroll offset requires x or y')
|
|
451
541
|
this.requests.push({ type: 'scroll_to', id: node.id, ...axes })
|
|
452
|
-
this.operations.
|
|
542
|
+
this.operations.scrollTo(node.id, axes)
|
|
453
543
|
}
|
|
454
544
|
|
|
455
545
|
scrollIntoView(handle: NativeNode): void {
|
|
@@ -457,7 +547,7 @@ export class Transaction implements NativeBatch {
|
|
|
457
547
|
requireCapability(this.capabilities, 'scroll_control_v1', 'operation:scroll_into_view')
|
|
458
548
|
const node = own(this.nodes, handle)
|
|
459
549
|
this.requests.push({ type: 'scroll_into_view', id: node.id })
|
|
460
|
-
this.operations.
|
|
550
|
+
this.operations.scrollIntoView(node.id)
|
|
461
551
|
}
|
|
462
552
|
|
|
463
553
|
/** The modal overlay highest in paint order: deepest, then later in tree order. */
|
|
@@ -478,8 +568,8 @@ export class Transaction implements NativeBatch {
|
|
|
478
568
|
return active?.id
|
|
479
569
|
}
|
|
480
570
|
|
|
481
|
-
private validateRequests(): void {
|
|
482
|
-
for (const request of
|
|
571
|
+
private validateRequests(requests: readonly Request[] = this.requests): void {
|
|
572
|
+
for (const request of requests) {
|
|
483
573
|
const node = this.nodes.get(request.id)
|
|
484
574
|
if (!node)
|
|
485
575
|
throw new NativeStateError('invalid_handle', 'Request target is removed by this batch')
|
|
@@ -613,6 +703,7 @@ export class Transaction implements NativeBatch {
|
|
|
613
703
|
* totals it was opened over, in time proportional to the batch.
|
|
614
704
|
*/
|
|
615
705
|
commit(nodes: Map<number, NodeState>, text: CommittedText): void {
|
|
706
|
+
// A continuation commits only after `rebase` onto the committed graph.
|
|
616
707
|
if (nodes !== this.nodes.base || text !== this.text)
|
|
617
708
|
throw new NativeStateError(
|
|
618
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
|
}
|