@inflatable-cookie/longhorn-poodle-svelte 0.1.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/LICENSE +21 -0
- package/README.md +37 -0
- package/package.json +171 -0
- package/src/bindings/index.ts +4 -0
- package/src/bindings/layout.svelte.ts +143 -0
- package/src/bindings/lifecycle.svelte.ts +246 -0
- package/src/bindings/optimistic.svelte.ts +96 -0
- package/src/bindings/scheduler.ts +13 -0
- package/src/bindings/surface-transfer-actions.ts +281 -0
- package/src/bindings/surface-transfer.svelte.ts +230 -0
- package/src/bindings/surfaces.svelte.ts +179 -0
- package/src/bindings/transfer-actions.ts +240 -0
- package/src/bindings/transfer-model.ts +138 -0
- package/src/bindings/transfer.svelte.ts +388 -0
- package/src/bindings/window-chrome.ts +63 -0
- package/src/bindings/window-drag.ts +161 -0
- package/src/commands/poodle/CommandPaletteBinding.svelte +51 -0
- package/src/commands/poodle/KeybindingSettings.svelte +138 -0
- package/src/commands/poodle/projectors.ts +37 -0
- package/src/commands/poodle.ts +9 -0
- package/src/commands/svelte/lifecycle.ts +13 -0
- package/src/commands/svelte/session.svelte.ts +212 -0
- package/src/commands/svelte.ts +2 -0
- package/src/config/poodle/BackupSettingsPage.svelte +295 -0
- package/src/config/poodle/RestoreSettingsPage.svelte +535 -0
- package/src/config/poodle/StorageSettingsPage.svelte +361 -0
- package/src/config/poodle/restore-model.ts +63 -0
- package/src/config/poodle/types.ts +11 -0
- package/src/config/poodle.ts +8 -0
- package/src/history/svelte/lifecycle.ts +13 -0
- package/src/history/svelte/session.svelte.ts +97 -0
- package/src/history/svelte.ts +2 -0
- package/src/history-tree/svelte/lifecycle.ts +11 -0
- package/src/history-tree/svelte/session.svelte.ts +27 -0
- package/src/history-tree/svelte.ts +3 -0
- package/src/licence/poodle/LicenceActivation.svelte +62 -0
- package/src/licence/poodle/LicenceSeats.svelte +57 -0
- package/src/licence/poodle/LicenceStatus.svelte +34 -0
- package/src/licence/poodle/LicenceSurface.svelte.ts +43 -0
- package/src/licence/poodle.ts +3 -0
- package/src/native-content/index.ts +4 -0
- package/src/native-content/lifecycle.ts +13 -0
- package/src/native-content/session.svelte.ts +526 -0
- package/src/native-content/viewport.ts +27 -0
- package/src/native-content/visibility.ts +19 -0
- package/src/notifications/poodle/NotificationPanel.svelte +66 -0
- package/src/notifications/poodle/NotificationToastHost.svelte +40 -0
- package/src/notifications/poodle/NotificationToastStack.svelte +22 -0
- package/src/notifications/poodle/projectors.ts +30 -0
- package/src/notifications/poodle.ts +4 -0
- package/src/notifications/svelte/lifecycle.ts +11 -0
- package/src/notifications/svelte/session.svelte.ts +57 -0
- package/src/notifications/svelte.ts +2 -0
- package/src/operation/poodle/OperationPanel.svelte +200 -0
- package/src/operation/poodle/projectors.ts +78 -0
- package/src/operation/poodle.ts +2 -0
- package/src/operation/svelte/lifecycle.ts +13 -0
- package/src/operation/svelte/session.svelte.ts +80 -0
- package/src/operation/svelte.ts +2 -0
- package/src/poodle/LayoutDockRegion.svelte +148 -0
- package/src/poodle/LayoutSplitView.svelte +120 -0
- package/src/poodle/LayoutTabs.svelte +91 -0
- package/src/poodle/binding.ts +401 -0
- package/src/poodle/document.ts +117 -0
- package/src/poodle/index.ts +8 -0
- package/src/poodle/projectors.ts +125 -0
- package/src/poodle/transfer.ts +188 -0
- package/src/poodle/types.ts +89 -0
- package/src/settings/poodle/SettingsShell.svelte +382 -0
- package/src/settings/poodle.ts +2 -0
- package/src/settings/svelte/context.ts +61 -0
- package/src/settings/svelte/document.svelte.ts +202 -0
- package/src/settings/svelte/errors.ts +84 -0
- package/src/settings/svelte/lifecycle.ts +18 -0
- package/src/settings/svelte/page.svelte.ts +266 -0
- package/src/settings/svelte/runtime.svelte.ts +187 -0
- package/src/settings/svelte/scope.svelte.ts +123 -0
- package/src/settings/svelte/session.svelte.ts +288 -0
- package/src/settings/svelte/types.ts +90 -0
- package/src/settings/svelte.ts +6 -0
- package/src/svelte.d.ts +6 -0
- package/src/update/poodle/UpdateCentre.svelte +64 -0
- package/src/update/poodle/UpdateSettings.svelte +64 -0
- package/src/update/poodle/UpdateStatus.svelte +51 -0
- package/src/update/poodle/UpdateSurface.svelte.ts +88 -0
- package/src/update/poodle.ts +3 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SettingsActivationRequirement,
|
|
3
|
+
SettingsApplyUnitId,
|
|
4
|
+
SettingsEntryId,
|
|
5
|
+
SettingsOpaqueValue,
|
|
6
|
+
SettingsPageDefinition,
|
|
7
|
+
SettingsRecoveryState,
|
|
8
|
+
SettingsScopeId,
|
|
9
|
+
SettingsScopeSnapshot,
|
|
10
|
+
} from "@inflatable-cookie/longhorn/settings/protocol";
|
|
11
|
+
import type { SettingsPageSession } from "./page.svelte.ts";
|
|
12
|
+
import type {
|
|
13
|
+
SettingsPageRenderContext,
|
|
14
|
+
SettingsRoute,
|
|
15
|
+
} from "./types.ts";
|
|
16
|
+
|
|
17
|
+
export interface SettingsContextHost {
|
|
18
|
+
readonly activationRequirements: readonly SettingsActivationRequirement[];
|
|
19
|
+
readonly recovery: SettingsRecoveryState | null;
|
|
20
|
+
scopeSnapshot(scopeId: SettingsScopeId): SettingsScopeSnapshot | undefined;
|
|
21
|
+
requestReset(
|
|
22
|
+
applyUnitId: SettingsApplyUnitId,
|
|
23
|
+
entryIds: readonly SettingsEntryId[],
|
|
24
|
+
): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createSettingsPageRenderContext(
|
|
28
|
+
host: SettingsContextHost,
|
|
29
|
+
page: SettingsPageDefinition,
|
|
30
|
+
route: SettingsRoute,
|
|
31
|
+
session: SettingsPageSession,
|
|
32
|
+
): SettingsPageRenderContext {
|
|
33
|
+
return {
|
|
34
|
+
page,
|
|
35
|
+
route,
|
|
36
|
+
get dirty() {
|
|
37
|
+
return session.dirty;
|
|
38
|
+
},
|
|
39
|
+
get busy() {
|
|
40
|
+
return session.busy;
|
|
41
|
+
},
|
|
42
|
+
get activationRequirements() {
|
|
43
|
+
return host.activationRequirements;
|
|
44
|
+
},
|
|
45
|
+
get recovery() {
|
|
46
|
+
return host.recovery;
|
|
47
|
+
},
|
|
48
|
+
snapshot: (scopeId) => host.scopeSnapshot(scopeId),
|
|
49
|
+
unitStatus: (applyUnitId) => session.status(applyUnitId),
|
|
50
|
+
draft: (applyUnitId) => session.draft(applyUnitId),
|
|
51
|
+
change: (
|
|
52
|
+
applyUnitId: SettingsApplyUnitId,
|
|
53
|
+
intent: SettingsOpaqueValue,
|
|
54
|
+
) => session.change(applyUnitId, intent),
|
|
55
|
+
apply: (applyUnitId) => session.apply(applyUnitId),
|
|
56
|
+
cancel: () => session.cancel(),
|
|
57
|
+
requestReset: (applyUnitId, entryIds) =>
|
|
58
|
+
host.requestReset(applyUnitId, entryIds),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import type { SettingsClient } from "@inflatable-cookie/longhorn/settings";
|
|
2
|
+
import type {
|
|
3
|
+
SettingsApplyUnitDefinition,
|
|
4
|
+
SettingsPageDefinition,
|
|
5
|
+
SettingsRegistrySnapshot,
|
|
6
|
+
SettingsRequestId,
|
|
7
|
+
SettingsScopeId,
|
|
8
|
+
SettingsScopeSnapshot,
|
|
9
|
+
} from "@inflatable-cookie/longhorn/settings/protocol";
|
|
10
|
+
import {
|
|
11
|
+
projectSettingsRegistry,
|
|
12
|
+
resolveSettingsDeepLink,
|
|
13
|
+
type SettingsRegistryProjection,
|
|
14
|
+
} from "@inflatable-cookie/longhorn/settings";
|
|
15
|
+
import {
|
|
16
|
+
MissingSettingsRendererError,
|
|
17
|
+
SettingsRegistryUnavailableError,
|
|
18
|
+
SettingsScopeNotLoadedError,
|
|
19
|
+
} from "./errors.ts";
|
|
20
|
+
import {
|
|
21
|
+
SettingsPageSession,
|
|
22
|
+
type SettingsPageSessionHost,
|
|
23
|
+
} from "./page.svelte.ts";
|
|
24
|
+
import { SettingsScopeState } from "./scope.svelte.ts";
|
|
25
|
+
import type {
|
|
26
|
+
SettingsPageRenderer,
|
|
27
|
+
SettingsRendererResolver,
|
|
28
|
+
SettingsRoute,
|
|
29
|
+
} from "./types.ts";
|
|
30
|
+
|
|
31
|
+
export interface SettingsDocumentOptions {
|
|
32
|
+
readonly client: SettingsClient;
|
|
33
|
+
readonly nextRequestId: () => SettingsRequestId;
|
|
34
|
+
readonly onScopeFailure: (error: unknown) => void;
|
|
35
|
+
readonly onMutationError: (error: unknown) => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class SettingsSessionDocument {
|
|
39
|
+
readonly #options: SettingsDocumentOptions;
|
|
40
|
+
#registry = $state.raw<SettingsRegistrySnapshot | undefined>(undefined);
|
|
41
|
+
#projection = $state.raw<SettingsRegistryProjection | undefined>(undefined);
|
|
42
|
+
#route = $state.raw<SettingsRoute | undefined>(undefined);
|
|
43
|
+
#focusRevision = $state(0);
|
|
44
|
+
#renderers = new Map<string, SettingsPageRenderer>();
|
|
45
|
+
#scopes = new Map<SettingsScopeId, SettingsScopeState>();
|
|
46
|
+
#pages = new Map<string, SettingsPageSession>();
|
|
47
|
+
|
|
48
|
+
constructor(options: SettingsDocumentOptions) {
|
|
49
|
+
this.#options = options;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get registry(): SettingsRegistrySnapshot | undefined {
|
|
53
|
+
return this.#registry;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get projection(): SettingsRegistryProjection | undefined {
|
|
57
|
+
return this.#projection;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get route(): SettingsRoute | undefined {
|
|
61
|
+
return this.#route;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get focusRevision(): number {
|
|
65
|
+
return this.#focusRevision;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get currentPage(): SettingsPageDefinition | undefined {
|
|
69
|
+
const pageId = this.#route?.pageId;
|
|
70
|
+
return this.#registry?.pages.find(({ id }) => id === pageId);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get currentRenderer(): SettingsPageRenderer | undefined {
|
|
74
|
+
const page = this.currentPage;
|
|
75
|
+
return page === undefined ? undefined : this.#renderers.get(page.id);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get currentPageSession(): SettingsPageSession | undefined {
|
|
79
|
+
const page = this.currentPage;
|
|
80
|
+
return page === undefined ? undefined : this.#pageSession(page);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
currentScopes(): SettingsScopeState[] {
|
|
84
|
+
return (
|
|
85
|
+
this.currentPage?.readableScopeIds.flatMap((scopeId) => {
|
|
86
|
+
const scope = this.#scopes.get(scopeId);
|
|
87
|
+
return scope === undefined ? [] : [scope];
|
|
88
|
+
}) ?? []
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
scopeSnapshot(scopeId: SettingsScopeId): SettingsScopeSnapshot | undefined {
|
|
93
|
+
return this.#scopes.get(scopeId)?.snapshot;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async installRegistry(
|
|
97
|
+
registry: SettingsRegistrySnapshot,
|
|
98
|
+
resolver: SettingsRendererResolver,
|
|
99
|
+
preferredRoute?: SettingsRoute,
|
|
100
|
+
): Promise<"ready" | "unsupported"> {
|
|
101
|
+
const renderers = new Map<string, SettingsPageRenderer>();
|
|
102
|
+
for (const page of registry.pages) {
|
|
103
|
+
const renderer = resolver(page.rendererId, page);
|
|
104
|
+
if (renderer === undefined) {
|
|
105
|
+
throw new MissingSettingsRendererError(page);
|
|
106
|
+
}
|
|
107
|
+
renderers.set(page.id, renderer);
|
|
108
|
+
}
|
|
109
|
+
const projection = projectSettingsRegistry(registry);
|
|
110
|
+
this.#registry = registry;
|
|
111
|
+
this.#projection = projection;
|
|
112
|
+
this.#renderers = renderers;
|
|
113
|
+
if (projection.pages.length === 0) {
|
|
114
|
+
return "unsupported";
|
|
115
|
+
}
|
|
116
|
+
const route =
|
|
117
|
+
preferredRoute !== undefined &&
|
|
118
|
+
registry.pages.some(({ id }) => id === preferredRoute.pageId)
|
|
119
|
+
? preferredRoute
|
|
120
|
+
: { pageId: projection.pages[0]!.id };
|
|
121
|
+
await this.installRoute(route);
|
|
122
|
+
return "ready";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async installRoute(route: SettingsRoute): Promise<void> {
|
|
126
|
+
const registry = this.requiredRegistry();
|
|
127
|
+
const { page } = resolveSettingsDeepLink(registry, route);
|
|
128
|
+
await Promise.all(
|
|
129
|
+
page.readableScopeIds.map((scopeId) =>
|
|
130
|
+
this.#scopeState(registry, scopeId).start(),
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
this.#route = route;
|
|
134
|
+
this.#pageSession(page);
|
|
135
|
+
this.#focusRevision += 1;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async clearAuthority(): Promise<void> {
|
|
139
|
+
for (const page of this.#pages.values()) {
|
|
140
|
+
page.stop();
|
|
141
|
+
}
|
|
142
|
+
this.#pages.clear();
|
|
143
|
+
const scopes = [...this.#scopes.values()];
|
|
144
|
+
this.#scopes.clear();
|
|
145
|
+
await Promise.all(scopes.map((scope) => scope.stop()));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async stop(): Promise<void> {
|
|
149
|
+
await this.clearAuthority();
|
|
150
|
+
this.#registry = undefined;
|
|
151
|
+
this.#projection = undefined;
|
|
152
|
+
this.#route = undefined;
|
|
153
|
+
this.#renderers.clear();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
requiredRegistry(): SettingsRegistrySnapshot {
|
|
157
|
+
if (this.#registry === undefined) {
|
|
158
|
+
throw new SettingsRegistryUnavailableError();
|
|
159
|
+
}
|
|
160
|
+
return this.#registry;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
#scopeState(
|
|
164
|
+
registry: SettingsRegistrySnapshot,
|
|
165
|
+
scopeId: SettingsScopeId,
|
|
166
|
+
): SettingsScopeState {
|
|
167
|
+
const existing = this.#scopes.get(scopeId);
|
|
168
|
+
if (existing !== undefined) return existing;
|
|
169
|
+
const state = new SettingsScopeState({
|
|
170
|
+
client: this.#options.client,
|
|
171
|
+
registry,
|
|
172
|
+
scopeId,
|
|
173
|
+
nextRequestId: this.#options.nextRequestId,
|
|
174
|
+
onFailure: (failure) => this.#options.onScopeFailure(failure.error),
|
|
175
|
+
});
|
|
176
|
+
this.#scopes.set(scopeId, state);
|
|
177
|
+
return state;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#pageSession(page: SettingsPageDefinition): SettingsPageSession {
|
|
181
|
+
const existing = this.#pages.get(page.id);
|
|
182
|
+
if (existing !== undefined) return existing;
|
|
183
|
+
const host: SettingsPageSessionHost = {
|
|
184
|
+
client: this.#options.client,
|
|
185
|
+
registry: () => this.requiredRegistry(),
|
|
186
|
+
scope: (unit) => this.#requiredScope(unit),
|
|
187
|
+
nextRequestId: this.#options.nextRequestId,
|
|
188
|
+
onError: this.#options.onMutationError,
|
|
189
|
+
};
|
|
190
|
+
const session = new SettingsPageSession(page, host);
|
|
191
|
+
this.#pages.set(page.id, session);
|
|
192
|
+
return session;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
#requiredScope(unit: SettingsApplyUnitDefinition): SettingsScopeState {
|
|
196
|
+
const scope = this.#scopes.get(unit.scopeId);
|
|
197
|
+
if (scope === undefined) {
|
|
198
|
+
throw new SettingsScopeNotLoadedError(unit.scopeId);
|
|
199
|
+
}
|
|
200
|
+
return scope;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { SettingsPageDefinition } from "@inflatable-cookie/longhorn/settings/protocol";
|
|
2
|
+
|
|
3
|
+
export class MissingSettingsRendererError extends Error {
|
|
4
|
+
readonly page: SettingsPageDefinition;
|
|
5
|
+
|
|
6
|
+
constructor(page: SettingsPageDefinition) {
|
|
7
|
+
super(
|
|
8
|
+
`settings renderer ${page.rendererId} is missing for page ${page.id}`,
|
|
9
|
+
);
|
|
10
|
+
this.name = "MissingSettingsRendererError";
|
|
11
|
+
this.page = page;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class SettingsApplyUnitUnavailableError extends Error {
|
|
16
|
+
constructor(applyUnitId: string) {
|
|
17
|
+
super(`settings apply unit ${applyUnitId} is unavailable on this page`);
|
|
18
|
+
this.name = "SettingsApplyUnitUnavailableError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class SettingsDraftUnavailableError extends Error {
|
|
23
|
+
constructor(applyUnitId: string) {
|
|
24
|
+
super(`settings apply unit ${applyUnitId} has no staged draft`);
|
|
25
|
+
this.name = "SettingsDraftUnavailableError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class SettingsMutationPendingError extends Error {
|
|
30
|
+
constructor() {
|
|
31
|
+
super("settings mutation is still pending");
|
|
32
|
+
this.name = "SettingsMutationPendingError";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class SettingsPageApplyAmbiguityError extends Error {
|
|
37
|
+
constructor(count: number) {
|
|
38
|
+
super(`page Apply cannot imply atomicity across ${count} apply units`);
|
|
39
|
+
this.name = "SettingsPageApplyAmbiguityError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class SettingsPageUnavailableError extends Error {
|
|
44
|
+
constructor() {
|
|
45
|
+
super("settings page is unavailable");
|
|
46
|
+
this.name = "SettingsPageUnavailableError";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class SettingsRegistryUnavailableError extends Error {
|
|
51
|
+
constructor() {
|
|
52
|
+
super("settings registry is unavailable");
|
|
53
|
+
this.name = "SettingsRegistryUnavailableError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class SettingsRendererResolverUnavailableError extends Error {
|
|
58
|
+
constructor() {
|
|
59
|
+
super("settings renderer resolver is unavailable");
|
|
60
|
+
this.name = "SettingsRendererResolverUnavailableError";
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class SettingsResetUnsupportedError extends Error {
|
|
65
|
+
constructor(applyUnitId: string) {
|
|
66
|
+
super(`settings apply unit ${applyUnitId} does not support reset`);
|
|
67
|
+
this.name = "SettingsResetUnsupportedError";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class SettingsScopeNotLoadedError extends Error {
|
|
72
|
+
constructor(scopeId: string) {
|
|
73
|
+
super(`settings scope ${scopeId} is not loaded for the current page`);
|
|
74
|
+
this.name = "SettingsScopeNotLoadedError";
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class SettingsScopeUnavailableError extends Error {
|
|
79
|
+
constructor(scopeId: string) {
|
|
80
|
+
super(`settings scope ${scopeId} has no current authority`);
|
|
81
|
+
this.name = "SettingsScopeUnavailableError";
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { onMount } from "svelte";
|
|
2
|
+
|
|
3
|
+
import type { SettingsSession } from "./session.svelte.ts";
|
|
4
|
+
import type { SettingsRendererResolver } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
export function useSettingsSession(
|
|
7
|
+
getSession: () => SettingsSession,
|
|
8
|
+
getRendererResolver: () => SettingsRendererResolver,
|
|
9
|
+
): void {
|
|
10
|
+
onMount(() => {
|
|
11
|
+
const session = getSession();
|
|
12
|
+
const rendererResolver = getRendererResolver();
|
|
13
|
+
void session.start(rendererResolver).catch(() => undefined);
|
|
14
|
+
return () => {
|
|
15
|
+
void session.stop();
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import type { SettingsClient } from "@inflatable-cookie/longhorn/settings";
|
|
2
|
+
import type {
|
|
3
|
+
SettingsApplyUnitDefinition,
|
|
4
|
+
SettingsApplyUnitId,
|
|
5
|
+
SettingsEntryId,
|
|
6
|
+
SettingsMutationResult,
|
|
7
|
+
SettingsOpaqueValue,
|
|
8
|
+
SettingsPageDefinition,
|
|
9
|
+
SettingsRegistrySnapshot,
|
|
10
|
+
SettingsRequestId,
|
|
11
|
+
} from "@inflatable-cookie/longhorn/settings/protocol";
|
|
12
|
+
import type { SettingsScopeState } from "./scope.svelte.ts";
|
|
13
|
+
import type { SettingsDraft, SettingsUnitStatus } from "./types.ts";
|
|
14
|
+
import {
|
|
15
|
+
SettingsApplyUnitUnavailableError,
|
|
16
|
+
SettingsDraftUnavailableError,
|
|
17
|
+
SettingsMutationPendingError,
|
|
18
|
+
SettingsPageApplyAmbiguityError,
|
|
19
|
+
SettingsResetUnsupportedError,
|
|
20
|
+
SettingsScopeUnavailableError,
|
|
21
|
+
} from "./errors.ts";
|
|
22
|
+
|
|
23
|
+
export interface SettingsPageSessionHost {
|
|
24
|
+
readonly client: SettingsClient;
|
|
25
|
+
registry(): SettingsRegistrySnapshot;
|
|
26
|
+
scope(applyUnit: SettingsApplyUnitDefinition): SettingsScopeState;
|
|
27
|
+
nextRequestId(): SettingsRequestId;
|
|
28
|
+
onError(error: unknown): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class SettingsPageSession {
|
|
32
|
+
readonly #page: SettingsPageDefinition;
|
|
33
|
+
readonly #host: SettingsPageSessionHost;
|
|
34
|
+
#drafts = $state.raw<ReadonlyMap<SettingsApplyUnitId, SettingsDraft>>(
|
|
35
|
+
new Map(),
|
|
36
|
+
);
|
|
37
|
+
#statuses = $state.raw<
|
|
38
|
+
ReadonlyMap<SettingsApplyUnitId, SettingsUnitStatus>
|
|
39
|
+
>(new Map());
|
|
40
|
+
#generation = 0;
|
|
41
|
+
|
|
42
|
+
constructor(page: SettingsPageDefinition, host: SettingsPageSessionHost) {
|
|
43
|
+
this.#page = page;
|
|
44
|
+
this.#host = host;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get dirty(): boolean {
|
|
48
|
+
return this.#drafts.size > 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get busy(): boolean {
|
|
52
|
+
return [...this.#statuses.values()].some(
|
|
53
|
+
({ kind }) => kind === "pending",
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get drafts(): readonly SettingsDraft[] {
|
|
58
|
+
return [...this.#drafts.values()];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
draft(applyUnitId: SettingsApplyUnitId): SettingsDraft | undefined {
|
|
62
|
+
return this.#drafts.get(applyUnitId);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
status(applyUnitId: SettingsApplyUnitId): SettingsUnitStatus {
|
|
66
|
+
return this.#statuses.get(applyUnitId) ?? { kind: "idle" };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async change(
|
|
70
|
+
applyUnitId: SettingsApplyUnitId,
|
|
71
|
+
intent: SettingsOpaqueValue,
|
|
72
|
+
): Promise<void> {
|
|
73
|
+
const unit = this.#applyUnit(applyUnitId);
|
|
74
|
+
if (unit.timing === "immediate") {
|
|
75
|
+
await this.#executeApply(unit, intent, false);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.#drafts = new Map(this.#drafts).set(applyUnitId, {
|
|
79
|
+
applyUnitId,
|
|
80
|
+
intent,
|
|
81
|
+
});
|
|
82
|
+
this.#setStatus(applyUnitId, { kind: "idle" });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async apply(applyUnitId: SettingsApplyUnitId): Promise<void> {
|
|
86
|
+
const unit = this.#applyUnit(applyUnitId);
|
|
87
|
+
const draft = this.#drafts.get(applyUnitId);
|
|
88
|
+
if (unit.timing !== "staged" || draft === undefined) {
|
|
89
|
+
throw new SettingsDraftUnavailableError(applyUnitId);
|
|
90
|
+
}
|
|
91
|
+
await this.#executeApply(unit, draft.intent, true);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async applyOnlyDraft(): Promise<boolean> {
|
|
95
|
+
if (this.#drafts.size !== 1) {
|
|
96
|
+
if (this.#drafts.size === 0) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
throw new SettingsPageApplyAmbiguityError(this.#drafts.size);
|
|
100
|
+
}
|
|
101
|
+
const draft = this.drafts[0]!;
|
|
102
|
+
await this.apply(draft.applyUnitId);
|
|
103
|
+
return this.status(draft.applyUnitId).kind === "saved";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
cancel(): void {
|
|
107
|
+
if (this.busy) {
|
|
108
|
+
throw new SettingsMutationPendingError();
|
|
109
|
+
}
|
|
110
|
+
const affected = [...this.#drafts.keys()];
|
|
111
|
+
this.#drafts = new Map();
|
|
112
|
+
const statuses = new Map(this.#statuses);
|
|
113
|
+
for (const applyUnitId of affected) {
|
|
114
|
+
statuses.set(applyUnitId, { kind: "idle" });
|
|
115
|
+
}
|
|
116
|
+
this.#statuses = statuses;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async reset(
|
|
120
|
+
applyUnitId: SettingsApplyUnitId,
|
|
121
|
+
entryIds: readonly SettingsEntryId[],
|
|
122
|
+
): Promise<void> {
|
|
123
|
+
const unit = this.#applyUnit(applyUnitId);
|
|
124
|
+
this.#assertNotPending(applyUnitId);
|
|
125
|
+
if (!unit.resetSupported) {
|
|
126
|
+
throw new SettingsResetUnsupportedError(applyUnitId);
|
|
127
|
+
}
|
|
128
|
+
const registry = this.#host.registry();
|
|
129
|
+
const scope = this.#host.scope(unit);
|
|
130
|
+
const authority = scope.snapshot?.authority;
|
|
131
|
+
if (authority === undefined) {
|
|
132
|
+
throw new SettingsScopeUnavailableError(unit.scopeId);
|
|
133
|
+
}
|
|
134
|
+
const generation = this.#generation;
|
|
135
|
+
this.#setStatus(applyUnitId, { kind: "pending" });
|
|
136
|
+
try {
|
|
137
|
+
const result = await this.#host.client.reset(registry, {
|
|
138
|
+
protocolVersion: 1,
|
|
139
|
+
requestId: this.#host.nextRequestId(),
|
|
140
|
+
pageId: this.#page.id,
|
|
141
|
+
applyUnitId,
|
|
142
|
+
scopeId: unit.scopeId,
|
|
143
|
+
authority,
|
|
144
|
+
entryIds: [...entryIds],
|
|
145
|
+
});
|
|
146
|
+
this.#acceptResult(unit, result, generation, true);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
this.#acceptFailure(applyUnitId, error, generation);
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
stop(): void {
|
|
154
|
+
++this.#generation;
|
|
155
|
+
this.#drafts = new Map();
|
|
156
|
+
this.#statuses = new Map();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async #executeApply(
|
|
160
|
+
unit: SettingsApplyUnitDefinition,
|
|
161
|
+
intent: SettingsOpaqueValue,
|
|
162
|
+
clearDraft: boolean,
|
|
163
|
+
): Promise<void> {
|
|
164
|
+
this.#assertNotPending(unit.id);
|
|
165
|
+
const registry = this.#host.registry();
|
|
166
|
+
const scope = this.#host.scope(unit);
|
|
167
|
+
const authority = scope.snapshot?.authority;
|
|
168
|
+
if (authority === undefined) {
|
|
169
|
+
throw new SettingsScopeUnavailableError(unit.scopeId);
|
|
170
|
+
}
|
|
171
|
+
const generation = this.#generation;
|
|
172
|
+
this.#setStatus(unit.id, { kind: "pending" });
|
|
173
|
+
try {
|
|
174
|
+
const result = await this.#host.client.apply(registry, {
|
|
175
|
+
protocolVersion: 1,
|
|
176
|
+
requestId: this.#host.nextRequestId(),
|
|
177
|
+
pageId: this.#page.id,
|
|
178
|
+
applyUnitId: unit.id,
|
|
179
|
+
scopeId: unit.scopeId,
|
|
180
|
+
authority,
|
|
181
|
+
intent,
|
|
182
|
+
});
|
|
183
|
+
this.#acceptResult(unit, result, generation, clearDraft);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
this.#acceptFailure(unit.id, error, generation);
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
#acceptResult(
|
|
191
|
+
unit: SettingsApplyUnitDefinition,
|
|
192
|
+
result: SettingsMutationResult,
|
|
193
|
+
generation: number,
|
|
194
|
+
clearDraft: boolean,
|
|
195
|
+
): void {
|
|
196
|
+
if (generation !== this.#generation) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (result.status === "applied") {
|
|
200
|
+
this.#host.scope(unit).accept(result.snapshot);
|
|
201
|
+
if (clearDraft) {
|
|
202
|
+
const drafts = new Map(this.#drafts);
|
|
203
|
+
drafts.delete(unit.id);
|
|
204
|
+
this.#drafts = drafts;
|
|
205
|
+
}
|
|
206
|
+
this.#setStatus(unit.id, {
|
|
207
|
+
kind: "saved",
|
|
208
|
+
receipt: result.receipt,
|
|
209
|
+
});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (result.status === "conflict") {
|
|
213
|
+
this.#host.scope(unit).accept(result.snapshot);
|
|
214
|
+
this.#setStatus(unit.id, {
|
|
215
|
+
kind: "conflict",
|
|
216
|
+
conflict: result.conflict,
|
|
217
|
+
});
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (result.snapshot !== null) {
|
|
221
|
+
this.#host.scope(unit).accept(result.snapshot);
|
|
222
|
+
}
|
|
223
|
+
this.#setStatus(unit.id, {
|
|
224
|
+
kind: "rejected",
|
|
225
|
+
rejection: result.rejection,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
#acceptFailure(
|
|
230
|
+
applyUnitId: SettingsApplyUnitId,
|
|
231
|
+
error: unknown,
|
|
232
|
+
generation: number,
|
|
233
|
+
): void {
|
|
234
|
+
if (generation !== this.#generation) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
this.#setStatus(applyUnitId, { kind: "failed", error });
|
|
238
|
+
this.#host.onError(error);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
#applyUnit(applyUnitId: SettingsApplyUnitId): SettingsApplyUnitDefinition {
|
|
242
|
+
if (!this.#page.writableApplyUnitIds.includes(applyUnitId)) {
|
|
243
|
+
throw new SettingsApplyUnitUnavailableError(applyUnitId);
|
|
244
|
+
}
|
|
245
|
+
const unit = this.#host
|
|
246
|
+
.registry()
|
|
247
|
+
.applyUnits.find(({ id }) => id === applyUnitId);
|
|
248
|
+
if (unit === undefined) {
|
|
249
|
+
throw new SettingsApplyUnitUnavailableError(applyUnitId);
|
|
250
|
+
}
|
|
251
|
+
return unit;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#setStatus(
|
|
255
|
+
applyUnitId: SettingsApplyUnitId,
|
|
256
|
+
status: SettingsUnitStatus,
|
|
257
|
+
): void {
|
|
258
|
+
this.#statuses = new Map(this.#statuses).set(applyUnitId, status);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
#assertNotPending(applyUnitId: SettingsApplyUnitId): void {
|
|
262
|
+
if (this.status(applyUnitId).kind === "pending") {
|
|
263
|
+
throw new SettingsMutationPendingError();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|