@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
|
@@ -1,20 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { ControllerLifecycle } from "../core/controller-lifecycle.ts";
|
|
2
|
+
import { isNewerNotificationSnapshot, NotificationClient } from "./client.ts";
|
|
3
|
+
import { NOTIFICATION_PROTOCOL_VERSION } from "./generated/protocol.ts";
|
|
4
|
+
import type {
|
|
5
|
+
NotificationActionProjection,
|
|
6
|
+
NotificationId,
|
|
7
|
+
NotificationMutationResult,
|
|
8
|
+
NotificationRecordProjection,
|
|
9
|
+
NotificationRejection,
|
|
10
|
+
NotificationRequestId,
|
|
11
|
+
NotificationSnapshot,
|
|
11
12
|
} from "./generated/protocol.ts";
|
|
12
|
-
import { notificationSeverityTone
|
|
13
|
+
import { notificationSeverityTone } from "./tone.ts";
|
|
14
|
+
import type { NotificationStatusTone } from "./tone.ts";
|
|
13
15
|
import { toastAction, toastTitle } from "./toast.ts";
|
|
14
16
|
|
|
15
17
|
import type { NotificationPort } from "./ports.ts";
|
|
16
18
|
|
|
17
|
-
export type NotificationControllerStatus =
|
|
19
|
+
export type NotificationControllerStatus =
|
|
20
|
+
| { readonly kind: "idle" }
|
|
21
|
+
| { readonly kind: "loading" }
|
|
22
|
+
| { readonly kind: "ready" }
|
|
23
|
+
| { readonly kind: "failed"; readonly error: unknown };
|
|
24
|
+
export type NotificationPaginationStatus =
|
|
25
|
+
| { readonly kind: "idle" }
|
|
26
|
+
| { readonly kind: "loading" }
|
|
27
|
+
| { readonly kind: "failed"; readonly error: unknown };
|
|
18
28
|
export type NotificationPendingKind = "markSeen" | "dismiss" | "action";
|
|
19
29
|
|
|
20
30
|
export interface NotificationPendingCommand {
|
|
@@ -23,8 +33,12 @@ export interface NotificationPendingCommand {
|
|
|
23
33
|
readonly kind: NotificationPendingKind;
|
|
24
34
|
}
|
|
25
35
|
|
|
26
|
-
export interface NotificationCommandRejection extends NotificationPendingCommand {
|
|
27
|
-
|
|
36
|
+
export interface NotificationCommandRejection extends NotificationPendingCommand {
|
|
37
|
+
readonly rejection: NotificationRejection;
|
|
38
|
+
}
|
|
39
|
+
export interface NotificationCommandFailure extends NotificationPendingCommand {
|
|
40
|
+
readonly error: unknown;
|
|
41
|
+
}
|
|
28
42
|
|
|
29
43
|
export interface NotificationActionInvocation {
|
|
30
44
|
readonly notificationId: NotificationId;
|
|
@@ -42,7 +56,7 @@ export interface NotificationToastProjection {
|
|
|
42
56
|
readonly title: string;
|
|
43
57
|
readonly description: string;
|
|
44
58
|
readonly tone: NotificationStatusTone;
|
|
45
|
-
readonly action?: NotificationActionProjection;
|
|
59
|
+
readonly action?: NotificationActionProjection | undefined;
|
|
46
60
|
}
|
|
47
61
|
|
|
48
62
|
export interface NotificationControllerOptions {
|
|
@@ -53,171 +67,356 @@ export interface NotificationControllerOptions {
|
|
|
53
67
|
}
|
|
54
68
|
|
|
55
69
|
export class NotificationController {
|
|
56
|
-
readonly client: NotificationClient;
|
|
57
|
-
readonly observers = new Set<() => void>();
|
|
58
|
-
readonly pending = new Map<NotificationRequestId, NotificationPendingCommand>();
|
|
59
|
-
readonly pageSize: number;
|
|
60
|
-
readonly toastSelector: ((record: NotificationRecordProjection) => boolean) | undefined;
|
|
61
|
-
readonly actionExecutor: NotificationActionExecutor | undefined;
|
|
62
|
-
status: NotificationControllerStatus = { kind: "idle" };
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
readonly #client: NotificationClient;
|
|
71
|
+
readonly #observers = new Set<() => void>();
|
|
72
|
+
readonly #pending = new Map<NotificationRequestId, NotificationPendingCommand>();
|
|
73
|
+
readonly #pageSize: number;
|
|
74
|
+
readonly #toastSelector: ((record: NotificationRecordProjection) => boolean) | undefined;
|
|
75
|
+
readonly #actionExecutor: NotificationActionExecutor | undefined;
|
|
76
|
+
#status: NotificationControllerStatus = { kind: "idle" };
|
|
77
|
+
#pagination: NotificationPaginationStatus = { kind: "idle" };
|
|
78
|
+
#snapshot: NotificationSnapshot | undefined;
|
|
79
|
+
#records: Array<NotificationRecordProjection> = [];
|
|
80
|
+
#toasts: Array<NotificationToastProjection> = [];
|
|
81
|
+
#selectedNotificationId: NotificationId | undefined;
|
|
82
|
+
#commandRejection: NotificationCommandRejection | undefined;
|
|
83
|
+
#commandFailure: NotificationCommandFailure | undefined;
|
|
84
|
+
readonly #lifecycle = new ControllerLifecycle();
|
|
85
|
+
#toastSequence = 0;
|
|
86
|
+
#pageLoad: Promise<void> | undefined;
|
|
73
87
|
|
|
74
88
|
constructor(options: NotificationControllerOptions) {
|
|
75
|
-
this
|
|
76
|
-
this
|
|
77
|
-
this
|
|
78
|
-
this
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
get
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
this
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
89
|
+
this.#client = new NotificationClient(options.port);
|
|
90
|
+
this.#pageSize = options.pageSize ?? 100;
|
|
91
|
+
this.#toastSelector = options.toast?.select;
|
|
92
|
+
this.#actionExecutor = options.actions;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get status(): NotificationControllerStatus {
|
|
96
|
+
return { ...this.#status };
|
|
97
|
+
}
|
|
98
|
+
get pagination(): NotificationPaginationStatus {
|
|
99
|
+
return { ...this.#pagination };
|
|
100
|
+
}
|
|
101
|
+
get snapshot(): NotificationSnapshot | undefined {
|
|
102
|
+
return structuredClone(this.#snapshot);
|
|
103
|
+
}
|
|
104
|
+
get records(): ReadonlyArray<NotificationRecordProjection> {
|
|
105
|
+
return structuredClone(this.#records);
|
|
106
|
+
}
|
|
107
|
+
get toasts(): ReadonlyArray<NotificationToastProjection> {
|
|
108
|
+
return structuredClone(this.#toasts);
|
|
109
|
+
}
|
|
110
|
+
get selectedNotificationId(): NotificationId | undefined {
|
|
111
|
+
return this.#selectedNotificationId;
|
|
112
|
+
}
|
|
113
|
+
get selected(): NotificationRecordProjection | undefined {
|
|
114
|
+
return structuredClone(
|
|
115
|
+
this.#records.find((record) => record.notificationId === this.#selectedNotificationId),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
get commandRejection(): NotificationCommandRejection | undefined {
|
|
119
|
+
return structuredClone(this.#commandRejection);
|
|
120
|
+
}
|
|
121
|
+
get commandFailure(): NotificationCommandFailure | undefined {
|
|
122
|
+
return this.#commandFailure === undefined ? undefined : { ...this.#commandFailure };
|
|
123
|
+
}
|
|
124
|
+
get hasMore(): boolean {
|
|
125
|
+
return this.#snapshot?.page.hasMore ?? false;
|
|
126
|
+
}
|
|
127
|
+
get pendingCommands(): ReadonlyArray<NotificationPendingCommand> {
|
|
128
|
+
return Array.from(this.#pending.values(), (pending) => ({ ...pending }));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
observe(observer: () => void): () => void {
|
|
132
|
+
this.#observers.add(observer);
|
|
133
|
+
return () => this.#observers.delete(observer);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
start(): Promise<void> {
|
|
137
|
+
return this.#lifecycle.start({
|
|
138
|
+
loading: () => this.#setStatus({ kind: "loading" }),
|
|
139
|
+
connect: ({ isCurrent, reportFailure, watch }) => {
|
|
140
|
+
const subscription = this.#client.subscribe(
|
|
141
|
+
(snapshot) => {
|
|
142
|
+
if (isCurrent()) {
|
|
143
|
+
this.#install(snapshot);
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
reportFailure,
|
|
147
|
+
this.#pageSize,
|
|
148
|
+
);
|
|
149
|
+
return watch(subscription, () => this.#setStatus({ kind: "ready" }));
|
|
150
|
+
},
|
|
151
|
+
failed: (error) => this.#setStatus({ kind: "failed", error }),
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Replaces the current subscription, including after a reported failure. */
|
|
156
|
+
restart(): Promise<void> {
|
|
157
|
+
return this.#lifecycle.restart(
|
|
158
|
+
this.status,
|
|
159
|
+
() => this.stop(),
|
|
160
|
+
() => this.start(),
|
|
96
161
|
);
|
|
97
|
-
this.subscription = subscription;
|
|
98
|
-
try {
|
|
99
|
-
await subscription.ready;
|
|
100
|
-
if (this.isCurrent(lifecycle)) this.setStatus({ kind: "ready" });
|
|
101
|
-
} catch (error) {
|
|
102
|
-
if (this.isCurrent(lifecycle)) this.setStatus({ kind: "failed", error });
|
|
103
|
-
}
|
|
104
162
|
}
|
|
105
163
|
|
|
106
|
-
|
|
107
|
-
this.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (subscription !== undefined) await subscription.dispose();
|
|
164
|
+
stop(): Promise<void> {
|
|
165
|
+
return this.#lifecycle.stop(() => {
|
|
166
|
+
this.#snapshot = undefined;
|
|
167
|
+
this.#records = [];
|
|
168
|
+
this.#toasts = [];
|
|
169
|
+
this.#selectedNotificationId = undefined;
|
|
170
|
+
this.#pending.clear();
|
|
171
|
+
this.#pageLoad = undefined;
|
|
172
|
+
this.#pagination = { kind: "idle" };
|
|
173
|
+
this.#commandFailure = undefined;
|
|
174
|
+
this.#commandRejection = undefined;
|
|
175
|
+
this.#setStatus({ kind: "idle" });
|
|
176
|
+
});
|
|
120
177
|
}
|
|
121
178
|
|
|
122
179
|
select(notificationId: NotificationId | undefined): void {
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
|
|
180
|
+
if (
|
|
181
|
+
notificationId !== undefined &&
|
|
182
|
+
!this.#records.some((record) => record.notificationId === notificationId)
|
|
183
|
+
) {
|
|
184
|
+
throw new NotificationControllerUnknownRecordError(notificationId);
|
|
185
|
+
}
|
|
186
|
+
this.#selectedNotificationId = notificationId;
|
|
187
|
+
this.#notify();
|
|
126
188
|
}
|
|
127
189
|
|
|
128
190
|
async loadMore(): Promise<void> {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
191
|
+
if (this.#pageLoad !== undefined) {
|
|
192
|
+
return this.#pageLoad;
|
|
193
|
+
}
|
|
194
|
+
const current = this.#requireSnapshot();
|
|
195
|
+
if (!current.page.hasMore) {
|
|
134
196
|
return;
|
|
135
197
|
}
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
this
|
|
139
|
-
this
|
|
198
|
+
const lifecycle = this.#lifecycle.revision;
|
|
199
|
+
const load = this.#applyPage(current, this.#records.length, lifecycle);
|
|
200
|
+
this.#pageLoad = load;
|
|
201
|
+
this.#setPagination({ kind: "loading" });
|
|
202
|
+
try {
|
|
203
|
+
await load;
|
|
204
|
+
if (this.#pageLoad === load) {
|
|
205
|
+
this.#setPagination({ kind: "idle" });
|
|
206
|
+
}
|
|
207
|
+
} catch (error) {
|
|
208
|
+
// A load that a newer snapshot or a stop has abandoned no longer owns status.
|
|
209
|
+
if (this.#pageLoad === load) {
|
|
210
|
+
this.#setPagination({ kind: "failed", error });
|
|
211
|
+
}
|
|
212
|
+
throw error;
|
|
213
|
+
} finally {
|
|
214
|
+
if (this.#pageLoad === load) {
|
|
215
|
+
this.#pageLoad = undefined;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
140
218
|
}
|
|
141
219
|
|
|
142
|
-
markSeen(notificationId: NotificationId): Promise<NotificationMutationResult> {
|
|
143
|
-
|
|
220
|
+
markSeen(notificationId: NotificationId): Promise<NotificationMutationResult> {
|
|
221
|
+
return this.#mutateRecord(notificationId, "markSeen");
|
|
222
|
+
}
|
|
223
|
+
dismiss(notificationId: NotificationId): Promise<NotificationMutationResult> {
|
|
224
|
+
return this.#mutateRecord(notificationId, "dismiss");
|
|
225
|
+
}
|
|
144
226
|
|
|
145
227
|
dismissToast(id: string): void {
|
|
146
|
-
const next = this
|
|
147
|
-
if (next.length === this
|
|
148
|
-
|
|
149
|
-
|
|
228
|
+
const next = this.#toasts.filter((toast) => toast.id !== id);
|
|
229
|
+
if (next.length === this.#toasts.length) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
this.#toasts = next;
|
|
233
|
+
this.#notify();
|
|
150
234
|
}
|
|
151
235
|
|
|
152
236
|
async invokeAction(notificationId: NotificationId, referenceId: string): Promise<void> {
|
|
153
|
-
const record = this
|
|
237
|
+
const record = this.#requireRecord(notificationId);
|
|
154
238
|
const action = record.draft.actions.find((candidate) => candidate.referenceId === referenceId);
|
|
155
|
-
if (action === undefined)
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
239
|
+
if (action === undefined) {
|
|
240
|
+
throw new NotificationControllerUnknownActionError(notificationId, referenceId);
|
|
241
|
+
}
|
|
242
|
+
if (this.#actionExecutor === undefined) {
|
|
243
|
+
throw new NotificationControllerActionUnavailableError();
|
|
244
|
+
}
|
|
245
|
+
const pending = {
|
|
246
|
+
requestId: this.#client.nextRequestId(),
|
|
247
|
+
notificationId,
|
|
248
|
+
kind: "action" as const,
|
|
249
|
+
};
|
|
250
|
+
await this.#runPending(pending, async () => {
|
|
251
|
+
await this.#actionExecutor?.admitAndExecute({
|
|
252
|
+
notificationId,
|
|
253
|
+
sourceId: record.draft.sourceId,
|
|
254
|
+
referenceId: action.referenceId,
|
|
255
|
+
});
|
|
160
256
|
});
|
|
161
257
|
}
|
|
162
258
|
|
|
163
259
|
isPending(notificationId: NotificationId, kind: NotificationPendingKind): boolean {
|
|
164
|
-
return [...this
|
|
260
|
+
return [...this.#pending.values()].some(
|
|
261
|
+
(item) => item.notificationId === notificationId && item.kind === kind,
|
|
262
|
+
);
|
|
165
263
|
}
|
|
166
264
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
265
|
+
async #mutateRecord(
|
|
266
|
+
notificationId: NotificationId,
|
|
267
|
+
kind: "markSeen" | "dismiss",
|
|
268
|
+
): Promise<NotificationMutationResult> {
|
|
269
|
+
const snapshot = this.#requireSnapshot();
|
|
270
|
+
this.#requireRecord(notificationId);
|
|
170
271
|
const command = {
|
|
171
272
|
kind,
|
|
172
|
-
requestId: this
|
|
273
|
+
requestId: this.#client.nextRequestId(),
|
|
173
274
|
protocolVersion: NOTIFICATION_PROTOCOL_VERSION,
|
|
174
275
|
authority: snapshot.authority,
|
|
175
276
|
expectedLedgerRevision: snapshot.ledgerRevision,
|
|
176
277
|
notificationId,
|
|
177
278
|
} as const;
|
|
178
|
-
return this
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
279
|
+
return this.#runPending(
|
|
280
|
+
{ requestId: command.requestId, notificationId, kind },
|
|
281
|
+
async (lifecycle) => {
|
|
282
|
+
const result = await this.#client.mutate(command);
|
|
283
|
+
if (!this.#lifecycle.isCurrent(lifecycle)) {
|
|
284
|
+
return result;
|
|
285
|
+
}
|
|
286
|
+
if (result.status === "rejected") {
|
|
287
|
+
this.#commandRejection = {
|
|
288
|
+
requestId: command.requestId,
|
|
289
|
+
notificationId,
|
|
290
|
+
kind,
|
|
291
|
+
rejection: structuredClone(result.rejection),
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
this.#install(result.snapshot);
|
|
295
|
+
return result;
|
|
296
|
+
},
|
|
297
|
+
);
|
|
184
298
|
}
|
|
185
299
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
300
|
+
async #runPending<Result>(
|
|
301
|
+
pending: NotificationPendingCommand,
|
|
302
|
+
run: (lifecycle: number) => Promise<Result>,
|
|
303
|
+
): Promise<Result> {
|
|
304
|
+
if (this.isPending(pending.notificationId, pending.kind)) {
|
|
305
|
+
throw new NotificationControllerCommandPendingError(pending.notificationId, pending.kind);
|
|
306
|
+
}
|
|
307
|
+
const lifecycle = this.#lifecycle.revision;
|
|
308
|
+
this.#pending.set(pending.requestId, pending);
|
|
309
|
+
this.#commandFailure = undefined;
|
|
310
|
+
this.#commandRejection = undefined;
|
|
311
|
+
this.#notify();
|
|
312
|
+
try {
|
|
313
|
+
return await run(lifecycle);
|
|
314
|
+
} catch (error) {
|
|
315
|
+
if (this.#lifecycle.isCurrent(lifecycle)) {
|
|
316
|
+
this.#commandFailure = { ...pending, error };
|
|
317
|
+
}
|
|
196
318
|
throw error;
|
|
197
319
|
} finally {
|
|
198
|
-
if (this.isCurrent(lifecycle)) {
|
|
320
|
+
if (this.#lifecycle.isCurrent(lifecycle)) {
|
|
321
|
+
this.#pending.delete(pending.requestId);
|
|
322
|
+
this.#notify();
|
|
323
|
+
}
|
|
199
324
|
}
|
|
200
325
|
}
|
|
201
326
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
327
|
+
/**
|
|
328
|
+
* Applies one requested page only while the live state is still the basis it
|
|
329
|
+
* was asked against. A page is a slice, so when the ledger has moved past the
|
|
330
|
+
* basis it resyncs page zero instead of installing the slice as state.
|
|
331
|
+
*/
|
|
332
|
+
async #applyPage(basis: NotificationSnapshot, offset: number, lifecycle: number): Promise<void> {
|
|
333
|
+
const response = (await this.#client.snapshot(offset, this.#pageSize)).snapshot;
|
|
334
|
+
const live = this.#pageFence(basis, offset, lifecycle);
|
|
335
|
+
if (live === undefined) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (!isSameLedgerPosition(response, live)) {
|
|
339
|
+
await this.#resyncFirstPage(lifecycle);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const known = new Set(this.#records.map((record) => record.notificationId));
|
|
343
|
+
this.#records = [
|
|
344
|
+
...this.#records,
|
|
345
|
+
...structuredClone(
|
|
346
|
+
response.page.records.filter((record) => !known.has(record.notificationId)),
|
|
347
|
+
),
|
|
348
|
+
];
|
|
349
|
+
this.#snapshot = {
|
|
350
|
+
...live,
|
|
351
|
+
page: { ...live.page, hasMore: response.page.hasMore, records: [...this.#records] },
|
|
352
|
+
};
|
|
353
|
+
this.#notify();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The live snapshot, when a page requested at `offset` against `basis` may still apply. */
|
|
357
|
+
#pageFence(
|
|
358
|
+
basis: NotificationSnapshot,
|
|
359
|
+
offset: number,
|
|
360
|
+
lifecycle: number,
|
|
361
|
+
): NotificationSnapshot | undefined {
|
|
362
|
+
const live = this.#snapshot;
|
|
363
|
+
if (
|
|
364
|
+
!this.#lifecycle.isCurrent(lifecycle) ||
|
|
365
|
+
live === undefined ||
|
|
366
|
+
this.#records.length !== offset
|
|
367
|
+
) {
|
|
368
|
+
return undefined;
|
|
369
|
+
}
|
|
370
|
+
return isSameLedgerPosition(live, basis) ? live : undefined;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
async #resyncFirstPage(lifecycle: number): Promise<void> {
|
|
374
|
+
const first = (await this.#client.snapshot(0, this.#pageSize)).snapshot;
|
|
375
|
+
if (this.#lifecycle.isCurrent(lifecycle)) {
|
|
376
|
+
this.#install(first);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
#setPagination(pagination: NotificationPaginationStatus): void {
|
|
381
|
+
this.#pagination = pagination;
|
|
382
|
+
this.#notify();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
#install(incoming: NotificationSnapshot): void {
|
|
386
|
+
if (!isNewerNotificationSnapshot(incoming, this.#snapshot)) {
|
|
387
|
+
return;
|
|
211
388
|
}
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
this.
|
|
389
|
+
const candidate = structuredClone(incoming);
|
|
390
|
+
const previous = this.#snapshot;
|
|
391
|
+
this.#snapshot = candidate;
|
|
392
|
+
this.#records = [...candidate.page.records];
|
|
393
|
+
// A newer snapshot abandons any page request it was asked against, and clearing
|
|
394
|
+
// the load withdraws that request's right to set status. A failure recorded
|
|
395
|
+
// against the older state must not outlive the snapshot, even with no further pages.
|
|
396
|
+
this.#pageLoad = undefined;
|
|
397
|
+
this.#pagination = { kind: "idle" };
|
|
398
|
+
if (previous !== undefined && this.#toastSelector !== undefined) {
|
|
399
|
+
const existing = new Set(this.#toasts.map((toast) => toast.notificationId));
|
|
400
|
+
const fresh = candidate.page.records.filter(
|
|
401
|
+
(record) =>
|
|
402
|
+
record.readState === "unseen" &&
|
|
403
|
+
record.lastChangedLedgerRevision > previous.ledgerRevision &&
|
|
404
|
+
this.#toastSelector?.(structuredClone(record)) === true &&
|
|
405
|
+
!existing.has(record.notificationId),
|
|
406
|
+
);
|
|
407
|
+
this.#toasts = [...this.#toasts, ...fresh.map((record) => this.#projectToast(record))];
|
|
408
|
+
}
|
|
409
|
+
const retained = new Set(this.#records.map((record) => record.notificationId));
|
|
410
|
+
this.#toasts = this.#toasts.filter((toast) => retained.has(toast.notificationId));
|
|
411
|
+
if (this.#selectedNotificationId !== undefined && !retained.has(this.#selectedNotificationId)) {
|
|
412
|
+
this.#selectedNotificationId = undefined;
|
|
413
|
+
}
|
|
414
|
+
this.#notify();
|
|
216
415
|
}
|
|
217
416
|
|
|
218
|
-
|
|
417
|
+
#projectToast(record: NotificationRecordProjection): NotificationToastProjection {
|
|
219
418
|
return {
|
|
220
|
-
id: `notification-toast:${++this
|
|
419
|
+
id: `notification-toast:${++this.#toastSequence}`,
|
|
221
420
|
notificationId: record.notificationId,
|
|
222
421
|
title: toastTitle(record.draft),
|
|
223
422
|
description: record.draft.summary,
|
|
@@ -226,15 +425,71 @@ export class NotificationController {
|
|
|
226
425
|
};
|
|
227
426
|
}
|
|
228
427
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
428
|
+
#requireSnapshot(): NotificationSnapshot {
|
|
429
|
+
if (!this.#lifecycle.active || this.#snapshot === undefined) {
|
|
430
|
+
throw new NotificationControllerUnavailableError();
|
|
431
|
+
}
|
|
432
|
+
return this.#snapshot;
|
|
433
|
+
}
|
|
434
|
+
#requireRecord(id: NotificationId): NotificationRecordProjection {
|
|
435
|
+
const record = this.#records.find((candidate) => candidate.notificationId === id);
|
|
436
|
+
if (record === undefined) {
|
|
437
|
+
throw new NotificationControllerUnknownRecordError(id);
|
|
438
|
+
}
|
|
439
|
+
return record;
|
|
440
|
+
}
|
|
441
|
+
#setStatus(status: NotificationControllerStatus): void {
|
|
442
|
+
this.#status = status;
|
|
443
|
+
this.#notify();
|
|
444
|
+
}
|
|
445
|
+
#notify(): void {
|
|
446
|
+
for (const observer of this.#observers) {
|
|
447
|
+
observer();
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function isSameLedgerPosition(a: NotificationSnapshot, b: NotificationSnapshot): boolean {
|
|
453
|
+
return (
|
|
454
|
+
a.authority.authorityId === b.authority.authorityId &&
|
|
455
|
+
a.authority.authorityEpoch === b.authority.authorityEpoch &&
|
|
456
|
+
a.ledgerRevision === b.ledgerRevision
|
|
457
|
+
);
|
|
234
458
|
}
|
|
235
459
|
|
|
236
|
-
export class NotificationControllerUnavailableError extends Error {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
460
|
+
export class NotificationControllerUnavailableError extends Error {
|
|
461
|
+
constructor() {
|
|
462
|
+
super("notification controller is not ready");
|
|
463
|
+
this.name = "NotificationControllerUnavailableError";
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
export class NotificationControllerUnknownRecordError extends Error {
|
|
467
|
+
constructor(readonly notificationId: string) {
|
|
468
|
+
super(`unknown notification: ${notificationId}`);
|
|
469
|
+
this.name = "NotificationControllerUnknownRecordError";
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
export class NotificationControllerUnknownActionError extends Error {
|
|
473
|
+
constructor(
|
|
474
|
+
readonly notificationId: string,
|
|
475
|
+
readonly referenceId: string,
|
|
476
|
+
) {
|
|
477
|
+
super(`unknown notification action ${referenceId} on ${notificationId}`);
|
|
478
|
+
this.name = "NotificationControllerUnknownActionError";
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
export class NotificationControllerActionUnavailableError extends Error {
|
|
482
|
+
constructor() {
|
|
483
|
+
super("notification action executor is not configured");
|
|
484
|
+
this.name = "NotificationControllerActionUnavailableError";
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
export class NotificationControllerCommandPendingError extends Error {
|
|
488
|
+
constructor(
|
|
489
|
+
readonly notificationId: string,
|
|
490
|
+
readonly kind: NotificationPendingKind,
|
|
491
|
+
) {
|
|
492
|
+
super(`notification ${kind} already pending for ${notificationId}`);
|
|
493
|
+
this.name = "NotificationControllerCommandPendingError";
|
|
494
|
+
}
|
|
495
|
+
}
|
|
@@ -7,6 +7,8 @@ export interface DirectNotificationHandlers {
|
|
|
7
7
|
readonly nextRequestId: NotificationPort["nextRequestId"];
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export function createDirectNotificationPort(
|
|
10
|
+
export function createDirectNotificationPort(
|
|
11
|
+
handlers: DirectNotificationHandlers,
|
|
12
|
+
): NotificationPort {
|
|
11
13
|
return { ...handlers };
|
|
12
14
|
}
|
|
@@ -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 NOTIFICATIONS_FIELDS
|
|
6
|
+
export const NOTIFICATIONS_FIELDS = {
|
|
7
7
|
"NotificationAuthorityProjection": ["authorityId", "authorityEpoch"],
|
|
8
8
|
"NotificationActionProjection": ["referenceId", "label"],
|
|
9
9
|
"NotificationDraftProjection": ["sourceId", "severity", "title", "summary", "causeId", "actions", "replacementKey", "producerToken", "retentionClass", "presentationTimeUnixMs"],
|
|
@@ -16,4 +16,5 @@ export const NOTIFICATIONS_FIELDS: Record<string, readonly string[]> = {
|
|
|
16
16
|
"NotificationRemovalProjection": ["record", "reason"],
|
|
17
17
|
"NotificationRejection": ["code", "detail", "refreshRequired"],
|
|
18
18
|
"NotificationChangedEvent": ["protocolVersion", "requestId", "authority", "previousLedgerRevision", "committedLedgerRevision", "affectedNotificationIds", "kind"],
|
|
19
|
-
|
|
19
|
+
"NotificationHostError": ["code", "message", "retryable"],
|
|
20
|
+
} as const;
|