@molecule/app-svelte 3.5.1 → 3.6.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/README.md +226 -72
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/live-store.d.ts +0 -8
- package/dist/hooks/live-store.d.ts.map +1 -1
- package/dist/hooks/live-store.js +0 -13
- package/dist/hooks/maybe-readable.d.ts +36 -0
- package/dist/hooks/maybe-readable.d.ts.map +1 -0
- package/dist/hooks/maybe-readable.js +36 -0
- package/dist/hooks/useAIModels.d.ts +8 -2
- package/dist/hooks/useAIModels.d.ts.map +1 -1
- package/dist/hooks/useAIModels.js +44 -17
- package/dist/hooks/useAuth.d.ts +10 -4
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +39 -13
- package/dist/hooks/useForm.d.ts +11 -3
- package/dist/hooks/useForm.d.ts.map +1 -1
- package/dist/hooks/useForm.js +89 -57
- package/dist/hooks/useHttp.d.ts +28 -22
- package/dist/hooks/useHttp.d.ts.map +1 -1
- package/dist/hooks/useHttp.js +67 -96
- package/dist/hooks/useOAuth.d.ts +23 -7
- package/dist/hooks/useOAuth.d.ts.map +1 -1
- package/dist/hooks/useOAuth.js +51 -18
- package/dist/hooks/useOptimisticValue.d.ts +11 -4
- package/dist/hooks/useOptimisticValue.d.ts.map +1 -1
- package/dist/hooks/useOptimisticValue.js +40 -49
- package/dist/hooks/usePush.d.ts +7 -3
- package/dist/hooks/usePush.d.ts.map +1 -1
- package/dist/hooks/usePush.js +26 -6
- package/dist/hooks/useRouter.d.ts +13 -7
- package/dist/hooks/useRouter.d.ts.map +1 -1
- package/dist/hooks/useRouter.js +39 -18
- package/dist/hooks/useStorage.d.ts +22 -9
- package/dist/hooks/useStorage.d.ts.map +1 -1
- package/dist/hooks/useStorage.js +146 -69
- package/dist/hooks/useStore.d.ts +14 -7
- package/dist/hooks/useStore.d.ts.map +1 -1
- package/dist/hooks/useStore.js +46 -9
- package/dist/hooks/useTranslation.d.ts +6 -2
- package/dist/hooks/useTranslation.d.ts.map +1 -1
- package/dist/hooks/useTranslation.js +11 -6
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/link/Link.svelte +33 -0
- package/dist/link/Link.svelte.d.ts +12 -0
- package/dist/link/Link.svelte.d.ts.map +1 -0
- package/dist/link/index.d.ts +23 -0
- package/dist/link/index.d.ts.map +1 -0
- package/dist/link/index.js +22 -0
- package/dist/link/types.d.ts +26 -0
- package/dist/link/types.d.ts.map +1 -0
- package/dist/link/types.js +6 -0
- package/dist/stores/chat.d.ts +4 -3
- package/dist/stores/chat.d.ts.map +1 -1
- package/dist/stores/chat.js +14 -15
- package/package.json +3 -3
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reactive argument every hook accepts: a plain value, or a readable store
|
|
3
|
+
* of one. A Svelte hook runs once per component, so the only way it can see a
|
|
4
|
+
* new argument is a store the app (or a `{#key}` block's parent) sets — the way
|
|
5
|
+
* React's hooks see a re-render.
|
|
6
|
+
*
|
|
7
|
+
* A store may hold `undefined`: that means the argument was omitted, and the
|
|
8
|
+
* parameter's default applies, exactly as for a missing argument.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { type Readable } from 'svelte/store';
|
|
13
|
+
/**
|
|
14
|
+
* A plain value or a readable store of one (a store of `undefined` stands for
|
|
15
|
+
* an omitted argument). Hooks read the current value when they use it and
|
|
16
|
+
* follow changes for as long as the component lives.
|
|
17
|
+
*/
|
|
18
|
+
export type MaybeReadable<T> = T | Readable<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Whether a value is a readable store. A molecule store (`@molecule/app-state`'s
|
|
21
|
+
* `Store`: it has `getState` AND `subscribe`) is a VALUE, never a source —
|
|
22
|
+
* `useStore` takes the store itself as its argument.
|
|
23
|
+
*
|
|
24
|
+
* @param value - A hook argument: a plain value or a store.
|
|
25
|
+
* @returns True for a Svelte readable store.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isReadable(value: unknown): value is Readable<unknown>;
|
|
28
|
+
/**
|
|
29
|
+
* The readable-store view of a hook argument: the store itself when it is one,
|
|
30
|
+
* a constant store of the plain value otherwise.
|
|
31
|
+
*
|
|
32
|
+
* @param value - A plain value or a store of one.
|
|
33
|
+
* @returns A readable store of the value.
|
|
34
|
+
*/
|
|
35
|
+
export declare function toReadable<T>(value: MaybeReadable<T>): Readable<T>;
|
|
36
|
+
//# sourceMappingURL=maybe-readable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maybe-readable.d.ts","sourceRoot":"","sources":["../../src/hooks/maybe-readable.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAEtD;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;AAE9C;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAAC,OAAO,CAAC,CAOrE;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAElE"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reactive argument every hook accepts: a plain value, or a readable store
|
|
3
|
+
* of one. A Svelte hook runs once per component, so the only way it can see a
|
|
4
|
+
* new argument is a store the app (or a `{#key}` block's parent) sets — the way
|
|
5
|
+
* React's hooks see a re-render.
|
|
6
|
+
*
|
|
7
|
+
* A store may hold `undefined`: that means the argument was omitted, and the
|
|
8
|
+
* parameter's default applies, exactly as for a missing argument.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { readable } from 'svelte/store';
|
|
13
|
+
/**
|
|
14
|
+
* Whether a value is a readable store. A molecule store (`@molecule/app-state`'s
|
|
15
|
+
* `Store`: it has `getState` AND `subscribe`) is a VALUE, never a source —
|
|
16
|
+
* `useStore` takes the store itself as its argument.
|
|
17
|
+
*
|
|
18
|
+
* @param value - A hook argument: a plain value or a store.
|
|
19
|
+
* @returns True for a Svelte readable store.
|
|
20
|
+
*/
|
|
21
|
+
export function isReadable(value) {
|
|
22
|
+
return (typeof value === 'object' &&
|
|
23
|
+
value !== null &&
|
|
24
|
+
typeof value.subscribe === 'function' &&
|
|
25
|
+
typeof value.getState !== 'function');
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The readable-store view of a hook argument: the store itself when it is one,
|
|
29
|
+
* a constant store of the plain value otherwise.
|
|
30
|
+
*
|
|
31
|
+
* @param value - A plain value or a store of one.
|
|
32
|
+
* @returns A readable store of the value.
|
|
33
|
+
*/
|
|
34
|
+
export function toReadable(value) {
|
|
35
|
+
return isReadable(value) ? value : readable(value);
|
|
36
|
+
}
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { type Readable } from 'svelte/store';
|
|
15
15
|
import type { AppModelDefinition, AppModeModelDefaults } from '@molecule/app-ai-models';
|
|
16
|
+
import { type MaybeReadable } from './maybe-readable.js';
|
|
16
17
|
/**
|
|
17
18
|
* Test-only: drops every cached model list so the next `useAIModels` call
|
|
18
19
|
* refetches. Exposed for unit tests; do not call from production code.
|
|
@@ -43,8 +44,13 @@ export interface UseAIModelsResult {
|
|
|
43
44
|
* subsequent mounts return the cached result, and a scope whose fetch failed
|
|
44
45
|
* does not refetch on a later mount.
|
|
45
46
|
*
|
|
47
|
+
* The project id may be a plain value or a readable store of one (a store
|
|
48
|
+
* holding `undefined` means the unscoped catalog): setting it resubscribes to
|
|
49
|
+
* the new scope's cache and fetches it if nobody has yet, on the same
|
|
50
|
+
* component, the way React's re-render does.
|
|
51
|
+
*
|
|
46
52
|
* @param projectId - Optional project scope: includes that project's custom
|
|
47
|
-
* ("bring your own AI") models on servers that support them.
|
|
53
|
+
* ("bring your own AI") models on servers that support them (or a store of it).
|
|
48
54
|
* @returns Models, free-tier model, loading flag, and error, as stores.
|
|
49
55
|
* @example
|
|
50
56
|
* ```svelte
|
|
@@ -54,5 +60,5 @@ export interface UseAIModelsResult {
|
|
|
54
60
|
* </script>
|
|
55
61
|
* ```
|
|
56
62
|
*/
|
|
57
|
-
export declare function useAIModels(projectId?: string): UseAIModelsResult;
|
|
63
|
+
export declare function useAIModels(projectId?: MaybeReadable<string | undefined>): UseAIModelsResult;
|
|
58
64
|
//# sourceMappingURL=useAIModels.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAIModels.d.ts","sourceRoot":"","sources":["../../src/hooks/useAIModels.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,
|
|
1
|
+
{"version":3,"file":"useAIModels.d.ts","sourceRoot":"","sources":["../../src/hooks/useAIModels.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAgB,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAEpE,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAMvF,OAAO,EAAE,KAAK,aAAa,EAAc,MAAM,qBAAqB,CAAA;AA8FpE;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CASzC;AAWD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,8EAA8E;IAC9E,MAAM,EAAE,QAAQ,CAAC,kBAAkB,EAAE,CAAC,CAAA;IACtC;;;OAGG;IACH,QAAQ,EAAE,QAAQ,CAAC,oBAAoB,GAAG,SAAS,CAAC,CAAA;IACpD,gEAAgE;IAChE,aAAa,EAAE,QAAQ,CAAC,kBAAkB,GAAG,SAAS,CAAC,CAAA;IACvD,mDAAmD;IACnD,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAC1B,+CAA+C;IAC/C,KAAK,EAAE,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,WAAW,CAAC,SAAS,CAAC,EAAE,aAAa,CAAC,MAAM,GAAG,SAAS,CAAC,GAAG,iBAAiB,CAuD5F"}
|
|
@@ -12,10 +12,11 @@
|
|
|
12
12
|
* @module
|
|
13
13
|
*/
|
|
14
14
|
import { onDestroy } from 'svelte';
|
|
15
|
-
import { derived, writable } from 'svelte/store';
|
|
15
|
+
import { derived, get, writable } from 'svelte/store';
|
|
16
16
|
import { loadAIModelCatalog, pickFreeTierModel } from '@molecule/app-ai-models';
|
|
17
17
|
import { useHttpClient } from '../context.js';
|
|
18
18
|
import { fieldStore } from './live-store.js';
|
|
19
|
+
import { toReadable } from './maybe-readable.js';
|
|
19
20
|
/** One cache entry per scope — `''` for the unscoped catalog, else the projectId. */
|
|
20
21
|
const caches = new Map();
|
|
21
22
|
/**
|
|
@@ -111,8 +112,13 @@ export function resetAIModelsCache() {
|
|
|
111
112
|
* subsequent mounts return the cached result, and a scope whose fetch failed
|
|
112
113
|
* does not refetch on a later mount.
|
|
113
114
|
*
|
|
115
|
+
* The project id may be a plain value or a readable store of one (a store
|
|
116
|
+
* holding `undefined` means the unscoped catalog): setting it resubscribes to
|
|
117
|
+
* the new scope's cache and fetches it if nobody has yet, on the same
|
|
118
|
+
* component, the way React's re-render does.
|
|
119
|
+
*
|
|
114
120
|
* @param projectId - Optional project scope: includes that project's custom
|
|
115
|
-
* ("bring your own AI") models on servers that support them.
|
|
121
|
+
* ("bring your own AI") models on servers that support them (or a store of it).
|
|
116
122
|
* @returns Models, free-tier model, loading flag, and error, as stores.
|
|
117
123
|
* @example
|
|
118
124
|
* ```svelte
|
|
@@ -124,26 +130,47 @@ export function resetAIModelsCache() {
|
|
|
124
130
|
*/
|
|
125
131
|
export function useAIModels(projectId) {
|
|
126
132
|
const http = useHttpClient();
|
|
127
|
-
const
|
|
128
|
-
|
|
133
|
+
const projectIdStore = toReadable(projectId);
|
|
134
|
+
let scope = get(projectIdStore) ?? '';
|
|
135
|
+
const initialCache = cacheFor(scope);
|
|
129
136
|
const snapshot = writable({
|
|
130
|
-
models:
|
|
131
|
-
defaults:
|
|
132
|
-
error:
|
|
137
|
+
models: initialCache.models,
|
|
138
|
+
defaults: initialCache.defaults,
|
|
139
|
+
error: initialCache.error,
|
|
133
140
|
});
|
|
134
141
|
const listener = (next) => snapshot.set(next);
|
|
135
|
-
|
|
142
|
+
/** React's [http, scope] effect for one scope: resync, then fetch if new. */
|
|
143
|
+
const attachScope = (scopeToAttach) => {
|
|
144
|
+
const cache = cacheFor(scopeToAttach);
|
|
145
|
+
listenersFor(scopeToAttach).add(listener);
|
|
146
|
+
// Resync from the cache the scope now names (React's effect re-syncs what
|
|
147
|
+
// its lazy useState init may have raced).
|
|
148
|
+
snapshot.set({ models: cache.models, defaults: cache.defaults, error: cache.error });
|
|
149
|
+
if (!cache.models && !cache.error) {
|
|
150
|
+
void fetchOnce(http, scopeToAttach).catch((_error) => {
|
|
151
|
+
// fetchOnce mirrors the error into cache.error, which the listener delivers.
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const detachScope = (scopeToDetach) => {
|
|
156
|
+
listenersFor(scopeToDetach).delete(listener);
|
|
157
|
+
};
|
|
158
|
+
attachScope(scope);
|
|
136
159
|
onDestroy(() => {
|
|
137
|
-
|
|
160
|
+
detachScope(scope);
|
|
138
161
|
});
|
|
139
|
-
//
|
|
140
|
-
//
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
162
|
+
// Follow the project id: a changed scope re-runs the effect — resubscribe,
|
|
163
|
+
// resync from that scope's cache, fetch it if nobody has. The store calls
|
|
164
|
+
// back at once with the id already read, which changes nothing.
|
|
165
|
+
const stopProjectId = projectIdStore.subscribe((next) => {
|
|
166
|
+
const nextScope = next ?? '';
|
|
167
|
+
if (Object.is(nextScope, scope))
|
|
168
|
+
return;
|
|
169
|
+
detachScope(scope);
|
|
170
|
+
scope = nextScope;
|
|
171
|
+
attachScope(scope);
|
|
172
|
+
});
|
|
173
|
+
onDestroy(stopProjectId);
|
|
147
174
|
const loading = derived(snapshot, (current) => !current.models && !current.error);
|
|
148
175
|
return {
|
|
149
176
|
models: derived(snapshot, (current) => current.models ?? []),
|
package/dist/hooks/useAuth.d.ts
CHANGED
|
@@ -5,11 +5,15 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @module
|
|
7
7
|
*/
|
|
8
|
-
import type
|
|
8
|
+
import { type Readable } from 'svelte/store';
|
|
9
9
|
import type { AuthClient, AuthState, RegisterExtraShape } from '@molecule/app-auth';
|
|
10
|
+
import { type MaybeReadable } from './maybe-readable.js';
|
|
10
11
|
/** Options for {@link useAuth}. */
|
|
11
12
|
export interface UseAuthOptions {
|
|
12
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Whether to automatically refresh the session on mount, and again whenever
|
|
15
|
+
* the flag turns on or the session decision changes while it is on.
|
|
16
|
+
*/
|
|
13
17
|
autoRefresh?: boolean;
|
|
14
18
|
}
|
|
15
19
|
/**
|
|
@@ -63,7 +67,9 @@ export interface UseAuthResult<T = unknown, TRegister extends RegisterExtraShape
|
|
|
63
67
|
* app-owned mutation instead passes its canonical response to `setUser`. For
|
|
64
68
|
* a sign-in the app itself runs, call `setSession({ accessToken, user })`.
|
|
65
69
|
*
|
|
66
|
-
* @param options - Hook options (`autoRefresh`)
|
|
70
|
+
* @param options - Hook options (`autoRefresh`), or a store of them: setting
|
|
71
|
+
* `autoRefresh` later probes on the same component, the way React's
|
|
72
|
+
* re-render re-runs its effect.
|
|
67
73
|
* @returns Auth state as stores and the actions as functions.
|
|
68
74
|
* @example
|
|
69
75
|
* ```svelte
|
|
@@ -80,7 +86,7 @@ export interface UseAuthResult<T = unknown, TRegister extends RegisterExtraShape
|
|
|
80
86
|
* {/if}
|
|
81
87
|
* ```
|
|
82
88
|
*/
|
|
83
|
-
export declare function useAuth<T = unknown, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>>(options?: UseAuthOptions): UseAuthResult<T, TRegister>;
|
|
89
|
+
export declare function useAuth<T = unknown, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>>(options?: MaybeReadable<UseAuthOptions | undefined>): UseAuthResult<T, TRegister>;
|
|
84
90
|
/**
|
|
85
91
|
* The authenticated user, React's `useUser` in Svelte's idiom: a readable
|
|
86
92
|
* store of the cached profile (or null).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useAuth.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useAuth.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAO,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,KAAK,EACV,UAAU,EAEV,SAAS,EAGT,kBAAkB,EACnB,MAAM,oBAAoB,CAAA;AAI3B,OAAO,EAAE,KAAK,aAAa,EAAc,MAAM,qBAAqB,CAAA;AAEpE,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,aAAa,CAC5B,CAAC,GAAG,OAAO,EACX,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC;IAEtE;;;;OAIG;IACH,MAAM,EAAE,QAAQ,CAAC,SAAS,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AACH,wBAAgB,OAAO,CAAC,CAAC,GAAG,OAAO,KAAK,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAGzD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,IAAI,QAAQ,CAAC,OAAO,CAAC,CAGtD"}
|
package/dist/hooks/useAuth.js
CHANGED
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @module
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { onDestroy } from 'svelte';
|
|
9
|
+
import { get } from 'svelte/store';
|
|
9
10
|
import { useAuthClient } from '../context.js';
|
|
10
11
|
import { fieldStore, liveStore } from './live-store.js';
|
|
12
|
+
import { toReadable } from './maybe-readable.js';
|
|
11
13
|
/**
|
|
12
14
|
* Authentication state and actions, React's `useAuth` in Svelte's idiom: call
|
|
13
15
|
* it during component init under `setMoleculeContext({ auth })`.
|
|
@@ -19,7 +21,9 @@ import { fieldStore, liveStore } from './live-store.js';
|
|
|
19
21
|
* app-owned mutation instead passes its canonical response to `setUser`. For
|
|
20
22
|
* a sign-in the app itself runs, call `setSession({ accessToken, user })`.
|
|
21
23
|
*
|
|
22
|
-
* @param options - Hook options (`autoRefresh`)
|
|
24
|
+
* @param options - Hook options (`autoRefresh`), or a store of them: setting
|
|
25
|
+
* `autoRefresh` later probes on the same component, the way React's
|
|
26
|
+
* re-render re-runs its effect.
|
|
23
27
|
* @returns Auth state as stores and the actions as functions.
|
|
24
28
|
* @example
|
|
25
29
|
* ```svelte
|
|
@@ -38,18 +42,40 @@ import { fieldStore, liveStore } from './live-store.js';
|
|
|
38
42
|
*/
|
|
39
43
|
export function useAuth(options) {
|
|
40
44
|
const client = useAuthClient();
|
|
41
|
-
const
|
|
45
|
+
const optionsStore = toReadable(options);
|
|
46
|
+
const autoRefresh = () => get(optionsStore)?.autoRefresh ?? false;
|
|
42
47
|
const state = liveStore(() => client.getState(), (notify) => client.onAuthChange(notify));
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
// React's autoRefresh effect, re-run when `autoRefresh`, `authenticated` or
|
|
49
|
+
// `loading` change (its dependency array): while autoRefresh is on and the
|
|
50
|
+
// session is neither signed in nor being decided, probe once per change.
|
|
51
|
+
let lastProbe = null;
|
|
52
|
+
const maybeProbe = () => {
|
|
53
|
+
const current = client.getState();
|
|
54
|
+
const next = {
|
|
55
|
+
autoRefresh: autoRefresh(),
|
|
56
|
+
authenticated: current.authenticated,
|
|
57
|
+
loading: current.loading,
|
|
58
|
+
};
|
|
59
|
+
if (lastProbe !== null &&
|
|
60
|
+
next.autoRefresh === lastProbe.autoRefresh &&
|
|
61
|
+
next.authenticated === lastProbe.authenticated &&
|
|
62
|
+
next.loading === lastProbe.loading) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
lastProbe = next;
|
|
66
|
+
if (next.autoRefresh && !next.authenticated && !next.loading) {
|
|
67
|
+
void client.refresh().catch((_error) => {
|
|
68
|
+
// No session to refresh: the state already says unauthenticated.
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
maybeProbe();
|
|
73
|
+
const stopState = state.subscribe(() => maybeProbe());
|
|
74
|
+
const stopOptions = optionsStore.subscribe(() => maybeProbe());
|
|
75
|
+
onDestroy(() => {
|
|
76
|
+
stopState();
|
|
77
|
+
stopOptions();
|
|
78
|
+
});
|
|
53
79
|
return {
|
|
54
80
|
status: fieldStore(state, 'status'),
|
|
55
81
|
state,
|
package/dist/hooks/useForm.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { type Readable } from 'svelte/store';
|
|
7
7
|
import type { FieldRegistration, FieldState, FormController, FormOptions, FormResetOptions, FormState, FormValuesConstraint, RegisterOptions } from '@molecule/app-forms';
|
|
8
|
+
import { type MaybeReadable } from './maybe-readable.js';
|
|
8
9
|
/**
|
|
9
10
|
* Options for useForm hook.
|
|
10
11
|
*
|
|
@@ -87,8 +88,15 @@ export interface UseFormResult<T extends FormValuesConstraint<T>> {
|
|
|
87
88
|
* take the loaded values. A changed `resetKey` starts a fresh form; fresh
|
|
88
89
|
* `defaultValues` without one leave the drafts alone.
|
|
89
90
|
*
|
|
90
|
-
*
|
|
91
|
-
* `
|
|
91
|
+
* The options may be a plain value or a readable store of one (a store holding
|
|
92
|
+
* `undefined` means the options were omitted): setting the store re-runs the
|
|
93
|
+
* work React's re-render does, on the same controller — a changed `values`
|
|
94
|
+
* object is applied (dirty fields kept by default) and a changed `resetKey`
|
|
95
|
+
* replaces the controller with a fresh one. Edits, touched fields and pending
|
|
96
|
+
* submits survive every other change.
|
|
97
|
+
*
|
|
98
|
+
* @param options - Form options (or a store of them). `defaultValues`, `mode`
|
|
99
|
+
* and the form-level `validate` are forwarded to the provider.
|
|
92
100
|
* @returns Form state stores and methods
|
|
93
101
|
* @example
|
|
94
102
|
* ```svelte
|
|
@@ -105,7 +113,7 @@ export interface UseFormResult<T extends FormValuesConstraint<T>> {
|
|
|
105
113
|
* </form>
|
|
106
114
|
* ```
|
|
107
115
|
*/
|
|
108
|
-
export declare function useForm<T extends FormValuesConstraint<T> = Record<string, unknown>>(options?: UseFormOptions<T>): UseFormResult<T>;
|
|
116
|
+
export declare function useForm<T extends FormValuesConstraint<T> = Record<string, unknown>>(options?: MaybeReadable<UseFormOptions<T> | undefined>): UseFormResult<T>;
|
|
109
117
|
/**
|
|
110
118
|
* Hook to watch a specific field value, React's `useWatch` in Svelte's idiom:
|
|
111
119
|
* a readable store of the field's current value.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../src/hooks/useForm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,
|
|
1
|
+
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../src/hooks/useForm.ts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;;GAOG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,oBAAoB,CAAC,CAAC,CAAC,EAC7D,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,EACvB,IAAI,EAAE,MAAM,CAAC,GACZ,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAUlC"}
|
package/dist/hooks/useForm.js
CHANGED
|
@@ -4,10 +4,9 @@
|
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
import { onDestroy } from 'svelte';
|
|
7
|
-
import { derived, writable } from 'svelte/store';
|
|
7
|
+
import { derived, get, writable } from 'svelte/store';
|
|
8
8
|
import { createForm as createBondedForm, formValuesEqual, snapshotFormValue, } from '@molecule/app-forms';
|
|
9
|
-
|
|
10
|
-
let formHandoff = null;
|
|
9
|
+
import { toReadable } from './maybe-readable.js';
|
|
11
10
|
/**
|
|
12
11
|
* Hook for form state management, React's `useForm` in Svelte's idiom: call it
|
|
13
12
|
* during component init (no provider needed — the controller comes from the
|
|
@@ -21,8 +20,15 @@ let formHandoff = null;
|
|
|
21
20
|
* take the loaded values. A changed `resetKey` starts a fresh form; fresh
|
|
22
21
|
* `defaultValues` without one leave the drafts alone.
|
|
23
22
|
*
|
|
24
|
-
*
|
|
25
|
-
* `
|
|
23
|
+
* The options may be a plain value or a readable store of one (a store holding
|
|
24
|
+
* `undefined` means the options were omitted): setting the store re-runs the
|
|
25
|
+
* work React's re-render does, on the same controller — a changed `values`
|
|
26
|
+
* object is applied (dirty fields kept by default) and a changed `resetKey`
|
|
27
|
+
* replaces the controller with a fresh one. Edits, touched fields and pending
|
|
28
|
+
* submits survive every other change.
|
|
29
|
+
*
|
|
30
|
+
* @param options - Form options (or a store of them). `defaultValues`, `mode`
|
|
31
|
+
* and the form-level `validate` are forwarded to the provider.
|
|
26
32
|
* @returns Form state stores and methods
|
|
27
33
|
* @example
|
|
28
34
|
* ```svelte
|
|
@@ -40,64 +46,85 @@ let formHandoff = null;
|
|
|
40
46
|
* ```
|
|
41
47
|
*/
|
|
42
48
|
export function useForm(options = {}) {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const form = sameForm ? adopted.form : createForm(formOptions);
|
|
51
|
-
const lane = sameForm
|
|
52
|
-
? adopted
|
|
53
|
-
: { dead: false, form, resetKey, lastExternal: null, stateTuples: new Map() };
|
|
54
|
-
lane.resetKey = resetKey;
|
|
55
|
-
if (sameForm)
|
|
56
|
-
lane.dead = false;
|
|
57
|
-
// React's mounted flag: false once the host is gone with no replacement.
|
|
58
|
-
let mounted = !lane.dead;
|
|
49
|
+
const optionsStore = toReadable(options);
|
|
50
|
+
const readOptions = () => get(optionsStore) ?? {};
|
|
51
|
+
const initial = readOptions();
|
|
52
|
+
// React's mounted flag: false once the component is gone. The controller
|
|
53
|
+
// itself stays as React leaves it: subscribed-to-nothing, never destroyed
|
|
54
|
+
// mid-flight.
|
|
55
|
+
let mounted = true;
|
|
59
56
|
onDestroy(() => {
|
|
60
57
|
mounted = false;
|
|
61
|
-
lane.dead = true;
|
|
62
|
-
// Offer the lane to a mount in the same turn (React's re-render); the
|
|
63
|
-
// timer clears it when no replacement adopted, so a mount after a settled
|
|
64
|
-
// teardown starts fresh. The controller itself stays as React leaves it:
|
|
65
|
-
// subscribed-to-nothing, never destroyed mid-flight.
|
|
66
|
-
formHandoff = lane;
|
|
67
|
-
const offered = lane;
|
|
68
|
-
setTimeout(() => {
|
|
69
|
-
if (formHandoff === offered)
|
|
70
|
-
formHandoff = null;
|
|
71
|
-
}, 0);
|
|
72
58
|
});
|
|
73
|
-
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
lane.lastExternal = null;
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
if (lane.lastExternal !== null && formValuesEqual(lane.lastExternal.values, values))
|
|
83
|
-
return;
|
|
84
|
-
lane.lastExternal = { values: snapshotFormValue(values) };
|
|
85
|
-
form.updateValues(values, { keepDirtyValues });
|
|
59
|
+
// The controller (React's formRef): created once, and replaced only when the
|
|
60
|
+
// resetKey changes by Object.is. Every method below reads the current one at
|
|
61
|
+
// call time, as React's re-created callbacks read theirs.
|
|
62
|
+
const createFor = (opts) => {
|
|
63
|
+
const { createForm = createBondedForm, resetKey: _resetKey, ...formOptions } = opts;
|
|
64
|
+
return createForm(formOptions);
|
|
86
65
|
};
|
|
87
|
-
|
|
66
|
+
let form = createFor(initial);
|
|
67
|
+
let formResetKey = initial.resetKey;
|
|
88
68
|
// Subscribed state (the controller rebuilds its state object only on change),
|
|
89
69
|
// read the way React's cached useSyncExternalStore snapshot is: one read at
|
|
90
70
|
// creation, one re-read right after subscribing, one per notification. The
|
|
91
71
|
// mirror is what asState's visible value reads (React's stateSnapshots).
|
|
92
72
|
let currentState = form.getState();
|
|
93
73
|
const state = writable(currentState);
|
|
94
|
-
|
|
95
|
-
|
|
74
|
+
let stopState = null;
|
|
75
|
+
const attachState = (controller) => {
|
|
76
|
+
stopState?.();
|
|
77
|
+
stopState = controller.subscribe(() => {
|
|
78
|
+
currentState = controller.getState();
|
|
79
|
+
state.set(currentState);
|
|
80
|
+
});
|
|
81
|
+
currentState = controller.getState();
|
|
96
82
|
state.set(currentState);
|
|
97
83
|
};
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
84
|
+
attachState(form);
|
|
85
|
+
onDestroy(() => {
|
|
86
|
+
stopState?.();
|
|
87
|
+
});
|
|
88
|
+
// The last external `values` applied, compared by content (React's ref): it
|
|
89
|
+
// survives an argument change; a reset may change the controller baseline
|
|
90
|
+
// without making an unchanged record external again.
|
|
91
|
+
let lastExternal = null;
|
|
92
|
+
const applyExternalValues = (opts) => {
|
|
93
|
+
const values = opts.values;
|
|
94
|
+
if (values === undefined) {
|
|
95
|
+
lastExternal = null;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (lastExternal !== null &&
|
|
99
|
+
lastExternal.form === form &&
|
|
100
|
+
formValuesEqual(lastExternal.values, values)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
lastExternal = { form, values: snapshotFormValue(values) };
|
|
104
|
+
form.updateValues(values, { keepDirtyValues: opts.resetOptions?.keepDirtyValues !== false });
|
|
105
|
+
};
|
|
106
|
+
applyExternalValues(initial);
|
|
107
|
+
// asState's [edit, fill] tuples, per controller (React's per-form Map): a
|
|
108
|
+
// replaced controller starts a fresh map, so tuples are rebuilt for it.
|
|
109
|
+
let stateTuples = new Map();
|
|
110
|
+
// What a changed options source does, on the same component: a changed
|
|
111
|
+
// resetKey replaces the controller (fresh defaults, fresh tuples, external
|
|
112
|
+
// values re-applied); otherwise a changed `values` is applied with the
|
|
113
|
+
// latest keepDirtyValues. The store calls back at once with the options the
|
|
114
|
+
// form was created with, which changes nothing.
|
|
115
|
+
const stopOptions = optionsStore.subscribe((next) => {
|
|
116
|
+
const opts = (next ?? {});
|
|
117
|
+
if (!Object.is(opts.resetKey, formResetKey)) {
|
|
118
|
+
form = createFor(opts);
|
|
119
|
+
formResetKey = opts.resetKey;
|
|
120
|
+
stateTuples = new Map();
|
|
121
|
+
attachState(form);
|
|
122
|
+
applyExternalValues(opts);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
applyExternalValues(opts);
|
|
126
|
+
});
|
|
127
|
+
onDestroy(stopOptions);
|
|
101
128
|
// Field methods
|
|
102
129
|
const register = (name, registerOptions) => form.register({ ...registerOptions, name: name });
|
|
103
130
|
const getValue = (name) => form.getValue(name);
|
|
@@ -122,7 +149,7 @@ export function useForm(options = {}) {
|
|
|
122
149
|
// The value the caller sees comes from the subscribed snapshot, not a
|
|
123
150
|
// fresh read: a fill that lands between renders must not jump ahead.
|
|
124
151
|
const visible = name === undefined ? currentState.values : currentState.values[name];
|
|
125
|
-
const existing =
|
|
152
|
+
const existing = stateTuples.get(name);
|
|
126
153
|
if (existing)
|
|
127
154
|
return [visible, ...existing];
|
|
128
155
|
const edit = (value) => {
|
|
@@ -145,13 +172,18 @@ export function useForm(options = {}) {
|
|
|
145
172
|
: value;
|
|
146
173
|
form.updateValues(name === undefined ? next : { [name]: next }, { keepDirtyValues: true });
|
|
147
174
|
};
|
|
148
|
-
|
|
175
|
+
stateTuples.set(name, [edit, fill]);
|
|
149
176
|
return [visible, edit, fill];
|
|
150
177
|
};
|
|
151
|
-
const handleSubmit = (onSubmit, onError) =>
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
178
|
+
const handleSubmit = (onSubmit, onError) => {
|
|
179
|
+
// The controller this runner was created for (React's per-render form): a
|
|
180
|
+
// submit runner from before a resetKey change does not call its handler.
|
|
181
|
+
const boundForm = form;
|
|
182
|
+
return form.handleSubmit((values) => {
|
|
183
|
+
if (mounted && form === boundForm)
|
|
184
|
+
return onSubmit(values);
|
|
185
|
+
}, onError);
|
|
186
|
+
};
|
|
155
187
|
return {
|
|
156
188
|
formState: { subscribe: state.subscribe },
|
|
157
189
|
isValid: derived(state, (current) => current.isValid),
|