@statewalker/platform.core 0.1.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mikhail Kotelnikov
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,74 @@
1
+ # @statewalker/platform.core
2
+
3
+ Type-only vocabulary of platform-capability commands shared by every application composed of `statewalker-workbench` fragments. A platform-specific implementation is provided by a peer fragment (`@statewalker/platform.browser` for browsers; future `platform-node` / `platform-electron`); this package stays free of runtime browser code and runs unchanged under Node.
4
+
5
+ ## Why it exists
6
+
7
+ Fragments need a single, typed surface to ask the host environment to do environment-y things — pick a directory, save a file, copy to clipboard, persist a preference, sync URL state — without binding to any particular host. Keeping the contract here (and the implementations in `platform.browser` / future siblings) means a fragment that uses `runPickDirectory` works the same in a browser app, a Node test, and a future Electron build.
8
+
9
+ ## Commands
10
+
11
+ Each command is a self-contained folder under `src/commands/` exporting four things: the string key constant, the payload / result type declarations, and the `run*` / `handle*` factory pair created via `@statewalker/shared-commands`.
12
+
13
+ | Folder | Key | Purpose |
14
+ |---|---|---|
15
+ | [`pick-directory/`](src/commands/pick-directory/) | `platform:pick-directory` | Ask the environment to present a directory picker; returns `{ files: FilesApi; label }`. |
16
+ | [`pick-file/`](src/commands/pick-file/) | `platform:pick-file` | File picker returning `{ blobs: Blob[]; names }`. |
17
+ | [`download-to-files/`](src/commands/download-to-files/) | `platform:download-to-files` | Stream a URL into a `FilesApi` path with optional resume + `AbortSignal`. |
18
+ | [`copy-to-clipboard/`](src/commands/copy-to-clipboard/) | `platform:copy-to-clipboard` | Copy text. |
19
+ | [`download-blob/`](src/commands/download-blob/) | `platform:download-blob` | Trigger a browser "Save As" for a `Blob`. |
20
+ | [`preference-get/`](src/commands/preference-get/) | `platform:preference-get` | Read a durable host-provided key/value pair. |
21
+ | [`preference-set/`](src/commands/preference-set/) | `platform:preference-set` | Write a durable host-provided key/value pair. |
22
+
23
+ `src/commands/index.ts` re-exports every folder; `src/index.ts` re-exports `./commands/index.ts` plus the adapters and `UrlStateView`.
24
+
25
+ ## Adapters
26
+
27
+ The package owns three context-bound adapters consumed by every fragment:
28
+
29
+ ```ts
30
+ import { getCommands, getUrlStateView } from "@statewalker/platform.core";
31
+
32
+ const commands = getCommands(ctx); // Commands bus (auto-created on first access)
33
+ const urlState = getUrlStateView(ctx); // URL ↔ state sync model
34
+ ```
35
+
36
+ - **`Commands`** — the single `Commands` bus shared by every fragment in a composed app. Auto-creates on first access; no explicit bootstrap step.
37
+ - **`UrlStateView`** — bidirectional URL ↔ model state synchronisation. Fragments register `UrlSerializer`s; the browser binding (in `platform.browser`) flushes serialised state to `location.hash` and feeds incoming `hashchange` back. Auto-created on first access.
38
+
39
+ ## Usage
40
+
41
+ ```ts
42
+ import { getCommands, runPickDirectory } from "@statewalker/platform.core";
43
+
44
+ const { files, label } = await runPickDirectory(getCommands(ctx), {
45
+ title: "Select workspace",
46
+ });
47
+ ```
48
+
49
+ Registering a serializer with `UrlStateView`:
50
+
51
+ ```ts
52
+ import { getUrlStateView, type UrlSerializer } from "@statewalker/platform.core";
53
+
54
+ const serializer: UrlSerializer = {
55
+ serialize: (state) => ({ ...state, query: { ...state.query, session: activeSessionId } }),
56
+ deserialize: (state) => switchToSession(state.query.session),
57
+ };
58
+
59
+ const dispose = getUrlStateView(ctx).register(serializer);
60
+ ```
61
+
62
+ ## Internals
63
+
64
+ - **Zero runtime DOM dependencies.** `UrlStateView` extends `BaseClass` and is built from observable primitives only; nothing in the package touches `window`, `document`, or any browser global.
65
+ - **Command-folder layout** — every command lives in its own folder so a future code-gen (e.g. JSON-RPC bindings) can iterate cleanly without text-grepping.
66
+ - **Loop prevention in `UrlStateView`** — a single `#syncing` flag blocks reentrancy: `applyUrl()` is a no-op while `sync()` is running, and vice versa.
67
+
68
+ ## Related
69
+
70
+ - `@statewalker/platform.browser` — browser implementation of every command above plus `bindUrlState`.
71
+
72
+ ## License
73
+
74
+ MIT — see the monorepo root `LICENSE`.
@@ -0,0 +1,160 @@
1
+ import { Commands } from "@statewalker/shared-commands";
2
+ import { BaseClass } from "@statewalker/shared-baseclass";
3
+ import { FilesApi } from "@statewalker/webrun-files";
4
+ //#region src/adapters.d.ts
5
+ /**
6
+ * Resolve the workspace-scoped `Commands` bus for the given context bag.
7
+ * Delegates to `workspace.requireAdapter(Commands)`, which auto-instantiates
8
+ * a single shared bus on first lookup so every fragment composed under the
9
+ * same workspace dispatches through the same instance.
10
+ */
11
+ declare function getCommands(ctx: Record<string, unknown>): Commands;
12
+ /**
13
+ * @deprecated The commands bus is now workspace-scoped and auto-instantiated;
14
+ * explicit registration is a no-op. Kept for one release so legacy bootstrap
15
+ * code keeps compiling. Will be removed in a follow-up phase.
16
+ */
17
+ declare function setCommands(_ctx: Record<string, unknown>, _commands: Commands): void;
18
+ /**
19
+ * @deprecated The commands bus is now workspace-scoped and lifecycle-managed by
20
+ * the workspace; explicit removal is a no-op. Kept for one release so legacy
21
+ * teardown code keeps compiling. Will be removed in a follow-up phase.
22
+ */
23
+ declare function removeCommands(_ctx: Record<string, unknown>): void;
24
+ //#endregion
25
+ //#region src/commands/copy-to-clipboard/index.d.ts
26
+ declare const COPY_TO_CLIPBOARD_COMMAND_KEY = "platform:copy-to-clipboard";
27
+ interface CopyToClipboardPayload {
28
+ text: string;
29
+ }
30
+ declare const CopyToClipboardCommand: import("@statewalker/shared-commands").CommandDeclaration<CopyToClipboardPayload, void>;
31
+ //#endregion
32
+ //#region src/commands/download-blob/index.d.ts
33
+ declare const DOWNLOAD_BLOB_COMMAND_KEY = "platform:download-blob";
34
+ interface DownloadBlobPayload {
35
+ blob: Blob;
36
+ filename: string;
37
+ }
38
+ declare const DownloadBlobCommand: import("@statewalker/shared-commands").CommandDeclaration<DownloadBlobPayload, void>;
39
+ //#endregion
40
+ //#region src/commands/download-to-files/index.d.ts
41
+ declare const DOWNLOAD_TO_FILES_COMMAND_KEY = "platform:download-to-files";
42
+ interface DownloadProgress {
43
+ loaded: number;
44
+ total?: number;
45
+ }
46
+ interface DownloadToFilesPayload {
47
+ url: string;
48
+ files: FilesApi;
49
+ path: string;
50
+ resume?: boolean;
51
+ onProgress?: (progress: DownloadProgress) => void;
52
+ signal?: AbortSignal;
53
+ }
54
+ interface DownloadToFilesResult {
55
+ bytes: number;
56
+ }
57
+ declare const DownloadToFilesCommand: import("@statewalker/shared-commands").CommandDeclaration<DownloadToFilesPayload, DownloadToFilesResult>;
58
+ //#endregion
59
+ //#region src/commands/pick-directory/index.d.ts
60
+ declare const PICK_DIRECTORY_COMMAND_KEY = "platform:pick-directory";
61
+ interface PickDirectoryPayload {
62
+ title?: string;
63
+ }
64
+ interface PickDirectoryResult {
65
+ files: FilesApi;
66
+ label: string;
67
+ }
68
+ declare const PickDirectoryCommand: import("@statewalker/shared-commands").CommandDeclaration<PickDirectoryPayload, PickDirectoryResult>;
69
+ //#endregion
70
+ //#region src/commands/pick-file/index.d.ts
71
+ declare const PICK_FILE_COMMAND_KEY = "platform:pick-file";
72
+ interface PickFilePayload {
73
+ title?: string;
74
+ accept?: string[];
75
+ multiple?: boolean;
76
+ }
77
+ interface PickFileResult {
78
+ blobs: Blob[];
79
+ names: string[];
80
+ }
81
+ declare const PickFileCommand: import("@statewalker/shared-commands").CommandDeclaration<PickFilePayload, PickFileResult>;
82
+ //#endregion
83
+ //#region src/commands/preference-get/index.d.ts
84
+ declare const PREFERENCE_GET_COMMAND_KEY = "platform:preference-get";
85
+ interface PreferenceGetPayload {
86
+ key: string;
87
+ }
88
+ interface PreferenceGetResult {
89
+ value: unknown | undefined;
90
+ }
91
+ declare const PreferenceGetCommand: import("@statewalker/shared-commands").CommandDeclaration<PreferenceGetPayload, PreferenceGetResult>;
92
+ //#endregion
93
+ //#region src/commands/preference-set/index.d.ts
94
+ declare const PREFERENCE_SET_COMMAND_KEY = "platform:preference-set";
95
+ interface PreferenceSetPayload {
96
+ key: string;
97
+ value: unknown;
98
+ }
99
+ declare const PreferenceSetCommand: import("@statewalker/shared-commands").CommandDeclaration<PreferenceSetPayload, void>;
100
+ //#endregion
101
+ //#region src/errors.d.ts
102
+ /**
103
+ * Canonical signal that the user dismissed an interactive prompt (folder
104
+ * picker, dialog, file picker, etc.). Throwing this class — instead of a
105
+ * raw `DOMException` with `name === "AbortError"` or an ad-hoc string — lets
106
+ * every consumer distinguish cancellation from real failure with a single
107
+ * `instanceof` check.
108
+ */
109
+ declare class UserCancelledError extends Error {
110
+ readonly name = "UserCancelledError";
111
+ constructor(message?: string);
112
+ }
113
+ /** Returns `true` if `err` is an instance of {@link UserCancelledError}. */
114
+ declare function isUserCancelled(err: unknown): boolean;
115
+ //#endregion
116
+ //#region src/url-state-view.d.ts
117
+ /**
118
+ * Serialized URL state — what gets written to / read from the URL hash.
119
+ */
120
+ interface UrlState {
121
+ path: string;
122
+ query: Record<string, string>;
123
+ }
124
+ /**
125
+ * A reversible mapping between application state and the URL.
126
+ * `serialize` builds an outgoing URL state from the in-memory model;
127
+ * `deserialize` applies an incoming URL state back onto the model.
128
+ */
129
+ interface UrlSerializer {
130
+ serialize(state: UrlState): UrlState;
131
+ deserialize(state: UrlState): void;
132
+ }
133
+ /**
134
+ * Navigation token — workspace-scoped bidirectional URL ↔ model state
135
+ * synchronisation. Apps reach the workspace-scoped instance via
136
+ * `workspace.requireAdapter(Navigation)`. Models register
137
+ * `UrlSerializer`s; the browser binding (provided by
138
+ * `@statewalker/platform-browser`) flushes the serialized state to
139
+ * `location.hash` whenever `sync()` notifies, and feeds incoming
140
+ * `hashchange` events back to all deserializers via `applyUrl()`.
141
+ *
142
+ * A directional lock prevents feedback loops: at any moment sync flows
143
+ * in only one direction.
144
+ *
145
+ * Inlined into `platform-api` (was re-exported from `workbench-views`)
146
+ * so the substrate can retire the legacy package — see the
147
+ * `workbench-canonical-substrate` capability spec.
148
+ */
149
+ declare class Navigation extends BaseClass {
150
+ #private;
151
+ register(serializer: UrlSerializer): () => void;
152
+ sync(): void;
153
+ buildState(): UrlState;
154
+ applyUrl(state: UrlState): void;
155
+ get isSyncing(): boolean;
156
+ }
157
+ declare const getUrlStateView: (context: Record<string, unknown>, optional?: boolean) => Navigation, setUrlStateView: (context: Record<string, unknown>, value: Navigation) => void, removeUrlStateView: (context: Record<string, unknown>) => void;
158
+ //#endregion
159
+ export { COPY_TO_CLIPBOARD_COMMAND_KEY, CopyToClipboardCommand, CopyToClipboardPayload, DOWNLOAD_BLOB_COMMAND_KEY, DOWNLOAD_TO_FILES_COMMAND_KEY, DownloadBlobCommand, DownloadBlobPayload, DownloadProgress, DownloadToFilesCommand, DownloadToFilesPayload, DownloadToFilesResult, Navigation, Navigation as UrlStateView, PICK_DIRECTORY_COMMAND_KEY, PICK_FILE_COMMAND_KEY, PREFERENCE_GET_COMMAND_KEY, PREFERENCE_SET_COMMAND_KEY, PickDirectoryCommand, PickDirectoryPayload, PickDirectoryResult, PickFileCommand, PickFilePayload, PickFileResult, PreferenceGetCommand, PreferenceGetPayload, PreferenceGetResult, PreferenceSetCommand, PreferenceSetPayload, UrlSerializer, UrlState, UserCancelledError, getCommands, getUrlStateView, isUserCancelled, removeCommands, removeUrlStateView, setCommands, setUrlStateView };
160
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/adapters.ts","../src/commands/copy-to-clipboard/index.ts","../src/commands/download-blob/index.ts","../src/commands/download-to-files/index.ts","../src/commands/pick-directory/index.ts","../src/commands/pick-file/index.ts","../src/commands/preference-get/index.ts","../src/commands/preference-set/index.ts","../src/errors.ts","../src/url-state-view.ts"],"mappings":";;;;;;;;;;iBASgB,YAAY,KAAK,0BAA0B;;;;;;iBAS3C,YAAY,MAAM,yBAAyB,WAAW;;;;;;iBAWtD,eAAe,MAAM;;;cC3BxB;UAEI;EACf;;cAGW,+DAAsB,mBAAA;;;cCNtB;UAEI;EACf,MAAM;EACN;;cAGW,4DAAmB,mBAAA;;;cCNnB;UAEI;EACf;EACA;;UAGe;EACf;EACA,OAAO;EACP;EACA;EACA,cAAc,UAAU;EACxB,SAAS;;UAGM;EACf;;cAGW,+DAAsB,mBAAA,wBAAA;;;cCpBtB;UAEI;EACf;;UAGe;EACf,OAAO;EACP;;cAGW,6DAAoB,mBAAA,sBAAA;;;cCZpB;UAEI;EACf;EACA;EACA;;UAGe;EACf,OAAO;EACP;;cAGW,wDAAe,mBAAA,iBAAA;;;cCbf;UAEI;EACf;;UAGe;EACf;;cAGW,6DAAoB,mBAAA,sBAAA;;;cCVpB;UAEI;EACf;EACA;;cAGW,6DAAoB,mBAAA;;;;;;;;;;cCFpB,2BAA2B;WAC7B;EAET,YAAY;;;iBAME,gBAAgB;;;;;;UCVf;EACf;EACA,OAAO;;;;;;;UAQQ;EACf,UAAU,OAAO,WAAW;EAC5B,YAAY,OAAO;;;;;;;;;;;;;;;;;;cAmBR,mBAAmB;;EAI9B,SAAS,YAAY;EAOrB;EAUA,cAAc;EAQd,SAAS,OAAO;MAYZ;;cAOQ,kBAAe,SAAA,yBAAA,uBAAA,YAAE,kBAAe,SAAA,yBAAA,OAAA,qBAAE,qBAAkB,SAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,139 @@
1
+ import { Command, Commands, passthrough } from "@statewalker/shared-commands";
2
+ import { getWorkspace } from "@statewalker/workspace.core";
3
+ import { newAdapter } from "@statewalker/shared-adapters";
4
+ import { BaseClass } from "@statewalker/shared-baseclass";
5
+ //#region src/adapters.ts
6
+ /**
7
+ * Resolve the workspace-scoped `Commands` bus for the given context bag.
8
+ * Delegates to `workspace.requireAdapter(Commands)`, which auto-instantiates
9
+ * a single shared bus on first lookup so every fragment composed under the
10
+ * same workspace dispatches through the same instance.
11
+ */
12
+ function getCommands(ctx) {
13
+ return getWorkspace(ctx).requireAdapter(Commands);
14
+ }
15
+ /**
16
+ * @deprecated The commands bus is now workspace-scoped and auto-instantiated;
17
+ * explicit registration is a no-op. Kept for one release so legacy bootstrap
18
+ * code keeps compiling. Will be removed in a follow-up phase.
19
+ */
20
+ function setCommands(_ctx, _commands) {
21
+ console.warn("[platform-api] setCommands is deprecated and a no-op: the Commands bus is now workspace-bound and auto-instantiated via workspace.requireAdapter(Commands).");
22
+ }
23
+ /**
24
+ * @deprecated The commands bus is now workspace-scoped and lifecycle-managed by
25
+ * the workspace; explicit removal is a no-op. Kept for one release so legacy
26
+ * teardown code keeps compiling. Will be removed in a follow-up phase.
27
+ */
28
+ function removeCommands(_ctx) {
29
+ console.warn("[platform-api] removeCommands is deprecated and a no-op: the Commands bus lifecycle is owned by workspace.close().");
30
+ }
31
+ //#endregion
32
+ //#region src/commands/copy-to-clipboard/index.ts
33
+ const COPY_TO_CLIPBOARD_COMMAND_KEY = "platform:copy-to-clipboard";
34
+ const CopyToClipboardCommand = Command.silent(COPY_TO_CLIPBOARD_COMMAND_KEY).input(passthrough()).output(passthrough()).build();
35
+ //#endregion
36
+ //#region src/commands/download-blob/index.ts
37
+ const DOWNLOAD_BLOB_COMMAND_KEY = "platform:download-blob";
38
+ const DownloadBlobCommand = Command.silent(DOWNLOAD_BLOB_COMMAND_KEY).input(passthrough()).output(passthrough()).build();
39
+ //#endregion
40
+ //#region src/commands/download-to-files/index.ts
41
+ const DOWNLOAD_TO_FILES_COMMAND_KEY = "platform:download-to-files";
42
+ const DownloadToFilesCommand = Command.silent(DOWNLOAD_TO_FILES_COMMAND_KEY).input(passthrough()).output(passthrough()).build();
43
+ //#endregion
44
+ //#region src/commands/pick-directory/index.ts
45
+ const PICK_DIRECTORY_COMMAND_KEY = "platform:pick-directory";
46
+ const PickDirectoryCommand = Command.silent(PICK_DIRECTORY_COMMAND_KEY).input(passthrough()).output(passthrough()).build();
47
+ //#endregion
48
+ //#region src/commands/pick-file/index.ts
49
+ const PICK_FILE_COMMAND_KEY = "platform:pick-file";
50
+ const PickFileCommand = Command.silent(PICK_FILE_COMMAND_KEY).input(passthrough()).output(passthrough()).build();
51
+ //#endregion
52
+ //#region src/commands/preference-get/index.ts
53
+ const PREFERENCE_GET_COMMAND_KEY = "platform:preference-get";
54
+ const PreferenceGetCommand = Command.silent(PREFERENCE_GET_COMMAND_KEY).input(passthrough()).output(passthrough()).build();
55
+ //#endregion
56
+ //#region src/commands/preference-set/index.ts
57
+ const PREFERENCE_SET_COMMAND_KEY = "platform:preference-set";
58
+ const PreferenceSetCommand = Command.silent(PREFERENCE_SET_COMMAND_KEY).input(passthrough()).output(passthrough()).build();
59
+ //#endregion
60
+ //#region src/errors.ts
61
+ /**
62
+ * Canonical signal that the user dismissed an interactive prompt (folder
63
+ * picker, dialog, file picker, etc.). Throwing this class — instead of a
64
+ * raw `DOMException` with `name === "AbortError"` or an ad-hoc string — lets
65
+ * every consumer distinguish cancellation from real failure with a single
66
+ * `instanceof` check.
67
+ */
68
+ var UserCancelledError = class extends Error {
69
+ name = "UserCancelledError";
70
+ constructor(message = "User cancelled the operation") {
71
+ super(message);
72
+ }
73
+ };
74
+ /** Returns `true` if `err` is an instance of {@link UserCancelledError}. */
75
+ function isUserCancelled(err) {
76
+ return err instanceof UserCancelledError;
77
+ }
78
+ //#endregion
79
+ //#region src/url-state-view.ts
80
+ /**
81
+ * Navigation token — workspace-scoped bidirectional URL ↔ model state
82
+ * synchronisation. Apps reach the workspace-scoped instance via
83
+ * `workspace.requireAdapter(Navigation)`. Models register
84
+ * `UrlSerializer`s; the browser binding (provided by
85
+ * `@statewalker/platform-browser`) flushes the serialized state to
86
+ * `location.hash` whenever `sync()` notifies, and feeds incoming
87
+ * `hashchange` events back to all deserializers via `applyUrl()`.
88
+ *
89
+ * A directional lock prevents feedback loops: at any moment sync flows
90
+ * in only one direction.
91
+ *
92
+ * Inlined into `platform-api` (was re-exported from `workbench-views`)
93
+ * so the substrate can retire the legacy package — see the
94
+ * `workbench-canonical-substrate` capability spec.
95
+ */
96
+ var Navigation = class extends BaseClass {
97
+ #serializers = /* @__PURE__ */ new Set();
98
+ #syncing = false;
99
+ register(serializer) {
100
+ this.#serializers.add(serializer);
101
+ return () => {
102
+ this.#serializers.delete(serializer);
103
+ };
104
+ }
105
+ sync() {
106
+ if (this.#syncing) return;
107
+ this.#syncing = true;
108
+ try {
109
+ this.notify();
110
+ } finally {
111
+ this.#syncing = false;
112
+ }
113
+ }
114
+ buildState() {
115
+ let state = {
116
+ path: "",
117
+ query: {}
118
+ };
119
+ for (const s of this.#serializers) state = s.serialize(state);
120
+ return state;
121
+ }
122
+ applyUrl(state) {
123
+ if (this.#syncing) return;
124
+ this.#syncing = true;
125
+ try {
126
+ for (const s of this.#serializers) s.deserialize(state);
127
+ } finally {
128
+ this.#syncing = false;
129
+ }
130
+ }
131
+ get isSyncing() {
132
+ return this.#syncing;
133
+ }
134
+ };
135
+ const [getUrlStateView, setUrlStateView, removeUrlStateView] = newAdapter("model:url-state", () => new Navigation());
136
+ //#endregion
137
+ export { COPY_TO_CLIPBOARD_COMMAND_KEY, CopyToClipboardCommand, DOWNLOAD_BLOB_COMMAND_KEY, DOWNLOAD_TO_FILES_COMMAND_KEY, DownloadBlobCommand, DownloadToFilesCommand, Navigation, Navigation as UrlStateView, PICK_DIRECTORY_COMMAND_KEY, PICK_FILE_COMMAND_KEY, PREFERENCE_GET_COMMAND_KEY, PREFERENCE_SET_COMMAND_KEY, PickDirectoryCommand, PickFileCommand, PreferenceGetCommand, PreferenceSetCommand, UserCancelledError, getCommands, getUrlStateView, isUserCancelled, removeCommands, removeUrlStateView, setCommands, setUrlStateView };
138
+
139
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/adapters.ts","../src/commands/copy-to-clipboard/index.ts","../src/commands/download-blob/index.ts","../src/commands/download-to-files/index.ts","../src/commands/pick-directory/index.ts","../src/commands/pick-file/index.ts","../src/commands/preference-get/index.ts","../src/commands/preference-set/index.ts","../src/errors.ts","../src/url-state-view.ts"],"sourcesContent":["import { Commands } from \"@statewalker/shared-commands\";\nimport { getWorkspace } from \"@statewalker/workspace.core\";\n\n/**\n * Resolve the workspace-scoped `Commands` bus for the given context bag.\n * Delegates to `workspace.requireAdapter(Commands)`, which auto-instantiates\n * a single shared bus on first lookup so every fragment composed under the\n * same workspace dispatches through the same instance.\n */\nexport function getCommands(ctx: Record<string, unknown>): Commands {\n return getWorkspace(ctx).requireAdapter(Commands);\n}\n\n/**\n * @deprecated The commands bus is now workspace-scoped and auto-instantiated;\n * explicit registration is a no-op. Kept for one release so legacy bootstrap\n * code keeps compiling. Will be removed in a follow-up phase.\n */\nexport function setCommands(_ctx: Record<string, unknown>, _commands: Commands): void {\n console.warn(\n \"[platform-api] setCommands is deprecated and a no-op: the Commands bus is now workspace-bound and auto-instantiated via workspace.requireAdapter(Commands).\",\n );\n}\n\n/**\n * @deprecated The commands bus is now workspace-scoped and lifecycle-managed by\n * the workspace; explicit removal is a no-op. Kept for one release so legacy\n * teardown code keeps compiling. Will be removed in a follow-up phase.\n */\nexport function removeCommands(_ctx: Record<string, unknown>): void {\n console.warn(\n \"[platform-api] removeCommands is deprecated and a no-op: the Commands bus lifecycle is owned by workspace.close().\",\n );\n}\n","import { Command, passthrough } from \"@statewalker/shared-commands\";\n\nexport const COPY_TO_CLIPBOARD_COMMAND_KEY = \"platform:copy-to-clipboard\";\n\nexport interface CopyToClipboardPayload {\n text: string;\n}\n\nexport const CopyToClipboardCommand = Command.silent(COPY_TO_CLIPBOARD_COMMAND_KEY)\n .input(passthrough<CopyToClipboardPayload>())\n .output(passthrough<void>())\n .build();\n","import { Command, passthrough } from \"@statewalker/shared-commands\";\n\nexport const DOWNLOAD_BLOB_COMMAND_KEY = \"platform:download-blob\";\n\nexport interface DownloadBlobPayload {\n blob: Blob;\n filename: string;\n}\n\nexport const DownloadBlobCommand = Command.silent(DOWNLOAD_BLOB_COMMAND_KEY)\n .input(passthrough<DownloadBlobPayload>())\n .output(passthrough<void>())\n .build();\n","import { Command, passthrough } from \"@statewalker/shared-commands\";\nimport type { FilesApi } from \"@statewalker/webrun-files\";\n\nexport const DOWNLOAD_TO_FILES_COMMAND_KEY = \"platform:download-to-files\";\n\nexport interface DownloadProgress {\n loaded: number;\n total?: number;\n}\n\nexport interface DownloadToFilesPayload {\n url: string;\n files: FilesApi;\n path: string;\n resume?: boolean;\n onProgress?: (progress: DownloadProgress) => void;\n signal?: AbortSignal;\n}\n\nexport interface DownloadToFilesResult {\n bytes: number;\n}\n\nexport const DownloadToFilesCommand = Command.silent(DOWNLOAD_TO_FILES_COMMAND_KEY)\n .input(passthrough<DownloadToFilesPayload>())\n .output(passthrough<DownloadToFilesResult>())\n .build();\n","import { Command, passthrough } from \"@statewalker/shared-commands\";\nimport type { FilesApi } from \"@statewalker/webrun-files\";\n\nexport const PICK_DIRECTORY_COMMAND_KEY = \"platform:pick-directory\";\n\nexport interface PickDirectoryPayload {\n title?: string;\n}\n\nexport interface PickDirectoryResult {\n files: FilesApi;\n label: string;\n}\n\nexport const PickDirectoryCommand = Command.silent(PICK_DIRECTORY_COMMAND_KEY)\n .input(passthrough<PickDirectoryPayload>())\n .output(passthrough<PickDirectoryResult>())\n .build();\n","import { Command, passthrough } from \"@statewalker/shared-commands\";\n\nexport const PICK_FILE_COMMAND_KEY = \"platform:pick-file\";\n\nexport interface PickFilePayload {\n title?: string;\n accept?: string[];\n multiple?: boolean;\n}\n\nexport interface PickFileResult {\n blobs: Blob[];\n names: string[];\n}\n\nexport const PickFileCommand = Command.silent(PICK_FILE_COMMAND_KEY)\n .input(passthrough<PickFilePayload>())\n .output(passthrough<PickFileResult>())\n .build();\n","import { Command, passthrough } from \"@statewalker/shared-commands\";\n\nexport const PREFERENCE_GET_COMMAND_KEY = \"platform:preference-get\";\n\nexport interface PreferenceGetPayload {\n key: string;\n}\n\nexport interface PreferenceGetResult {\n value: unknown | undefined;\n}\n\nexport const PreferenceGetCommand = Command.silent(PREFERENCE_GET_COMMAND_KEY)\n .input(passthrough<PreferenceGetPayload>())\n .output(passthrough<PreferenceGetResult>())\n .build();\n","import { Command, passthrough } from \"@statewalker/shared-commands\";\n\nexport const PREFERENCE_SET_COMMAND_KEY = \"platform:preference-set\";\n\nexport interface PreferenceSetPayload {\n key: string;\n value: unknown;\n}\n\nexport const PreferenceSetCommand = Command.silent(PREFERENCE_SET_COMMAND_KEY)\n .input(passthrough<PreferenceSetPayload>())\n .output(passthrough<void>())\n .build();\n","/**\n * Canonical signal that the user dismissed an interactive prompt (folder\n * picker, dialog, file picker, etc.). Throwing this class — instead of a\n * raw `DOMException` with `name === \"AbortError\"` or an ad-hoc string — lets\n * every consumer distinguish cancellation from real failure with a single\n * `instanceof` check.\n */\nexport class UserCancelledError extends Error {\n readonly name = \"UserCancelledError\";\n\n constructor(message = \"User cancelled the operation\") {\n super(message);\n }\n}\n\n/** Returns `true` if `err` is an instance of {@link UserCancelledError}. */\nexport function isUserCancelled(err: unknown): boolean {\n return err instanceof UserCancelledError;\n}\n","import { newAdapter } from \"@statewalker/shared-adapters\";\nimport { BaseClass } from \"@statewalker/shared-baseclass\";\n\n/**\n * Serialized URL state — what gets written to / read from the URL hash.\n */\nexport interface UrlState {\n path: string;\n query: Record<string, string>;\n}\n\n/**\n * A reversible mapping between application state and the URL.\n * `serialize` builds an outgoing URL state from the in-memory model;\n * `deserialize` applies an incoming URL state back onto the model.\n */\nexport interface UrlSerializer {\n serialize(state: UrlState): UrlState;\n deserialize(state: UrlState): void;\n}\n\n/**\n * Navigation token — workspace-scoped bidirectional URL ↔ model state\n * synchronisation. Apps reach the workspace-scoped instance via\n * `workspace.requireAdapter(Navigation)`. Models register\n * `UrlSerializer`s; the browser binding (provided by\n * `@statewalker/platform-browser`) flushes the serialized state to\n * `location.hash` whenever `sync()` notifies, and feeds incoming\n * `hashchange` events back to all deserializers via `applyUrl()`.\n *\n * A directional lock prevents feedback loops: at any moment sync flows\n * in only one direction.\n *\n * Inlined into `platform-api` (was re-exported from `workbench-views`)\n * so the substrate can retire the legacy package — see the\n * `workbench-canonical-substrate` capability spec.\n */\nexport class Navigation extends BaseClass {\n #serializers: Set<UrlSerializer> = new Set();\n #syncing = false;\n\n register(serializer: UrlSerializer): () => void {\n this.#serializers.add(serializer);\n return () => {\n this.#serializers.delete(serializer);\n };\n }\n\n sync(): void {\n if (this.#syncing) return;\n this.#syncing = true;\n try {\n this.notify();\n } finally {\n this.#syncing = false;\n }\n }\n\n buildState(): UrlState {\n let state: UrlState = { path: \"\", query: {} };\n for (const s of this.#serializers) {\n state = s.serialize(state);\n }\n return state;\n }\n\n applyUrl(state: UrlState): void {\n if (this.#syncing) return;\n this.#syncing = true;\n try {\n for (const s of this.#serializers) {\n s.deserialize(state);\n }\n } finally {\n this.#syncing = false;\n }\n }\n\n get isSyncing(): boolean {\n return this.#syncing;\n }\n}\n\nexport { Navigation as UrlStateView };\n\nexport const [getUrlStateView, setUrlStateView, removeUrlStateView] = newAdapter<Navigation>(\n \"model:url-state\",\n () => new Navigation(),\n);\n"],"mappings":";;;;;;;;;;;AASA,SAAgB,YAAY,KAAwC;CAClE,OAAO,aAAa,GAAG,CAAC,CAAC,eAAe,QAAQ;AAClD;;;;;;AAOA,SAAgB,YAAY,MAA+B,WAA2B;CACpF,QAAQ,KACN,6JACF;AACF;;;;;;AAOA,SAAgB,eAAe,MAAqC;CAClE,QAAQ,KACN,oHACF;AACF;;;AC/BA,MAAa,gCAAgC;AAM7C,MAAa,yBAAyB,QAAQ,OAAO,6BAA6B,CAAC,CAChF,MAAM,YAAoC,CAAC,CAAC,CAC5C,OAAO,YAAkB,CAAC,CAAC,CAC3B,MAAM;;;ACTT,MAAa,4BAA4B;AAOzC,MAAa,sBAAsB,QAAQ,OAAO,yBAAyB,CAAC,CACzE,MAAM,YAAiC,CAAC,CAAC,CACzC,OAAO,YAAkB,CAAC,CAAC,CAC3B,MAAM;;;ACTT,MAAa,gCAAgC;AAoB7C,MAAa,yBAAyB,QAAQ,OAAO,6BAA6B,CAAC,CAChF,MAAM,YAAoC,CAAC,CAAC,CAC5C,OAAO,YAAmC,CAAC,CAAC,CAC5C,MAAM;;;ACvBT,MAAa,6BAA6B;AAW1C,MAAa,uBAAuB,QAAQ,OAAO,0BAA0B,CAAC,CAC3E,MAAM,YAAkC,CAAC,CAAC,CAC1C,OAAO,YAAiC,CAAC,CAAC,CAC1C,MAAM;;;ACfT,MAAa,wBAAwB;AAarC,MAAa,kBAAkB,QAAQ,OAAO,qBAAqB,CAAC,CACjE,MAAM,YAA6B,CAAC,CAAC,CACrC,OAAO,YAA4B,CAAC,CAAC,CACrC,MAAM;;;AChBT,MAAa,6BAA6B;AAU1C,MAAa,uBAAuB,QAAQ,OAAO,0BAA0B,CAAC,CAC3E,MAAM,YAAkC,CAAC,CAAC,CAC1C,OAAO,YAAiC,CAAC,CAAC,CAC1C,MAAM;;;ACbT,MAAa,6BAA6B;AAO1C,MAAa,uBAAuB,QAAQ,OAAO,0BAA0B,CAAC,CAC3E,MAAM,YAAkC,CAAC,CAAC,CAC1C,OAAO,YAAkB,CAAC,CAAC,CAC3B,MAAM;;;;;;;;;;ACLT,IAAa,qBAAb,cAAwC,MAAM;CAC5C,OAAgB;CAEhB,YAAY,UAAU,gCAAgC;EACpD,MAAM,OAAO;CACf;AACF;;AAGA,SAAgB,gBAAgB,KAAuB;CACrD,OAAO,eAAe;AACxB;;;;;;;;;;;;;;;;;;;ACmBA,IAAa,aAAb,cAAgC,UAAU;CACxC,+BAAmC,IAAI,IAAI;CAC3C,WAAW;CAEX,SAAS,YAAuC;EAC9C,KAAK,aAAa,IAAI,UAAU;EAChC,aAAa;GACX,KAAK,aAAa,OAAO,UAAU;EACrC;CACF;CAEA,OAAa;EACX,IAAI,KAAK,UAAU;EACnB,KAAK,WAAW;EAChB,IAAI;GACF,KAAK,OAAO;EACd,UAAU;GACR,KAAK,WAAW;EAClB;CACF;CAEA,aAAuB;EACrB,IAAI,QAAkB;GAAE,MAAM;GAAI,OAAO,CAAC;EAAE;EAC5C,KAAK,MAAM,KAAK,KAAK,cACnB,QAAQ,EAAE,UAAU,KAAK;EAE3B,OAAO;CACT;CAEA,SAAS,OAAuB;EAC9B,IAAI,KAAK,UAAU;EACnB,KAAK,WAAW;EAChB,IAAI;GACF,KAAK,MAAM,KAAK,KAAK,cACnB,EAAE,YAAY,KAAK;EAEvB,UAAU;GACR,KAAK,WAAW;EAClB;CACF;CAEA,IAAI,YAAqB;EACvB,OAAO,KAAK;CACd;AACF;AAIA,MAAa,CAAC,iBAAiB,iBAAiB,sBAAsB,WACpE,yBACM,IAAI,WAAW,CACvB"}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@statewalker/platform.core",
3
+ "version": "0.1.1",
4
+ "private": false,
5
+ "type": "module",
6
+ "description": "Platform-capability intent vocabulary shared by every application composed of statewalker-workbench fragments.",
7
+ "homepage": "https://github.com/statewalker/statewalker-workbench",
8
+ "author": {
9
+ "name": "Mikhail Kotelnikov",
10
+ "email": "mikhail.kotelnikov@gmail.com"
11
+ },
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+ssh://git@github.com/statewalker/statewalker-workbench.git"
16
+ },
17
+ "exports": {
18
+ ".": "./src/index.ts"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "src"
23
+ ],
24
+ "dependencies": {
25
+ "@statewalker/shared-adapters": "^0.1.0",
26
+ "@statewalker/shared-baseclass": "^0.1.0",
27
+ "@statewalker/shared-commands": "^0.2.0",
28
+ "@statewalker/webrun-files": "^0.7.0",
29
+ "@statewalker/workspace.core": "0.1.1"
30
+ },
31
+ "devDependencies": {
32
+ "rimraf": "^6.1.3",
33
+ "tsdown": "^0.22.14",
34
+ "typescript": "^7.0.2",
35
+ "vitest": "^4.1.10"
36
+ },
37
+ "sideEffects": false,
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "scripts": {
42
+ "build": "tsdown",
43
+ "dev": "tsdown --watch",
44
+ "test": "vitest run --passWithNoTests",
45
+ "test:watch": "vitest --passWithNoTests",
46
+ "typecheck": "tsc --noEmit",
47
+ "clean": "rimraf dist",
48
+ "lint": "biome check --write .",
49
+ "format": "biome format --write ."
50
+ }
51
+ }
@@ -0,0 +1,34 @@
1
+ import { Commands } from "@statewalker/shared-commands";
2
+ import { getWorkspace } from "@statewalker/workspace.core";
3
+
4
+ /**
5
+ * Resolve the workspace-scoped `Commands` bus for the given context bag.
6
+ * Delegates to `workspace.requireAdapter(Commands)`, which auto-instantiates
7
+ * a single shared bus on first lookup so every fragment composed under the
8
+ * same workspace dispatches through the same instance.
9
+ */
10
+ export function getCommands(ctx: Record<string, unknown>): Commands {
11
+ return getWorkspace(ctx).requireAdapter(Commands);
12
+ }
13
+
14
+ /**
15
+ * @deprecated The commands bus is now workspace-scoped and auto-instantiated;
16
+ * explicit registration is a no-op. Kept for one release so legacy bootstrap
17
+ * code keeps compiling. Will be removed in a follow-up phase.
18
+ */
19
+ export function setCommands(_ctx: Record<string, unknown>, _commands: Commands): void {
20
+ console.warn(
21
+ "[platform-api] setCommands is deprecated and a no-op: the Commands bus is now workspace-bound and auto-instantiated via workspace.requireAdapter(Commands).",
22
+ );
23
+ }
24
+
25
+ /**
26
+ * @deprecated The commands bus is now workspace-scoped and lifecycle-managed by
27
+ * the workspace; explicit removal is a no-op. Kept for one release so legacy
28
+ * teardown code keeps compiling. Will be removed in a follow-up phase.
29
+ */
30
+ export function removeCommands(_ctx: Record<string, unknown>): void {
31
+ console.warn(
32
+ "[platform-api] removeCommands is deprecated and a no-op: the Commands bus lifecycle is owned by workspace.close().",
33
+ );
34
+ }
@@ -0,0 +1,12 @@
1
+ import { Command, passthrough } from "@statewalker/shared-commands";
2
+
3
+ export const COPY_TO_CLIPBOARD_COMMAND_KEY = "platform:copy-to-clipboard";
4
+
5
+ export interface CopyToClipboardPayload {
6
+ text: string;
7
+ }
8
+
9
+ export const CopyToClipboardCommand = Command.silent(COPY_TO_CLIPBOARD_COMMAND_KEY)
10
+ .input(passthrough<CopyToClipboardPayload>())
11
+ .output(passthrough<void>())
12
+ .build();
@@ -0,0 +1,13 @@
1
+ import { Command, passthrough } from "@statewalker/shared-commands";
2
+
3
+ export const DOWNLOAD_BLOB_COMMAND_KEY = "platform:download-blob";
4
+
5
+ export interface DownloadBlobPayload {
6
+ blob: Blob;
7
+ filename: string;
8
+ }
9
+
10
+ export const DownloadBlobCommand = Command.silent(DOWNLOAD_BLOB_COMMAND_KEY)
11
+ .input(passthrough<DownloadBlobPayload>())
12
+ .output(passthrough<void>())
13
+ .build();
@@ -0,0 +1,27 @@
1
+ import { Command, passthrough } from "@statewalker/shared-commands";
2
+ import type { FilesApi } from "@statewalker/webrun-files";
3
+
4
+ export const DOWNLOAD_TO_FILES_COMMAND_KEY = "platform:download-to-files";
5
+
6
+ export interface DownloadProgress {
7
+ loaded: number;
8
+ total?: number;
9
+ }
10
+
11
+ export interface DownloadToFilesPayload {
12
+ url: string;
13
+ files: FilesApi;
14
+ path: string;
15
+ resume?: boolean;
16
+ onProgress?: (progress: DownloadProgress) => void;
17
+ signal?: AbortSignal;
18
+ }
19
+
20
+ export interface DownloadToFilesResult {
21
+ bytes: number;
22
+ }
23
+
24
+ export const DownloadToFilesCommand = Command.silent(DOWNLOAD_TO_FILES_COMMAND_KEY)
25
+ .input(passthrough<DownloadToFilesPayload>())
26
+ .output(passthrough<DownloadToFilesResult>())
27
+ .build();
@@ -0,0 +1,7 @@
1
+ export * from "./copy-to-clipboard/index.js";
2
+ export * from "./download-blob/index.js";
3
+ export * from "./download-to-files/index.js";
4
+ export * from "./pick-directory/index.js";
5
+ export * from "./pick-file/index.js";
6
+ export * from "./preference-get/index.js";
7
+ export * from "./preference-set/index.js";
@@ -0,0 +1,18 @@
1
+ import { Command, passthrough } from "@statewalker/shared-commands";
2
+ import type { FilesApi } from "@statewalker/webrun-files";
3
+
4
+ export const PICK_DIRECTORY_COMMAND_KEY = "platform:pick-directory";
5
+
6
+ export interface PickDirectoryPayload {
7
+ title?: string;
8
+ }
9
+
10
+ export interface PickDirectoryResult {
11
+ files: FilesApi;
12
+ label: string;
13
+ }
14
+
15
+ export const PickDirectoryCommand = Command.silent(PICK_DIRECTORY_COMMAND_KEY)
16
+ .input(passthrough<PickDirectoryPayload>())
17
+ .output(passthrough<PickDirectoryResult>())
18
+ .build();
@@ -0,0 +1,19 @@
1
+ import { Command, passthrough } from "@statewalker/shared-commands";
2
+
3
+ export const PICK_FILE_COMMAND_KEY = "platform:pick-file";
4
+
5
+ export interface PickFilePayload {
6
+ title?: string;
7
+ accept?: string[];
8
+ multiple?: boolean;
9
+ }
10
+
11
+ export interface PickFileResult {
12
+ blobs: Blob[];
13
+ names: string[];
14
+ }
15
+
16
+ export const PickFileCommand = Command.silent(PICK_FILE_COMMAND_KEY)
17
+ .input(passthrough<PickFilePayload>())
18
+ .output(passthrough<PickFileResult>())
19
+ .build();
@@ -0,0 +1,16 @@
1
+ import { Command, passthrough } from "@statewalker/shared-commands";
2
+
3
+ export const PREFERENCE_GET_COMMAND_KEY = "platform:preference-get";
4
+
5
+ export interface PreferenceGetPayload {
6
+ key: string;
7
+ }
8
+
9
+ export interface PreferenceGetResult {
10
+ value: unknown | undefined;
11
+ }
12
+
13
+ export const PreferenceGetCommand = Command.silent(PREFERENCE_GET_COMMAND_KEY)
14
+ .input(passthrough<PreferenceGetPayload>())
15
+ .output(passthrough<PreferenceGetResult>())
16
+ .build();
@@ -0,0 +1,13 @@
1
+ import { Command, passthrough } from "@statewalker/shared-commands";
2
+
3
+ export const PREFERENCE_SET_COMMAND_KEY = "platform:preference-set";
4
+
5
+ export interface PreferenceSetPayload {
6
+ key: string;
7
+ value: unknown;
8
+ }
9
+
10
+ export const PreferenceSetCommand = Command.silent(PREFERENCE_SET_COMMAND_KEY)
11
+ .input(passthrough<PreferenceSetPayload>())
12
+ .output(passthrough<void>())
13
+ .build();
package/src/errors.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Canonical signal that the user dismissed an interactive prompt (folder
3
+ * picker, dialog, file picker, etc.). Throwing this class — instead of a
4
+ * raw `DOMException` with `name === "AbortError"` or an ad-hoc string — lets
5
+ * every consumer distinguish cancellation from real failure with a single
6
+ * `instanceof` check.
7
+ */
8
+ export class UserCancelledError extends Error {
9
+ readonly name = "UserCancelledError";
10
+
11
+ constructor(message = "User cancelled the operation") {
12
+ super(message);
13
+ }
14
+ }
15
+
16
+ /** Returns `true` if `err` is an instance of {@link UserCancelledError}. */
17
+ export function isUserCancelled(err: unknown): boolean {
18
+ return err instanceof UserCancelledError;
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./adapters.js";
2
+ export * from "./commands/index.js";
3
+ export * from "./errors.js";
4
+ export * from "./url-state-view.js";
@@ -0,0 +1,89 @@
1
+ import { newAdapter } from "@statewalker/shared-adapters";
2
+ import { BaseClass } from "@statewalker/shared-baseclass";
3
+
4
+ /**
5
+ * Serialized URL state — what gets written to / read from the URL hash.
6
+ */
7
+ export interface UrlState {
8
+ path: string;
9
+ query: Record<string, string>;
10
+ }
11
+
12
+ /**
13
+ * A reversible mapping between application state and the URL.
14
+ * `serialize` builds an outgoing URL state from the in-memory model;
15
+ * `deserialize` applies an incoming URL state back onto the model.
16
+ */
17
+ export interface UrlSerializer {
18
+ serialize(state: UrlState): UrlState;
19
+ deserialize(state: UrlState): void;
20
+ }
21
+
22
+ /**
23
+ * Navigation token — workspace-scoped bidirectional URL ↔ model state
24
+ * synchronisation. Apps reach the workspace-scoped instance via
25
+ * `workspace.requireAdapter(Navigation)`. Models register
26
+ * `UrlSerializer`s; the browser binding (provided by
27
+ * `@statewalker/platform-browser`) flushes the serialized state to
28
+ * `location.hash` whenever `sync()` notifies, and feeds incoming
29
+ * `hashchange` events back to all deserializers via `applyUrl()`.
30
+ *
31
+ * A directional lock prevents feedback loops: at any moment sync flows
32
+ * in only one direction.
33
+ *
34
+ * Inlined into `platform-api` (was re-exported from `workbench-views`)
35
+ * so the substrate can retire the legacy package — see the
36
+ * `workbench-canonical-substrate` capability spec.
37
+ */
38
+ export class Navigation extends BaseClass {
39
+ #serializers: Set<UrlSerializer> = new Set();
40
+ #syncing = false;
41
+
42
+ register(serializer: UrlSerializer): () => void {
43
+ this.#serializers.add(serializer);
44
+ return () => {
45
+ this.#serializers.delete(serializer);
46
+ };
47
+ }
48
+
49
+ sync(): void {
50
+ if (this.#syncing) return;
51
+ this.#syncing = true;
52
+ try {
53
+ this.notify();
54
+ } finally {
55
+ this.#syncing = false;
56
+ }
57
+ }
58
+
59
+ buildState(): UrlState {
60
+ let state: UrlState = { path: "", query: {} };
61
+ for (const s of this.#serializers) {
62
+ state = s.serialize(state);
63
+ }
64
+ return state;
65
+ }
66
+
67
+ applyUrl(state: UrlState): void {
68
+ if (this.#syncing) return;
69
+ this.#syncing = true;
70
+ try {
71
+ for (const s of this.#serializers) {
72
+ s.deserialize(state);
73
+ }
74
+ } finally {
75
+ this.#syncing = false;
76
+ }
77
+ }
78
+
79
+ get isSyncing(): boolean {
80
+ return this.#syncing;
81
+ }
82
+ }
83
+
84
+ export { Navigation as UrlStateView };
85
+
86
+ export const [getUrlStateView, setUrlStateView, removeUrlStateView] = newAdapter<Navigation>(
87
+ "model:url-state",
88
+ () => new Navigation(),
89
+ );