@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.
Files changed (86) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -0
  3. package/package.json +171 -0
  4. package/src/bindings/index.ts +4 -0
  5. package/src/bindings/layout.svelte.ts +143 -0
  6. package/src/bindings/lifecycle.svelte.ts +246 -0
  7. package/src/bindings/optimistic.svelte.ts +96 -0
  8. package/src/bindings/scheduler.ts +13 -0
  9. package/src/bindings/surface-transfer-actions.ts +281 -0
  10. package/src/bindings/surface-transfer.svelte.ts +230 -0
  11. package/src/bindings/surfaces.svelte.ts +179 -0
  12. package/src/bindings/transfer-actions.ts +240 -0
  13. package/src/bindings/transfer-model.ts +138 -0
  14. package/src/bindings/transfer.svelte.ts +388 -0
  15. package/src/bindings/window-chrome.ts +63 -0
  16. package/src/bindings/window-drag.ts +161 -0
  17. package/src/commands/poodle/CommandPaletteBinding.svelte +51 -0
  18. package/src/commands/poodle/KeybindingSettings.svelte +138 -0
  19. package/src/commands/poodle/projectors.ts +37 -0
  20. package/src/commands/poodle.ts +9 -0
  21. package/src/commands/svelte/lifecycle.ts +13 -0
  22. package/src/commands/svelte/session.svelte.ts +212 -0
  23. package/src/commands/svelte.ts +2 -0
  24. package/src/config/poodle/BackupSettingsPage.svelte +295 -0
  25. package/src/config/poodle/RestoreSettingsPage.svelte +535 -0
  26. package/src/config/poodle/StorageSettingsPage.svelte +361 -0
  27. package/src/config/poodle/restore-model.ts +63 -0
  28. package/src/config/poodle/types.ts +11 -0
  29. package/src/config/poodle.ts +8 -0
  30. package/src/history/svelte/lifecycle.ts +13 -0
  31. package/src/history/svelte/session.svelte.ts +97 -0
  32. package/src/history/svelte.ts +2 -0
  33. package/src/history-tree/svelte/lifecycle.ts +11 -0
  34. package/src/history-tree/svelte/session.svelte.ts +27 -0
  35. package/src/history-tree/svelte.ts +3 -0
  36. package/src/licence/poodle/LicenceActivation.svelte +62 -0
  37. package/src/licence/poodle/LicenceSeats.svelte +57 -0
  38. package/src/licence/poodle/LicenceStatus.svelte +34 -0
  39. package/src/licence/poodle/LicenceSurface.svelte.ts +43 -0
  40. package/src/licence/poodle.ts +3 -0
  41. package/src/native-content/index.ts +4 -0
  42. package/src/native-content/lifecycle.ts +13 -0
  43. package/src/native-content/session.svelte.ts +526 -0
  44. package/src/native-content/viewport.ts +27 -0
  45. package/src/native-content/visibility.ts +19 -0
  46. package/src/notifications/poodle/NotificationPanel.svelte +66 -0
  47. package/src/notifications/poodle/NotificationToastHost.svelte +40 -0
  48. package/src/notifications/poodle/NotificationToastStack.svelte +22 -0
  49. package/src/notifications/poodle/projectors.ts +30 -0
  50. package/src/notifications/poodle.ts +4 -0
  51. package/src/notifications/svelte/lifecycle.ts +11 -0
  52. package/src/notifications/svelte/session.svelte.ts +57 -0
  53. package/src/notifications/svelte.ts +2 -0
  54. package/src/operation/poodle/OperationPanel.svelte +200 -0
  55. package/src/operation/poodle/projectors.ts +78 -0
  56. package/src/operation/poodle.ts +2 -0
  57. package/src/operation/svelte/lifecycle.ts +13 -0
  58. package/src/operation/svelte/session.svelte.ts +80 -0
  59. package/src/operation/svelte.ts +2 -0
  60. package/src/poodle/LayoutDockRegion.svelte +148 -0
  61. package/src/poodle/LayoutSplitView.svelte +120 -0
  62. package/src/poodle/LayoutTabs.svelte +91 -0
  63. package/src/poodle/binding.ts +401 -0
  64. package/src/poodle/document.ts +117 -0
  65. package/src/poodle/index.ts +8 -0
  66. package/src/poodle/projectors.ts +125 -0
  67. package/src/poodle/transfer.ts +188 -0
  68. package/src/poodle/types.ts +89 -0
  69. package/src/settings/poodle/SettingsShell.svelte +382 -0
  70. package/src/settings/poodle.ts +2 -0
  71. package/src/settings/svelte/context.ts +61 -0
  72. package/src/settings/svelte/document.svelte.ts +202 -0
  73. package/src/settings/svelte/errors.ts +84 -0
  74. package/src/settings/svelte/lifecycle.ts +18 -0
  75. package/src/settings/svelte/page.svelte.ts +266 -0
  76. package/src/settings/svelte/runtime.svelte.ts +187 -0
  77. package/src/settings/svelte/scope.svelte.ts +123 -0
  78. package/src/settings/svelte/session.svelte.ts +288 -0
  79. package/src/settings/svelte/types.ts +90 -0
  80. package/src/settings/svelte.ts +6 -0
  81. package/src/svelte.d.ts +6 -0
  82. package/src/update/poodle/UpdateCentre.svelte +64 -0
  83. package/src/update/poodle/UpdateSettings.svelte +64 -0
  84. package/src/update/poodle/UpdateStatus.svelte +51 -0
  85. package/src/update/poodle/UpdateSurface.svelte.ts +88 -0
  86. package/src/update/poodle.ts +3 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 inflatable-cookie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @inflatable-cookie/longhorn-poodle-svelte
2
+
3
+ Reactive Svelte bindings over the Longhorn domains, and the Poodle projections
4
+ that render them. Peers on `@inflatable-cookie/longhorn`, `svelte`, and
5
+ `@inflatable-cookie/poodle-svelte`; the Poodle peer is optional, so a consumer
6
+ that only wants the bindings never installs it.
7
+
8
+ ```sh
9
+ effigy check:svelte
10
+ effigy test:vitest
11
+ ```
12
+
13
+ The root exports the framework-agnostic reactive state — layout, surfaces,
14
+ transfer, and surface transfer — as `.svelte.ts` runes modules:
15
+
16
+ - `@inflatable-cookie/longhorn-poodle-svelte` — lifecycle, client status, scheduler
17
+ - `/layout`, `/surfaces`, `/transfer`, `/surface-transfer`
18
+
19
+ Per-domain subpaths carry the two projection tiers. `/<domain>/svelte` is the
20
+ reactive controller for a domain; `/<domain>/poodle` is the Poodle shell over
21
+ that controller:
22
+
23
+ `commands`, `history`, `history-tree`, `notifications`, `operation`, and
24
+ `settings` expose both. `config` exposes `/config/poodle` only.
25
+
26
+ `/poodle` is the layout projection itself — the binding, document projectors,
27
+ and the `LayoutTabs`, `LayoutDockRegion`, and `LayoutSplitView` components that
28
+ drive Poodle's primitives. `/native-content` projects native content islands
29
+ into Svelte.
30
+
31
+ Poodle is consumed at its package root and nowhere else, and the framework only
32
+ through its published specifiers. Both rules are enforced by
33
+ `tests/boundary.test.ts`, as is the pinned-pack resolution that keeps the suite
34
+ from silently binding to sibling Poodle source.
35
+
36
+ The Rust sibling for the GPUI host will be `longhorn-poodle-gpui`; the framework
37
+ suffix in this package's name is what keeps that from being a rename.
package/package.json ADDED
@@ -0,0 +1,171 @@
1
+ {
2
+ "name": "@inflatable-cookie/longhorn-poodle-svelte",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/inflatable-cookie/longhorn.git",
8
+ "directory": "packages/longhorn-poodle-svelte"
9
+ },
10
+ "homepage": "https://github.com/inflatable-cookie/longhorn#readme",
11
+ "type": "module",
12
+ "types": "./src/bindings/index.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./src/bindings/index.ts",
16
+ "svelte": "./src/bindings/index.ts",
17
+ "import": "./src/bindings/index.ts",
18
+ "default": "./src/bindings/index.ts"
19
+ },
20
+ "./commands/poodle": {
21
+ "types": "./src/commands/poodle.ts",
22
+ "svelte": "./src/commands/poodle.ts",
23
+ "import": "./src/commands/poodle.ts",
24
+ "default": "./src/commands/poodle.ts"
25
+ },
26
+ "./commands/svelte": {
27
+ "types": "./src/commands/svelte.ts",
28
+ "svelte": "./src/commands/svelte.ts",
29
+ "import": "./src/commands/svelte.ts",
30
+ "default": "./src/commands/svelte.ts"
31
+ },
32
+ "./config/poodle": {
33
+ "types": "./src/config/poodle.ts",
34
+ "svelte": "./src/config/poodle.ts",
35
+ "import": "./src/config/poodle.ts",
36
+ "default": "./src/config/poodle.ts"
37
+ },
38
+ "./history-tree/svelte": {
39
+ "types": "./src/history-tree/svelte.ts",
40
+ "svelte": "./src/history-tree/svelte.ts",
41
+ "import": "./src/history-tree/svelte.ts",
42
+ "default": "./src/history-tree/svelte.ts"
43
+ },
44
+ "./history/svelte": {
45
+ "types": "./src/history/svelte.ts",
46
+ "svelte": "./src/history/svelte.ts",
47
+ "import": "./src/history/svelte.ts",
48
+ "default": "./src/history/svelte.ts"
49
+ },
50
+ "./layout": {
51
+ "types": "./src/bindings/layout.svelte.ts",
52
+ "svelte": "./src/bindings/layout.svelte.ts",
53
+ "import": "./src/bindings/layout.svelte.ts",
54
+ "default": "./src/bindings/layout.svelte.ts"
55
+ },
56
+ "./licence/poodle": {
57
+ "types": "./src/licence/poodle.ts",
58
+ "svelte": "./src/licence/poodle.ts",
59
+ "import": "./src/licence/poodle.ts",
60
+ "default": "./src/licence/poodle.ts"
61
+ },
62
+ "./native-content": {
63
+ "types": "./src/native-content/index.ts",
64
+ "svelte": "./src/native-content/index.ts",
65
+ "import": "./src/native-content/index.ts",
66
+ "default": "./src/native-content/index.ts"
67
+ },
68
+ "./notifications/poodle": {
69
+ "types": "./src/notifications/poodle.ts",
70
+ "svelte": "./src/notifications/poodle.ts",
71
+ "import": "./src/notifications/poodle.ts",
72
+ "default": "./src/notifications/poodle.ts"
73
+ },
74
+ "./notifications/svelte": {
75
+ "types": "./src/notifications/svelte.ts",
76
+ "svelte": "./src/notifications/svelte.ts",
77
+ "import": "./src/notifications/svelte.ts",
78
+ "default": "./src/notifications/svelte.ts"
79
+ },
80
+ "./operation/poodle": {
81
+ "types": "./src/operation/poodle.ts",
82
+ "svelte": "./src/operation/poodle.ts",
83
+ "import": "./src/operation/poodle.ts",
84
+ "default": "./src/operation/poodle.ts"
85
+ },
86
+ "./operation/svelte": {
87
+ "types": "./src/operation/svelte.ts",
88
+ "svelte": "./src/operation/svelte.ts",
89
+ "import": "./src/operation/svelte.ts",
90
+ "default": "./src/operation/svelte.ts"
91
+ },
92
+ "./package.json": "./package.json",
93
+ "./poodle": {
94
+ "types": "./src/poodle/index.ts",
95
+ "svelte": "./src/poodle/index.ts",
96
+ "import": "./src/poodle/index.ts",
97
+ "default": "./src/poodle/index.ts"
98
+ },
99
+ "./poodle/binding": {
100
+ "types": "./src/poodle/binding.ts",
101
+ "svelte": "./src/poodle/binding.ts",
102
+ "import": "./src/poodle/binding.ts",
103
+ "default": "./src/poodle/binding.ts"
104
+ },
105
+ "./poodle/transfer": {
106
+ "types": "./src/poodle/transfer.ts",
107
+ "svelte": "./src/poodle/transfer.ts",
108
+ "import": "./src/poodle/transfer.ts",
109
+ "default": "./src/poodle/transfer.ts"
110
+ },
111
+ "./settings/poodle": {
112
+ "types": "./src/settings/poodle.ts",
113
+ "svelte": "./src/settings/poodle.ts",
114
+ "import": "./src/settings/poodle.ts",
115
+ "default": "./src/settings/poodle.ts"
116
+ },
117
+ "./settings/svelte": {
118
+ "types": "./src/settings/svelte.ts",
119
+ "svelte": "./src/settings/svelte.ts",
120
+ "import": "./src/settings/svelte.ts",
121
+ "default": "./src/settings/svelte.ts"
122
+ },
123
+ "./surface-transfer": {
124
+ "types": "./src/bindings/surface-transfer.svelte.ts",
125
+ "svelte": "./src/bindings/surface-transfer.svelte.ts",
126
+ "import": "./src/bindings/surface-transfer.svelte.ts",
127
+ "default": "./src/bindings/surface-transfer.svelte.ts"
128
+ },
129
+ "./surfaces": {
130
+ "types": "./src/bindings/surfaces.svelte.ts",
131
+ "svelte": "./src/bindings/surfaces.svelte.ts",
132
+ "import": "./src/bindings/surfaces.svelte.ts",
133
+ "default": "./src/bindings/surfaces.svelte.ts"
134
+ },
135
+ "./transfer": {
136
+ "types": "./src/bindings/transfer.svelte.ts",
137
+ "svelte": "./src/bindings/transfer.svelte.ts",
138
+ "import": "./src/bindings/transfer.svelte.ts",
139
+ "default": "./src/bindings/transfer.svelte.ts"
140
+ },
141
+ "./update/poodle": {
142
+ "types": "./src/update/poodle.ts",
143
+ "svelte": "./src/update/poodle.ts",
144
+ "import": "./src/update/poodle.ts",
145
+ "default": "./src/update/poodle.ts"
146
+ }
147
+ },
148
+ "files": [
149
+ "src",
150
+ "README.md",
151
+ "LICENSE"
152
+ ],
153
+ "peerDependencies": {
154
+ "@inflatable-cookie/longhorn": "0.1.0",
155
+ "@inflatable-cookie/poodle-core": "0.4.2",
156
+ "@inflatable-cookie/poodle-svelte": "0.4.2",
157
+ "svelte": ">=5.38.6 <=5.56.8"
158
+ },
159
+ "peerDependenciesMeta": {
160
+ "@inflatable-cookie/poodle-core": {
161
+ "optional": true
162
+ },
163
+ "@inflatable-cookie/poodle-svelte": {
164
+ "optional": true
165
+ }
166
+ },
167
+ "sideEffects": false,
168
+ "publishConfig": {
169
+ "access": "public"
170
+ }
171
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./lifecycle.svelte.ts";
2
+ export * from "./optimistic.svelte.ts";
3
+ export * from "./window-chrome.ts";
4
+ export * from "./window-drag.ts";
@@ -0,0 +1,143 @@
1
+ import type {
2
+ LayoutMutationRejection,
3
+ LayoutMutationRequest,
4
+ LayoutMutationReceipt,
5
+ } from "@inflatable-cookie/longhorn/layout";
6
+ import type {
7
+ SurfaceDocument,
8
+ } from "@inflatable-cookie/longhorn/surfaces";
9
+
10
+ import {
11
+ OptimisticProjectionState,
12
+ UnsupportedCapabilityError,
13
+ type ClientStatus,
14
+ type OptimisticProjector,
15
+ } from "./index.ts";
16
+
17
+ export type LayoutDispatchResult =
18
+ | { readonly status: "committed"; readonly receipt: LayoutMutationReceipt }
19
+ | {
20
+ readonly status: "rejected";
21
+ readonly rejection: LayoutMutationRejection;
22
+ };
23
+
24
+ export type LayoutDispatcher = (
25
+ request: LayoutMutationRequest,
26
+ ) => Promise<LayoutDispatchResult>;
27
+
28
+ export interface LayoutStateOptions {
29
+ readonly dispatch?: LayoutDispatcher;
30
+ readonly unsupportedReason?: string;
31
+ }
32
+
33
+ export class LayoutState {
34
+ readonly #options: LayoutStateOptions;
35
+ readonly #projection = new OptimisticProjectionState<SurfaceDocument>(
36
+ (document) => document.revision,
37
+ );
38
+ #status = $state.raw<ClientStatus>({ kind: "idle" });
39
+ #destroyed = false;
40
+
41
+ constructor(options: LayoutStateOptions) {
42
+ this.#options = options;
43
+ }
44
+
45
+ get status(): ClientStatus {
46
+ return this.#status;
47
+ }
48
+
49
+ get authoritative(): SurfaceDocument | undefined {
50
+ return this.#projection.authoritative;
51
+ }
52
+
53
+ get projected(): SurfaceDocument | undefined {
54
+ return this.#projection.projected;
55
+ }
56
+
57
+ get pendingRequestIds(): readonly string[] {
58
+ return this.#projection.pendingRequestIds;
59
+ }
60
+
61
+ async start(): Promise<void> {
62
+ this.#assertAlive();
63
+ this.#status =
64
+ this.#options.dispatch === undefined
65
+ ? {
66
+ kind: "unsupported",
67
+ reason:
68
+ this.#options.unsupportedReason ??
69
+ "layout dispatch is unavailable",
70
+ }
71
+ : { kind: "loading" };
72
+ }
73
+
74
+ accept(document: SurfaceDocument): boolean {
75
+ this.#assertAlive();
76
+ const accepted = this.#projection.accept(document);
77
+ if (accepted) {
78
+ this.#status = { kind: "ready" };
79
+ }
80
+ return accepted;
81
+ }
82
+
83
+ reconnecting(): void {
84
+ this.#assertAlive();
85
+ this.#status =
86
+ this.#projection.authoritative === undefined
87
+ ? { kind: "loading" }
88
+ : { kind: "reconnecting" };
89
+ }
90
+
91
+ async dispatch(
92
+ request: LayoutMutationRequest,
93
+ project: OptimisticProjector<SurfaceDocument>,
94
+ ): Promise<LayoutDispatchResult> {
95
+ this.#assertAlive();
96
+ const dispatch = this.#options.dispatch;
97
+ if (dispatch === undefined) {
98
+ const reason =
99
+ this.#options.unsupportedReason ??
100
+ "layout dispatch is unavailable";
101
+ this.#status = { kind: "unsupported", reason };
102
+ throw new UnsupportedCapabilityError(reason);
103
+ }
104
+
105
+ this.#projection.begin(request.request_id, project);
106
+ try {
107
+ const result = await dispatch(request);
108
+ const document =
109
+ result.status === "committed"
110
+ ? result.receipt.authoritative_document
111
+ : result.rejection.authoritative_document;
112
+ this.#projection.settle(request.request_id, document);
113
+ this.#status = { kind: "ready" };
114
+ return result;
115
+ } catch (error) {
116
+ this.#projection.cancel(request.request_id);
117
+ this.#status = { kind: "failed", error };
118
+ throw error;
119
+ }
120
+ }
121
+
122
+ cancel(requestId: string): void {
123
+ this.#projection.cancel(requestId);
124
+ }
125
+
126
+ async stop(): Promise<void> {
127
+ this.#projection.clear();
128
+ this.#status = { kind: "idle" };
129
+ }
130
+
131
+ async destroy(): Promise<void> {
132
+ if (!this.#destroyed) {
133
+ await this.stop();
134
+ this.#destroyed = true;
135
+ }
136
+ }
137
+
138
+ #assertAlive(): void {
139
+ if (this.#destroyed) {
140
+ throw new Error("layout state has been destroyed");
141
+ }
142
+ }
143
+ }
@@ -0,0 +1,246 @@
1
+ import { onMount } from "svelte";
2
+
3
+ import type {
4
+ ConnectionFailure,
5
+ ConnectionFailureReporter,
6
+ } from "@inflatable-cookie/longhorn/core";
7
+
8
+ export type ClientStatus =
9
+ | { readonly kind: "idle" }
10
+ | { readonly kind: "loading" }
11
+ | { readonly kind: "ready" }
12
+ | { readonly kind: "reconnecting" }
13
+ | { readonly kind: "unsupported"; readonly reason: string }
14
+ | { readonly kind: "failed"; readonly error: unknown };
15
+
16
+ export interface ReactiveConnection<Snapshot> {
17
+ readonly ready: Promise<unknown>;
18
+ current(): Snapshot | undefined;
19
+ dispose(): Promise<void>;
20
+ }
21
+
22
+ export type ReactiveConnectionFactory<Snapshot> = (
23
+ listener: (snapshot: Snapshot) => void,
24
+ onFailure: ConnectionFailureReporter,
25
+ ) => ReactiveConnection<Snapshot>;
26
+
27
+ export type ClientCapability<Snapshot> =
28
+ | {
29
+ readonly kind: "supported";
30
+ readonly connect: ReactiveConnectionFactory<Snapshot>;
31
+ }
32
+ | { readonly kind: "unsupported"; readonly reason: string };
33
+
34
+ export interface ReactiveClientStateOptions<Snapshot> {
35
+ readonly capability: ClientCapability<Snapshot>;
36
+ readonly onSnapshot?: (snapshot: Snapshot) => void;
37
+ readonly onFailure?: (failure: ConnectionFailure) => void;
38
+ }
39
+
40
+ export interface StartableClientState {
41
+ start(): Promise<void>;
42
+ stop(): Promise<void>;
43
+ }
44
+
45
+ export class ReactiveClientState<Snapshot>
46
+ implements StartableClientState
47
+ {
48
+ readonly #options: ReactiveClientStateOptions<Snapshot>;
49
+ #status = $state.raw<ClientStatus>({ kind: "idle" });
50
+ #snapshot = $state.raw<Snapshot | undefined>(undefined);
51
+ #connection: ReactiveConnection<Snapshot> | undefined;
52
+ #startTask: Promise<void> | undefined;
53
+ #stopTask: Promise<void> | undefined;
54
+ #generation = 0;
55
+ #destroyed = false;
56
+
57
+ constructor(options: ReactiveClientStateOptions<Snapshot>) {
58
+ this.#options = options;
59
+ }
60
+
61
+ get status(): ClientStatus {
62
+ return this.#status;
63
+ }
64
+
65
+ get snapshot(): Snapshot | undefined {
66
+ return this.#snapshot;
67
+ }
68
+
69
+ start(): Promise<void> {
70
+ if (this.#destroyed) {
71
+ return Promise.reject(new ClientStateDestroyedError());
72
+ }
73
+ if (this.#startTask !== undefined) {
74
+ return this.#startTask;
75
+ }
76
+
77
+ const afterStop = this.#stopTask;
78
+ this.#startTask =
79
+ afterStop === undefined
80
+ ? this.#begin(false)
81
+ : afterStop.then(() => this.#begin(false));
82
+ return this.#startTask;
83
+ }
84
+
85
+ reconnect(): Promise<void> {
86
+ if (this.#destroyed) {
87
+ return Promise.reject(new ClientStateDestroyedError());
88
+ }
89
+ if (this.#status.kind === "unsupported") {
90
+ return Promise.resolve();
91
+ }
92
+
93
+ const task = this.#stop(false).then(() => this.#begin(true));
94
+ this.#startTask = task;
95
+ return task;
96
+ }
97
+
98
+ stop(): Promise<void> {
99
+ return this.#stop(true);
100
+ }
101
+
102
+ async destroy(): Promise<void> {
103
+ if (this.#destroyed) {
104
+ await this.#stopTask;
105
+ return;
106
+ }
107
+ this.#destroyed = true;
108
+ await this.#stop(true);
109
+ }
110
+
111
+ fail(error: unknown): void {
112
+ if (!this.#destroyed) {
113
+ this.#status = { kind: "failed", error };
114
+ }
115
+ }
116
+
117
+ async #begin(reconnecting: boolean): Promise<void> {
118
+ const capability = this.#options.capability;
119
+ if (capability.kind === "unsupported") {
120
+ this.#status = {
121
+ kind: "unsupported",
122
+ reason: capability.reason,
123
+ };
124
+ return;
125
+ }
126
+
127
+ const generation = ++this.#generation;
128
+ this.#status =
129
+ reconnecting || this.#snapshot !== undefined
130
+ ? { kind: "reconnecting" }
131
+ : { kind: "loading" };
132
+
133
+ let connection: ReactiveConnection<Snapshot>;
134
+ try {
135
+ connection = capability.connect(
136
+ (snapshot) => {
137
+ if (generation === this.#generation && !this.#destroyed) {
138
+ this.#accept(snapshot);
139
+ }
140
+ },
141
+ (failure) => {
142
+ if (generation === this.#generation && !this.#destroyed) {
143
+ this.#status = { kind: "failed", error: failure.error };
144
+ this.#options.onFailure?.(failure);
145
+ }
146
+ },
147
+ );
148
+ } catch (error) {
149
+ this.#status = { kind: "failed", error };
150
+ throw error;
151
+ }
152
+ this.#connection = connection;
153
+
154
+ try {
155
+ await connection.ready;
156
+ if (generation !== this.#generation || this.#destroyed) {
157
+ return;
158
+ }
159
+ const snapshot = connection.current();
160
+ if (snapshot === undefined) {
161
+ throw new ClientSnapshotUnavailableError();
162
+ }
163
+ if (this.#snapshot !== snapshot) {
164
+ this.#accept(snapshot);
165
+ } else {
166
+ this.#status = { kind: "ready" };
167
+ }
168
+ } catch (error) {
169
+ if (generation === this.#generation && !this.#destroyed) {
170
+ this.#status = { kind: "failed", error };
171
+ }
172
+ try {
173
+ await connection.dispose();
174
+ } catch {
175
+ // The connection retains exact teardown failure evidence.
176
+ }
177
+ throw error;
178
+ }
179
+ }
180
+
181
+ #accept(snapshot: Snapshot): void {
182
+ this.#snapshot = snapshot;
183
+ this.#status = { kind: "ready" };
184
+ this.#options.onSnapshot?.(snapshot);
185
+ }
186
+
187
+ #stop(clearSnapshot: boolean): Promise<void> {
188
+ if (this.#stopTask !== undefined) {
189
+ return this.#stopTask;
190
+ }
191
+
192
+ const connection = this.#connection;
193
+ this.#connection = undefined;
194
+ this.#startTask = undefined;
195
+ ++this.#generation;
196
+ if (clearSnapshot) {
197
+ this.#snapshot = undefined;
198
+ }
199
+ this.#status = { kind: "idle" };
200
+
201
+ this.#stopTask = (async () => {
202
+ try {
203
+ await connection?.dispose();
204
+ } catch (error) {
205
+ this.#status = { kind: "failed", error };
206
+ throw error;
207
+ } finally {
208
+ this.#stopTask = undefined;
209
+ }
210
+ })();
211
+ return this.#stopTask;
212
+ }
213
+ }
214
+
215
+ export class ClientStateDestroyedError extends Error {
216
+ constructor() {
217
+ super("client state has been destroyed");
218
+ this.name = "ClientStateDestroyedError";
219
+ }
220
+ }
221
+
222
+ export class ClientSnapshotUnavailableError extends Error {
223
+ constructor() {
224
+ super("client connection became ready without a snapshot");
225
+ this.name = "ClientSnapshotUnavailableError";
226
+ }
227
+ }
228
+
229
+ export class UnsupportedCapabilityError extends Error {
230
+ constructor(reason: string) {
231
+ super(`capability is unsupported: ${reason}`);
232
+ this.name = "UnsupportedCapabilityError";
233
+ }
234
+ }
235
+
236
+ export function useClientState(
237
+ state: StartableClientState,
238
+ reportFailure?: (error: unknown) => void,
239
+ ): void {
240
+ onMount(() => {
241
+ void state.start().catch((error) => reportFailure?.(error));
242
+ return () => {
243
+ void state.stop().catch((error) => reportFailure?.(error));
244
+ };
245
+ });
246
+ }