@inflatable-cookie/longhorn 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.
Files changed (167) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -7
  3. package/src/agent-control/generated/protocol.ts +228 -0
  4. package/src/agent-control/index.ts +1 -5
  5. package/src/agent-control/inject.ts +4 -1
  6. package/src/agent-control/selection.ts +42 -25
  7. package/src/agent-control/shim.ts +428 -198
  8. package/src/bridge/direct.ts +1 -1
  9. package/src/bridge/generated/fields.ts +4 -3
  10. package/src/bridge/generated/protocol.ts +62 -7
  11. package/src/bridge/generated/variant-fields.ts +1 -1
  12. package/src/bridge/job.ts +49 -12
  13. package/src/bridge/loopback.ts +7 -25
  14. package/src/bridge/operation.ts +13 -43
  15. package/src/bridge/router.ts +10 -23
  16. package/src/bridge/runtime/connection.ts +78 -109
  17. package/src/bridge/runtime/policy.ts +8 -28
  18. package/src/bridge/runtime/retry.ts +6 -18
  19. package/src/bridge/session.ts +4 -14
  20. package/src/bridge/stream/connection.ts +24 -55
  21. package/src/bridge/stream/source.ts +2 -6
  22. package/src/bridge/stream/tracker.ts +12 -12
  23. package/src/bridge/supervision.ts +104 -97
  24. package/src/bridge/validation/base.ts +21 -63
  25. package/src/bridge/validation/jobs.ts +11 -25
  26. package/src/bridge/validation/negotiation.ts +28 -43
  27. package/src/bridge/validation/operations.ts +29 -65
  28. package/src/bridge/validation/streams.ts +3 -10
  29. package/src/commands/client.ts +37 -28
  30. package/src/commands/controller.ts +119 -96
  31. package/src/commands/generated/fields.ts +7 -6
  32. package/src/commands/generated/protocol.ts +28 -1
  33. package/src/commands/generated/variant-fields.ts +1 -1
  34. package/src/commands/keyboard.ts +94 -100
  35. package/src/commands/physical-code.ts +11 -0
  36. package/src/commands/ports.ts +8 -13
  37. package/src/commands/projectors.ts +100 -75
  38. package/src/commands/validation.ts +434 -241
  39. package/src/config/client.ts +9 -22
  40. package/src/config/generated/base.ts +22 -6
  41. package/src/config/generated/fields.ts +4 -3
  42. package/src/config/generated/label-template.ts +1 -1
  43. package/src/config/generated/labels.ts +1 -1
  44. package/src/config/generated/protocol.ts +1 -1
  45. package/src/config/generated/restore.ts +35 -12
  46. package/src/config/generated/variant-fields.ts +1 -1
  47. package/src/config/names.ts +6 -12
  48. package/src/config/validation/commands.ts +11 -25
  49. package/src/config/validation/outcomes.ts +163 -70
  50. package/src/config/validation/primitives.ts +35 -80
  51. package/src/config/validation/projection.ts +100 -128
  52. package/src/core/connection.ts +187 -83
  53. package/src/core/controller-lifecycle.ts +144 -0
  54. package/src/core/generated/identifiers.ts +4 -0
  55. package/src/core/identifiers.ts +40 -0
  56. package/src/core/index.ts +10 -1
  57. package/src/core/json-value.ts +58 -0
  58. package/src/core/listeners.ts +35 -0
  59. package/src/core/record.ts +80 -0
  60. package/src/core/transport.ts +9 -11
  61. package/src/core/wire-copy.ts +7 -0
  62. package/src/history/client.ts +11 -13
  63. package/src/history/controller.ts +106 -108
  64. package/src/history/direct.ts +1 -3
  65. package/src/history/generated/fields.ts +4 -3
  66. package/src/history/generated/protocol.ts +8 -5
  67. package/src/history/generated/variant-fields.ts +1 -1
  68. package/src/history/ports.ts +7 -4
  69. package/src/history/serialized.ts +9 -13
  70. package/src/history/validation.ts +69 -75
  71. package/src/history-tree/client.ts +89 -11
  72. package/src/history-tree/controller.ts +380 -96
  73. package/src/history-tree/direct.ts +3 -1
  74. package/src/history-tree/generated/fields.ts +5 -3
  75. package/src/history-tree/generated/protocol.ts +43 -7
  76. package/src/history-tree/generated/variant-fields.ts +7 -1
  77. package/src/history-tree/ports.ts +12 -5
  78. package/src/history-tree/serialized.ts +39 -11
  79. package/src/history-tree/validation.ts +286 -78
  80. package/src/layout/generated/protocol.ts +4 -3
  81. package/src/layout/generated/variant-fields.ts +47 -0
  82. package/src/layout/index.ts +1 -1
  83. package/src/layout/ratio.ts +10 -9
  84. package/src/layout/validation.ts +98 -32
  85. package/src/layout/visibility.ts +1 -2
  86. package/src/licence/client.ts +48 -7
  87. package/src/licence/controller.ts +127 -70
  88. package/src/licence/direct.ts +3 -1
  89. package/src/licence/generated/fields.ts +4 -3
  90. package/src/licence/generated/key-conformance.json +28 -0
  91. package/src/licence/generated/protocol.ts +9 -5
  92. package/src/licence/generated/variant-fields.ts +1 -1
  93. package/src/licence/key.ts +42 -18
  94. package/src/licence/ports.ts +10 -3
  95. package/src/licence/serialized.ts +32 -9
  96. package/src/licence/validation.ts +134 -43
  97. package/src/native-content/client.ts +207 -234
  98. package/src/native-content/generated/fields.ts +6 -6
  99. package/src/native-content/generated/protocol.ts +16 -5
  100. package/src/native-content/generated/variant-fields.ts +1 -1
  101. package/src/native-content/ports.ts +11 -16
  102. package/src/native-content/serialized.ts +12 -19
  103. package/src/native-content/validation/base.ts +57 -80
  104. package/src/native-content/validation/messages.ts +24 -35
  105. package/src/native-content/validation/state.ts +31 -33
  106. package/src/notifications/client.ts +85 -51
  107. package/src/notifications/controller.ts +400 -145
  108. package/src/notifications/direct.ts +3 -1
  109. package/src/notifications/generated/fields.ts +4 -3
  110. package/src/notifications/generated/labels.ts +1 -1
  111. package/src/notifications/generated/protocol.ts +5 -1
  112. package/src/notifications/generated/variant-fields.ts +1 -1
  113. package/src/notifications/ports.ts +9 -4
  114. package/src/notifications/serialized.ts +11 -9
  115. package/src/notifications/tone.ts +1 -1
  116. package/src/notifications/validation.ts +165 -71
  117. package/src/operation/bridge.ts +187 -60
  118. package/src/operation/client.ts +48 -62
  119. package/src/operation/controller.ts +67 -97
  120. package/src/operation/direct.ts +1 -3
  121. package/src/operation/generated/fields.ts +4 -3
  122. package/src/operation/generated/labels.ts +1 -1
  123. package/src/operation/generated/protocol.ts +5 -1
  124. package/src/operation/generated/variant-fields.ts +1 -1
  125. package/src/operation/ports.ts +12 -10
  126. package/src/operation/serialized.ts +8 -9
  127. package/src/operation/validation.ts +189 -104
  128. package/src/settings/client.ts +11 -44
  129. package/src/settings/connection.ts +35 -53
  130. package/src/settings/generated/fields.ts +5 -4
  131. package/src/settings/generated/protocol.ts +14 -5
  132. package/src/settings/generated/variant-fields.ts +1 -1
  133. package/src/settings/names.ts +2 -4
  134. package/src/settings/registry.ts +23 -25
  135. package/src/settings/validation/authority.ts +21 -19
  136. package/src/settings/validation/command.ts +22 -41
  137. package/src/settings/validation/error.ts +2 -8
  138. package/src/settings/validation/primitives.ts +46 -97
  139. package/src/settings/validation/projection.ts +8 -24
  140. package/src/settings/validation/registry.ts +33 -79
  141. package/src/surface-transfer/client.ts +6 -18
  142. package/src/surface-transfer/generated/fields.ts +3 -3
  143. package/src/surface-transfer/generated/protocol.ts +3 -3
  144. package/src/surface-transfer/generated/variant-fields.ts +1 -1
  145. package/src/surface-transfer/validation.ts +91 -86
  146. package/src/surfaces/client.ts +33 -55
  147. package/src/surfaces/generated/fields.ts +3 -3
  148. package/src/surfaces/generated/protocol.ts +1 -1
  149. package/src/surfaces/generated/variant-fields.ts +1 -1
  150. package/src/surfaces/validation.ts +227 -99
  151. package/src/transfer/client.ts +26 -60
  152. package/src/transfer/connection.ts +29 -51
  153. package/src/transfer/generated/fields.ts +7 -6
  154. package/src/transfer/generated/protocol.ts +13 -5
  155. package/src/transfer/generated/variant-fields.ts +1 -1
  156. package/src/transfer/index.ts +8 -1
  157. package/src/transfer/payload.ts +20 -35
  158. package/src/transfer/validation.ts +169 -119
  159. package/src/update/client.ts +86 -9
  160. package/src/update/controller.ts +264 -116
  161. package/src/update/direct.ts +3 -1
  162. package/src/update/generated/fields.ts +5 -4
  163. package/src/update/generated/protocol.ts +25 -9
  164. package/src/update/generated/variant-fields.ts +4 -2
  165. package/src/update/ports.ts +14 -6
  166. package/src/update/serialized.ts +36 -10
  167. package/src/update/validation.ts +118 -48
@@ -0,0 +1,80 @@
1
+ // Record members stay unknown until a domain validator checks them.
2
+ export function isRecord(value: unknown): value is Record<string, unknown> {
3
+ return typeof value === "object" && value !== null && !Array.isArray(value);
4
+ }
5
+
6
+ export function readWireRecord(
7
+ value: unknown,
8
+ invalidObject: () => never,
9
+ ): Record<string, unknown> {
10
+ return isRecord(value) ? value : invalidObject();
11
+ }
12
+
13
+ // A required wire field must be an own, enumerable property to appear in JSON.
14
+ export function checkWireFields(
15
+ value: Record<string, unknown>,
16
+ required: ReadonlyArray<string> | undefined,
17
+ invalidField: (code: "unknown_field" | "missing_field", key: string) => never,
18
+ optional: ReadonlyArray<string> = [],
19
+ ): void {
20
+ if (required === undefined) {
21
+ return;
22
+ }
23
+ const present = new Set(Object.keys(value));
24
+ const allowed = new Set([...required, ...optional]);
25
+ for (const key of present) {
26
+ if (!allowed.has(key)) {
27
+ invalidField("unknown_field", key);
28
+ }
29
+ }
30
+ for (const key of required) {
31
+ if (!present.has(key)) {
32
+ invalidField("missing_field", key);
33
+ }
34
+ }
35
+ }
36
+
37
+ export function wireVariantFields(
38
+ fields: Readonly<Record<string, Readonly<Record<string, ReadonlyArray<string>>>>>,
39
+ discriminants: Readonly<Record<string, string>>,
40
+ type: string,
41
+ value: Record<string, unknown>,
42
+ invalidVariant: (discriminant: unknown) => never,
43
+ ): ReadonlyArray<string> {
44
+ const tag = discriminants[type];
45
+ const discriminant = tag === undefined ? undefined : value[tag];
46
+ const variants = fields[type];
47
+ if (
48
+ typeof discriminant !== "string" ||
49
+ variants === undefined ||
50
+ !Object.hasOwn(variants, discriminant)
51
+ ) {
52
+ return invalidVariant(discriminant);
53
+ }
54
+ return variants[discriminant] ?? invalidVariant(discriminant);
55
+ }
56
+
57
+ // JSON writes a hole as `null` and `forEach` skips it. `in` is the test
58
+ // `forEach` skips by, also for a state proxy, and it reads no item.
59
+ export function hasHole(values: ReadonlyArray<unknown>): boolean {
60
+ for (let index = 0; index < values.length; index += 1) {
61
+ if (!(index in values)) {
62
+ return true;
63
+ }
64
+ }
65
+ return false;
66
+ }
67
+
68
+ export function unsignedInteger(value: unknown): value is number {
69
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
70
+ }
71
+
72
+ export function unsigned32(value: unknown): value is number {
73
+ const maximum = 0xff_ff_ff_ff;
74
+ return unsignedInteger(value) && value <= maximum;
75
+ }
76
+
77
+ export function signed32(value: unknown): value is number {
78
+ const limit = 0x80_00_00_00;
79
+ return typeof value === "number" && Number.isInteger(value) && value >= -limit && value < limit;
80
+ }
@@ -1,24 +1,22 @@
1
1
  export type Unlisten = () => void | Promise<void>;
2
2
 
3
3
  export interface InvokeTransport {
4
- invoke(
5
- command: string,
6
- arguments_: Record<string, unknown>,
7
- ): Promise<unknown>;
4
+ invoke(command: string, arguments_: Record<string, unknown>): Promise<unknown>;
5
+ }
6
+
7
+ export interface EventListenOptions {
8
+ /** Selects a host-targeted event or an event delivered inside this renderer. */
9
+ readonly scope: "current-webview" | "renderer-local";
8
10
  }
9
11
 
10
12
  export interface EventTransport extends InvokeTransport {
11
13
  listen(
12
14
  event: string,
13
15
  listener: (payload: unknown) => void,
16
+ options?: EventListenOptions,
14
17
  ): Promise<Unlisten>;
15
18
  }
16
19
 
17
- export function isEventTransport(
18
- transport: InvokeTransport,
19
- ): transport is EventTransport {
20
- return (
21
- "listen" in transport &&
22
- typeof (transport as { listen?: unknown }).listen === "function"
23
- );
20
+ export function isEventTransport(transport: InvokeTransport): transport is EventTransport {
21
+ return "listen" in transport && typeof (transport as { listen?: unknown }).listen === "function";
24
22
  }
@@ -0,0 +1,7 @@
1
+ // Serialized ports simulate the JSON transport, including its handling of
2
+ // undefined fields, so a structured clone would exercise a different wire.
3
+ // A client that must hold what it dispatched takes the same copy.
4
+ export function wireCopy<Value>(value: Value): Value {
5
+ // oxlint-disable-next-line unicorn/prefer-structured-clone,typescript/no-unsafe-type-assertion -- Typed port inputs retain their wire type after the JSON round trip; checked clients validate the raw output.
6
+ return JSON.parse(JSON.stringify(value)) as Value;
7
+ }
@@ -1,3 +1,5 @@
1
+ import { listenChecked } from "../core/connection.ts";
2
+ import type { ConnectionFailureReporter } from "../core/connection.ts";
1
3
  import {
2
4
  assertValidHistoryChangedEvent,
3
5
  assertValidHistoryNavigationCommand,
@@ -14,11 +16,7 @@ import type {
14
16
  HistoryPageSnapshot,
15
17
  HistorySnapshot,
16
18
  } from "./generated/protocol.ts";
17
- import type {
18
- CheckedHistoryPort,
19
- HistoryPort,
20
- HistoryUnlisten,
21
- } from "./ports.ts";
19
+ import type { CheckedHistoryPort, HistoryPort, HistoryUnlisten } from "./ports.ts";
22
20
 
23
21
  export class HistoryClient implements CheckedHistoryPort {
24
22
  readonly #port: HistoryPort;
@@ -44,22 +42,22 @@ export class HistoryClient implements CheckedHistoryPort {
44
42
  return value;
45
43
  }
46
44
 
47
- async navigate(
48
- command: HistoryNavigationCommand,
49
- ): Promise<HistoryNavigationResult> {
45
+ async navigate(command: HistoryNavigationCommand): Promise<HistoryNavigationResult> {
50
46
  assertValidHistoryNavigationCommand(command);
51
47
  const value = await this.#port.navigate(command);
52
48
  assertValidHistoryNavigationResult(value);
53
49
  return value;
54
50
  }
55
51
 
56
- async listen(
52
+ listen(
57
53
  listener: (event: HistoryChangedEvent) => void,
54
+ onFailure?: ConnectionFailureReporter,
58
55
  ): Promise<HistoryUnlisten> {
59
- if (this.#port.listen === undefined) return () => {};
60
- return this.#port.listen((value) => {
61
- assertValidHistoryChangedEvent(value);
62
- listener(value);
56
+ return listenChecked({
57
+ listen: (receive) => this.#port.listen?.(receive) ?? (() => {}),
58
+ validate: assertValidHistoryChangedEvent,
59
+ listener,
60
+ onFailure,
63
61
  });
64
62
  }
65
63
  }
@@ -1,17 +1,18 @@
1
+ import { ControllerLifecycle } from "../core/controller-lifecycle.ts";
1
2
  import { HistoryClient } from "./client.ts";
2
- import {
3
- HISTORY_PROTOCOL_VERSION,
4
- type HistoryChangedEvent,
5
- type HistoryEntryId,
6
- type HistoryEntryRecord,
7
- type HistoryNavigationRejectionProjection,
8
- type HistoryNavigationResult,
9
- type HistoryNavigationTargetProjection,
10
- type HistoryPageSnapshot,
11
- type HistorySnapshot,
3
+ import { HISTORY_PROTOCOL_VERSION } from "./generated/protocol.ts";
4
+ import type {
5
+ HistoryChangedEvent,
6
+ HistoryEntryId,
7
+ HistoryEntryRecord,
8
+ HistoryNavigationRejectionProjection,
9
+ HistoryNavigationResult,
10
+ HistoryNavigationTargetProjection,
11
+ HistoryPageSnapshot,
12
+ HistorySnapshot,
12
13
  } from "./generated/protocol.ts";
13
14
  import { HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE } from "./generated/protocol.ts";
14
- import type { HistoryPort, HistoryUnlisten } from "./ports.ts";
15
+ import type { HistoryPort } from "./ports.ts";
15
16
 
16
17
  export type HistoryControllerStatus =
17
18
  | { readonly kind: "idle" }
@@ -24,6 +25,12 @@ export interface HistoryControllerOptions {
24
25
  readonly pageSize?: number;
25
26
  }
26
27
 
28
+ interface HistoryProjection {
29
+ readonly snapshot: HistorySnapshot;
30
+ readonly page: HistoryPageSnapshot;
31
+ readonly pageNumber: number;
32
+ }
33
+
27
34
  export class HistoryController {
28
35
  readonly #client: HistoryClient;
29
36
  readonly #observers = new Set<() => void>();
@@ -35,11 +42,9 @@ export class HistoryController {
35
42
  #page = 1;
36
43
  #pageSize: number;
37
44
  #navigationPending = false;
38
- #started = false;
39
- #lifecycleRevision = 0;
45
+ readonly #lifecycle = new ControllerLifecycle();
40
46
  #loadRevision = 0;
41
47
  #navigationRevision = 0;
42
- #unlisten: HistoryUnlisten[] = [];
43
48
 
44
49
  constructor(options: HistoryControllerOptions) {
45
50
  this.#client = new HistoryClient(options.port);
@@ -47,7 +52,7 @@ export class HistoryController {
47
52
  }
48
53
 
49
54
  get status(): HistoryControllerStatus {
50
- return this.#status;
55
+ return this.#lifecycle.eventFailure ?? this.#status;
51
56
  }
52
57
 
53
58
  get snapshot(): HistorySnapshot | undefined {
@@ -78,10 +83,12 @@ export class HistoryController {
78
83
  return this.#pageSnapshot?.totalEntries ?? 0;
79
84
  }
80
85
 
81
- get entries(): readonly HistoryEntryRecord[] {
86
+ get entries(): ReadonlyArray<HistoryEntryRecord> {
82
87
  const entries = this.#pageSnapshot?.entries ?? [];
83
88
  const query = this.#filter.trim().toLocaleLowerCase();
84
- if (query.length === 0) return entries;
89
+ if (query.length === 0) {
90
+ return entries;
91
+ }
85
92
  return entries.filter(
86
93
  (entry) =>
87
94
  entry.label.toLocaleLowerCase().includes(query) ||
@@ -106,54 +113,61 @@ export class HistoryController {
106
113
  return () => this.#observers.delete(observer);
107
114
  }
108
115
 
109
- async start(): Promise<void> {
110
- if (this.#started) return;
111
- this.#started = true;
112
- const lifecycleRevision = ++this.#lifecycleRevision;
113
- this.#setStatus({ kind: "loading" });
114
- try {
115
- const unlisten = await this.#client.listen((event) => {
116
- this.#handleChanged(event);
117
- });
118
- if (!this.#isCurrentLifecycle(lifecycleRevision)) {
119
- await unlisten();
120
- return;
121
- }
122
- this.#unlisten.push(unlisten);
123
- await this.refresh();
124
- } catch (error) {
125
- if (this.#isCurrentLifecycle(lifecycleRevision)) {
126
- this.#setStatus({ kind: "failed", error });
127
- }
128
- }
116
+ start(): Promise<void> {
117
+ return this.#lifecycle.start({
118
+ loading: () => this.#setStatus({ kind: "loading" }),
119
+ connect: async ({ isCurrent, own, reportFailure }) => {
120
+ const unlisten = await this.#client.listen((event) => {
121
+ if (isCurrent()) {
122
+ this.#handleChanged(event);
123
+ }
124
+ }, reportFailure);
125
+ await own(unlisten);
126
+ if (isCurrent()) {
127
+ await this.refresh();
128
+ }
129
+ },
130
+ failed: (error) => this.#setStatus({ kind: "failed", error }),
131
+ });
129
132
  }
130
133
 
131
- async stop(): Promise<void> {
132
- this.#started = false;
133
- this.#lifecycleRevision += 1;
134
- this.#loadRevision += 1;
135
- this.#navigationRevision += 1;
136
- const unlisten = this.#unlisten.splice(0);
137
- await Promise.allSettled(unlisten.map((dispose) => dispose()));
138
- this.#snapshot = undefined;
139
- this.#pageSnapshot = undefined;
140
- this.#rejection = undefined;
141
- this.#filter = "";
142
- this.#page = 1;
143
- this.#navigationPending = false;
144
- this.#setStatus({ kind: "idle" });
134
+ /** Replaces the current subscription, including after a reported failure. */
135
+ restart(): Promise<void> {
136
+ return this.#lifecycle.restart(
137
+ this.status,
138
+ () => this.stop(),
139
+ () => this.start(),
140
+ );
141
+ }
142
+
143
+ stop(): Promise<void> {
144
+ return this.#lifecycle.stop(() => {
145
+ this.#loadRevision += 1;
146
+ this.#navigationRevision += 1;
147
+ this.#snapshot = undefined;
148
+ this.#pageSnapshot = undefined;
149
+ this.#rejection = undefined;
150
+ this.#filter = "";
151
+ this.#page = 1;
152
+ this.#navigationPending = false;
153
+ this.#setStatus({ kind: "idle" });
154
+ });
145
155
  }
146
156
 
147
157
  async refresh(): Promise<void> {
148
- if (!this.#started) return;
158
+ if (!this.#lifecycle.active) {
159
+ return;
160
+ }
149
161
  const revision = ++this.#loadRevision;
150
162
  try {
151
163
  const state = await this.#readAuthoritativeState();
152
- if (!this.#started || revision !== this.#loadRevision) return;
153
- this.#install(state.snapshot, state.page);
164
+ if (!this.#lifecycle.active || revision !== this.#loadRevision) {
165
+ return;
166
+ }
167
+ this.#install(state);
154
168
  this.#setStatus({ kind: "ready" });
155
169
  } catch (error) {
156
- if (this.#started && revision === this.#loadRevision) {
170
+ if (this.#lifecycle.active && revision === this.#loadRevision) {
157
171
  this.#setStatus({ kind: "failed", error });
158
172
  }
159
173
  }
@@ -199,11 +213,9 @@ export class HistoryController {
199
213
  return this.#navigate({ kind: "checkoutRoot" });
200
214
  }
201
215
 
202
- async #navigate(
203
- target: HistoryNavigationTargetProjection,
204
- ): Promise<HistoryNavigationResult> {
216
+ async #navigate(target: HistoryNavigationTargetProjection): Promise<HistoryNavigationResult> {
205
217
  const snapshot = this.#snapshot;
206
- if (!this.#started || snapshot === undefined) {
218
+ if (!this.#lifecycle.active || snapshot === undefined) {
207
219
  throw new HistoryControllerUnavailableError();
208
220
  }
209
221
  const revision = ++this.#navigationRevision;
@@ -219,27 +231,20 @@ export class HistoryController {
219
231
  expectedRevision: snapshot.summary.revision,
220
232
  target,
221
233
  });
222
- if (
223
- !this.#started ||
224
- revision !== this.#navigationRevision
225
- ) {
234
+ if (!this.#lifecycle.active || revision !== this.#navigationRevision) {
226
235
  throw new HistoryLateResultError();
227
236
  }
228
- this.#rejection =
229
- result.status === "rejected" ? result.rejection : undefined;
230
- const page = await this.#readPageForSnapshot(result.snapshot);
231
- if (
232
- !this.#started ||
233
- revision !== this.#navigationRevision
234
- ) {
237
+ this.#rejection = result.status === "rejected" ? result.rejection : undefined;
238
+ const projection = await this.#readPageForSnapshot(result.snapshot);
239
+ if (!this.#lifecycle.active || revision !== this.#navigationRevision) {
235
240
  throw new HistoryLateResultError();
236
241
  }
237
- this.#install(result.snapshot, page);
242
+ this.#install(projection);
238
243
  this.#setStatus({ kind: "ready" });
239
244
  return result;
240
245
  } catch (error) {
241
246
  if (
242
- this.#started &&
247
+ this.#lifecycle.active &&
243
248
  revision === this.#navigationRevision &&
244
249
  !(error instanceof HistoryLateResultError)
245
250
  ) {
@@ -254,38 +259,36 @@ export class HistoryController {
254
259
  }
255
260
  }
256
261
 
257
- async #readAuthoritativeState(): Promise<{
258
- snapshot: HistorySnapshot;
259
- page: HistoryPageSnapshot;
260
- }> {
262
+ async #readAuthoritativeState(): Promise<HistoryProjection> {
261
263
  for (let attempt = 0; attempt < 2; attempt += 1) {
262
264
  const snapshot = await this.#client.snapshot();
263
- const page = await this.#readPageForSnapshot(snapshot);
264
- if (sameAuthority(snapshot, page)) return { snapshot, page };
265
+ const projection = await this.#readPageForSnapshot(snapshot);
266
+ if (sameAuthority(snapshot, projection.page)) {
267
+ return projection;
268
+ }
265
269
  }
266
270
  throw new HistoryProjectionGapError();
267
271
  }
268
272
 
269
- async #readPageForSnapshot(
270
- snapshot: HistorySnapshot,
271
- ): Promise<HistoryPageSnapshot> {
272
- const maximumPage = Math.max(
273
- 1,
274
- Math.ceil(snapshot.summary.retainedEntryCount / this.#pageSize),
275
- );
276
- this.#page = Math.min(this.#page, maximumPage);
277
- return this.#client.page({
273
+ async #readPageForSnapshot(snapshot: HistorySnapshot): Promise<HistoryProjection> {
274
+ const pageSize = this.#pageSize;
275
+ const maximumPage = Math.max(1, Math.ceil(snapshot.summary.retainedEntryCount / pageSize));
276
+ const pageNumber = Math.min(this.#page, maximumPage);
277
+ const page = await this.#client.page({
278
278
  protocolVersion: HISTORY_PROTOCOL_VERSION,
279
279
  authorityEpoch: snapshot.authorityEpoch,
280
280
  historyId: snapshot.summary.historyId,
281
281
  expectedRevision: snapshot.summary.revision,
282
- offset: (this.#page - 1) * this.#pageSize,
283
- limit: this.#pageSize,
282
+ offset: (pageNumber - 1) * pageSize,
283
+ limit: pageSize,
284
284
  });
285
+ return { snapshot, page, pageNumber };
285
286
  }
286
287
 
287
- #install(snapshot: HistorySnapshot, page: HistoryPageSnapshot): void {
288
- if (!sameAuthority(snapshot, page)) throw new HistoryProjectionGapError();
288
+ #install({ snapshot, page, pageNumber }: HistoryProjection): void {
289
+ if (!sameAuthority(snapshot, page)) {
290
+ throw new HistoryProjectionGapError();
291
+ }
289
292
  const current = this.#snapshot;
290
293
  if (
291
294
  current !== undefined &&
@@ -297,11 +300,13 @@ export class HistoryController {
297
300
  }
298
301
  this.#snapshot = snapshot;
299
302
  this.#pageSnapshot = page;
300
- this.#notify();
303
+ this.#page = pageNumber;
301
304
  }
302
305
 
303
306
  #handleChanged(event: HistoryChangedEvent): void {
304
- if (!this.#started) return;
307
+ if (!this.#lifecycle.active) {
308
+ return;
309
+ }
305
310
  const snapshot = this.#snapshot;
306
311
  if (snapshot === undefined) {
307
312
  void this.refresh();
@@ -314,21 +319,21 @@ export class HistoryController {
314
319
  void this.refresh();
315
320
  return;
316
321
  }
317
- if (event.committedRevision <= snapshot.summary.revision) return;
322
+ if (event.committedRevision <= snapshot.summary.revision) {
323
+ return;
324
+ }
318
325
  void this.refresh();
319
326
  }
320
327
 
321
- #isCurrentLifecycle(revision: number): boolean {
322
- return this.#started && revision === this.#lifecycleRevision;
323
- }
324
-
325
328
  #setStatus(status: HistoryControllerStatus): void {
326
329
  this.#status = status;
327
330
  this.#notify();
328
331
  }
329
332
 
330
333
  #notify(): void {
331
- for (const observer of this.#observers) observer();
334
+ for (const observer of this.#observers) {
335
+ observer();
336
+ }
332
337
  }
333
338
  }
334
339
 
@@ -353,10 +358,7 @@ export class HistoryLateResultError extends Error {
353
358
  }
354
359
  }
355
360
 
356
- function sameAuthority(
357
- snapshot: HistorySnapshot,
358
- page: HistoryPageSnapshot,
359
- ): boolean {
361
+ function sameAuthority(snapshot: HistorySnapshot, page: HistoryPageSnapshot): boolean {
360
362
  return (
361
363
  page.authorityEpoch === snapshot.authorityEpoch &&
362
364
  page.historyId === snapshot.summary.historyId &&
@@ -372,11 +374,7 @@ function checkedPage(value: number): number {
372
374
  }
373
375
 
374
376
  function checkedPageSize(value: number): number {
375
- if (
376
- !Number.isSafeInteger(value) ||
377
- value < 1 ||
378
- value > HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE
379
- ) {
377
+ if (!Number.isSafeInteger(value) || value < 1 || value > HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE) {
380
378
  throw new RangeError(
381
379
  `history page size must be between 1 and ${HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE}`,
382
380
  );
@@ -8,9 +8,7 @@ export interface DirectHistoryHandlers {
8
8
  readonly nextPlanId: HistoryPort["nextPlanId"];
9
9
  }
10
10
 
11
- export function createDirectHistoryPort(
12
- handlers: DirectHistoryHandlers,
13
- ): HistoryPort {
11
+ export function createDirectHistoryPort(handlers: DirectHistoryHandlers): HistoryPort {
14
12
  return {
15
13
  snapshot: handlers.snapshot,
16
14
  page: handlers.page,
@@ -1,9 +1,9 @@
1
- // @generated by `effigy generate:history`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per protocol type, from the Rust structs.
3
3
  // A `record()` given one of these rejects unknown and missing keys —
4
4
  // contract 010's Boundary Validation Target.
5
5
 
6
- export const HISTORY_FIELDS: Record<string, readonly string[]> = {
6
+ export const HISTORY_FIELDS = {
7
7
  "HistoryBaselineProjection": ["prunedEntryCount", "prunedEncodedWeight", "lastPrunedEntryId", "lastPrunedSequence"],
8
8
  "HistorySummaryProjection": ["historyId", "revision", "mode", "undoDepth", "redoDepth", "currentEntryId", "nextUndoLabel", "nextRedoLabel", "retainedEntryCount", "retainedEncodedWeight", "retainedBaseline"],
9
9
  "HistorySnapshot": ["protocolVersion", "authorityEpoch", "summary"],
@@ -15,4 +15,5 @@ export const HISTORY_FIELDS: Record<string, readonly string[]> = {
15
15
  "HistoryNavigationReceiptProjection": ["historyId", "planId", "previousRevision", "committedRevision", "direction", "movedEntryIds", "sourcePosition", "authoritativePosition"],
16
16
  "HistoryNavigationRejectionProjection": ["code", "detail", "refreshRequired"],
17
17
  "HistoryChangedEvent": ["protocolVersion", "authorityEpoch", "historyId", "previousRevision", "committedRevision", "kind"],
18
- };
18
+ "HistoryHostError": ["code", "message", "retryable"],
19
+ } as const;
@@ -1,8 +1,7 @@
1
- // @generated by `effigy generate:history`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Rust serde types are the wire authority. Product payloads are intentionally absent.
3
3
 
4
4
  export const HISTORY_PROTOCOL_VERSION = 1 as const;
5
- export const HISTORY_MAXIMUM_OPAQUE_ID_BYTES = 128 as const;
6
5
  export const HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE = 4096 as const;
7
6
  export const HISTORY_MODES = ["linear"] as const;
8
7
  export const HISTORY_ENTRY_POSITIONS = ["past","current","future"] as const;
@@ -151,7 +150,7 @@ encodedWeight: number,
151
150
  */
152
151
  position: HistoryProjectionPosition, };
153
152
 
154
- export type HistoryPageFloorProjection = { "kind": "origin" } | { "kind": "baseline",
153
+ export type HistoryPageFloorProjection = { "kind": "origin", } | { "kind": "baseline",
155
154
  /**
156
155
  * How many entries were pruned before the oldest retained one.
157
156
  */
@@ -232,11 +231,11 @@ retainedBaseline: HistoryBaselineProjection,
232
231
  */
233
232
  floor: HistoryPageFloorProjection, };
234
233
 
235
- export type HistoryNavigationTargetProjection = { "kind": "undo" } | { "kind": "redo" } | { "kind": "checkout",
234
+ export type HistoryNavigationTargetProjection = { "kind": "undo", } | { "kind": "redo", } | { "kind": "checkout",
236
235
  /**
237
236
  * Stable entry identity, never a presentation index.
238
237
  */
239
- entryId: HistoryEntryId, } | { "kind": "checkoutRoot" };
238
+ entryId: HistoryEntryId, } | { "kind": "checkoutRoot", };
240
239
 
241
240
  export type HistoryNavigationCommand = {
242
241
  /**
@@ -387,3 +386,7 @@ committedRevision: HistoryRevision,
387
386
  * Coarse transition category.
388
387
  */
389
388
  kind: HistoryChangedKind, };
389
+
390
+ export type HistoryHostErrorCode = "authorityUnavailable" | "stateUnavailable" | "eventPublication";
391
+
392
+ export type HistoryHostError = { "code": HistoryHostErrorCode, "message": string, "retryable": boolean, };
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:history`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per tagged-union variant, from the Rust enums.
3
3
  // Keyed by type, then by discriminant. The discriminant itself is in
4
4
  // each list, because `exact()` compares every key.
@@ -1,3 +1,5 @@
1
+ import type { ConnectionFailureReporter } from "../core/connection.ts";
2
+ import type { Unlisten } from "../core/transport.ts";
1
3
  import type {
2
4
  HistoryChangedEvent,
3
5
  HistoryNavigationCommand,
@@ -8,15 +10,15 @@ import type {
8
10
  HistorySnapshot,
9
11
  } from "./generated/protocol.ts";
10
12
 
11
- export type HistoryUnlisten = () => void | Promise<void>;
13
+ export type HistoryUnlisten = Unlisten;
12
14
 
13
15
  export interface HistoryPort {
14
16
  snapshot(): Promise<unknown>;
15
17
  page(command: HistoryPageCommand): Promise<unknown>;
16
18
  navigate(command: HistoryNavigationCommand): Promise<unknown>;
17
- listen?(
18
- listener: (event: unknown) => void,
19
- ): HistoryUnlisten | Promise<HistoryUnlisten>;
19
+ listen?:
20
+ | ((listener: (event: unknown) => void) => HistoryUnlisten | Promise<HistoryUnlisten>)
21
+ | undefined;
20
22
  nextPlanId(): HistoryPlanId;
21
23
  }
22
24
 
@@ -26,6 +28,7 @@ export interface CheckedHistoryPort {
26
28
  navigate(command: HistoryNavigationCommand): Promise<HistoryNavigationResult>;
27
29
  listen?(
28
30
  listener: (event: HistoryChangedEvent) => void,
31
+ onFailure?: ConnectionFailureReporter,
29
32
  ): Promise<HistoryUnlisten>;
30
33
  nextPlanId(): HistoryPlanId;
31
34
  }