@inflatable-cookie/longhorn 0.4.3 → 0.6.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 (126) hide show
  1. package/package.json +1 -1
  2. package/src/agent-control/inject.ts +4 -1
  3. package/src/agent-control/selection.ts +35 -20
  4. package/src/agent-control/shim.ts +302 -144
  5. package/src/bridge/direct.ts +1 -1
  6. package/src/bridge/generated/fields.ts +3 -2
  7. package/src/bridge/generated/protocol.ts +7 -3
  8. package/src/bridge/job.ts +4 -14
  9. package/src/bridge/loopback.ts +7 -25
  10. package/src/bridge/operation.ts +13 -43
  11. package/src/bridge/router.ts +10 -23
  12. package/src/bridge/runtime/connection.ts +77 -105
  13. package/src/bridge/runtime/policy.ts +8 -28
  14. package/src/bridge/runtime/retry.ts +6 -18
  15. package/src/bridge/session.ts +4 -14
  16. package/src/bridge/stream/connection.ts +16 -38
  17. package/src/bridge/stream/source.ts +2 -6
  18. package/src/bridge/stream/tracker.ts +7 -12
  19. package/src/bridge/supervision.ts +11 -26
  20. package/src/bridge/validation/base.ts +17 -39
  21. package/src/bridge/validation/jobs.ts +11 -25
  22. package/src/bridge/validation/negotiation.ts +27 -37
  23. package/src/bridge/validation/operations.ts +28 -52
  24. package/src/bridge/validation/streams.ts +3 -10
  25. package/src/commands/client.ts +21 -26
  26. package/src/commands/controller.ts +32 -47
  27. package/src/commands/generated/fields.ts +3 -2
  28. package/src/commands/generated/protocol.ts +4 -0
  29. package/src/commands/keyboard.ts +92 -89
  30. package/src/commands/ports.ts +6 -12
  31. package/src/commands/projectors.ts +50 -48
  32. package/src/commands/validation.ts +70 -97
  33. package/src/config/client.ts +6 -19
  34. package/src/config/generated/fields.ts +2 -2
  35. package/src/config/names.ts +6 -12
  36. package/src/config/validation/commands.ts +5 -17
  37. package/src/config/validation/outcomes.ts +31 -39
  38. package/src/config/validation/primitives.ts +36 -53
  39. package/src/config/validation/projection.ts +66 -94
  40. package/src/core/connection.ts +70 -61
  41. package/src/core/record.ts +4 -0
  42. package/src/core/transport.ts +4 -15
  43. package/src/core/wire-copy.ts +6 -0
  44. package/src/history/client.ts +6 -12
  45. package/src/history/controller.ts +46 -46
  46. package/src/history/direct.ts +1 -3
  47. package/src/history/generated/fields.ts +2 -2
  48. package/src/history/ports.ts +4 -6
  49. package/src/history/serialized.ts +9 -13
  50. package/src/history/validation.ts +71 -55
  51. package/src/history-tree/client.ts +87 -11
  52. package/src/history-tree/controller.ts +313 -70
  53. package/src/history-tree/direct.ts +3 -1
  54. package/src/history-tree/generated/fields.ts +2 -2
  55. package/src/history-tree/ports.ts +3 -1
  56. package/src/history-tree/serialized.ts +39 -11
  57. package/src/history-tree/validation.ts +262 -63
  58. package/src/layout/generated/protocol.ts +2 -2
  59. package/src/layout/ratio.ts +2 -8
  60. package/src/layout/validation.ts +10 -19
  61. package/src/layout/visibility.ts +1 -2
  62. package/src/licence/client.ts +44 -7
  63. package/src/licence/controller.ts +76 -40
  64. package/src/licence/direct.ts +3 -1
  65. package/src/licence/generated/fields.ts +2 -2
  66. package/src/licence/generated/key-conformance.json +28 -0
  67. package/src/licence/generated/protocol.ts +1 -1
  68. package/src/licence/key.ts +40 -12
  69. package/src/licence/ports.ts +3 -1
  70. package/src/licence/serialized.ts +32 -9
  71. package/src/licence/validation.ts +120 -36
  72. package/src/native-content/client.ts +178 -236
  73. package/src/native-content/generated/fields.ts +2 -2
  74. package/src/native-content/ports.ts +9 -15
  75. package/src/native-content/serialized.ts +12 -19
  76. package/src/native-content/validation/base.ts +57 -50
  77. package/src/native-content/validation/messages.ts +21 -35
  78. package/src/native-content/validation/state.ts +31 -33
  79. package/src/notifications/client.ts +87 -31
  80. package/src/notifications/controller.ts +215 -61
  81. package/src/notifications/direct.ts +3 -1
  82. package/src/notifications/generated/fields.ts +2 -2
  83. package/src/notifications/ports.ts +7 -3
  84. package/src/notifications/serialized.ts +11 -9
  85. package/src/notifications/validation.ts +147 -43
  86. package/src/operation/bridge.ts +117 -50
  87. package/src/operation/client.ts +38 -24
  88. package/src/operation/controller.ts +33 -42
  89. package/src/operation/direct.ts +1 -3
  90. package/src/operation/generated/fields.ts +2 -2
  91. package/src/operation/ports.ts +10 -9
  92. package/src/operation/serialized.ts +8 -9
  93. package/src/operation/validation.ts +152 -65
  94. package/src/settings/client.ts +11 -44
  95. package/src/settings/connection.ts +26 -40
  96. package/src/settings/generated/fields.ts +3 -3
  97. package/src/settings/generated/protocol.ts +9 -4
  98. package/src/settings/names.ts +2 -4
  99. package/src/settings/registry.ts +23 -25
  100. package/src/settings/validation/authority.ts +19 -17
  101. package/src/settings/validation/command.ts +17 -37
  102. package/src/settings/validation/error.ts +2 -8
  103. package/src/settings/validation/primitives.ts +26 -42
  104. package/src/settings/validation/projection.ts +8 -24
  105. package/src/settings/validation/registry.ts +25 -73
  106. package/src/surface-transfer/client.ts +6 -18
  107. package/src/surface-transfer/generated/fields.ts +2 -2
  108. package/src/surface-transfer/validation.ts +31 -49
  109. package/src/surfaces/client.ts +12 -22
  110. package/src/surfaces/generated/fields.ts +2 -2
  111. package/src/surfaces/validation.ts +40 -49
  112. package/src/transfer/client.ts +26 -60
  113. package/src/transfer/connection.ts +12 -20
  114. package/src/transfer/generated/fields.ts +5 -5
  115. package/src/transfer/generated/protocol.ts +4 -4
  116. package/src/transfer/payload.ts +13 -31
  117. package/src/transfer/validation.ts +39 -75
  118. package/src/update/client.ts +59 -9
  119. package/src/update/controller.ts +113 -48
  120. package/src/update/direct.ts +3 -1
  121. package/src/update/generated/fields.ts +3 -3
  122. package/src/update/generated/protocol.ts +7 -3
  123. package/src/update/generated/variant-fields.ts +1 -0
  124. package/src/update/ports.ts +6 -2
  125. package/src/update/serialized.ts +34 -10
  126. package/src/update/validation.ts +115 -35
@@ -5,37 +5,23 @@ export type SnapshotEventAction =
5
5
  | { readonly kind: "refresh" }
6
6
  | { readonly kind: "snapshot"; readonly value: unknown };
7
7
 
8
- export type ConnectionFailurePhase =
9
- | "registration"
10
- | "event"
11
- | "snapshot"
12
- | "unlisten";
8
+ export type ConnectionFailurePhase = "registration" | "event" | "snapshot" | "unlisten";
13
9
 
14
10
  export interface ConnectionFailure {
15
11
  readonly phase: ConnectionFailurePhase;
16
12
  readonly error: unknown;
17
13
  }
18
14
 
19
- export type ConnectionFailureReporter = (
20
- failure: ConnectionFailure,
21
- ) => void;
15
+ export type ConnectionFailureReporter = (failure: ConnectionFailure) => void;
22
16
 
23
17
  export interface CheckedSnapshotConnectionOptions<Snapshot> {
24
- readonly listen: (
25
- listener: (payload: unknown) => void,
26
- ) => Promise<Unlisten>;
18
+ readonly listen: (listener: (payload: unknown) => void) => Promise<Unlisten>;
27
19
  readonly loadSnapshot: () => Promise<unknown>;
28
20
  readonly validateSnapshot: (value: unknown) => Snapshot;
29
- readonly handleEvent: (
30
- payload: unknown,
31
- current: Snapshot | undefined,
32
- ) => SnapshotEventAction;
33
- readonly isNewer: (
34
- candidate: Snapshot,
35
- current: Snapshot | undefined,
36
- ) => boolean;
37
- readonly onSnapshot?: (snapshot: Snapshot) => void;
38
- readonly onFailure?: ConnectionFailureReporter;
21
+ readonly handleEvent: (payload: unknown, current: Snapshot | undefined) => SnapshotEventAction;
22
+ readonly isNewer: (candidate: Snapshot, current: Snapshot | undefined) => boolean;
23
+ readonly onSnapshot?: ((snapshot: Snapshot) => void) | undefined;
24
+ readonly onFailure?: ConnectionFailureReporter | undefined;
39
25
  readonly disposedBeforeReadyError?: () => Error;
40
26
  }
41
27
 
@@ -43,7 +29,7 @@ export class CheckedSnapshotConnection<Snapshot> {
43
29
  readonly ready: Promise<Snapshot | undefined>;
44
30
 
45
31
  readonly #options: CheckedSnapshotConnectionOptions<Snapshot>;
46
- readonly #failures: ConnectionFailure[] = [];
32
+ readonly #failures: Array<ConnectionFailure> = [];
47
33
  #current: Snapshot | undefined;
48
34
  #unlisten: Unlisten | undefined;
49
35
  #cleanup: Promise<void> | undefined;
@@ -61,44 +47,51 @@ export class CheckedSnapshotConnection<Snapshot> {
61
47
  return this.#current;
62
48
  }
63
49
 
64
- failures(): readonly ConnectionFailure[] {
50
+ failures(): ReadonlyArray<ConnectionFailure> {
65
51
  return [...this.#failures];
66
52
  }
67
53
 
54
+ /**
55
+ * Accepts a snapshot the caller obtained from its own request (for example,
56
+ * a mutation result), under the same `isNewer` rule the refresh pump uses.
57
+ * It is a no-op after dispose.
58
+ */
59
+ offer(candidate: Snapshot): void {
60
+ if (!this.#disposed && this.#accept(candidate)) {
61
+ this.#options.onSnapshot?.(candidate);
62
+ }
63
+ }
64
+
68
65
  async dispose(): Promise<void> {
69
66
  this.#disposed = true;
70
67
 
71
- let cleanupError: unknown;
68
+ let cleanupFailure: { error: unknown } | undefined;
72
69
  try {
73
70
  await this.#closeListener();
74
71
  } catch (error) {
75
- cleanupError = error;
72
+ cleanupFailure = { error };
76
73
  this.#recordFailure("unlisten", error);
77
74
  }
78
75
 
79
- let readyError: unknown;
76
+ let readyFailure: ConnectionFailure | undefined;
80
77
  try {
81
78
  await this.ready;
82
79
  } catch (error) {
83
- if (this.#failures.some((failure) => failure.error === error)) {
84
- readyError = error;
85
- }
80
+ readyFailure = this.#failures.find((failure) => Object.is(failure.error, error));
86
81
  }
87
82
 
88
- if (cleanupError === undefined) {
83
+ if (cleanupFailure === undefined) {
89
84
  try {
90
85
  await this.#closeListener();
91
86
  } catch (error) {
92
- cleanupError = error;
87
+ cleanupFailure = { error };
93
88
  this.#recordFailure("unlisten", error);
94
89
  }
95
90
  }
96
91
 
97
- if (cleanupError !== undefined) {
98
- throw cleanupError;
99
- }
100
- if (readyError !== undefined) {
101
- throw readyError;
92
+ const failure = cleanupFailure ?? readyFailure;
93
+ if (failure !== undefined) {
94
+ throw failure.error;
102
95
  }
103
96
  }
104
97
 
@@ -125,6 +118,11 @@ export class CheckedSnapshotConnection<Snapshot> {
125
118
  return undefined;
126
119
  }
127
120
 
121
+ if (this.#failures.length > 0) {
122
+ await this.#closeAfterFailure();
123
+ return this.#current;
124
+ }
125
+
128
126
  this.#refreshPending = true;
129
127
  try {
130
128
  await this.#synchronize();
@@ -150,6 +148,7 @@ export class CheckedSnapshotConnection<Snapshot> {
150
148
  return;
151
149
  }
152
150
 
151
+ let candidate: Snapshot;
153
152
  try {
154
153
  const action = this.#options.handleEvent(payload, this.#current);
155
154
  switch (action.kind) {
@@ -158,28 +157,29 @@ export class CheckedSnapshotConnection<Snapshot> {
158
157
  case "refresh":
159
158
  this.#refreshPending = true;
160
159
  if (this.#registered) {
161
- void this.#synchronize().catch((error) => {
160
+ void this.#synchronize().catch((error: unknown) => {
162
161
  this.#recordFailure("snapshot", error);
163
162
  void this.#closeAfterFailure();
164
163
  });
165
164
  }
166
165
  return;
167
166
  case "snapshot":
168
- this.#accept(this.#options.validateSnapshot(action.value));
169
- return;
167
+ candidate = this.#options.validateSnapshot(action.value);
168
+ if (!this.#accept(candidate)) {
169
+ return;
170
+ }
171
+ break;
170
172
  }
171
173
  } catch (error) {
172
174
  this.#recordFailure("event", error);
173
175
  void this.#closeAfterFailure();
176
+ return;
174
177
  }
178
+ this.#notifySnapshot(candidate, this.#registered && this.#pump === undefined);
175
179
  }
176
180
 
177
181
  #synchronize(): Promise<void> {
178
- if (
179
- !this.#registered ||
180
- this.#disposed ||
181
- this.#failures.length > 0
182
- ) {
182
+ if (!this.#registered || this.#disposed || this.#failures.length > 0) {
183
183
  return Promise.resolve();
184
184
  }
185
185
  this.#pump ??= Promise.resolve().then(() => this.#runPump());
@@ -188,45 +188,54 @@ export class CheckedSnapshotConnection<Snapshot> {
188
188
 
189
189
  async #runPump(): Promise<void> {
190
190
  try {
191
- while (
192
- this.#refreshPending &&
193
- !this.#disposed &&
194
- this.#failures.length === 0
195
- ) {
191
+ while (this.#refreshPending && !this.#disposed && this.#failures.length === 0) {
196
192
  this.#refreshPending = false;
197
193
  const value = await this.#options.loadSnapshot();
198
194
  if (this.#disposed || this.#failures.length > 0) {
199
195
  return;
200
196
  }
201
- this.#accept(this.#options.validateSnapshot(value));
197
+ const candidate = this.#options.validateSnapshot(value);
198
+ if (this.#accept(candidate)) {
199
+ this.#notifySnapshot(candidate, false);
200
+ }
202
201
  }
203
202
  } finally {
204
203
  this.#pump = undefined;
205
204
  }
206
205
  }
207
206
 
208
- #accept(candidate: Snapshot): void {
207
+ #accept(candidate: Snapshot): boolean {
209
208
  if (!this.#options.isNewer(candidate, this.#current)) {
210
- return;
209
+ return false;
211
210
  }
212
211
  this.#current = candidate;
213
- this.#options.onSnapshot?.(candidate);
212
+ return true;
214
213
  }
215
214
 
216
- #recordFailure(
217
- phase: ConnectionFailurePhase,
218
- error: unknown,
219
- ): void {
215
+ #notifySnapshot(candidate: Snapshot, propagate: boolean): void {
216
+ try {
217
+ this.#options.onSnapshot?.(candidate);
218
+ } catch (error) {
219
+ // A callback during registration or a query can otherwise reject that operation.
220
+ if (propagate) {
221
+ throw error;
222
+ }
223
+ }
224
+ }
225
+
226
+ #recordFailure(phase: ConnectionFailurePhase, error: unknown): void {
220
227
  if (
221
- this.#failures.some(
222
- (failure) => failure.phase === phase && failure.error === error,
223
- )
228
+ this.#failures.some((failure) => failure.phase === phase && Object.is(failure.error, error))
224
229
  ) {
225
230
  return;
226
231
  }
227
232
  const failure = { phase, error };
228
233
  this.#failures.push(failure);
229
- this.#options.onFailure?.(failure);
234
+ try {
235
+ this.#options.onFailure?.(failure);
236
+ } catch {
237
+ // A failure reporter cannot interrupt teardown or replace the recorded error.
238
+ }
230
239
  }
231
240
 
232
241
  async #closeAfterFailure(): Promise<void> {
@@ -0,0 +1,4 @@
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
+ }
@@ -1,24 +1,13 @@
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>;
8
5
  }
9
6
 
10
7
  export interface EventTransport extends InvokeTransport {
11
- listen(
12
- event: string,
13
- listener: (payload: unknown) => void,
14
- ): Promise<Unlisten>;
8
+ listen(event: string, listener: (payload: unknown) => void): Promise<Unlisten>;
15
9
  }
16
10
 
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
- );
11
+ export function isEventTransport(transport: InvokeTransport): transport is EventTransport {
12
+ return "listen" in transport && typeof (transport as { listen?: unknown }).listen === "function";
24
13
  }
@@ -0,0 +1,6 @@
1
+ // Serialized ports simulate the JSON transport, including its handling of
2
+ // undefined fields, so a structured clone would exercise a different wire.
3
+ export function wireCopy<Value>(value: Value): Value {
4
+ // 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.
5
+ return JSON.parse(JSON.stringify(value)) as Value;
6
+ }
@@ -14,11 +14,7 @@ import type {
14
14
  HistoryPageSnapshot,
15
15
  HistorySnapshot,
16
16
  } from "./generated/protocol.ts";
17
- import type {
18
- CheckedHistoryPort,
19
- HistoryPort,
20
- HistoryUnlisten,
21
- } from "./ports.ts";
17
+ import type { CheckedHistoryPort, HistoryPort, HistoryUnlisten } from "./ports.ts";
22
18
 
23
19
  export class HistoryClient implements CheckedHistoryPort {
24
20
  readonly #port: HistoryPort;
@@ -44,19 +40,17 @@ export class HistoryClient implements CheckedHistoryPort {
44
40
  return value;
45
41
  }
46
42
 
47
- async navigate(
48
- command: HistoryNavigationCommand,
49
- ): Promise<HistoryNavigationResult> {
43
+ async navigate(command: HistoryNavigationCommand): Promise<HistoryNavigationResult> {
50
44
  assertValidHistoryNavigationCommand(command);
51
45
  const value = await this.#port.navigate(command);
52
46
  assertValidHistoryNavigationResult(value);
53
47
  return value;
54
48
  }
55
49
 
56
- async listen(
57
- listener: (event: HistoryChangedEvent) => void,
58
- ): Promise<HistoryUnlisten> {
59
- if (this.#port.listen === undefined) return () => {};
50
+ async listen(listener: (event: HistoryChangedEvent) => void): Promise<HistoryUnlisten> {
51
+ if (this.#port.listen === undefined) {
52
+ return () => {};
53
+ }
60
54
  return this.#port.listen((value) => {
61
55
  assertValidHistoryChangedEvent(value);
62
56
  listener(value);
@@ -1,14 +1,14 @@
1
1
  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,
2
+ import { HISTORY_PROTOCOL_VERSION } from "./generated/protocol.ts";
3
+ import type {
4
+ HistoryChangedEvent,
5
+ HistoryEntryId,
6
+ HistoryEntryRecord,
7
+ HistoryNavigationRejectionProjection,
8
+ HistoryNavigationResult,
9
+ HistoryNavigationTargetProjection,
10
+ HistoryPageSnapshot,
11
+ HistorySnapshot,
12
12
  } from "./generated/protocol.ts";
13
13
  import { HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE } from "./generated/protocol.ts";
14
14
  import type { HistoryPort, HistoryUnlisten } from "./ports.ts";
@@ -39,7 +39,7 @@ export class HistoryController {
39
39
  #lifecycleRevision = 0;
40
40
  #loadRevision = 0;
41
41
  #navigationRevision = 0;
42
- #unlisten: HistoryUnlisten[] = [];
42
+ #unlisten: Array<HistoryUnlisten> = [];
43
43
 
44
44
  constructor(options: HistoryControllerOptions) {
45
45
  this.#client = new HistoryClient(options.port);
@@ -78,10 +78,12 @@ export class HistoryController {
78
78
  return this.#pageSnapshot?.totalEntries ?? 0;
79
79
  }
80
80
 
81
- get entries(): readonly HistoryEntryRecord[] {
81
+ get entries(): ReadonlyArray<HistoryEntryRecord> {
82
82
  const entries = this.#pageSnapshot?.entries ?? [];
83
83
  const query = this.#filter.trim().toLocaleLowerCase();
84
- if (query.length === 0) return entries;
84
+ if (query.length === 0) {
85
+ return entries;
86
+ }
85
87
  return entries.filter(
86
88
  (entry) =>
87
89
  entry.label.toLocaleLowerCase().includes(query) ||
@@ -107,7 +109,9 @@ export class HistoryController {
107
109
  }
108
110
 
109
111
  async start(): Promise<void> {
110
- if (this.#started) return;
112
+ if (this.#started) {
113
+ return;
114
+ }
111
115
  this.#started = true;
112
116
  const lifecycleRevision = ++this.#lifecycleRevision;
113
117
  this.#setStatus({ kind: "loading" });
@@ -145,11 +149,15 @@ export class HistoryController {
145
149
  }
146
150
 
147
151
  async refresh(): Promise<void> {
148
- if (!this.#started) return;
152
+ if (!this.#started) {
153
+ return;
154
+ }
149
155
  const revision = ++this.#loadRevision;
150
156
  try {
151
157
  const state = await this.#readAuthoritativeState();
152
- if (!this.#started || revision !== this.#loadRevision) return;
158
+ if (!this.#started || revision !== this.#loadRevision) {
159
+ return;
160
+ }
153
161
  this.#install(state.snapshot, state.page);
154
162
  this.#setStatus({ kind: "ready" });
155
163
  } catch (error) {
@@ -199,9 +207,7 @@ export class HistoryController {
199
207
  return this.#navigate({ kind: "checkoutRoot" });
200
208
  }
201
209
 
202
- async #navigate(
203
- target: HistoryNavigationTargetProjection,
204
- ): Promise<HistoryNavigationResult> {
210
+ async #navigate(target: HistoryNavigationTargetProjection): Promise<HistoryNavigationResult> {
205
211
  const snapshot = this.#snapshot;
206
212
  if (!this.#started || snapshot === undefined) {
207
213
  throw new HistoryControllerUnavailableError();
@@ -219,19 +225,12 @@ export class HistoryController {
219
225
  expectedRevision: snapshot.summary.revision,
220
226
  target,
221
227
  });
222
- if (
223
- !this.#started ||
224
- revision !== this.#navigationRevision
225
- ) {
228
+ if (!this.#started || revision !== this.#navigationRevision) {
226
229
  throw new HistoryLateResultError();
227
230
  }
228
- this.#rejection =
229
- result.status === "rejected" ? result.rejection : undefined;
231
+ this.#rejection = result.status === "rejected" ? result.rejection : undefined;
230
232
  const page = await this.#readPageForSnapshot(result.snapshot);
231
- if (
232
- !this.#started ||
233
- revision !== this.#navigationRevision
234
- ) {
233
+ if (!this.#started || revision !== this.#navigationRevision) {
235
234
  throw new HistoryLateResultError();
236
235
  }
237
236
  this.#install(result.snapshot, page);
@@ -261,14 +260,14 @@ export class HistoryController {
261
260
  for (let attempt = 0; attempt < 2; attempt += 1) {
262
261
  const snapshot = await this.#client.snapshot();
263
262
  const page = await this.#readPageForSnapshot(snapshot);
264
- if (sameAuthority(snapshot, page)) return { snapshot, page };
263
+ if (sameAuthority(snapshot, page)) {
264
+ return { snapshot, page };
265
+ }
265
266
  }
266
267
  throw new HistoryProjectionGapError();
267
268
  }
268
269
 
269
- async #readPageForSnapshot(
270
- snapshot: HistorySnapshot,
271
- ): Promise<HistoryPageSnapshot> {
270
+ async #readPageForSnapshot(snapshot: HistorySnapshot): Promise<HistoryPageSnapshot> {
272
271
  const maximumPage = Math.max(
273
272
  1,
274
273
  Math.ceil(snapshot.summary.retainedEntryCount / this.#pageSize),
@@ -285,7 +284,9 @@ export class HistoryController {
285
284
  }
286
285
 
287
286
  #install(snapshot: HistorySnapshot, page: HistoryPageSnapshot): void {
288
- if (!sameAuthority(snapshot, page)) throw new HistoryProjectionGapError();
287
+ if (!sameAuthority(snapshot, page)) {
288
+ throw new HistoryProjectionGapError();
289
+ }
289
290
  const current = this.#snapshot;
290
291
  if (
291
292
  current !== undefined &&
@@ -301,7 +302,9 @@ export class HistoryController {
301
302
  }
302
303
 
303
304
  #handleChanged(event: HistoryChangedEvent): void {
304
- if (!this.#started) return;
305
+ if (!this.#started) {
306
+ return;
307
+ }
305
308
  const snapshot = this.#snapshot;
306
309
  if (snapshot === undefined) {
307
310
  void this.refresh();
@@ -314,7 +317,9 @@ export class HistoryController {
314
317
  void this.refresh();
315
318
  return;
316
319
  }
317
- if (event.committedRevision <= snapshot.summary.revision) return;
320
+ if (event.committedRevision <= snapshot.summary.revision) {
321
+ return;
322
+ }
318
323
  void this.refresh();
319
324
  }
320
325
 
@@ -328,7 +333,9 @@ export class HistoryController {
328
333
  }
329
334
 
330
335
  #notify(): void {
331
- for (const observer of this.#observers) observer();
336
+ for (const observer of this.#observers) {
337
+ observer();
338
+ }
332
339
  }
333
340
  }
334
341
 
@@ -353,10 +360,7 @@ export class HistoryLateResultError extends Error {
353
360
  }
354
361
  }
355
362
 
356
- function sameAuthority(
357
- snapshot: HistorySnapshot,
358
- page: HistoryPageSnapshot,
359
- ): boolean {
363
+ function sameAuthority(snapshot: HistorySnapshot, page: HistoryPageSnapshot): boolean {
360
364
  return (
361
365
  page.authorityEpoch === snapshot.authorityEpoch &&
362
366
  page.historyId === snapshot.summary.historyId &&
@@ -372,11 +376,7 @@ function checkedPage(value: number): number {
372
376
  }
373
377
 
374
378
  function checkedPageSize(value: number): number {
375
- if (
376
- !Number.isSafeInteger(value) ||
377
- value < 1 ||
378
- value > HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE
379
- ) {
379
+ if (!Number.isSafeInteger(value) || value < 1 || value > HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE) {
380
380
  throw new RangeError(
381
381
  `history page size must be between 1 and ${HISTORY_MAXIMUM_PROJECTION_PAGE_SIZE}`,
382
382
  );
@@ -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,
@@ -3,7 +3,7 @@
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,4 @@ 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
+ } as const;
@@ -14,9 +14,9 @@ export interface HistoryPort {
14
14
  snapshot(): Promise<unknown>;
15
15
  page(command: HistoryPageCommand): Promise<unknown>;
16
16
  navigate(command: HistoryNavigationCommand): Promise<unknown>;
17
- listen?(
18
- listener: (event: unknown) => void,
19
- ): HistoryUnlisten | Promise<HistoryUnlisten>;
17
+ listen?:
18
+ | ((listener: (event: unknown) => void) => HistoryUnlisten | Promise<HistoryUnlisten>)
19
+ | undefined;
20
20
  nextPlanId(): HistoryPlanId;
21
21
  }
22
22
 
@@ -24,8 +24,6 @@ export interface CheckedHistoryPort {
24
24
  snapshot(): Promise<HistorySnapshot>;
25
25
  page(command: HistoryPageCommand): Promise<HistoryPageSnapshot>;
26
26
  navigate(command: HistoryNavigationCommand): Promise<HistoryNavigationResult>;
27
- listen?(
28
- listener: (event: HistoryChangedEvent) => void,
29
- ): Promise<HistoryUnlisten>;
27
+ listen?(listener: (event: HistoryChangedEvent) => void): Promise<HistoryUnlisten>;
30
28
  nextPlanId(): HistoryPlanId;
31
29
  }
@@ -1,7 +1,5 @@
1
- import type {
2
- HistoryNavigationCommand,
3
- HistoryPageCommand,
4
- } from "./generated/protocol.ts";
1
+ import { wireCopy } from "../core/wire-copy.ts";
2
+ import type { HistoryNavigationCommand, HistoryPageCommand } from "./generated/protocol.ts";
5
3
  import type { HistoryPort, HistoryUnlisten } from "./ports.ts";
6
4
 
7
5
  export class SerializedHistoryPort implements HistoryPort {
@@ -12,27 +10,25 @@ export class SerializedHistoryPort implements HistoryPort {
12
10
  }
13
11
 
14
12
  async snapshot(): Promise<unknown> {
15
- return clone(await this.#inner.snapshot());
13
+ return wireCopy(await this.#inner.snapshot());
16
14
  }
17
15
 
18
16
  async page(command: HistoryPageCommand): Promise<unknown> {
19
- return clone(await this.#inner.page(clone(command)));
17
+ return wireCopy(await this.#inner.page(wireCopy(command)));
20
18
  }
21
19
 
22
20
  async navigate(command: HistoryNavigationCommand): Promise<unknown> {
23
- return clone(await this.#inner.navigate(clone(command)));
21
+ return wireCopy(await this.#inner.navigate(wireCopy(command)));
24
22
  }
25
23
 
26
24
  async listen(listener: (event: unknown) => void): Promise<HistoryUnlisten> {
27
- if (this.#inner.listen === undefined) return () => {};
28
- return this.#inner.listen((event) => listener(clone(event)));
25
+ if (this.#inner.listen === undefined) {
26
+ return () => {};
27
+ }
28
+ return this.#inner.listen((event) => listener(wireCopy(event)));
29
29
  }
30
30
 
31
31
  nextPlanId(): string {
32
32
  return this.#inner.nextPlanId();
33
33
  }
34
34
  }
35
-
36
- function clone<Value>(value: Value): Value {
37
- return JSON.parse(JSON.stringify(value)) as Value;
38
- }