@bun-native/core 0.33.0 → 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/src/drops.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { DropRejection } from '@bun-native/protocol'
2
+ import { Registration } from './registration.ts'
2
3
  import type { Listener, NativeNode } from './types.ts'
3
4
 
4
5
  /**
@@ -40,43 +41,26 @@ export interface NativeDropTarget {
40
41
  }
41
42
 
42
43
  /** Internal registry entry. Consumers only receive its frozen handle. */
43
- export class DropTargetRegistration {
44
+ export class DropTargetRegistration extends Registration<DropTargetEvent> {
44
45
  readonly handle: NativeDropTarget
45
- readonly controller = new AbortController()
46
- listener: Listener<DropTargetEvent> | undefined
47
46
  /** The host retains paths only for the target's latest accepted drop. */
48
47
  latestDrop: number | undefined
49
- private released = false
50
- private releasing: Promise<void> | undefined
51
48
 
52
49
  constructor(
53
- readonly id: number,
54
- readonly windowId: number,
50
+ id: number,
51
+ windowId: number,
55
52
  readonly nodeId: number,
56
53
  node: NativeNode,
57
54
  listener: Listener<DropTargetEvent>,
58
55
  release: (registration: DropTargetRegistration) => Promise<void>
59
56
  ) {
60
- this.listener = listener
57
+ super(id, windowId, listener)
61
58
  this.handle = publicHandle(this, node, release)
62
59
  }
63
60
 
64
- release(release: (registration: DropTargetRegistration) => Promise<void>): Promise<void> {
65
- this.releasing ??= this.released ? Promise.resolve() : release(this)
66
- return this.releasing
67
- }
68
-
69
- get live(): boolean {
70
- return !this.released
71
- }
72
-
73
- /** Synchronous local revocation; native removal happens separately. */
74
- revoke(): void {
75
- if (this.released) return
76
- this.released = true
77
- this.listener = undefined
61
+ override revoke(): void {
62
+ super.revoke()
78
63
  this.latestDrop = undefined
79
- this.controller.abort()
80
64
  }
81
65
  }
82
66
 
package/src/events.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { WindowEvent } from '@bun-native/runtime'
1
+ import type { WindowEvent } from '@bun-native/protocol'
2
2
  import {
3
3
  NativeStateError,
4
4
  type Listener,
@@ -211,14 +211,27 @@ export class EventBindings {
211
211
  return unsubscribe(subscription)
212
212
  }
213
213
 
214
- retain(ids: ReadonlyMap<number, unknown>): void {
215
- for (const [id, bindings] of this.nodes) {
216
- if (ids.has(id)) continue
214
+ /**
215
+ * Revoke the bindings and queued plans of committed nodes a batch removed.
216
+ * Only committed nodes hold bindings, and a commit records every committed
217
+ * node it removes, so this costs O(removed) instead of O(listeners).
218
+ */
219
+ forget(removed: ReadonlySet<number>): void {
220
+ if (!removed.size) return
221
+ let changed = false
222
+ for (const id of removed) {
223
+ const bindings = this.nodes.get(id)
224
+ if (!bindings) continue
217
225
  this.nodes.delete(id)
218
226
  for (const binding of bindings.values()) revoke(binding)
227
+ changed = true
219
228
  }
220
- for (const plan of this.plans) if (!ids.has(plan.targetId)) plan.cancellation.abort()
221
- this.changed()
229
+ for (const plan of this.plans)
230
+ if (removed.has(plan.targetId)) {
231
+ plan.cancellation.abort()
232
+ changed = true
233
+ }
234
+ if (changed) this.changed()
222
235
  }
223
236
 
224
237
  clear(): void {
package/src/files.ts CHANGED
@@ -9,6 +9,7 @@ import {
9
9
  type FileResult,
10
10
  type WriteMode,
11
11
  } from '@bun-native/protocol'
12
+ import { Registration } from './registration.ts'
12
13
 
13
14
  export interface FileGrantOptions {
14
15
  /** At most the root's launch access. Defaults to `read`. */
@@ -200,19 +201,18 @@ const fileCalls = {
200
201
  }
201
202
 
202
203
  /** Internal registry entry. Consumers only receive its frozen handle. */
203
- export class FileGrantRegistration {
204
+ export class FileGrantRegistration extends Registration {
204
205
  readonly handle: NativeFileGrant
205
- private revoked = false
206
- private closing: Promise<void> | undefined
207
206
 
208
207
  constructor(
209
- readonly id: number,
210
- readonly windowId: number,
208
+ id: number,
209
+ windowId: number,
211
210
  root: string,
212
211
  access: FileAccess,
213
212
  call: (registration: FileGrantRegistration, request: FileCall) => Promise<unknown>,
214
213
  close: (registration: FileGrantRegistration) => Promise<void>
215
214
  ) {
215
+ super(id, windowId)
216
216
  const run = <T>(request: () => FileCall) => {
217
217
  try {
218
218
  return call(this, request()) as Promise<T>
@@ -236,18 +236,7 @@ export class FileGrantRegistration {
236
236
  run<number>(() => fileCalls.write(target, bytes, value)),
237
237
  createDirectory: (target: string) => run<void>(() => fileCalls.createDirectory(target)),
238
238
  remove: (target: string) => run<void>(() => fileCalls.remove(target)),
239
- close: () => {
240
- this.closing ??= this.revoked ? Promise.resolve() : close(this)
241
- return this.closing
242
- },
239
+ close: () => this.release(close),
243
240
  })
244
241
  }
245
-
246
- get live(): boolean {
247
- return !this.revoked
248
- }
249
-
250
- revoke(): void {
251
- this.revoked = true
252
- }
253
242
  }
@@ -1,20 +1,20 @@
1
- import type {
2
- KeyModifiers,
3
- MessageDialogOptions as WireMessageDialogOptions,
1
+ import {
2
+ MAX_MESSAGE_DIALOG_BUTTONS,
3
+ MAX_MESSAGE_DIALOG_DETAIL_BYTES as MAX_DETAIL_BYTES,
4
+ MAX_MESSAGE_DIALOG_ID_BYTES as MAX_ID_BYTES,
5
+ MAX_MESSAGE_DIALOG_LABEL_BYTES as MAX_LABEL_BYTES,
6
+ MAX_MESSAGE_DIALOG_MESSAGE_BYTES as MAX_MESSAGE_BYTES,
7
+ MAX_MESSAGE_DIALOG_TITLE_BYTES as MAX_TITLE_BYTES,
8
+ ProtocolError,
9
+ type KeyModifiers,
10
+ type MessageDialogOptions as WireMessageDialogOptions,
4
11
  } from '@bun-native/protocol'
5
12
  import { NativeServiceError } from './services.ts'
6
13
  import { defaultTheme } from './theme.ts'
7
14
  import { NativeStateError, type NativeBatch, type NativeNode, type NativeWindow } from './types.ts'
8
15
 
9
- /** Buttons in one message dialog. */
10
- const MAX_MESSAGE_DIALOG_BUTTONS = 4
11
16
  /** Dialogs one window holds at a time: the shown one plus those queued behind it. */
12
17
  const MAX_MESSAGE_DIALOGS_PER_WINDOW = 8
13
- const MAX_TITLE_BYTES = 256
14
- const MAX_MESSAGE_BYTES = 2_048
15
- const MAX_DETAIL_BYTES = 4_096
16
- const MAX_LABEL_BYTES = 64
17
- const MAX_ID_BYTES = 64
18
18
 
19
19
  export interface MessageDialogButton {
20
20
  /** Returned in the result. 1-64 UTF-8 bytes without control characters, unique per dialog. */
@@ -262,6 +262,8 @@ export class MessageDialogs {
262
262
  ): void {
263
263
  const dialog = this.native.get(id)
264
264
  if (!dialog) return
265
+ if (result.kind === 'button' && !dialog.spec.buttons.some((button) => button.id === result.id))
266
+ throw new ProtocolError('Native message dialog chose a button it was not given')
265
267
  this.native.delete(id)
266
268
  dialog.nativeId = undefined
267
269
  if (dialog.state === 'closing' || dialog.state === 'settled') return
@@ -3,6 +3,7 @@ import {
3
3
  MAX_NOTIFICATION_TITLE_BYTES,
4
4
  type NotificationDismissal,
5
5
  } from '@bun-native/protocol'
6
+ import { Registration } from './registration.ts'
6
7
  import type { Listener } from './types.ts'
7
8
 
8
9
  export interface NotificationOptions {
@@ -72,52 +73,34 @@ export function notificationRequest(options: NotificationOptions): { title: stri
72
73
  return { title, body }
73
74
  }
74
75
 
75
- /** Internal registry entry. Consumers only receive its frozen handle. */
76
- export class NotificationRegistration {
76
+ /**
77
+ * Internal registry entry. Consumers only receive its frozen handle. `live`
78
+ * accepts activations; `deliverable` keeps a queued callback, including the
79
+ * final dismissal, runnable.
80
+ */
81
+ export class NotificationRegistration extends Registration<NotificationEvent> {
77
82
  readonly handle: NativeNotification
78
- readonly controller = new AbortController()
79
- listener: Listener<NotificationEvent> | undefined
80
83
  activatable = false
81
- private ended = false
82
- private withdrawing: Promise<void> | undefined
83
84
 
84
85
  constructor(
85
- readonly id: number,
86
- readonly windowId: number,
86
+ id: number,
87
+ windowId: number,
87
88
  title: string,
88
89
  body: string,
89
90
  listener: Listener<NotificationEvent>,
90
91
  withdraw: (registration: NotificationRegistration) => Promise<void>
91
92
  ) {
92
- this.listener = listener
93
+ super(id, windowId, listener)
93
94
  this.handle = publicHandle(this, title, body, withdraw)
94
95
  }
95
96
 
96
- withdraw(withdraw: (registration: NotificationRegistration) => Promise<void>): Promise<void> {
97
- this.withdrawing ??= this.ended ? Promise.resolve() : withdraw(this)
98
- return this.withdrawing
99
- }
100
-
101
- /** Accepts activations. */
102
- get live(): boolean {
103
- return !this.ended
104
- }
105
-
106
- /** A queued callback, including the final dismissal, may still run. */
107
97
  get deliverable(): boolean {
108
98
  return this.listener !== undefined
109
99
  }
110
100
 
111
101
  /** The server removed it: no activation follows, but the dismissal is delivered. */
112
102
  end(): void {
113
- this.ended = true
114
- }
115
-
116
- /** Synchronous local revocation; the server notification is closed separately. */
117
- revoke(): void {
118
- this.ended = true
119
- this.listener = undefined
120
- this.controller.abort()
103
+ this.released = true
121
104
  }
122
105
  }
123
106
 
@@ -136,6 +119,6 @@ function publicHandle(
136
119
  get active() {
137
120
  return registration.live
138
121
  },
139
- withdraw: () => registration.withdraw(withdraw),
122
+ withdraw: () => registration.release(withdraw),
140
123
  })
141
124
  }
package/src/order.ts ADDED
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Flag, by index in `desired`, a longest existing sibling subsequence; every
3
+ * other desired child needs a move. Children are distinct within each list:
4
+ * renderer nodes or native node identities.
5
+ */
6
+ export function stableChildren<T>(current: readonly T[], desired: readonly T[]): Uint8Array {
7
+ const stable = new Uint8Array(desired.length)
8
+ // A common prefix and suffix extend some longest subsequence, so only the
9
+ // middle needs the search; keyed edits usually leave both ends in place.
10
+ let start = 0
11
+ const shorter = Math.min(current.length, desired.length)
12
+ while (start < shorter && current[start] === desired[start]) stable[start++] = 1
13
+ let currentEnd = current.length
14
+ let desiredEnd = desired.length
15
+ while (
16
+ currentEnd > start &&
17
+ desiredEnd > start &&
18
+ current[currentEnd - 1] === desired[desiredEnd - 1]
19
+ ) {
20
+ currentEnd -= 1
21
+ stable[--desiredEnd] = 1
22
+ }
23
+ if (start === currentEnd || start === desiredEnd) return stable
24
+ const positions = new Map<T, number>()
25
+ for (let index = start; index < currentEnd; index += 1) positions.set(current[index]!, index)
26
+ // tails[length - 1]: desired index ending the lowest-positioned run of that length.
27
+ const size = desiredEnd - start
28
+ const tails = new Int32Array(size)
29
+ const tailPositions = new Int32Array(size)
30
+ const preceding = new Int32Array(size)
31
+ let length = 0
32
+ for (let index = start; index < desiredEnd; index += 1) {
33
+ const position = positions.get(desired[index]!)
34
+ if (position === undefined) continue
35
+ let low = 0
36
+ let high = length
37
+ while (low < high) {
38
+ const middle = (low + high) >>> 1
39
+ if (tailPositions[middle]! < position) low = middle + 1
40
+ else high = middle
41
+ }
42
+ preceding[index - start] = low > 0 ? tails[low - 1]! : -1
43
+ tails[low] = index
44
+ tailPositions[low] = position
45
+ if (low === length) length += 1
46
+ }
47
+ for (let index = length ? tails[length - 1]! : -1; index >= 0; index = preceding[index - start]!)
48
+ stable[index] = 1
49
+ return stable
50
+ }
package/src/processes.ts CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  type TerminalSize,
11
11
  } from '@bun-native/protocol'
12
12
  import type { NativeFileGrant } from './files.ts'
13
+ import { Registration } from './registration.ts'
13
14
  import type { Listener } from './types.ts'
14
15
 
15
16
  export interface ProcessOptions {
@@ -146,27 +147,23 @@ function processCall(name: string, value: unknown): ProcessCall {
146
147
  }
147
148
 
148
149
  /** Internal registry entry. Consumers only receive its frozen handle. */
149
- export class ProcessRegistration {
150
+ export class ProcessRegistration extends Registration<ProcessEvent> {
150
151
  readonly handle: NativeProcess
151
- readonly controller = new AbortController()
152
- listener: Listener<ProcessEvent> | undefined
153
152
  exit: NativeProcessExit | undefined
154
153
  /** The highest output chunk received from the host. */
155
154
  chunk = 0
156
- private revoked = false
157
155
  private exited = false
158
- private releasing: Promise<void> | undefined
159
156
 
160
157
  constructor(
161
- readonly id: number,
162
- readonly windowId: number,
158
+ id: number,
159
+ windowId: number,
163
160
  executable: string,
164
161
  terminal: boolean,
165
162
  listener: Listener<ProcessEvent>,
166
163
  call: (registration: ProcessRegistration, request: ProcessCall) => Promise<void>,
167
164
  release: (registration: ProcessRegistration) => Promise<void>
168
165
  ) {
169
- this.listener = listener
166
+ super(id, windowId, listener)
170
167
  const run = (name: string, value?: unknown) => {
171
168
  try {
172
169
  return call(this, processCall(name, value))
@@ -189,17 +186,10 @@ export class ProcessRegistration {
189
186
  closeInput: () => run('close'),
190
187
  resize: (size: TerminalSize) => run('resize', size),
191
188
  signal: (signal: ProcessSignal) => run('signal', signal),
192
- release: () => {
193
- this.releasing ??= this.revoked ? Promise.resolve() : release(this)
194
- return this.releasing
195
- },
189
+ release: () => this.release(release),
196
190
  })
197
191
  }
198
192
 
199
- get live(): boolean {
200
- return !this.revoked
201
- }
202
-
203
193
  /** True after the host reported the exit; no further output follows. */
204
194
  get ended(): boolean {
205
195
  return this.exited
@@ -208,11 +198,4 @@ export class ProcessRegistration {
208
198
  end(): void {
209
199
  this.exited = true
210
200
  }
211
-
212
- /** Synchronous local revocation; the native group is stopped separately. */
213
- revoke(): void {
214
- this.revoked = true
215
- this.listener = undefined
216
- this.controller.abort()
217
- }
218
201
  }
@@ -0,0 +1,39 @@
1
+ import type { Listener } from './types.ts'
2
+
3
+ /**
4
+ * Internal registry entry behind one window-owned service handle: its
5
+ * identity, owner window, listener, the signal that listener receives and one
6
+ * memoized native release. Consumers only receive the frozen handle.
7
+ */
8
+ export class Registration<Event = never> {
9
+ readonly controller = new AbortController()
10
+ listener: Listener<Event> | undefined
11
+ /** No further event is accepted and release is a no-op. */
12
+ protected released = false
13
+ private releasing: Promise<void> | undefined
14
+
15
+ constructor(
16
+ readonly id: number,
17
+ readonly windowId: number,
18
+ listener?: Listener<Event>
19
+ ) {
20
+ this.listener = listener
21
+ }
22
+
23
+ get live(): boolean {
24
+ return !this.released
25
+ }
26
+
27
+ /** Shared by the public handle; one native release per registration. */
28
+ release(release: (registration: this) => Promise<void>): Promise<void> {
29
+ this.releasing ??= this.released ? Promise.resolve() : release(this)
30
+ return this.releasing
31
+ }
32
+
33
+ /** Synchronous local revocation; the native resource is released separately. */
34
+ revoke(): void {
35
+ this.released = true
36
+ this.listener = undefined
37
+ this.controller.abort()
38
+ }
39
+ }
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
- readonly id: number,
86
- readonly windowId: number,
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
- this.listener = listener
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
@@ -136,14 +136,6 @@ export function makeNode<E extends Element>(
136
136
  }
137
137
  }
138
138
 
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
139
  export function own(nodes: ReadonlyMap<number, NodeState>, handle: NativeNode): NodeState {
148
140
  const id = identities.get(handle)
149
141
  const node = id === undefined ? undefined : nodes.get(id)
@@ -303,7 +295,7 @@ export class Transaction implements NativeBatch {
303
295
  private check(): void {
304
296
  if (!this.active)
305
297
  throw new NativeStateError('invalid_state', 'Batch callback has already returned')
306
- if (this.operations.length >= logicalOperationLimit(this.limits))
298
+ if (this.operations.length >= this.limits.max_operations)
307
299
  throw new NativeStateError('limit_exceeded', 'Batch operation limit exceeded')
308
300
  }
309
301
 
@@ -520,28 +512,6 @@ export class Transaction implements NativeBatch {
520
512
  }
521
513
  }
522
514
 
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
515
  /** The candidate's window text bytes, from the committed total and this batch's writes. */
546
516
  private textBytes(): number {
547
517
  const { base, writes, deletes } = this.nodes