@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.
- package/README.md +2 -2
- package/package.json +1 -7
- package/src/agent-control/generated/protocol.ts +228 -0
- package/src/agent-control/index.ts +1 -5
- package/src/agent-control/inject.ts +4 -1
- package/src/agent-control/selection.ts +42 -25
- package/src/agent-control/shim.ts +428 -198
- package/src/bridge/direct.ts +1 -1
- package/src/bridge/generated/fields.ts +4 -3
- package/src/bridge/generated/protocol.ts +62 -7
- package/src/bridge/generated/variant-fields.ts +1 -1
- package/src/bridge/job.ts +49 -12
- package/src/bridge/loopback.ts +7 -25
- package/src/bridge/operation.ts +13 -43
- package/src/bridge/router.ts +10 -23
- package/src/bridge/runtime/connection.ts +78 -109
- package/src/bridge/runtime/policy.ts +8 -28
- package/src/bridge/runtime/retry.ts +6 -18
- package/src/bridge/session.ts +4 -14
- package/src/bridge/stream/connection.ts +24 -55
- package/src/bridge/stream/source.ts +2 -6
- package/src/bridge/stream/tracker.ts +12 -12
- package/src/bridge/supervision.ts +104 -97
- package/src/bridge/validation/base.ts +21 -63
- package/src/bridge/validation/jobs.ts +11 -25
- package/src/bridge/validation/negotiation.ts +28 -43
- package/src/bridge/validation/operations.ts +29 -65
- package/src/bridge/validation/streams.ts +3 -10
- package/src/commands/client.ts +37 -28
- package/src/commands/controller.ts +119 -96
- package/src/commands/generated/fields.ts +7 -6
- package/src/commands/generated/protocol.ts +28 -1
- package/src/commands/generated/variant-fields.ts +1 -1
- package/src/commands/keyboard.ts +94 -100
- package/src/commands/physical-code.ts +11 -0
- package/src/commands/ports.ts +8 -13
- package/src/commands/projectors.ts +100 -75
- package/src/commands/validation.ts +434 -241
- package/src/config/client.ts +9 -22
- package/src/config/generated/base.ts +22 -6
- package/src/config/generated/fields.ts +4 -3
- package/src/config/generated/label-template.ts +1 -1
- package/src/config/generated/labels.ts +1 -1
- package/src/config/generated/protocol.ts +1 -1
- package/src/config/generated/restore.ts +35 -12
- package/src/config/generated/variant-fields.ts +1 -1
- package/src/config/names.ts +6 -12
- package/src/config/validation/commands.ts +11 -25
- package/src/config/validation/outcomes.ts +163 -70
- package/src/config/validation/primitives.ts +35 -80
- package/src/config/validation/projection.ts +100 -128
- package/src/core/connection.ts +187 -83
- package/src/core/controller-lifecycle.ts +144 -0
- package/src/core/generated/identifiers.ts +4 -0
- package/src/core/identifiers.ts +40 -0
- package/src/core/index.ts +10 -1
- package/src/core/json-value.ts +58 -0
- package/src/core/listeners.ts +35 -0
- package/src/core/record.ts +80 -0
- package/src/core/transport.ts +9 -11
- package/src/core/wire-copy.ts +7 -0
- package/src/history/client.ts +11 -13
- package/src/history/controller.ts +106 -108
- package/src/history/direct.ts +1 -3
- package/src/history/generated/fields.ts +4 -3
- package/src/history/generated/protocol.ts +8 -5
- package/src/history/generated/variant-fields.ts +1 -1
- package/src/history/ports.ts +7 -4
- package/src/history/serialized.ts +9 -13
- package/src/history/validation.ts +69 -75
- package/src/history-tree/client.ts +89 -11
- package/src/history-tree/controller.ts +380 -96
- package/src/history-tree/direct.ts +3 -1
- package/src/history-tree/generated/fields.ts +5 -3
- package/src/history-tree/generated/protocol.ts +43 -7
- package/src/history-tree/generated/variant-fields.ts +7 -1
- package/src/history-tree/ports.ts +12 -5
- package/src/history-tree/serialized.ts +39 -11
- package/src/history-tree/validation.ts +286 -78
- package/src/layout/generated/protocol.ts +4 -3
- package/src/layout/generated/variant-fields.ts +47 -0
- package/src/layout/index.ts +1 -1
- package/src/layout/ratio.ts +10 -9
- package/src/layout/validation.ts +98 -32
- package/src/layout/visibility.ts +1 -2
- package/src/licence/client.ts +48 -7
- package/src/licence/controller.ts +127 -70
- package/src/licence/direct.ts +3 -1
- package/src/licence/generated/fields.ts +4 -3
- package/src/licence/generated/key-conformance.json +28 -0
- package/src/licence/generated/protocol.ts +9 -5
- package/src/licence/generated/variant-fields.ts +1 -1
- package/src/licence/key.ts +42 -18
- package/src/licence/ports.ts +10 -3
- package/src/licence/serialized.ts +32 -9
- package/src/licence/validation.ts +134 -43
- package/src/native-content/client.ts +207 -234
- package/src/native-content/generated/fields.ts +6 -6
- package/src/native-content/generated/protocol.ts +16 -5
- package/src/native-content/generated/variant-fields.ts +1 -1
- package/src/native-content/ports.ts +11 -16
- package/src/native-content/serialized.ts +12 -19
- package/src/native-content/validation/base.ts +57 -80
- package/src/native-content/validation/messages.ts +24 -35
- package/src/native-content/validation/state.ts +31 -33
- package/src/notifications/client.ts +85 -51
- package/src/notifications/controller.ts +400 -145
- package/src/notifications/direct.ts +3 -1
- package/src/notifications/generated/fields.ts +4 -3
- package/src/notifications/generated/labels.ts +1 -1
- package/src/notifications/generated/protocol.ts +5 -1
- package/src/notifications/generated/variant-fields.ts +1 -1
- package/src/notifications/ports.ts +9 -4
- package/src/notifications/serialized.ts +11 -9
- package/src/notifications/tone.ts +1 -1
- package/src/notifications/validation.ts +165 -71
- package/src/operation/bridge.ts +187 -60
- package/src/operation/client.ts +48 -62
- package/src/operation/controller.ts +67 -97
- package/src/operation/direct.ts +1 -3
- package/src/operation/generated/fields.ts +4 -3
- package/src/operation/generated/labels.ts +1 -1
- package/src/operation/generated/protocol.ts +5 -1
- package/src/operation/generated/variant-fields.ts +1 -1
- package/src/operation/ports.ts +12 -10
- package/src/operation/serialized.ts +8 -9
- package/src/operation/validation.ts +189 -104
- package/src/settings/client.ts +11 -44
- package/src/settings/connection.ts +35 -53
- package/src/settings/generated/fields.ts +5 -4
- package/src/settings/generated/protocol.ts +14 -5
- package/src/settings/generated/variant-fields.ts +1 -1
- package/src/settings/names.ts +2 -4
- package/src/settings/registry.ts +23 -25
- package/src/settings/validation/authority.ts +21 -19
- package/src/settings/validation/command.ts +22 -41
- package/src/settings/validation/error.ts +2 -8
- package/src/settings/validation/primitives.ts +46 -97
- package/src/settings/validation/projection.ts +8 -24
- package/src/settings/validation/registry.ts +33 -79
- package/src/surface-transfer/client.ts +6 -18
- package/src/surface-transfer/generated/fields.ts +3 -3
- package/src/surface-transfer/generated/protocol.ts +3 -3
- package/src/surface-transfer/generated/variant-fields.ts +1 -1
- package/src/surface-transfer/validation.ts +91 -86
- package/src/surfaces/client.ts +33 -55
- package/src/surfaces/generated/fields.ts +3 -3
- package/src/surfaces/generated/protocol.ts +1 -1
- package/src/surfaces/generated/variant-fields.ts +1 -1
- package/src/surfaces/validation.ts +227 -99
- package/src/transfer/client.ts +26 -60
- package/src/transfer/connection.ts +29 -51
- package/src/transfer/generated/fields.ts +7 -6
- package/src/transfer/generated/protocol.ts +13 -5
- package/src/transfer/generated/variant-fields.ts +1 -1
- package/src/transfer/index.ts +8 -1
- package/src/transfer/payload.ts +20 -35
- package/src/transfer/validation.ts +169 -119
- package/src/update/client.ts +86 -9
- package/src/update/controller.ts +264 -116
- package/src/update/direct.ts +3 -1
- package/src/update/generated/fields.ts +5 -4
- package/src/update/generated/protocol.ts +25 -9
- package/src/update/generated/variant-fields.ts +4 -2
- package/src/update/ports.ts +14 -6
- package/src/update/serialized.ts +36 -10
- 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
|
+
}
|
package/src/core/transport.ts
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
export type Unlisten = () => void | Promise<void>;
|
|
2
2
|
|
|
3
3
|
export interface InvokeTransport {
|
|
4
|
-
invoke(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
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
|
+
}
|
package/src/history/client.ts
CHANGED
|
@@ -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
|
-
|
|
52
|
+
listen(
|
|
57
53
|
listener: (event: HistoryChangedEvent) => void,
|
|
54
|
+
onFailure?: ConnectionFailureReporter,
|
|
58
55
|
): Promise<HistoryUnlisten> {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
assertValidHistoryChangedEvent
|
|
62
|
-
listener
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
|
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
|
-
#
|
|
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():
|
|
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)
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
this.#
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
this.#
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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.#
|
|
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.#
|
|
153
|
-
|
|
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.#
|
|
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.#
|
|
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
|
-
|
|
230
|
-
|
|
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(
|
|
242
|
+
this.#install(projection);
|
|
238
243
|
this.#setStatus({ kind: "ready" });
|
|
239
244
|
return result;
|
|
240
245
|
} catch (error) {
|
|
241
246
|
if (
|
|
242
|
-
this.#
|
|
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
|
|
264
|
-
if (sameAuthority(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
|
-
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
|
|
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: (
|
|
283
|
-
limit:
|
|
282
|
+
offset: (pageNumber - 1) * pageSize,
|
|
283
|
+
limit: pageSize,
|
|
284
284
|
});
|
|
285
|
+
return { snapshot, page, pageNumber };
|
|
285
286
|
}
|
|
286
287
|
|
|
287
|
-
#install(snapshot
|
|
288
|
-
if (!sameAuthority(snapshot, page))
|
|
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.#
|
|
303
|
+
this.#page = pageNumber;
|
|
301
304
|
}
|
|
302
305
|
|
|
303
306
|
#handleChanged(event: HistoryChangedEvent): void {
|
|
304
|
-
if (!this.#
|
|
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)
|
|
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)
|
|
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
|
);
|
package/src/history/direct.ts
CHANGED
|
@@ -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:
|
|
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
|
|
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:
|
|
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:
|
|
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.
|
package/src/history/ports.ts
CHANGED
|
@@ -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 =
|
|
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
|
-
|
|
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
|
}
|