@inflatable-cookie/longhorn-poodle-svelte 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/bindings/drop-target.ts +39 -0
- package/src/bindings/element-geometry.ts +10 -0
- package/src/bindings/freeze-projection.ts +11 -0
- package/src/bindings/layout.svelte.ts +22 -25
- package/src/bindings/lifecycle.svelte.ts +32 -60
- package/src/bindings/mount.ts +18 -0
- package/src/bindings/optimistic.svelte.ts +6 -16
- package/src/bindings/sample-controller.svelte.ts +19 -0
- package/src/bindings/scheduler.ts +2 -0
- package/src/bindings/session-projection.svelte.ts +98 -0
- package/src/bindings/start-stop.ts +64 -0
- package/src/bindings/surface-transfer-actions.ts +54 -105
- package/src/bindings/surface-transfer.svelte.ts +85 -91
- package/src/bindings/surfaces.svelte.ts +34 -31
- package/src/bindings/teardown.ts +16 -0
- package/src/bindings/transfer-actions.ts +51 -45
- package/src/bindings/transfer-model.ts +9 -35
- package/src/bindings/transfer.svelte.ts +172 -144
- package/src/bindings/window-drag.ts +28 -20
- package/src/commands/poodle/CommandPaletteBinding.svelte +72 -13
- package/src/commands/poodle/KeybindingSettings.svelte +176 -93
- package/src/commands/poodle/projectors.ts +14 -15
- package/src/commands/svelte/lifecycle.ts +2 -9
- package/src/commands/svelte/session.svelte.ts +25 -35
- package/src/config/poodle/BackupSettingsPage.svelte +91 -103
- package/src/config/poodle/ConfigSnapshotState.svelte +26 -0
- package/src/config/poodle/RestoreSettingsPage.svelte +175 -162
- package/src/config/poodle/StorageSettingsPage.svelte +113 -120
- package/src/config/poodle/format.ts +15 -0
- package/src/config/poodle/page-state.svelte.ts +81 -0
- package/src/config/poodle/restore-model.ts +89 -22
- package/src/failures.ts +14 -0
- package/src/history/svelte/lifecycle.ts +2 -9
- package/src/history/svelte/session.svelte.ts +79 -42
- package/src/history-tree/svelte/lifecycle.ts +3 -8
- package/src/history-tree/svelte/session.svelte.ts +121 -19
- package/src/history-tree/svelte.ts +4 -1
- package/src/licence/poodle/LicenceActivation.svelte +50 -10
- package/src/licence/poodle/LicenceSurface.svelte.ts +36 -37
- package/src/native-content/lifecycle.ts +2 -9
- package/src/native-content/session.svelte.ts +116 -151
- package/src/native-content/viewport.ts +3 -1
- package/src/native-content/visibility.ts +1 -1
- package/src/notifications/poodle/NotificationPanel.svelte +98 -15
- package/src/notifications/poodle/NotificationToastHost.svelte +14 -23
- package/src/notifications/poodle/NotificationToastStack.svelte +8 -16
- package/src/notifications/poodle/projectors.ts +5 -3
- package/src/notifications/poodle/toasts.ts +34 -0
- package/src/notifications/svelte/lifecycle.ts +2 -7
- package/src/notifications/svelte/session.svelte.ts +116 -50
- package/src/operation/poodle/OperationPanel.svelte +22 -25
- package/src/operation/poodle/projectors.ts +8 -18
- package/src/operation/svelte/lifecycle.ts +2 -9
- package/src/operation/svelte/session.svelte.ts +70 -37
- package/src/poodle/LayoutDockRegion.svelte +11 -23
- package/src/poodle/LayoutSplitView.svelte +18 -14
- package/src/poodle/LayoutTabs.svelte +10 -18
- package/src/poodle/binding.ts +51 -92
- package/src/poodle/document.ts +12 -33
- package/src/poodle/index.ts +1 -4
- package/src/poodle/projectors.ts +21 -47
- package/src/poodle/transfer.ts +39 -90
- package/src/poodle/types.ts +6 -13
- package/src/settings/poodle/SettingsShell.svelte +168 -61
- package/src/settings/svelte/context.ts +6 -13
- package/src/settings/svelte/document.svelte.ts +65 -43
- package/src/settings/svelte/errors.ts +1 -4
- package/src/settings/svelte/lifecycle.ts +3 -1
- package/src/settings/svelte/page.svelte.ts +33 -39
- package/src/settings/svelte/runtime.svelte.ts +161 -81
- package/src/settings/svelte/scope.svelte.ts +47 -56
- package/src/settings/svelte/session.svelte.ts +30 -24
- package/src/settings/svelte/types.ts +4 -10
- package/src/update/poodle/UpdateCentre.svelte +14 -30
- package/src/update/poodle/UpdateSettings.svelte +10 -7
- package/src/update/poodle/UpdateStatus.svelte +12 -26
- package/src/update/poodle/UpdateSurface.svelte.ts +112 -72
- package/src/svelte.d.ts +0 -6
|
@@ -9,17 +9,13 @@ import {
|
|
|
9
9
|
TRANSFER_PROTOCOL_VERSION,
|
|
10
10
|
parseTransferPayload,
|
|
11
11
|
serializeTransferPayload,
|
|
12
|
-
type TransferCommitSelector,
|
|
13
12
|
} from "@inflatable-cookie/longhorn/transfer";
|
|
13
|
+
import { notify, reportError, resolveDropSelector } from "./drop-target.ts";
|
|
14
|
+
import type { DropSelector } from "./drop-target.ts";
|
|
14
15
|
|
|
15
16
|
import type { SurfaceTransferState } from "./surface-transfer.svelte.ts";
|
|
16
17
|
|
|
17
|
-
export type SurfaceDropSelector =
|
|
18
|
-
| {
|
|
19
|
-
readonly kind: "explicit_zone";
|
|
20
|
-
readonly dropZoneId: string;
|
|
21
|
-
}
|
|
22
|
-
| { readonly kind: "screen_point" };
|
|
18
|
+
export type SurfaceDropSelector = DropSelector;
|
|
23
19
|
|
|
24
20
|
export interface SurfaceTransferDragOptions {
|
|
25
21
|
readonly state: SurfaceTransferState;
|
|
@@ -49,7 +45,7 @@ export function surfaceTransferDrag(
|
|
|
49
45
|
let options = initialOptions;
|
|
50
46
|
let preparationGeneration = 0;
|
|
51
47
|
let armedRequest: SurfaceSessionStartRequest | undefined;
|
|
52
|
-
let removePointerListeners =
|
|
48
|
+
let removePointerListeners = noop;
|
|
53
49
|
const priorDraggable = node.getAttribute("draggable");
|
|
54
50
|
node.draggable = true;
|
|
55
51
|
|
|
@@ -57,20 +53,14 @@ export function surfaceTransferDrag(
|
|
|
57
53
|
++preparationGeneration;
|
|
58
54
|
armedRequest = undefined;
|
|
59
55
|
removePointerListeners();
|
|
60
|
-
removePointerListeners =
|
|
61
|
-
void options.state.cancelPreparation().catch((error) => {
|
|
56
|
+
removePointerListeners = noop;
|
|
57
|
+
void options.state.cancelPreparation().catch((error: unknown) => {
|
|
62
58
|
reportError(options.reportError, error);
|
|
63
59
|
});
|
|
64
60
|
}
|
|
65
61
|
|
|
66
62
|
function handlePointerDown(event: PointerEvent): void {
|
|
67
|
-
if (
|
|
68
|
-
event.button !== 0 ||
|
|
69
|
-
event.altKey ||
|
|
70
|
-
event.ctrlKey ||
|
|
71
|
-
event.metaKey ||
|
|
72
|
-
event.shiftKey
|
|
73
|
-
) {
|
|
63
|
+
if (event.button !== 0 || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
|
|
74
64
|
return;
|
|
75
65
|
}
|
|
76
66
|
cancel();
|
|
@@ -87,19 +77,19 @@ export function surfaceTransferDrag(
|
|
|
87
77
|
ownerWindow.removeEventListener("pointercancel", released, true);
|
|
88
78
|
};
|
|
89
79
|
}
|
|
90
|
-
void options.state
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
response.status !== "started"
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
80
|
+
void options.state
|
|
81
|
+
.prepare(request)
|
|
82
|
+
.then((response) => {
|
|
83
|
+
notify(options.onPreparation, response, options.reportError);
|
|
84
|
+
if (generation === preparationGeneration && response.status !== "started") {
|
|
85
|
+
armedRequest = undefined;
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
.catch((error: unknown) => {
|
|
89
|
+
if (generation === preparationGeneration) {
|
|
90
|
+
reportError(options.reportError, error);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
103
93
|
}
|
|
104
94
|
|
|
105
95
|
function handleDragStart(event: DragEvent): void {
|
|
@@ -114,14 +104,11 @@ export function surfaceTransferDrag(
|
|
|
114
104
|
) {
|
|
115
105
|
event.preventDefault();
|
|
116
106
|
cancel();
|
|
117
|
-
reportError(
|
|
118
|
-
options.reportError,
|
|
119
|
-
new StaleSurfaceTransferPreparationError(),
|
|
120
|
-
);
|
|
107
|
+
reportError(options.reportError, new StaleSurfaceTransferPreparationError());
|
|
121
108
|
return;
|
|
122
109
|
}
|
|
123
110
|
removePointerListeners();
|
|
124
|
-
removePointerListeners =
|
|
111
|
+
removePointerListeners = noop;
|
|
125
112
|
event.dataTransfer.setData(
|
|
126
113
|
LONGHORN_TRANSFER_MIME_TYPE,
|
|
127
114
|
serializeTransferPayload(preparation.response.session.payload),
|
|
@@ -135,7 +122,9 @@ export function surfaceTransferDrag(
|
|
|
135
122
|
|
|
136
123
|
return {
|
|
137
124
|
update(nextOptions) {
|
|
138
|
-
|
|
125
|
+
if (nextOptions.state !== options.state) {
|
|
126
|
+
cancel();
|
|
127
|
+
}
|
|
139
128
|
options = nextOptions;
|
|
140
129
|
},
|
|
141
130
|
destroy() {
|
|
@@ -152,21 +141,27 @@ export function surfaceTransferDrag(
|
|
|
152
141
|
};
|
|
153
142
|
}
|
|
154
143
|
|
|
144
|
+
function handleDragOver(event: DragEvent): void {
|
|
145
|
+
// The drag store exposes types during dragover and payload data on drop.
|
|
146
|
+
const dataTransfer = event.dataTransfer;
|
|
147
|
+
if (!dataTransfer?.types.includes(LONGHORN_TRANSFER_MIME_TYPE)) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
dataTransfer.dropEffect = "move";
|
|
152
|
+
}
|
|
153
|
+
|
|
155
154
|
export function surfaceTransferDrop(
|
|
156
155
|
node: HTMLElement,
|
|
157
156
|
initialOptions: SurfaceTransferDropOptions,
|
|
158
157
|
): SurfaceTransferAction<SurfaceTransferDropOptions> {
|
|
159
158
|
let options = initialOptions;
|
|
160
159
|
|
|
161
|
-
function handleDragOver(event: DragEvent): void {
|
|
162
|
-
if (readPayload(event.dataTransfer) === null) return;
|
|
163
|
-
event.preventDefault();
|
|
164
|
-
if (event.dataTransfer) event.dataTransfer.dropEffect = "move";
|
|
165
|
-
}
|
|
166
|
-
|
|
167
160
|
function handleDrop(event: DragEvent): void {
|
|
168
161
|
const payload = readPayload(event.dataTransfer);
|
|
169
|
-
if (payload === null)
|
|
162
|
+
if (payload === null) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
170
165
|
event.preventDefault();
|
|
171
166
|
let request: SurfaceTransferCommand;
|
|
172
167
|
try {
|
|
@@ -174,22 +169,26 @@ export function surfaceTransferDrop(
|
|
|
174
169
|
protocol_version: TRANSFER_PROTOCOL_VERSION,
|
|
175
170
|
request_id: options.nextRequestId(),
|
|
176
171
|
session_id: payload.session_id,
|
|
177
|
-
selector:
|
|
172
|
+
selector: resolveDropSelector(options.selector, () => ({
|
|
173
|
+
x: event.screenX,
|
|
174
|
+
y: event.screenY,
|
|
175
|
+
})),
|
|
178
176
|
};
|
|
179
177
|
} catch (error) {
|
|
180
178
|
reportError(options.reportError, error);
|
|
181
|
-
|
|
179
|
+
notify(options.onTerminal, undefined, options.reportError);
|
|
182
180
|
return;
|
|
183
181
|
}
|
|
184
|
-
void options.state
|
|
182
|
+
void options.state
|
|
183
|
+
.commit(request)
|
|
185
184
|
.then((response) => {
|
|
186
185
|
notify(options.onResponse, response, options.reportError);
|
|
187
186
|
})
|
|
188
|
-
.catch((error) => {
|
|
187
|
+
.catch((error: unknown) => {
|
|
189
188
|
reportError(options.reportError, error);
|
|
190
189
|
})
|
|
191
190
|
.finally(() => {
|
|
192
|
-
|
|
191
|
+
notify(options.onTerminal, undefined, options.reportError);
|
|
193
192
|
});
|
|
194
193
|
}
|
|
195
194
|
|
|
@@ -214,68 +213,18 @@ export class StaleSurfaceTransferPreparationError extends Error {
|
|
|
214
213
|
}
|
|
215
214
|
|
|
216
215
|
function readPayload(dataTransfer: DataTransfer | null | undefined) {
|
|
217
|
-
if (dataTransfer
|
|
218
|
-
const raw = dataTransfer.getData(LONGHORN_TRANSFER_MIME_TYPE);
|
|
219
|
-
if (raw.length === 0) return null;
|
|
220
|
-
try {
|
|
221
|
-
return parseTransferPayload(raw);
|
|
222
|
-
} catch {
|
|
216
|
+
if (dataTransfer === null || dataTransfer === undefined) {
|
|
223
217
|
return null;
|
|
224
218
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
selector: SurfaceDropSelector,
|
|
229
|
-
event: DragEvent,
|
|
230
|
-
): TransferCommitSelector {
|
|
231
|
-
if (selector.kind === "explicit_zone") {
|
|
232
|
-
return {
|
|
233
|
-
kind: "explicit_zone",
|
|
234
|
-
drop_zone_id: selector.dropZoneId,
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
if (
|
|
238
|
-
!Number.isSafeInteger(event.screenX) ||
|
|
239
|
-
!Number.isSafeInteger(event.screenY)
|
|
240
|
-
) {
|
|
241
|
-
throw new RangeError("drop screen point must use integer screen DIPs");
|
|
219
|
+
const raw = dataTransfer.getData(LONGHORN_TRANSFER_MIME_TYPE);
|
|
220
|
+
if (raw.length === 0) {
|
|
221
|
+
return null;
|
|
242
222
|
}
|
|
243
|
-
return {
|
|
244
|
-
kind: "screen_point",
|
|
245
|
-
point: { x: event.screenX, y: event.screenY },
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function reportError(
|
|
250
|
-
reporter: (error: unknown) => void,
|
|
251
|
-
error: unknown,
|
|
252
|
-
): void {
|
|
253
223
|
try {
|
|
254
|
-
|
|
224
|
+
return parseTransferPayload(raw);
|
|
255
225
|
} catch {
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function notify<T>(
|
|
261
|
-
listener: ((value: T) => void) | undefined,
|
|
262
|
-
value: T,
|
|
263
|
-
reporter: (error: unknown) => void,
|
|
264
|
-
): void {
|
|
265
|
-
try {
|
|
266
|
-
listener?.(value);
|
|
267
|
-
} catch (error) {
|
|
268
|
-
reportError(reporter, error);
|
|
226
|
+
return null;
|
|
269
227
|
}
|
|
270
228
|
}
|
|
271
229
|
|
|
272
|
-
function
|
|
273
|
-
listener: (() => void) | undefined,
|
|
274
|
-
reporter: (error: unknown) => void,
|
|
275
|
-
): void {
|
|
276
|
-
try {
|
|
277
|
-
listener?.();
|
|
278
|
-
} catch (error) {
|
|
279
|
-
reportError(reporter, error);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
230
|
+
function noop(): void {}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { SurfaceTransferClient } from "@inflatable-cookie/longhorn/surface-transfer";
|
|
2
|
+
import type {
|
|
3
|
+
SurfaceSessionResponse,
|
|
4
|
+
SurfaceSessionStartRequest,
|
|
5
|
+
SurfaceTransferCommand,
|
|
6
|
+
SurfaceTransferResponse,
|
|
7
7
|
} from "@inflatable-cookie/longhorn/surface-transfer";
|
|
8
8
|
import type { DragSessionId } from "@inflatable-cookie/longhorn/transfer";
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from "./
|
|
10
|
+
import { UnsupportedCapabilityError } from "./index.ts";
|
|
11
|
+
import type { ClientStatus } from "./index.ts";
|
|
12
|
+
import { StartStop } from "./start-stop.ts";
|
|
13
|
+
import { throwTeardownFailures } from "./teardown.ts";
|
|
14
14
|
|
|
15
15
|
export type SurfaceTransferPreparationState =
|
|
16
16
|
| { readonly status: "idle" }
|
|
@@ -50,8 +50,8 @@ export class SurfaceTransferState {
|
|
|
50
50
|
});
|
|
51
51
|
#activeSessionId: DragSessionId | undefined;
|
|
52
52
|
#generation = 0;
|
|
53
|
-
#tasks = new Set<Promise<unknown>>();
|
|
54
|
-
#
|
|
53
|
+
readonly #tasks = new Set<Promise<unknown>>();
|
|
54
|
+
readonly #lifetime = new StartStop();
|
|
55
55
|
#stopped = true;
|
|
56
56
|
#destroyed = false;
|
|
57
57
|
|
|
@@ -73,133 +73,127 @@ export class SurfaceTransferState {
|
|
|
73
73
|
|
|
74
74
|
async start(): Promise<void> {
|
|
75
75
|
this.#assertAlive();
|
|
76
|
-
this.#
|
|
77
|
-
|
|
78
|
-
this.#
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
76
|
+
await this.#lifetime.start(async () => {
|
|
77
|
+
this.#stopped = false;
|
|
78
|
+
this.#status =
|
|
79
|
+
this.#options.client === undefined
|
|
80
|
+
? {
|
|
81
|
+
kind: "unsupported",
|
|
82
|
+
reason:
|
|
83
|
+
this.#options.unsupportedReason ?? "Surface transfer capability is unavailable",
|
|
84
|
+
}
|
|
85
|
+
: { kind: "ready" };
|
|
86
|
+
});
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
async prepare(
|
|
89
|
-
request: SurfaceSessionStartRequest,
|
|
90
|
-
): Promise<SurfaceSessionResponse> {
|
|
89
|
+
async prepare(request: SurfaceSessionStartRequest): Promise<SurfaceSessionResponse> {
|
|
91
90
|
this.#assertRunning();
|
|
91
|
+
return this.#track(this.#prepare(request));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async #prepare(request: SurfaceSessionStartRequest): Promise<SurfaceSessionResponse> {
|
|
92
95
|
const client = this.#requireClient();
|
|
93
|
-
await this.#cancelActive();
|
|
94
96
|
const generation = ++this.#generation;
|
|
95
|
-
this.#
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
97
|
+
await this.#cancelActive();
|
|
98
|
+
if (generation === this.#generation) {
|
|
99
|
+
this.#preparation = { status: "preparing", request };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return client
|
|
103
|
+
.start(request)
|
|
104
|
+
.then(async (response) => {
|
|
105
|
+
if (generation !== this.#generation || this.#stopped) {
|
|
106
|
+
if (response.status === "started") {
|
|
107
|
+
await this.#options.cancelSession(response.session.payload.session_id);
|
|
108
|
+
}
|
|
109
|
+
return response;
|
|
103
110
|
}
|
|
111
|
+
this.#preparation = {
|
|
112
|
+
status: response.status === "started" ? "prepared" : "aborted",
|
|
113
|
+
response,
|
|
114
|
+
};
|
|
115
|
+
this.#activeSessionId =
|
|
116
|
+
response.status === "started" ? response.session.payload.session_id : undefined;
|
|
104
117
|
return response;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
response.status === "started"
|
|
113
|
-
? response.session.payload.session_id
|
|
114
|
-
: undefined;
|
|
115
|
-
return response;
|
|
116
|
-
}).catch((error) => {
|
|
117
|
-
if (generation === this.#generation && !this.#stopped) {
|
|
118
|
-
this.#preparation = { status: "failed", error };
|
|
119
|
-
this.#status = { kind: "failed", error };
|
|
120
|
-
}
|
|
121
|
-
throw error;
|
|
122
|
-
});
|
|
123
|
-
this.#track(task);
|
|
124
|
-
return task;
|
|
118
|
+
})
|
|
119
|
+
.catch((error: unknown) => {
|
|
120
|
+
if (generation === this.#generation && !this.#stopped) {
|
|
121
|
+
this.#preparation = { status: "failed", error };
|
|
122
|
+
}
|
|
123
|
+
throw error;
|
|
124
|
+
});
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
async commit(
|
|
128
|
-
request: SurfaceTransferCommand,
|
|
129
|
-
): Promise<SurfaceTransferResponse> {
|
|
127
|
+
async commit(request: SurfaceTransferCommand): Promise<SurfaceTransferResponse> {
|
|
130
128
|
this.#assertRunning();
|
|
129
|
+
return this.#track(this.#commit(request));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async #commit(request: SurfaceTransferCommand): Promise<SurfaceTransferResponse> {
|
|
131
133
|
const client = this.#requireClient();
|
|
132
134
|
this.#completion = { status: "committing", request };
|
|
133
135
|
try {
|
|
134
136
|
const response = await client.commit(request);
|
|
135
137
|
this.#completion = {
|
|
136
|
-
status:
|
|
137
|
-
response.status === "committed" ? "committed" : "aborted",
|
|
138
|
+
status: response.status === "committed" ? "committed" : "aborted",
|
|
138
139
|
response,
|
|
139
140
|
};
|
|
140
141
|
if (
|
|
141
|
-
|
|
142
|
-
response.abort.session_consumed
|
|
142
|
+
this.#activeSessionId === request.session_id &&
|
|
143
|
+
(response.status === "committed" || response.abort.session_consumed)
|
|
143
144
|
) {
|
|
144
145
|
this.#activeSessionId = undefined;
|
|
145
146
|
}
|
|
146
147
|
return response;
|
|
147
148
|
} catch (error) {
|
|
148
149
|
this.#completion = { status: "failed", error };
|
|
149
|
-
this.#status = { kind: "failed", error };
|
|
150
150
|
throw error;
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
cancelPreparation(): Promise<void> {
|
|
155
|
-
++this.#generation;
|
|
156
|
-
return this.#cancelActive();
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
stop(): Promise<void> {
|
|
160
|
-
if (this.#stopTask !== undefined) {
|
|
161
|
-
return this.#stopTask;
|
|
162
|
-
}
|
|
163
155
|
if (this.#stopped) {
|
|
164
156
|
return Promise.resolve();
|
|
165
157
|
}
|
|
166
|
-
this.#
|
|
167
|
-
|
|
168
|
-
});
|
|
169
|
-
return this.#stopTask;
|
|
158
|
+
++this.#generation;
|
|
159
|
+
return this.#track(this.#cancelActive());
|
|
170
160
|
}
|
|
171
161
|
|
|
172
|
-
|
|
173
|
-
this.#
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
162
|
+
stop(): Promise<void> {
|
|
163
|
+
return this.#lifetime.stop(async () => {
|
|
164
|
+
this.#stopped = true;
|
|
165
|
+
++this.#generation;
|
|
166
|
+
this.#status = { kind: "idle" };
|
|
167
|
+
const results = await Promise.allSettled([this.#cancelActive(), ...this.#tasks]);
|
|
168
|
+
this.#status = { kind: "idle" };
|
|
169
|
+
this.#preparation = { status: "idle" };
|
|
170
|
+
this.#completion = { status: "idle" };
|
|
171
|
+
throwTeardownFailures(results);
|
|
172
|
+
});
|
|
180
173
|
}
|
|
181
174
|
|
|
182
175
|
async destroy(): Promise<void> {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
this.#destroyed = true;
|
|
186
|
-
}
|
|
176
|
+
this.#destroyed = true;
|
|
177
|
+
await this.stop();
|
|
187
178
|
}
|
|
188
179
|
|
|
189
180
|
async #cancelActive(): Promise<void> {
|
|
190
181
|
const sessionId = this.#activeSessionId;
|
|
191
182
|
this.#activeSessionId = undefined;
|
|
183
|
+
this.#preparation = { status: "idle" };
|
|
192
184
|
if (sessionId !== undefined) {
|
|
193
185
|
await this.#options.cancelSession(sessionId);
|
|
194
186
|
}
|
|
195
|
-
this.#preparation = { status: "idle" };
|
|
196
187
|
}
|
|
197
188
|
|
|
198
|
-
#track(task: Promise<
|
|
189
|
+
#track<Result>(task: Promise<Result>): Promise<Result> {
|
|
199
190
|
this.#tasks.add(task);
|
|
200
|
-
void task
|
|
201
|
-
|
|
202
|
-
|
|
191
|
+
void task
|
|
192
|
+
.finally(() => {
|
|
193
|
+
this.#tasks.delete(task);
|
|
194
|
+
})
|
|
195
|
+
.catch(() => undefined);
|
|
196
|
+
return task;
|
|
203
197
|
}
|
|
204
198
|
|
|
205
199
|
#requireClient(): SurfaceTransferClient {
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { SurfaceClient } from "@inflatable-cookie/longhorn/surfaces";
|
|
2
|
+
import type {
|
|
3
|
+
SurfaceMutationRequest,
|
|
4
|
+
SurfaceMutationResponse,
|
|
5
|
+
SurfaceSnapshot,
|
|
6
6
|
} from "@inflatable-cookie/longhorn/surfaces";
|
|
7
7
|
|
|
8
8
|
import {
|
|
9
9
|
OptimisticProjectionState,
|
|
10
10
|
ReactiveClientState,
|
|
11
11
|
UnsupportedCapabilityError,
|
|
12
|
-
type ClientStatus,
|
|
13
|
-
type OptimisticProjector,
|
|
14
12
|
} from "./index.ts";
|
|
13
|
+
import type { ClientStatus, OptimisticProjector } from "./index.ts";
|
|
15
14
|
|
|
16
15
|
export type SurfaceMutationOperation =
|
|
17
16
|
| { readonly requestId: string; readonly status: "pending" }
|
|
@@ -31,13 +30,15 @@ export interface SurfaceStateOptions {
|
|
|
31
30
|
readonly unsupportedReason?: string;
|
|
32
31
|
}
|
|
33
32
|
|
|
33
|
+
const RECENT_OPERATION_LIMIT = 32;
|
|
34
|
+
|
|
34
35
|
export class SurfaceState {
|
|
35
36
|
readonly #client: SurfaceClient | undefined;
|
|
36
37
|
readonly #lifecycle: ReactiveClientState<SurfaceSnapshot>;
|
|
37
|
-
readonly #projection = new OptimisticProjectionState<
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
#operations = $state.raw<
|
|
38
|
+
readonly #projection = new OptimisticProjectionState<SurfaceSnapshot["document"]>(
|
|
39
|
+
(document) => document.revision,
|
|
40
|
+
);
|
|
41
|
+
#operations = $state.raw<ReadonlyArray<SurfaceMutationOperation>>([]);
|
|
41
42
|
#epoch: number | undefined;
|
|
42
43
|
|
|
43
44
|
constructor(options: SurfaceStateOptions) {
|
|
@@ -48,14 +49,11 @@ export class SurfaceState {
|
|
|
48
49
|
client === undefined
|
|
49
50
|
? {
|
|
50
51
|
kind: "unsupported",
|
|
51
|
-
reason:
|
|
52
|
-
options.unsupportedReason ??
|
|
53
|
-
"Surface capability is unavailable",
|
|
52
|
+
reason: options.unsupportedReason ?? "Surface capability is unavailable",
|
|
54
53
|
}
|
|
55
54
|
: {
|
|
56
55
|
kind: "supported",
|
|
57
|
-
connect: (listener, onFailure) =>
|
|
58
|
-
client.subscribe(listener, onFailure),
|
|
56
|
+
connect: (listener, onFailure) => client.subscribe(listener, onFailure),
|
|
59
57
|
},
|
|
60
58
|
onSnapshot: (snapshot) => this.#acceptSnapshot(snapshot),
|
|
61
59
|
});
|
|
@@ -82,11 +80,11 @@ export class SurfaceState {
|
|
|
82
80
|
return this.#projection.projected;
|
|
83
81
|
}
|
|
84
82
|
|
|
85
|
-
get pendingRequestIds():
|
|
83
|
+
get pendingRequestIds(): ReadonlyArray<string> {
|
|
86
84
|
return this.#projection.pendingRequestIds;
|
|
87
85
|
}
|
|
88
86
|
|
|
89
|
-
get operations():
|
|
87
|
+
get operations(): ReadonlyArray<SurfaceMutationOperation> {
|
|
90
88
|
return this.#operations;
|
|
91
89
|
}
|
|
92
90
|
|
|
@@ -113,16 +111,20 @@ export class SurfaceState {
|
|
|
113
111
|
|
|
114
112
|
const epoch = this.#epoch;
|
|
115
113
|
this.#projection.begin(request.request_id, project);
|
|
116
|
-
|
|
114
|
+
const pending = {
|
|
117
115
|
requestId: request.request_id,
|
|
118
116
|
status: "pending",
|
|
119
|
-
}
|
|
117
|
+
} satisfies SurfaceMutationOperation;
|
|
118
|
+
this.#setOperation(pending);
|
|
120
119
|
try {
|
|
121
120
|
const response = await client.mutate(request);
|
|
122
121
|
const document =
|
|
123
122
|
response.status === "committed"
|
|
124
123
|
? response.receipt.authoritative_document
|
|
125
124
|
: response.rejection.authoritative_document;
|
|
125
|
+
if (!this.#operations.includes(pending)) {
|
|
126
|
+
return response;
|
|
127
|
+
}
|
|
126
128
|
if (epoch !== undefined && this.#epoch === epoch) {
|
|
127
129
|
this.#projection.settle(request.request_id, document);
|
|
128
130
|
} else {
|
|
@@ -135,13 +137,10 @@ export class SurfaceState {
|
|
|
135
137
|
});
|
|
136
138
|
return response;
|
|
137
139
|
} catch (error) {
|
|
138
|
-
this.#
|
|
139
|
-
|
|
140
|
-
requestId: request.request_id,
|
|
141
|
-
|
|
142
|
-
error,
|
|
143
|
-
});
|
|
144
|
-
this.#lifecycle.fail(error);
|
|
140
|
+
if (this.#operations.includes(pending)) {
|
|
141
|
+
this.#projection.cancel(request.request_id);
|
|
142
|
+
this.#setOperation({ requestId: request.request_id, status: "failed", error });
|
|
143
|
+
}
|
|
145
144
|
throw error;
|
|
146
145
|
}
|
|
147
146
|
}
|
|
@@ -169,11 +168,15 @@ export class SurfaceState {
|
|
|
169
168
|
}
|
|
170
169
|
|
|
171
170
|
#setOperation(operation: SurfaceMutationOperation): void {
|
|
172
|
-
|
|
173
|
-
...this.#operations.filter(
|
|
174
|
-
({ requestId }) => requestId !== operation.requestId,
|
|
175
|
-
),
|
|
171
|
+
const operations = [
|
|
172
|
+
...this.#operations.filter(({ requestId }) => requestId !== operation.requestId),
|
|
176
173
|
operation,
|
|
177
174
|
];
|
|
175
|
+
const settled = operations.filter(({ status }) => status !== "pending");
|
|
176
|
+
if (settled.length > RECENT_OPERATION_LIMIT) {
|
|
177
|
+
const oldest = operations.findIndex(({ status }) => status !== "pending");
|
|
178
|
+
operations.splice(oldest, 1);
|
|
179
|
+
}
|
|
180
|
+
this.#operations = operations;
|
|
178
181
|
}
|
|
179
182
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Attempt every cleanup step and preserve the original error when only one fails.
|
|
2
|
+
export function throwTeardownFailures(results: ReadonlyArray<PromiseSettledResult<unknown>>): void {
|
|
3
|
+
const failures: Array<unknown> = [];
|
|
4
|
+
for (const result of results) {
|
|
5
|
+
if (result.status === "rejected") {
|
|
6
|
+
failures.push(result.reason);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
if (failures.length === 1) {
|
|
10
|
+
// Preserve the host rejection, including serialized failures.
|
|
11
|
+
throw failures[0];
|
|
12
|
+
}
|
|
13
|
+
if (failures.length > 1) {
|
|
14
|
+
throw new AggregateError(failures, "Multiple cleanup steps failed");
|
|
15
|
+
}
|
|
16
|
+
}
|