zero-query 1.0.9 → 1.2.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 -21
- package/README.md +2 -0
- package/cli/args.js +33 -33
- package/cli/commands/build-api.js +443 -0
- package/cli/commands/build.js +254 -216
- package/cli/commands/bundle.js +1228 -1183
- package/cli/commands/create.js +137 -121
- package/cli/commands/dev/devtools/index.js +56 -56
- package/cli/commands/dev/devtools/js/components.js +49 -49
- package/cli/commands/dev/devtools/js/core.js +423 -423
- package/cli/commands/dev/devtools/js/elements.js +421 -421
- package/cli/commands/dev/devtools/js/network.js +166 -166
- package/cli/commands/dev/devtools/js/performance.js +73 -73
- package/cli/commands/dev/devtools/js/router.js +105 -105
- package/cli/commands/dev/devtools/js/source.js +132 -132
- package/cli/commands/dev/devtools/js/stats.js +35 -35
- package/cli/commands/dev/devtools/js/tabs.js +79 -79
- package/cli/commands/dev/devtools/panel.html +95 -95
- package/cli/commands/dev/devtools/styles.css +244 -244
- package/cli/commands/dev/index.js +107 -107
- package/cli/commands/dev/logger.js +75 -75
- package/cli/commands/dev/overlay.js +858 -858
- package/cli/commands/dev/server.js +220 -167
- package/cli/commands/dev/validator.js +94 -94
- package/cli/commands/dev/watcher.js +172 -172
- package/cli/help.js +114 -112
- package/cli/index.js +52 -52
- package/cli/scaffold/default/LICENSE +21 -21
- package/cli/scaffold/default/app/app.js +207 -207
- package/cli/scaffold/default/app/components/about.js +201 -201
- package/cli/scaffold/default/app/components/api-demo.js +143 -143
- package/cli/scaffold/default/app/components/contact-card.js +231 -231
- package/cli/scaffold/default/app/components/contacts/contacts.css +706 -706
- package/cli/scaffold/default/app/components/contacts/contacts.html +200 -200
- package/cli/scaffold/default/app/components/contacts/contacts.js +196 -196
- package/cli/scaffold/default/app/components/counter.js +127 -127
- package/cli/scaffold/default/app/components/home.js +249 -249
- package/cli/scaffold/default/app/components/not-found.js +16 -16
- package/cli/scaffold/default/app/components/playground/playground.css +115 -115
- package/cli/scaffold/default/app/components/playground/playground.html +161 -161
- package/cli/scaffold/default/app/components/playground/playground.js +116 -116
- package/cli/scaffold/default/app/components/todos.js +225 -225
- package/cli/scaffold/default/app/components/toolkit/toolkit.css +97 -97
- package/cli/scaffold/default/app/components/toolkit/toolkit.html +146 -146
- package/cli/scaffold/default/app/components/toolkit/toolkit.js +280 -280
- package/cli/scaffold/default/app/routes.js +15 -15
- package/cli/scaffold/default/app/store.js +101 -101
- package/cli/scaffold/default/global.css +552 -552
- package/cli/scaffold/default/index.html +99 -99
- package/cli/scaffold/minimal/app/app.js +85 -85
- package/cli/scaffold/minimal/app/components/about.js +68 -68
- package/cli/scaffold/minimal/app/components/counter.js +122 -122
- package/cli/scaffold/minimal/app/components/home.js +68 -68
- package/cli/scaffold/minimal/app/components/not-found.js +16 -16
- package/cli/scaffold/minimal/app/routes.js +9 -9
- package/cli/scaffold/minimal/app/store.js +36 -36
- package/cli/scaffold/minimal/global.css +300 -300
- package/cli/scaffold/minimal/index.html +44 -44
- package/cli/scaffold/ssr/app/app.js +41 -41
- package/cli/scaffold/ssr/app/components/about.js +55 -55
- package/cli/scaffold/ssr/app/components/blog/index.js +65 -65
- package/cli/scaffold/ssr/app/components/blog/post.js +86 -86
- package/cli/scaffold/ssr/app/components/home.js +37 -37
- package/cli/scaffold/ssr/app/components/not-found.js +15 -15
- package/cli/scaffold/ssr/app/routes.js +8 -8
- package/cli/scaffold/ssr/global.css +228 -228
- package/cli/scaffold/ssr/index.html +37 -37
- package/cli/scaffold/ssr/package.json +8 -8
- package/cli/scaffold/ssr/server/data/posts.js +144 -144
- package/cli/scaffold/ssr/server/index.js +213 -213
- package/cli/scaffold/webrtc/app/app.js +11 -0
- package/cli/scaffold/webrtc/app/components/video-room.js +295 -0
- package/cli/scaffold/webrtc/app/lib/room.js +252 -0
- package/cli/scaffold/webrtc/assets/.gitkeep +0 -0
- package/cli/scaffold/webrtc/global.css +250 -0
- package/cli/scaffold/webrtc/index.html +21 -0
- package/cli/utils.js +305 -287
- package/dist/API.md +7264 -0
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +10313 -6252
- package/dist/zquery.min.js +8 -601
- package/index.d.ts +570 -365
- package/index.js +311 -232
- package/package.json +76 -69
- package/src/component.js +1709 -1454
- package/src/core.js +921 -921
- package/src/diff.js +497 -497
- package/src/errors.js +209 -209
- package/src/expression.js +922 -922
- package/src/http.js +242 -242
- package/src/package.json +1 -1
- package/src/reactive.js +255 -254
- package/src/router.js +843 -773
- package/src/ssr.js +418 -418
- package/src/store.js +318 -272
- package/src/utils.js +515 -515
- package/src/webrtc/e2ee.js +351 -0
- package/src/webrtc/errors.js +116 -0
- package/src/webrtc/ice.js +301 -0
- package/src/webrtc/index.js +131 -0
- package/src/webrtc/joinToken.js +119 -0
- package/src/webrtc/observe.js +172 -0
- package/src/webrtc/peer.js +351 -0
- package/src/webrtc/reactive.js +268 -0
- package/src/webrtc/room.js +625 -0
- package/src/webrtc/sdp.js +302 -0
- package/src/webrtc/sfu/index.js +43 -0
- package/src/webrtc/sfu/livekit.js +131 -0
- package/src/webrtc/sfu/mediasoup.js +150 -0
- package/src/webrtc/signaling.js +373 -0
- package/src/webrtc/turn.js +237 -0
- package/tests/_helpers/webrtcFakes.js +289 -0
- package/tests/audit.test.js +4158 -4158
- package/tests/cli.test.js +1136 -1023
- package/tests/compare.test.js +497 -0
- package/tests/component.test.js +3969 -3938
- package/tests/core.test.js +1910 -1910
- package/tests/dev-server.test.js +489 -0
- package/tests/diff.test.js +1416 -1416
- package/tests/docs.test.js +1664 -0
- package/tests/electron-features.test.js +864 -0
- package/tests/errors.test.js +619 -619
- package/tests/expression.test.js +1056 -1056
- package/tests/http.test.js +648 -648
- package/tests/reactive.test.js +819 -819
- package/tests/router.test.js +2327 -2327
- package/tests/ssr.test.js +870 -870
- package/tests/store.test.js +830 -830
- package/tests/test-minifier.js +153 -153
- package/tests/test-ssr.js +27 -27
- package/tests/utils.test.js +1377 -1377
- package/tests/webrtc/e2ee.test.js +283 -0
- package/tests/webrtc/ice.test.js +202 -0
- package/tests/webrtc/joinToken.test.js +89 -0
- package/tests/webrtc/observe.test.js +111 -0
- package/tests/webrtc/peer.test.js +373 -0
- package/tests/webrtc/reactive.test.js +235 -0
- package/tests/webrtc/room.test.js +406 -0
- package/tests/webrtc/sdp.test.js +151 -0
- package/tests/webrtc/sfu-livekit.test.js +119 -0
- package/tests/webrtc/sfu.test.js +160 -0
- package/tests/webrtc/signaling.test.js +251 -0
- package/tests/webrtc/turn.test.js +256 -0
- package/types/collection.d.ts +383 -383
- package/types/component.d.ts +186 -186
- package/types/errors.d.ts +135 -135
- package/types/http.d.ts +92 -92
- package/types/misc.d.ts +201 -201
- package/types/reactive.d.ts +98 -98
- package/types/router.d.ts +190 -190
- package/types/ssr.d.ts +102 -102
- package/types/store.d.ts +146 -145
- package/types/utils.d.ts +245 -245
- package/types/webrtc.d.ts +653 -0
package/types/ssr.d.ts
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Server-Side Rendering - render components to HTML strings.
|
|
3
|
-
*
|
|
4
|
-
* @module ssr
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { ComponentDefinition } from './component';
|
|
8
|
-
|
|
9
|
-
/** SSR application instance for server-side component rendering. */
|
|
10
|
-
export interface SSRApp {
|
|
11
|
-
/** Register a component for SSR. */
|
|
12
|
-
component(name: string, definition: ComponentDefinition): SSRApp;
|
|
13
|
-
|
|
14
|
-
/** Check whether a component is registered. */
|
|
15
|
-
has(name: string): boolean;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Render a component to an HTML string.
|
|
19
|
-
* @param componentName Registered component name.
|
|
20
|
-
* @param props Props to pass to the component.
|
|
21
|
-
* @param options Rendering options.
|
|
22
|
-
*/
|
|
23
|
-
renderToString(
|
|
24
|
-
componentName: string,
|
|
25
|
-
props?: Record<string, any>,
|
|
26
|
-
options?: { hydrate?: boolean; mode?: 'html' | 'fragment' },
|
|
27
|
-
): Promise<string>;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Render multiple components as a batch.
|
|
31
|
-
*/
|
|
32
|
-
renderBatch(
|
|
33
|
-
entries: Array<{ name: string; props?: Record<string, any>; options?: { hydrate?: boolean; mode?: 'html' | 'fragment' } }>,
|
|
34
|
-
): Promise<string[]>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Render a full HTML page with a component mounted in a shell.
|
|
38
|
-
*/
|
|
39
|
-
renderPage(options: {
|
|
40
|
-
component?: string;
|
|
41
|
-
props?: Record<string, any>;
|
|
42
|
-
title?: string;
|
|
43
|
-
description?: string;
|
|
44
|
-
styles?: string[];
|
|
45
|
-
scripts?: string[];
|
|
46
|
-
lang?: string;
|
|
47
|
-
meta?: string;
|
|
48
|
-
bodyAttrs?: string;
|
|
49
|
-
head?: {
|
|
50
|
-
canonical?: string;
|
|
51
|
-
og?: Record<string, string>;
|
|
52
|
-
};
|
|
53
|
-
}): Promise<string>;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Render a component into an existing HTML shell template.
|
|
57
|
-
*
|
|
58
|
-
* Unlike `renderPage()` which generates a full document from scratch,
|
|
59
|
-
* `renderShell()` takes your own `index.html` (with nav, footer, custom
|
|
60
|
-
* markup) and injects the SSR-rendered component body plus metadata.
|
|
61
|
-
*
|
|
62
|
-
* Handles `<z-outlet>` injection, `<title>` replacement, meta description,
|
|
63
|
-
* Open Graph tags, and `window.__SSR_DATA__` hydration data.
|
|
64
|
-
*
|
|
65
|
-
* @param shell - HTML template string (your index.html content).
|
|
66
|
-
* @param options - Rendering and metadata options.
|
|
67
|
-
*/
|
|
68
|
-
renderShell(shell: string, options?: {
|
|
69
|
-
/** Registered component name to render into `<z-outlet>`. */
|
|
70
|
-
component?: string;
|
|
71
|
-
/** Props passed to the component. */
|
|
72
|
-
props?: Record<string, any>;
|
|
73
|
-
/** Page title — replaces `<title>`. */
|
|
74
|
-
title?: string;
|
|
75
|
-
/** Meta description — replaces `<meta name="description">`. */
|
|
76
|
-
description?: string;
|
|
77
|
-
/** Open Graph tags to replace or inject (e.g. `{ title, description, type, image }`). */
|
|
78
|
-
og?: Record<string, string>;
|
|
79
|
-
/** Data embedded as `window.__SSR_DATA__` for client-side hydration. */
|
|
80
|
-
ssrData?: any;
|
|
81
|
-
/** Options passed through to `renderToString()` (hydrate, mode). */
|
|
82
|
-
renderOptions?: { hydrate?: boolean; mode?: 'html' | 'fragment' };
|
|
83
|
-
}): Promise<string>;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Create an SSR application instance. */
|
|
87
|
-
export function createSSRApp(): SSRApp;
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Quick one-shot render of a component definition to an HTML string.
|
|
91
|
-
* @param definition Component definition object.
|
|
92
|
-
* @param props Props to pass.
|
|
93
|
-
*/
|
|
94
|
-
export function renderToString(definition: ComponentDefinition, props?: Record<string, any>): string;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Escape HTML entities - exposed for use in SSR templates.
|
|
98
|
-
*/
|
|
99
|
-
export function escapeHtml(str: string): string;
|
|
100
|
-
|
|
101
|
-
// Re-exported from router for SSR server convenience
|
|
102
|
-
export { matchRoute, RouteMatch } from './router';
|
|
1
|
+
/**
|
|
2
|
+
* Server-Side Rendering - render components to HTML strings.
|
|
3
|
+
*
|
|
4
|
+
* @module ssr
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ComponentDefinition } from './component';
|
|
8
|
+
|
|
9
|
+
/** SSR application instance for server-side component rendering. */
|
|
10
|
+
export interface SSRApp {
|
|
11
|
+
/** Register a component for SSR. */
|
|
12
|
+
component(name: string, definition: ComponentDefinition): SSRApp;
|
|
13
|
+
|
|
14
|
+
/** Check whether a component is registered. */
|
|
15
|
+
has(name: string): boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Render a component to an HTML string.
|
|
19
|
+
* @param componentName Registered component name.
|
|
20
|
+
* @param props Props to pass to the component.
|
|
21
|
+
* @param options Rendering options.
|
|
22
|
+
*/
|
|
23
|
+
renderToString(
|
|
24
|
+
componentName: string,
|
|
25
|
+
props?: Record<string, any>,
|
|
26
|
+
options?: { hydrate?: boolean; mode?: 'html' | 'fragment' },
|
|
27
|
+
): Promise<string>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Render multiple components as a batch.
|
|
31
|
+
*/
|
|
32
|
+
renderBatch(
|
|
33
|
+
entries: Array<{ name: string; props?: Record<string, any>; options?: { hydrate?: boolean; mode?: 'html' | 'fragment' } }>,
|
|
34
|
+
): Promise<string[]>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Render a full HTML page with a component mounted in a shell.
|
|
38
|
+
*/
|
|
39
|
+
renderPage(options: {
|
|
40
|
+
component?: string;
|
|
41
|
+
props?: Record<string, any>;
|
|
42
|
+
title?: string;
|
|
43
|
+
description?: string;
|
|
44
|
+
styles?: string[];
|
|
45
|
+
scripts?: string[];
|
|
46
|
+
lang?: string;
|
|
47
|
+
meta?: string;
|
|
48
|
+
bodyAttrs?: string;
|
|
49
|
+
head?: {
|
|
50
|
+
canonical?: string;
|
|
51
|
+
og?: Record<string, string>;
|
|
52
|
+
};
|
|
53
|
+
}): Promise<string>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Render a component into an existing HTML shell template.
|
|
57
|
+
*
|
|
58
|
+
* Unlike `renderPage()` which generates a full document from scratch,
|
|
59
|
+
* `renderShell()` takes your own `index.html` (with nav, footer, custom
|
|
60
|
+
* markup) and injects the SSR-rendered component body plus metadata.
|
|
61
|
+
*
|
|
62
|
+
* Handles `<z-outlet>` injection, `<title>` replacement, meta description,
|
|
63
|
+
* Open Graph tags, and `window.__SSR_DATA__` hydration data.
|
|
64
|
+
*
|
|
65
|
+
* @param shell - HTML template string (your index.html content).
|
|
66
|
+
* @param options - Rendering and metadata options.
|
|
67
|
+
*/
|
|
68
|
+
renderShell(shell: string, options?: {
|
|
69
|
+
/** Registered component name to render into `<z-outlet>`. */
|
|
70
|
+
component?: string;
|
|
71
|
+
/** Props passed to the component. */
|
|
72
|
+
props?: Record<string, any>;
|
|
73
|
+
/** Page title — replaces `<title>`. */
|
|
74
|
+
title?: string;
|
|
75
|
+
/** Meta description — replaces `<meta name="description">`. */
|
|
76
|
+
description?: string;
|
|
77
|
+
/** Open Graph tags to replace or inject (e.g. `{ title, description, type, image }`). */
|
|
78
|
+
og?: Record<string, string>;
|
|
79
|
+
/** Data embedded as `window.__SSR_DATA__` for client-side hydration. */
|
|
80
|
+
ssrData?: any;
|
|
81
|
+
/** Options passed through to `renderToString()` (hydrate, mode). */
|
|
82
|
+
renderOptions?: { hydrate?: boolean; mode?: 'html' | 'fragment' };
|
|
83
|
+
}): Promise<string>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Create an SSR application instance. */
|
|
87
|
+
export function createSSRApp(): SSRApp;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Quick one-shot render of a component definition to an HTML string.
|
|
91
|
+
* @param definition Component definition object.
|
|
92
|
+
* @param props Props to pass.
|
|
93
|
+
*/
|
|
94
|
+
export function renderToString(definition: ComponentDefinition, props?: Record<string, any>): string;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Escape HTML entities - exposed for use in SSR templates.
|
|
98
|
+
*/
|
|
99
|
+
export function escapeHtml(str: string): string;
|
|
100
|
+
|
|
101
|
+
// Re-exported from router for SSR server convenience
|
|
102
|
+
export { matchRoute, RouteMatch } from './router';
|
package/types/store.d.ts
CHANGED
|
@@ -1,145 +1,146 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Store - reactive global state management.
|
|
3
|
-
*
|
|
4
|
-
* @module store
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { ReactiveProxy } from './reactive';
|
|
8
|
-
|
|
9
|
-
/** Store configuration. */
|
|
10
|
-
export interface StoreConfig<
|
|
11
|
-
S extends Record<string, any> = Record<string, any>,
|
|
12
|
-
A extends Record<string, (state: S, ...args: any[]) => any> = Record<string, (state: S, ...args: any[]) => any>,
|
|
13
|
-
G extends Record<string, (state: S) => any> = Record<string, (state: S) => any>,
|
|
14
|
-
> {
|
|
15
|
-
/** Initial state. Function form creates a fresh copy. */
|
|
16
|
-
state?: S | (() => S);
|
|
17
|
-
|
|
18
|
-
/** Named action functions. The first argument is always the reactive `state`. */
|
|
19
|
-
actions?: A;
|
|
20
|
-
|
|
21
|
-
/** Computed getters derived from state (lazily evaluated). */
|
|
22
|
-
getters?: G;
|
|
23
|
-
|
|
24
|
-
/** Log dispatched actions to the console. */
|
|
25
|
-
debug?: boolean;
|
|
26
|
-
|
|
27
|
-
/** Maximum number of action history entries to keep (default `1000`). */
|
|
28
|
-
maxHistory?: number;
|
|
29
|
-
|
|
30
|
-
/** Maximum number of undo checkpoints to keep (default `50`). */
|
|
31
|
-
maxUndo?: number;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** A store action history entry. */
|
|
35
|
-
export interface StoreHistoryEntry {
|
|
36
|
-
action: string;
|
|
37
|
-
args: any[];
|
|
38
|
-
timestamp: number;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** The reactive store instance. */
|
|
42
|
-
export interface StoreInstance<
|
|
43
|
-
S extends Record<string, any> = Record<string, any>,
|
|
44
|
-
A extends Record<string, (state: S, ...args: any[]) => any> = Record<string, (state: S, ...args: any[]) => any>,
|
|
45
|
-
G extends Record<string, (state: S) => any> = Record<string, (state: S) => any>,
|
|
46
|
-
> {
|
|
47
|
-
/** Reactive state proxy. Read / write triggers subscriptions. */
|
|
48
|
-
state: S & ReactiveProxy<S>;
|
|
49
|
-
|
|
50
|
-
/** Computed getters object. */
|
|
51
|
-
readonly getters: { readonly [K in keyof G]: ReturnType<G[K]> };
|
|
52
|
-
|
|
53
|
-
/** Log of all dispatched actions. */
|
|
54
|
-
readonly history: ReadonlyArray<StoreHistoryEntry>;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Execute a named action.
|
|
58
|
-
* @returns The action's return value.
|
|
59
|
-
*/
|
|
60
|
-
dispatch<K extends keyof A>(
|
|
61
|
-
name: K,
|
|
62
|
-
...args: A[K] extends (state: any, ...rest: infer P) => any ? P : any[]
|
|
63
|
-
): ReturnType<A[K]>;
|
|
64
|
-
dispatch(name: string, ...args: any[]): any;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Subscribe to changes on a specific state key.
|
|
68
|
-
* Callback receives `(key, newValue, oldValue)`.
|
|
69
|
-
* @returns An unsubscribe function.
|
|
70
|
-
*/
|
|
71
|
-
subscribe(key: string, fn: (key: string, value: any, oldValue: any) => void): () => void;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Subscribe to all state changes (wildcard).
|
|
75
|
-
* Callback receives `(key, newValue, oldValue)`.
|
|
76
|
-
* @returns An unsubscribe function.
|
|
77
|
-
*/
|
|
78
|
-
subscribe(fn: (key: string, value: any, oldValue: any) => void): () => void;
|
|
79
|
-
|
|
80
|
-
/** Deep clone of the current state. */
|
|
81
|
-
snapshot(): S;
|
|
82
|
-
|
|
83
|
-
/** Replace the entire state. */
|
|
84
|
-
replaceState(newState: Partial<S>): void;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Add middleware. Return `false` from `fn` to block the action.
|
|
88
|
-
* Chainable.
|
|
89
|
-
*/
|
|
90
|
-
use(fn: (actionName: string, args: any[], state: S) => boolean | void): StoreInstance<S, A, G>;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Replace state and clear action history.
|
|
94
|
-
* If no argument, resets to the original initial state.
|
|
95
|
-
*/
|
|
96
|
-
reset(initialState?: Partial<S>): void;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Batch multiple state changes - subscribers fire once at the end
|
|
100
|
-
* with only the latest value per key.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
*
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
*
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Store - reactive global state management.
|
|
3
|
+
*
|
|
4
|
+
* @module store
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ReactiveProxy } from './reactive';
|
|
8
|
+
|
|
9
|
+
/** Store configuration. */
|
|
10
|
+
export interface StoreConfig<
|
|
11
|
+
S extends Record<string, any> = Record<string, any>,
|
|
12
|
+
A extends Record<string, (state: S, ...args: any[]) => any> = Record<string, (state: S, ...args: any[]) => any>,
|
|
13
|
+
G extends Record<string, (state: S) => any> = Record<string, (state: S) => any>,
|
|
14
|
+
> {
|
|
15
|
+
/** Initial state. Function form creates a fresh copy. */
|
|
16
|
+
state?: S | (() => S);
|
|
17
|
+
|
|
18
|
+
/** Named action functions. The first argument is always the reactive `state`. */
|
|
19
|
+
actions?: A;
|
|
20
|
+
|
|
21
|
+
/** Computed getters derived from state (lazily evaluated). */
|
|
22
|
+
getters?: G;
|
|
23
|
+
|
|
24
|
+
/** Log dispatched actions to the console. */
|
|
25
|
+
debug?: boolean;
|
|
26
|
+
|
|
27
|
+
/** Maximum number of action history entries to keep (default `1000`). */
|
|
28
|
+
maxHistory?: number;
|
|
29
|
+
|
|
30
|
+
/** Maximum number of undo checkpoints to keep (default `50`). */
|
|
31
|
+
maxUndo?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A store action history entry. */
|
|
35
|
+
export interface StoreHistoryEntry {
|
|
36
|
+
action: string;
|
|
37
|
+
args: any[];
|
|
38
|
+
timestamp: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The reactive store instance. */
|
|
42
|
+
export interface StoreInstance<
|
|
43
|
+
S extends Record<string, any> = Record<string, any>,
|
|
44
|
+
A extends Record<string, (state: S, ...args: any[]) => any> = Record<string, (state: S, ...args: any[]) => any>,
|
|
45
|
+
G extends Record<string, (state: S) => any> = Record<string, (state: S) => any>,
|
|
46
|
+
> {
|
|
47
|
+
/** Reactive state proxy. Read / write triggers subscriptions. */
|
|
48
|
+
state: S & ReactiveProxy<S>;
|
|
49
|
+
|
|
50
|
+
/** Computed getters object. */
|
|
51
|
+
readonly getters: { readonly [K in keyof G]: ReturnType<G[K]> };
|
|
52
|
+
|
|
53
|
+
/** Log of all dispatched actions. */
|
|
54
|
+
readonly history: ReadonlyArray<StoreHistoryEntry>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Execute a named action.
|
|
58
|
+
* @returns The action's return value.
|
|
59
|
+
*/
|
|
60
|
+
dispatch<K extends keyof A>(
|
|
61
|
+
name: K,
|
|
62
|
+
...args: A[K] extends (state: any, ...rest: infer P) => any ? P : any[]
|
|
63
|
+
): ReturnType<A[K]>;
|
|
64
|
+
dispatch(name: string, ...args: any[]): any;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Subscribe to changes on a specific state key.
|
|
68
|
+
* Callback receives `(key, newValue, oldValue)`.
|
|
69
|
+
* @returns An unsubscribe function.
|
|
70
|
+
*/
|
|
71
|
+
subscribe(key: string, fn: (key: string, value: any, oldValue: any) => void): () => void;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Subscribe to all state changes (wildcard).
|
|
75
|
+
* Callback receives `(key, newValue, oldValue)`.
|
|
76
|
+
* @returns An unsubscribe function.
|
|
77
|
+
*/
|
|
78
|
+
subscribe(fn: (key: string, value: any, oldValue: any) => void): () => void;
|
|
79
|
+
|
|
80
|
+
/** Deep clone of the current state. */
|
|
81
|
+
snapshot(): S;
|
|
82
|
+
|
|
83
|
+
/** Replace the entire state. */
|
|
84
|
+
replaceState(newState: Partial<S>): void;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Add middleware. Return `false` from `fn` to block the action.
|
|
88
|
+
* Chainable.
|
|
89
|
+
*/
|
|
90
|
+
use(fn: (actionName: string, args: any[], state: S) => boolean | void): StoreInstance<S, A, G>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Replace state and clear action history.
|
|
94
|
+
* If no argument, resets to the original initial state.
|
|
95
|
+
*/
|
|
96
|
+
reset(initialState?: Partial<S>): void;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Batch multiple state changes - subscribers fire once at the end
|
|
100
|
+
* with only the latest value per key.
|
|
101
|
+
* @returns The callback's return value.
|
|
102
|
+
*/
|
|
103
|
+
batch<R = void>(fn: (state: S) => R): R;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Save a state snapshot for undo. Call before making changes you want to be undoable.
|
|
107
|
+
*/
|
|
108
|
+
checkpoint(): void;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Undo to the last checkpoint.
|
|
112
|
+
* @returns `true` if undo was performed, `false` if nothing to undo.
|
|
113
|
+
*/
|
|
114
|
+
undo(): boolean;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Redo the last undone state change.
|
|
118
|
+
* @returns `true` if redo was performed, `false` if nothing to redo.
|
|
119
|
+
*/
|
|
120
|
+
redo(): boolean;
|
|
121
|
+
|
|
122
|
+
/** Whether undo is available. */
|
|
123
|
+
readonly canUndo: boolean;
|
|
124
|
+
|
|
125
|
+
/** Whether redo is available. */
|
|
126
|
+
readonly canRedo: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Create a new global reactive store. */
|
|
130
|
+
export function createStore<
|
|
131
|
+
S extends Record<string, any>,
|
|
132
|
+
A extends Record<string, (state: S, ...args: any[]) => any>,
|
|
133
|
+
G extends Record<string, (state: S) => any>,
|
|
134
|
+
>(config: StoreConfig<S, A, G>): StoreInstance<S, A, G>;
|
|
135
|
+
export function createStore<
|
|
136
|
+
S extends Record<string, any>,
|
|
137
|
+
A extends Record<string, (state: S, ...args: any[]) => any>,
|
|
138
|
+
G extends Record<string, (state: S) => any>,
|
|
139
|
+
>(name: string, config: StoreConfig<S, A, G>): StoreInstance<S, A, G>;
|
|
140
|
+
|
|
141
|
+
/** Retrieve a previously created store by name (default: `'default'`). */
|
|
142
|
+
export function getStore<
|
|
143
|
+
S extends Record<string, any> = Record<string, any>,
|
|
144
|
+
A extends Record<string, (state: S, ...args: any[]) => any> = Record<string, (state: S, ...args: any[]) => any>,
|
|
145
|
+
G extends Record<string, (state: S) => any> = Record<string, (state: S) => any>,
|
|
146
|
+
>(name?: string): StoreInstance<S, A, G> | null;
|