@inflatable-cookie/longhorn-tauri 0.5.0 → 0.7.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflatable-cookie/longhorn-tauri",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -74,8 +74,8 @@
74
74
  "LICENSE"
75
75
  ],
76
76
  "peerDependencies": {
77
- "@inflatable-cookie/longhorn": "0.5.0",
78
- "@tauri-apps/api": "^2.10.1"
77
+ "@inflatable-cookie/longhorn": "0.7.0",
78
+ "@tauri-apps/api": "^2.13.0"
79
79
  },
80
80
  "sideEffects": false,
81
81
  "publishConfig": {
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  ConnectionFailureReporter,
3
+ EventListenOptions,
3
4
  EventTransport,
4
5
  Unlisten,
5
6
  } from "@inflatable-cookie/longhorn/core";
@@ -25,6 +26,7 @@ import { TauriBridgeAdapter } from "./bridge.ts";
25
26
  export const BRIDGE_DOMAIN_EVENT = "longhorn://bridge/domain";
26
27
  export const BRIDGE_PROGRESS_EVENT = "longhorn://bridge/progress";
27
28
  export const BRIDGE_TERMINAL_EVENT = "longhorn://bridge/terminal";
29
+ const bridgeDelivery = { scope: "renderer-local" } satisfies EventListenOptions;
28
30
 
29
31
  /**
30
32
  * Event-capable stream source. `connectBridgeStream` installs `listen` before
@@ -48,7 +50,7 @@ export class TauriBridgeStreamSource implements BridgeStreamSource {
48
50
  }
49
51
 
50
52
  listen(listener: (value: unknown) => void): Promise<Unlisten> {
51
- return this.#transport.listen(BRIDGE_DOMAIN_EVENT, listener);
53
+ return this.#transport.listen(BRIDGE_DOMAIN_EVENT, listener, bridgeDelivery);
52
54
  }
53
55
 
54
56
  loadSnapshot(): Promise<unknown> {
@@ -61,11 +63,9 @@ export interface BridgeJobListeners<Progress, Success, Detail> {
61
63
  readonly successCodec: BridgeCodec<Success>;
62
64
  readonly detailCodec: BridgeCodec<Detail>;
63
65
  readonly progress?: (event: BridgeProgressEvent<Progress>) => void;
64
- readonly terminal: (
65
- event: BridgeJobTerminalEvent<Success, Detail>,
66
- ) => void;
66
+ readonly terminal: (event: BridgeJobTerminalEvent<Success, Detail>) => void;
67
67
  /**
68
- * Reports a malformed event or a teardown failure, mirroring
68
+ * Reports a malformed event for this job or a teardown failure, mirroring
69
69
  * `CheckedSnapshotConnection`. A malformed terminal event is the only way
70
70
  * the job would otherwise never terminate for the consumer, so one parse
71
71
  * failure reports `{ phase: "event" }` and tears both listeners down.
@@ -75,7 +75,14 @@ export interface BridgeJobListeners<Progress, Success, Detail> {
75
75
 
76
76
  /**
77
77
  * Installs request/job-correlated progress and terminal listeners.
78
- * Wrong-correlation and post-terminal messages never reach consumers.
78
+ * Wrong-correlation messages are dropped before their payload is decoded, and
79
+ * post-terminal messages are dropped without decoding, so neither can fail
80
+ * this job or reach consumers.
81
+ *
82
+ * Contract 010 makes events projections, not durable delivery, and the bridge
83
+ * has no job-status query. A terminal that fires before its listener is
84
+ * registered (the jobId is only known once the job has started) is therefore
85
+ * not recovered here; see docs/knowledge/questions.md.
79
86
  */
80
87
  export async function listenTauriBridgeJob<Progress, Success, Detail>(
81
88
  requestId: BridgeRequestId,
@@ -84,70 +91,124 @@ export async function listenTauriBridgeJob<Progress, Success, Detail>(
84
91
  transport: EventTransport = new TauriEventTransport(),
85
92
  ): Promise<Unlisten> {
86
93
  const tracker = new BridgeJobTracker(requestId, jobId);
87
- let failed = false;
88
- let disposeProgress: Unlisten | undefined;
89
- let disposeTerminal: Unlisten | undefined;
94
+ // Set once the job has failed or been closed. Nothing reaches the consumer
95
+ // after that, and a registration that completes later is released at once.
96
+ let stopped = false;
97
+ let registered = false;
98
+ const unlistens: Array<Unlisten> = [];
90
99
 
91
- const close = async (): Promise<void> => {
92
- const pending = [disposeTerminal, disposeProgress].filter(
93
- (unlisten): unlisten is Unlisten => unlisten !== undefined,
100
+ // Last registered, first released.
101
+ const teardown = async (): Promise<void> => {
102
+ const pending = unlistens.splice(0).reverse();
103
+ const results = await Promise.allSettled(
104
+ pending.map((unlisten) => Promise.resolve().then(() => unlisten())),
94
105
  );
95
- disposeTerminal = undefined;
96
- disposeProgress = undefined;
97
- const results = await Promise.allSettled(pending.map((unlisten) => unlisten()));
98
106
  for (const result of results) {
99
- if (result.status === "rejected") throw result.reason;
107
+ if (result.status === "rejected") {
108
+ throw result.reason;
109
+ }
100
110
  }
101
111
  };
102
112
 
103
- const report = (error: unknown): void => {
104
- if (failed) return;
105
- failed = true;
106
- listeners.onFailure?.({ phase: "event", error });
107
- void close().catch((teardownError) => {
108
- listeners.onFailure?.({ phase: "unlisten", error: teardownError });
109
- });
113
+ const reportUnlistenFailure = (error: unknown): void => {
114
+ try {
115
+ listeners.onFailure?.({ phase: "unlisten", error });
116
+ } catch {
117
+ // A secondary reporter failure cannot replace the original error.
118
+ }
110
119
  };
111
120
 
112
- disposeProgress = await transport.listen(
113
- BRIDGE_PROGRESS_EVENT,
114
- (value) => {
115
- if (failed) return;
116
- try {
117
- const event = parseBridgeProgressEvent(
118
- value,
119
- listeners.progressCodec,
120
- );
121
- if (tracker.classifyProgress(event) === "accept") {
122
- listeners.progress?.(event);
123
- }
124
- } catch (error) {
125
- report(error);
121
+ const notify = (callback: () => void): void => {
122
+ try {
123
+ callback();
124
+ } catch (error) {
125
+ // A transport may deliver inline before returning its unlisten.
126
+ if (registered) {
127
+ throw error;
126
128
  }
127
- },
128
- );
129
+ }
130
+ };
131
+
132
+ const fail = (error: unknown): void => {
133
+ if (stopped) {
134
+ return;
135
+ }
136
+ stopped = true;
137
+ void teardown().catch(reportUnlistenFailure);
138
+ notify(() => listeners.onFailure?.({ phase: "event", error }));
139
+ };
140
+
141
+ const adopt = async (unlisten: Unlisten): Promise<void> => {
142
+ if (!stopped) {
143
+ unlistens.push(unlisten);
144
+ return;
145
+ }
146
+ try {
147
+ await unlisten();
148
+ } catch (error) {
149
+ reportUnlistenFailure(error);
150
+ }
151
+ };
152
+
153
+ // The channels are shared by every job, so admission comes before any
154
+ // job-specific decode: another job's payload cannot fail this one. A value
155
+ // from another request is refused on its request id alone. Identifiers that
156
+ // cannot be read as part of this request are malformed.
157
+ const admits = (value: unknown): boolean => {
158
+ try {
159
+ return tracker.admits(value);
160
+ } catch (error) {
161
+ fail(error);
162
+ return false;
163
+ }
164
+ };
165
+
166
+ const onProgress = (value: unknown): void => {
167
+ if (stopped || tracker.isTerminal() || !admits(value)) {
168
+ return;
169
+ }
170
+ let event: BridgeProgressEvent<Progress>;
171
+ try {
172
+ event = parseBridgeProgressEvent(value, listeners.progressCodec);
173
+ } catch (error) {
174
+ fail(error);
175
+ return;
176
+ }
177
+ if (tracker.classifyProgress(event) === "accept") {
178
+ notify(() => listeners.progress?.(event));
179
+ }
180
+ };
181
+
182
+ const onTerminal = (value: unknown): void => {
183
+ // After this job's terminal, input is not decoded at all: a late or
184
+ // malformed event cannot fail a job that has already settled.
185
+ if (stopped || tracker.isTerminal() || !admits(value)) {
186
+ return;
187
+ }
188
+ let event: BridgeJobTerminalEvent<Success, Detail>;
189
+ try {
190
+ event = parseBridgeJobTerminalEvent(value, listeners.successCodec, listeners.detailCodec);
191
+ } catch (error) {
192
+ fail(error);
193
+ return;
194
+ }
195
+ if (tracker.classifyTerminal(event) === "accept") {
196
+ notify(() => listeners.terminal(event));
197
+ }
198
+ };
199
+
200
+ const close = async (): Promise<void> => {
201
+ stopped = true;
202
+ await teardown();
203
+ };
204
+
129
205
  try {
130
- disposeTerminal = await transport.listen(
131
- BRIDGE_TERMINAL_EVENT,
132
- (value) => {
133
- if (failed) return;
134
- try {
135
- const event = parseBridgeJobTerminalEvent(
136
- value,
137
- listeners.successCodec,
138
- listeners.detailCodec,
139
- );
140
- if (tracker.classifyTerminal(event) === "accept") {
141
- listeners.terminal(event);
142
- }
143
- } catch (error) {
144
- report(error);
145
- }
146
- },
147
- );
206
+ await adopt(await transport.listen(BRIDGE_PROGRESS_EVENT, onProgress, bridgeDelivery));
207
+ await adopt(await transport.listen(BRIDGE_TERMINAL_EVENT, onTerminal, bridgeDelivery));
208
+ registered = true;
148
209
  } catch (error) {
149
- await disposeProgress();
150
- disposeProgress = undefined;
210
+ stopped = true;
211
+ await teardown().catch(reportUnlistenFailure);
151
212
  throw error;
152
213
  }
153
214
  return close;
package/src/bridge.ts CHANGED
@@ -9,15 +9,11 @@ import type {
9
9
  import type {
10
10
  BridgeOperationAdapter,
11
11
  BridgeOperationDescriptor,
12
- } from "@inflatable-cookie/longhorn/bridge";
13
- import {
14
- BridgeDomainClient,
15
- } from "@inflatable-cookie/longhorn/bridge";
16
- import type { BridgeNegotiationAdapter } from "@inflatable-cookie/longhorn/bridge";
17
- import {
18
12
  BridgeSession,
19
- BridgeSessionClient,
20
13
  } from "@inflatable-cookie/longhorn/bridge";
14
+ import { BridgeDomainClient } from "@inflatable-cookie/longhorn/bridge";
15
+ import type { BridgeNegotiationAdapter } from "@inflatable-cookie/longhorn/bridge";
16
+ import { BridgeSessionClient } from "@inflatable-cookie/longhorn/bridge";
21
17
 
22
18
  export const BRIDGE_HELLO_COMMAND = "longhorn_bridge_hello";
23
19
  export const BRIDGE_AUTHORITY_COMMAND = "longhorn_bridge_authority";
@@ -32,8 +28,7 @@ export const BRIDGE_RESYNC_COMMAND = "longhorn_bridge_resync";
32
28
  * Domain routes and payload vocabulary arrive only through operation
33
29
  * descriptors owned by adapted domain packages.
34
30
  */
35
- export class TauriBridgeAdapter
36
- implements BridgeNegotiationAdapter, BridgeOperationAdapter {
31
+ export class TauriBridgeAdapter implements BridgeNegotiationAdapter, BridgeOperationAdapter {
37
32
  readonly #transport: InvokeTransport;
38
33
 
39
34
  constructor(transport: InvokeTransport = new TauriTransport()) {
@@ -86,9 +81,7 @@ export async function connectTauriBridge(
86
81
  };
87
82
  }
88
83
 
89
- function commandFor<Request, Reply>(
90
- operation: BridgeOperationDescriptor<Request, Reply>,
91
- ): string {
84
+ function commandFor<Request, Reply>(operation: BridgeOperationDescriptor<Request, Reply>): string {
92
85
  switch (operation.kind) {
93
86
  case "query":
94
87
  return BRIDGE_QUERY_COMMAND;
@@ -1,4 +1,5 @@
1
- import { isEventTransport, type EventTransport, type InvokeTransport } from "@inflatable-cookie/longhorn/core";
1
+ import { isEventTransport } from "@inflatable-cookie/longhorn/core";
2
+ import type { EventTransport, InvokeTransport } from "@inflatable-cookie/longhorn/core";
2
3
  import type { ForkHistoryPort } from "@inflatable-cookie/longhorn/history-tree";
3
4
  export const FORK_HISTORY_SNAPSHOT_COMMAND = "longhorn_history_tree_snapshot";
4
5
  export const FORK_HISTORY_PATH_COMMAND = "longhorn_history_tree_path";
@@ -8,18 +9,25 @@ export const FORK_HISTORY_DELETE_COMMAND = "longhorn_history_tree_delete_continu
8
9
  export const FORK_HISTORY_PRUNE_COMMAND = "longhorn_history_tree_prune";
9
10
  export const FORK_HISTORY_NAVIGATE_COMMAND = "longhorn_history_tree_navigate";
10
11
  export const FORK_HISTORY_CHANGED_EVENT = "longhorn://history-tree/changed";
11
- export function createTauriForkHistoryPort(options: { readonly transport: InvokeTransport; readonly nextPlanId: () => string }): ForkHistoryPort {
12
+ export function createTauriForkHistoryPort(options: {
13
+ readonly transport: InvokeTransport;
14
+ readonly nextPlanId: () => string;
15
+ }): ForkHistoryPort {
12
16
  const events = isEventTransport(options.transport) ? options.transport : undefined;
13
17
  return {
14
18
  snapshot: () => options.transport.invoke(FORK_HISTORY_SNAPSHOT_COMMAND, {}),
15
19
  path: (command) => options.transport.invoke(FORK_HISTORY_PATH_COMMAND, { command }),
16
20
  branches: (command) => options.transport.invoke(FORK_HISTORY_BRANCHES_COMMAND, { command }),
17
- continuations: (command) => options.transport.invoke(FORK_HISTORY_CONTINUATIONS_COMMAND, { command }),
18
- deleteContinuation: (command) => options.transport.invoke(FORK_HISTORY_DELETE_COMMAND, { command }),
21
+ continuations: (command) =>
22
+ options.transport.invoke(FORK_HISTORY_CONTINUATIONS_COMMAND, { command }),
23
+ deleteContinuation: (command) =>
24
+ options.transport.invoke(FORK_HISTORY_DELETE_COMMAND, { command }),
19
25
  prune: (command) => options.transport.invoke(FORK_HISTORY_PRUNE_COMMAND, { command }),
20
26
  navigate: (command) => options.transport.invoke(FORK_HISTORY_NAVIGATE_COMMAND, { command }),
21
27
  listen: events === undefined ? undefined : (listener) => listen(events, listener),
22
28
  nextPlanId: options.nextPlanId,
23
29
  };
24
30
  }
25
- function listen(events: EventTransport, listener: (event: unknown) => void) { return events.listen(FORK_HISTORY_CHANGED_EVENT, listener); }
31
+ function listen(events: EventTransport, listener: (event: unknown) => void) {
32
+ return events.listen(FORK_HISTORY_CHANGED_EVENT, listener);
33
+ }
package/src/history.ts CHANGED
@@ -1,8 +1,5 @@
1
- import {
2
- isEventTransport,
3
- type EventTransport,
4
- type InvokeTransport,
5
- } from "@inflatable-cookie/longhorn/core";
1
+ import { isEventTransport } from "@inflatable-cookie/longhorn/core";
2
+ import type { EventTransport, InvokeTransport } from "@inflatable-cookie/longhorn/core";
6
3
 
7
4
  import type { HistoryPort } from "@inflatable-cookie/longhorn/history";
8
5
 
@@ -16,29 +13,17 @@ export interface TauriHistoryPortOptions {
16
13
  readonly nextPlanId: () => string;
17
14
  }
18
15
 
19
- export function createTauriHistoryPort(
20
- options: TauriHistoryPortOptions,
21
- ): HistoryPort {
22
- const events = isEventTransport(options.transport)
23
- ? options.transport
24
- : undefined;
16
+ export function createTauriHistoryPort(options: TauriHistoryPortOptions): HistoryPort {
17
+ const events = isEventTransport(options.transport) ? options.transport : undefined;
25
18
  return {
26
19
  snapshot: () => options.transport.invoke(HISTORY_SNAPSHOT_COMMAND, {}),
27
- page: (command) =>
28
- options.transport.invoke(HISTORY_PAGE_COMMAND, { command }),
29
- navigate: (command) =>
30
- options.transport.invoke(HISTORY_NAVIGATE_COMMAND, { command }),
31
- listen:
32
- events === undefined
33
- ? undefined
34
- : (listener) => listenChanged(events, listener),
20
+ page: (command) => options.transport.invoke(HISTORY_PAGE_COMMAND, { command }),
21
+ navigate: (command) => options.transport.invoke(HISTORY_NAVIGATE_COMMAND, { command }),
22
+ listen: events === undefined ? undefined : (listener) => listenChanged(events, listener),
35
23
  nextPlanId: options.nextPlanId,
36
24
  };
37
25
  }
38
26
 
39
- async function listenChanged(
40
- transport: EventTransport,
41
- listener: (event: unknown) => void,
42
- ) {
27
+ async function listenChanged(transport: EventTransport, listener: (event: unknown) => void) {
43
28
  return transport.listen(HISTORY_CHANGED_EVENT, listener);
44
29
  }
package/src/licence.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { isEventTransport, type EventTransport, type InvokeTransport } from "@inflatable-cookie/longhorn/core";
1
+ import { isEventTransport } from "@inflatable-cookie/longhorn/core";
2
+ import type { EventTransport, InvokeTransport } from "@inflatable-cookie/longhorn/core";
2
3
  import type {
3
4
  LicenceActivateCommand,
4
5
  LicenceDeactivateCommand,
@@ -23,16 +24,28 @@ export const LICENCE_CHANGED_EVENT = "longhorn://licence/changed";
23
24
  * Results are `unknown`, as every raw port's are — `LicenceClient` is what
24
25
  * narrows them. Commands going out are typed, because this side builds those.
25
26
  */
26
- export function createTauriLicencePort(options: { readonly transport: InvokeTransport }): LicencePort {
27
+ export function createTauriLicencePort(options: {
28
+ readonly transport: InvokeTransport;
29
+ }): LicencePort {
27
30
  const events = isEventTransport(options.transport) ? options.transport : undefined;
28
31
  return {
29
32
  snapshot: () => options.transport.invoke(LICENCE_SNAPSHOT_COMMAND, {}),
30
- activate: (command: LicenceActivateCommand) => options.transport.invoke(LICENCE_ACTIVATE_COMMAND, { command }),
31
- deactivate: (command: LicenceDeactivateCommand) => options.transport.invoke(LICENCE_DEACTIVATE_COMMAND, { command }),
32
- refresh: (command: LicenceRefreshCommand) => options.transport.invoke(LICENCE_REFRESH_COMMAND, { command }),
33
- releaseSeat: (command: LicenceReleaseSeatCommand) => options.transport.invoke(LICENCE_RELEASE_SEAT_COMMAND, { command }),
34
- renameSeat: (command: LicenceRenameSeatCommand) => options.transport.invoke(LICENCE_RENAME_SEAT_COMMAND, { command }),
33
+ activate: (command: LicenceActivateCommand) =>
34
+ options.transport.invoke(LICENCE_ACTIVATE_COMMAND, { command }),
35
+ deactivate: (command: LicenceDeactivateCommand) =>
36
+ options.transport.invoke(LICENCE_DEACTIVATE_COMMAND, { command }),
37
+ refresh: (command: LicenceRefreshCommand) =>
38
+ options.transport.invoke(LICENCE_REFRESH_COMMAND, { command }),
39
+ releaseSeat: (command: LicenceReleaseSeatCommand) =>
40
+ options.transport.invoke(LICENCE_RELEASE_SEAT_COMMAND, { command }),
41
+ renameSeat: (command: LicenceRenameSeatCommand) =>
42
+ options.transport.invoke(LICENCE_RENAME_SEAT_COMMAND, { command }),
35
43
  listen: events === undefined ? undefined : (listener) => listen(events, listener),
36
44
  };
37
45
  }
38
- function listen(events: EventTransport, listener: (event: unknown) => void): Promise<LicenceUnlisten> { return events.listen(LICENCE_CHANGED_EVENT, listener); }
46
+ function listen(
47
+ events: EventTransport,
48
+ listener: (event: unknown) => void,
49
+ ): Promise<LicenceUnlisten> {
50
+ return events.listen(LICENCE_CHANGED_EVENT, listener);
51
+ }
@@ -1,21 +1,13 @@
1
- import {
2
- isEventTransport,
3
- type EventTransport,
4
- type InvokeTransport,
5
- } from "@inflatable-cookie/longhorn/core";
1
+ import { isEventTransport } from "@inflatable-cookie/longhorn/core";
2
+ import type { EventTransport, InvokeTransport } from "@inflatable-cookie/longhorn/core";
6
3
 
7
4
  import type { NativeContentPort } from "@inflatable-cookie/longhorn/native-content";
8
5
 
9
- export const NATIVE_CONTENT_CONNECT_COMMAND =
10
- "longhorn_native_content_connect";
11
- export const NATIVE_CONTENT_SNAPSHOT_COMMAND =
12
- "longhorn_native_content_snapshot";
13
- export const NATIVE_CONTENT_UPDATE_DESIRED_COMMAND =
14
- "longhorn_native_content_update_desired";
15
- export const NATIVE_CONTENT_DECIDE_SIZE_COMMAND =
16
- "longhorn_native_content_decide_size";
17
- export const NATIVE_CONTENT_CHANGED_EVENT =
18
- "longhorn://native-content/changed";
6
+ export const NATIVE_CONTENT_CONNECT_COMMAND = "longhorn_native_content_connect";
7
+ export const NATIVE_CONTENT_SNAPSHOT_COMMAND = "longhorn_native_content_snapshot";
8
+ export const NATIVE_CONTENT_UPDATE_DESIRED_COMMAND = "longhorn_native_content_update_desired";
9
+ export const NATIVE_CONTENT_DECIDE_SIZE_COMMAND = "longhorn_native_content_decide_size";
10
+ export const NATIVE_CONTENT_CHANGED_EVENT = "longhorn://native-content/changed";
19
11
 
20
12
  export interface TauriNativeContentPortOptions {
21
13
  readonly transport: InvokeTransport;
@@ -25,31 +17,21 @@ export interface TauriNativeContentPortOptions {
25
17
  export function createTauriNativeContentPort(
26
18
  options: TauriNativeContentPortOptions,
27
19
  ): NativeContentPort {
28
- const events = isEventTransport(options.transport)
29
- ? options.transport
30
- : undefined;
20
+ const events = isEventTransport(options.transport) ? options.transport : undefined;
31
21
  return {
32
- connect: (request) =>
33
- options.transport.invoke(NATIVE_CONTENT_CONNECT_COMMAND, { request }),
34
- snapshot: (request) =>
35
- options.transport.invoke(NATIVE_CONTENT_SNAPSHOT_COMMAND, { request }),
22
+ connect: (request) => options.transport.invoke(NATIVE_CONTENT_CONNECT_COMMAND, { request }),
23
+ snapshot: (request) => options.transport.invoke(NATIVE_CONTENT_SNAPSHOT_COMMAND, { request }),
36
24
  updateDesired: (request) =>
37
25
  options.transport.invoke(NATIVE_CONTENT_UPDATE_DESIRED_COMMAND, {
38
26
  request,
39
27
  }),
40
28
  decideContentSize: (request) =>
41
29
  options.transport.invoke(NATIVE_CONTENT_DECIDE_SIZE_COMMAND, { request }),
42
- listen:
43
- events === undefined
44
- ? undefined
45
- : (listener) => listenChanged(events, listener),
30
+ listen: events === undefined ? undefined : (listener) => listenChanged(events, listener),
46
31
  nextRequestId: options.nextRequestId,
47
32
  };
48
33
  }
49
34
 
50
- function listenChanged(
51
- transport: EventTransport,
52
- listener: (event: unknown) => void,
53
- ) {
35
+ function listenChanged(transport: EventTransport, listener: (event: unknown) => void) {
54
36
  return transport.listen(NATIVE_CONTENT_CHANGED_EVENT, listener);
55
37
  }
@@ -1,4 +1,5 @@
1
- import { isEventTransport, type EventTransport, type InvokeTransport } from "@inflatable-cookie/longhorn/core";
1
+ import { isEventTransport } from "@inflatable-cookie/longhorn/core";
2
+ import type { EventTransport, InvokeTransport } from "@inflatable-cookie/longhorn/core";
2
3
 
3
4
  import type { NotificationPort } from "@inflatable-cookie/longhorn/notifications";
4
5
 
@@ -11,7 +12,9 @@ export interface TauriNotificationPortOptions {
11
12
  readonly nextRequestId: () => string;
12
13
  }
13
14
 
14
- export function createTauriNotificationPort(options: TauriNotificationPortOptions): NotificationPort {
15
+ export function createTauriNotificationPort(
16
+ options: TauriNotificationPortOptions,
17
+ ): NotificationPort {
15
18
  const events = isEventTransport(options.transport) ? options.transport : undefined;
16
19
  return {
17
20
  snapshot: (query) => options.transport.invoke(NOTIFICATION_SNAPSHOT_COMMAND, { query }),
package/src/operation.ts CHANGED
@@ -1,8 +1,5 @@
1
- import {
2
- isEventTransport,
3
- type EventTransport,
4
- type InvokeTransport,
5
- } from "@inflatable-cookie/longhorn/core";
1
+ import { isEventTransport } from "@inflatable-cookie/longhorn/core";
2
+ import type { EventTransport, InvokeTransport } from "@inflatable-cookie/longhorn/core";
6
3
 
7
4
  import type { OperationPort } from "@inflatable-cookie/longhorn/operation";
8
5
 
@@ -16,30 +13,17 @@ export interface TauriOperationPortOptions {
16
13
  readonly nextRequestId: () => string;
17
14
  }
18
15
 
19
- export function createTauriOperationPort(
20
- options: TauriOperationPortOptions,
21
- ): OperationPort {
22
- const events = isEventTransport(options.transport)
23
- ? options.transport
24
- : undefined;
16
+ export function createTauriOperationPort(options: TauriOperationPortOptions): OperationPort {
17
+ const events = isEventTransport(options.transport) ? options.transport : undefined;
25
18
  return {
26
- snapshot: (query) =>
27
- options.transport.invoke(OPERATION_SNAPSHOT_COMMAND, { query }),
28
- mutate: (command) =>
29
- options.transport.invoke(OPERATION_MUTATE_COMMAND, { command }),
30
- cancel: (command) =>
31
- options.transport.invoke(OPERATION_CANCEL_COMMAND, { command }),
32
- listen:
33
- events === undefined
34
- ? undefined
35
- : (listener) => listenChanged(events, listener),
19
+ snapshot: (query) => options.transport.invoke(OPERATION_SNAPSHOT_COMMAND, { query }),
20
+ mutate: (command) => options.transport.invoke(OPERATION_MUTATE_COMMAND, { command }),
21
+ cancel: (command) => options.transport.invoke(OPERATION_CANCEL_COMMAND, { command }),
22
+ listen: events === undefined ? undefined : (listener) => listenChanged(events, listener),
36
23
  nextRequestId: options.nextRequestId,
37
24
  };
38
25
  }
39
26
 
40
- function listenChanged(
41
- transport: EventTransport,
42
- listener: (event: unknown) => void,
43
- ) {
27
+ function listenChanged(transport: EventTransport, listener: (event: unknown) => void) {
44
28
  return transport.listen(OPERATION_CHANGED_EVENT, listener);
45
29
  }
@@ -1,17 +1,36 @@
1
1
  import { listen } from "@tauri-apps/api/event";
2
+ import type { Options } from "@tauri-apps/api/event";
3
+ import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
2
4
 
3
- import type { EventTransport, Unlisten } from "@inflatable-cookie/longhorn/core";
5
+ import type {
6
+ EventListenOptions,
7
+ EventTransport,
8
+ Unlisten,
9
+ } from "@inflatable-cookie/longhorn/core";
4
10
 
5
11
  import { TauriTransport } from "./index.ts";
6
12
 
7
13
  /// Optional raw Tauri invoke/listen edge.
8
- export class TauriEventTransport
9
- extends TauriTransport
10
- implements EventTransport {
11
- listen(
14
+ export class TauriEventTransport extends TauriTransport implements EventTransport {
15
+ async listen(
12
16
  event: string,
13
17
  listener: (payload: unknown) => void,
18
+ options?: EventListenOptions,
14
19
  ): Promise<Unlisten> {
15
- return listen(event, ({ payload }) => listener(payload));
20
+ if (options?.scope === "renderer-local") {
21
+ const webview = window;
22
+ const receive = (message: Event): void => {
23
+ if ("detail" in message) {
24
+ listener(message.detail);
25
+ }
26
+ };
27
+ webview.addEventListener(event, receive);
28
+ return () => webview.removeEventListener(event, receive);
29
+ }
30
+ const tauriOptions: Options | undefined =
31
+ options?.scope === "current-webview"
32
+ ? { target: { kind: "WebviewWindow", label: getCurrentWebviewWindow().label } }
33
+ : undefined;
34
+ return listen(event, ({ payload }) => listener(payload), tauriOptions);
16
35
  }
17
36
  }
@@ -1,12 +1,74 @@
1
- import { invoke } from "@tauri-apps/api/core";
1
+ import { Channel, invoke } from "@tauri-apps/api/core";
2
2
 
3
3
  import type { InvokeTransport } from "@inflatable-cookie/longhorn/core";
4
4
 
5
+ /** An operational error returned by a Tauri host adapter. */
6
+ export class TauriHostError extends Error {
7
+ constructor(
8
+ readonly code: string,
9
+ message: string,
10
+ readonly retryable: boolean,
11
+ ) {
12
+ super(message);
13
+ this.name = "TauriHostError";
14
+ }
15
+ }
16
+
5
17
  export class TauriTransport implements InvokeTransport {
6
- invoke(
18
+ async invoke(command: string, arguments_: Record<string, unknown>): Promise<unknown> {
19
+ try {
20
+ return await invoke(command, arguments_);
21
+ } catch (error) {
22
+ if (isHostErrorEnvelope(error)) {
23
+ throw new TauriHostError(error.code, error.message, error.retryable);
24
+ }
25
+ throw error;
26
+ }
27
+ }
28
+
29
+ /** Invokes a command with a channel for its live progress. */
30
+ async invokeWithProgress(
7
31
  command: string,
8
32
  arguments_: Record<string, unknown>,
33
+ listener: (event: unknown) => void,
9
34
  ): Promise<unknown> {
10
- return invoke(command, arguments_);
35
+ const onProgress = new Channel(listener);
36
+ try {
37
+ return await this.invoke(command, { ...arguments_, onProgress });
38
+ } finally {
39
+ // A rejected invoke may never create a Rust channel to close the callback.
40
+ const internals: unknown = "__TAURI_INTERNALS__" in window && window.__TAURI_INTERNALS__;
41
+ if (hasCallbackCleanup(internals)) {
42
+ internals.unregisterCallback(onProgress.id);
43
+ }
44
+ }
11
45
  }
12
46
  }
47
+
48
+ function isHostErrorEnvelope(
49
+ value: unknown,
50
+ ): value is Pick<TauriHostError, "code" | "message" | "retryable"> {
51
+ return (
52
+ typeof value === "object" &&
53
+ value !== null &&
54
+ Object.keys(value).length === 3 &&
55
+ Object.hasOwn(value, "code") &&
56
+ Object.hasOwn(value, "message") &&
57
+ Object.hasOwn(value, "retryable") &&
58
+ "code" in value &&
59
+ typeof value.code === "string" &&
60
+ "message" in value &&
61
+ typeof value.message === "string" &&
62
+ "retryable" in value &&
63
+ typeof value.retryable === "boolean"
64
+ );
65
+ }
66
+
67
+ function hasCallbackCleanup(value: unknown): value is { unregisterCallback(id: number): void } {
68
+ return (
69
+ typeof value === "object" &&
70
+ value !== null &&
71
+ "unregisterCallback" in value &&
72
+ typeof value.unregisterCallback === "function"
73
+ );
74
+ }
package/src/update.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { isEventTransport, type EventTransport, type InvokeTransport } from "@inflatable-cookie/longhorn/core";
1
+ import { isEventTransport } from "@inflatable-cookie/longhorn/core";
2
+ import type { EventTransport, InvokeTransport } from "@inflatable-cookie/longhorn/core";
2
3
  import type {
3
4
  UpdateApplyCommand,
4
5
  UpdateCancelCommand,
@@ -9,6 +10,7 @@ import type {
9
10
  UpdateSelectChannelCommand,
10
11
  UpdateUnlisten,
11
12
  } from "@inflatable-cookie/longhorn/update";
13
+ import type { TauriTransport } from "./transport/index.ts";
12
14
  export const UPDATE_SNAPSHOT_COMMAND = "longhorn_update_snapshot";
13
15
  export const UPDATE_CHECK_COMMAND = "longhorn_update_check";
14
16
  export const UPDATE_SELECT_CHANNEL_COMMAND = "longhorn_update_select_channel";
@@ -17,37 +19,55 @@ export const UPDATE_PREPARE_COMMAND = "longhorn_update_prepare";
17
19
  export const UPDATE_APPLY_COMMAND = "longhorn_update_apply";
18
20
  export const UPDATE_CANCEL_COMMAND = "longhorn_update_cancel";
19
21
  export const UPDATE_CHANGED_EVENT = "longhorn://update/changed";
20
- export const UPDATE_PROGRESS_EVENT = "longhorn://update/progress";
21
22
  /**
22
23
  * The update seam.
23
24
  *
24
- * Seven commands and two events, mirroring the staged `UpdateController`.
25
- * Each is its own invoke because each is its own capability on the host side:
26
- * reading state, reaching the network, changing what this install follows,
27
- * staging verified bytes, replacing the running application, and discarding
28
- * what was staged are six different grants.
29
- *
30
- * `UPDATE_PROGRESS_EVENT` carries a progress value rather than an invalidation
31
- * hint, because the host does not hold the authority lock across the transfer
32
- * and a snapshot read mid-transfer cannot show the bytes.
25
+ * Committed changes produce an invalidation hint. Live byte progress arrives
26
+ * on a channel owned by the prepare invocation.
33
27
  *
34
28
  * Results are `unknown`, as every other raw port's are. What comes back over a
35
29
  * transport is untrusted until a validator says otherwise; `UpdateClient` is
36
30
  * what narrows them. Commands going *out* are typed, because those this side
37
31
  * constructs.
38
32
  */
39
- export function createTauriUpdatePort(options: { readonly transport: InvokeTransport }): UpdatePort {
33
+ export function createTauriUpdatePort(options: {
34
+ readonly transport: InvokeTransport;
35
+ }): UpdatePort {
40
36
  const events = isEventTransport(options.transport) ? options.transport : undefined;
37
+ const transport = options.transport;
38
+ const progress = hasProgress(transport) ? transport : undefined;
41
39
  return {
42
- snapshot: () => options.transport.invoke(UPDATE_SNAPSHOT_COMMAND, {}),
43
- check: (command: UpdateCheckCommand) => options.transport.invoke(UPDATE_CHECK_COMMAND, { command }),
44
- selectChannel: (command: UpdateSelectChannelCommand) => options.transport.invoke(UPDATE_SELECT_CHANNEL_COMMAND, { command }),
45
- defer: (command: UpdateDeferCommand) => options.transport.invoke(UPDATE_DEFER_COMMAND, { command }),
46
- prepare: (command: UpdatePrepareCommand) => options.transport.invoke(UPDATE_PREPARE_COMMAND, { command }),
47
- apply: (command: UpdateApplyCommand) => options.transport.invoke(UPDATE_APPLY_COMMAND, { command }),
48
- cancel: (command: UpdateCancelCommand) => options.transport.invoke(UPDATE_CANCEL_COMMAND, { command }),
49
- listen: events === undefined ? undefined : (listener) => listen(events, UPDATE_CHANGED_EVENT, listener),
50
- listenProgress: events === undefined ? undefined : (listener) => listen(events, UPDATE_PROGRESS_EVENT, listener),
40
+ snapshot: () => transport.invoke(UPDATE_SNAPSHOT_COMMAND, {}),
41
+ check: (command: UpdateCheckCommand) =>
42
+ options.transport.invoke(UPDATE_CHECK_COMMAND, { command }),
43
+ selectChannel: (command: UpdateSelectChannelCommand) =>
44
+ options.transport.invoke(UPDATE_SELECT_CHANNEL_COMMAND, { command }),
45
+ defer: (command: UpdateDeferCommand) =>
46
+ options.transport.invoke(UPDATE_DEFER_COMMAND, { command }),
47
+ prepare: (command: UpdatePrepareCommand) =>
48
+ options.transport.invoke(UPDATE_PREPARE_COMMAND, { command }),
49
+ prepareWithProgress:
50
+ progress === undefined
51
+ ? undefined
52
+ : (command, listener) =>
53
+ progress.invokeWithProgress(UPDATE_PREPARE_COMMAND, { command }, listener),
54
+ apply: (command: UpdateApplyCommand) =>
55
+ options.transport.invoke(UPDATE_APPLY_COMMAND, { command }),
56
+ cancel: (command: UpdateCancelCommand) =>
57
+ options.transport.invoke(UPDATE_CANCEL_COMMAND, { command }),
58
+ listen:
59
+ events === undefined
60
+ ? undefined
61
+ : (listener) => listen(events, UPDATE_CHANGED_EVENT, listener),
51
62
  };
52
63
  }
53
- function listen(events: EventTransport, event: string, listener: (event: unknown) => void): Promise<UpdateUnlisten> { return events.listen(event, listener); }
64
+ function hasProgress(transport: InvokeTransport): transport is TauriTransport {
65
+ return "invokeWithProgress" in transport && typeof transport.invokeWithProgress === "function";
66
+ }
67
+ function listen(
68
+ events: EventTransport,
69
+ event: string,
70
+ listener: (event: unknown) => void,
71
+ ): Promise<UpdateUnlisten> {
72
+ return events.listen(event, listener);
73
+ }