@molecule/app-svelte 3.7.1 → 3.8.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 +363 -76
- package/dist/hooks/live-store.d.ts +18 -2
- package/dist/hooks/live-store.d.ts.map +1 -1
- package/dist/hooks/live-store.js +9 -5
- package/dist/hooks/useAIModels.d.ts +4 -1
- package/dist/hooks/useAIModels.d.ts.map +1 -1
- package/dist/hooks/useAIModels.js +8 -2
- package/dist/hooks/useForm.d.ts +6 -6
- package/dist/hooks/useForm.d.ts.map +1 -1
- package/dist/hooks/useForm.js +51 -23
- package/dist/hooks/useStore.d.ts +18 -1
- package/dist/hooks/useStore.d.ts.map +1 -1
- package/dist/hooks/useStore.js +5 -30
- package/dist/hooks/useTheme.d.ts.map +1 -1
- package/dist/hooks/useTheme.js +42 -13
- package/dist/index.d.ts +79 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +79 -0
- package/dist/link/Link.svelte +13 -3
- package/dist/link/Link.svelte.d.ts +1 -1
- package/dist/link/Link.svelte.d.ts.map +1 -1
- package/dist/link/types.d.ts +3 -1
- package/dist/link/types.d.ts.map +1 -1
- package/dist/stores/auth.d.ts +2 -2
- package/dist/stores/change-password.d.ts +1 -1
- package/dist/stores/change-password.d.ts.map +1 -1
- package/dist/stores/forms.d.ts.map +1 -1
- package/dist/stores/forms.js +4 -6
- package/dist/stores/login.d.ts +2 -2
- package/dist/stores/login.d.ts.map +1 -1
- package/dist/stores/promise.d.ts +6 -3
- package/dist/stores/promise.d.ts.map +1 -1
- package/dist/stores/promise.js +0 -1
- package/dist/stores/signup.d.ts +2 -2
- package/dist/stores/signup.d.ts.map +1 -1
- package/package.json +3 -3
|
@@ -13,6 +13,20 @@
|
|
|
13
13
|
import { type Readable } from 'svelte/store';
|
|
14
14
|
import type { PromiseStatus } from '@molecule/app-utilities';
|
|
15
15
|
import type { UsePromiseStore } from './usePromise.js';
|
|
16
|
+
/**
|
|
17
|
+
* A readable store that always holds `read()`'s value, plus a way to re-read
|
|
18
|
+
* the service now (`refresh`), which a hydrating hook uses to move from the
|
|
19
|
+
* value the server rendered to the live one.
|
|
20
|
+
*
|
|
21
|
+
* @template T - The value the store holds.
|
|
22
|
+
*/
|
|
23
|
+
export interface LiveStore<T> extends Readable<T> {
|
|
24
|
+
/**
|
|
25
|
+
* Re-reads the service immediately and publishes the value, without waiting
|
|
26
|
+
* for the service to notify. React's binding does this right after hydration.
|
|
27
|
+
*/
|
|
28
|
+
refresh(): void;
|
|
29
|
+
}
|
|
16
30
|
/**
|
|
17
31
|
* A readable store that always holds `read()`'s value.
|
|
18
32
|
*
|
|
@@ -23,9 +37,11 @@ import type { UsePromiseStore } from './usePromise.js';
|
|
|
23
37
|
*
|
|
24
38
|
* @param read - Reads the service's current value.
|
|
25
39
|
* @param subscribe - Subscribes the service; returns its unsubscribe.
|
|
26
|
-
* @
|
|
40
|
+
* @param initial - The value before the first live read, when a caller has
|
|
41
|
+
* one (a hydrating hook seeds the server's value, then calls `refresh`).
|
|
42
|
+
* @returns A readable store of the current value, with `refresh`.
|
|
27
43
|
*/
|
|
28
|
-
export declare function liveStore<T>(read: () => T, subscribe: (notify: () => void) => () => void):
|
|
44
|
+
export declare function liveStore<T>(read: () => T, subscribe: (notify: () => void) => () => void, initial?: T): LiveStore<T>;
|
|
29
45
|
/**
|
|
30
46
|
* One picked field of a state store, as its own readable store.
|
|
31
47
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"live-store.d.ts","sourceRoot":"","sources":["../../src/hooks/live-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAgB,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAEpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAE5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD
|
|
1
|
+
{"version":3,"file":"live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,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAClD,OAAO,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,GAC7B,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,CAW5B"}
|
package/dist/hooks/live-store.js
CHANGED
|
@@ -22,13 +22,17 @@ import { derived, get, writable } from 'svelte/store';
|
|
|
22
22
|
*
|
|
23
23
|
* @param read - Reads the service's current value.
|
|
24
24
|
* @param subscribe - Subscribes the service; returns its unsubscribe.
|
|
25
|
-
* @
|
|
25
|
+
* @param initial - The value before the first live read, when a caller has
|
|
26
|
+
* one (a hydrating hook seeds the server's value, then calls `refresh`).
|
|
27
|
+
* @returns A readable store of the current value, with `refresh`.
|
|
26
28
|
*/
|
|
27
|
-
export function liveStore(read, subscribe) {
|
|
28
|
-
const store = writable(
|
|
29
|
-
|
|
29
|
+
export function liveStore(read, subscribe, initial = read()) {
|
|
30
|
+
const store = writable(initial);
|
|
31
|
+
/** Re-reads the service now; `subscribe`'s callback is the same read. */
|
|
32
|
+
const refresh = () => store.set(read());
|
|
33
|
+
const stop = subscribe(refresh);
|
|
30
34
|
onDestroy(stop);
|
|
31
|
-
return { subscribe: store.subscribe };
|
|
35
|
+
return { subscribe: store.subscribe, refresh };
|
|
32
36
|
}
|
|
33
37
|
/**
|
|
34
38
|
* One picked field of a state store, as its own readable store.
|
|
@@ -23,7 +23,10 @@ export declare function resetAIModelsCache(): void;
|
|
|
23
23
|
* Result returned by `useAIModels`.
|
|
24
24
|
*/
|
|
25
25
|
export interface UseAIModelsResult {
|
|
26
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Available models, or an empty array while loading or after a failed fetch, as a readable store. The list
|
|
28
|
+
* keeps its identity until the catalog changes, the empty one included.
|
|
29
|
+
*/
|
|
27
30
|
models: Readable<AppModelDefinition[]>;
|
|
28
31
|
/**
|
|
29
32
|
* Per-mode server default model ids for the requester's tier, or `undefined`
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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;AAqGpE;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CASzC;AAWD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,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"}
|
|
@@ -17,6 +17,12 @@ import { loadAIModelCatalog, pickFreeTierModel } from '@molecule/app-ai-models';
|
|
|
17
17
|
import { useHttpClient } from '../context.js';
|
|
18
18
|
import { fieldStore } from './live-store.js';
|
|
19
19
|
import { toReadable } from './maybe-readable.js';
|
|
20
|
+
/**
|
|
21
|
+
* The list returned while the catalog loads or after it fails. One module-level array, like the catalog's
|
|
22
|
+
* own lists, which every caller on a scope shares: a new `[]` per read would change the identity of an
|
|
23
|
+
* unchanged catalog.
|
|
24
|
+
*/
|
|
25
|
+
const NO_MODELS = [];
|
|
20
26
|
/** One cache entry per scope — `''` for the unscoped catalog, else the projectId. */
|
|
21
27
|
const caches = new Map();
|
|
22
28
|
/**
|
|
@@ -173,9 +179,9 @@ export function useAIModels(projectId) {
|
|
|
173
179
|
onDestroy(stopProjectId);
|
|
174
180
|
const loading = derived(snapshot, (current) => !current.models && !current.error);
|
|
175
181
|
return {
|
|
176
|
-
models: derived(snapshot, (current) => current.models ??
|
|
182
|
+
models: derived(snapshot, (current) => current.models ?? NO_MODELS),
|
|
177
183
|
defaults: fieldStore(snapshot, 'defaults'),
|
|
178
|
-
freeTierModel: derived(snapshot, (current) => pickFreeTierModel(current.models ??
|
|
184
|
+
freeTierModel: derived(snapshot, (current) => pickFreeTierModel(current.models ?? NO_MODELS)),
|
|
179
185
|
loading,
|
|
180
186
|
error: fieldStore(snapshot, 'error'),
|
|
181
187
|
};
|
package/dist/hooks/useForm.d.ts
CHANGED
|
@@ -118,8 +118,8 @@ export declare function useForm<T extends FormValuesConstraint<T> = Record<strin
|
|
|
118
118
|
* Hook to watch a specific field value, React's `useWatch` in Svelte's idiom:
|
|
119
119
|
* a readable store of the field's current value.
|
|
120
120
|
*
|
|
121
|
-
* @param form - Form controller
|
|
122
|
-
* @param name - Field name to watch
|
|
121
|
+
* @param form - Form controller, or a readable store of one
|
|
122
|
+
* @param name - Field name to watch, or a readable store of one
|
|
123
123
|
* @returns A readable store of the current field value
|
|
124
124
|
* @example
|
|
125
125
|
* ```svelte
|
|
@@ -132,14 +132,14 @@ export declare function useForm<T extends FormValuesConstraint<T> = Record<strin
|
|
|
132
132
|
* <p>{$country}</p>
|
|
133
133
|
* ```
|
|
134
134
|
*/
|
|
135
|
-
export declare function useWatch<T extends FormValuesConstraint<T>, K extends keyof T>(form: FormController<T
|
|
135
|
+
export declare function useWatch<T extends FormValuesConstraint<T>, K extends keyof T>(form: MaybeReadable<FormController<T>>, name: MaybeReadable<K>): Readable<T[K]>;
|
|
136
136
|
/**
|
|
137
137
|
* Hook to get field-level state, React's `useFieldState` in Svelte's idiom: a
|
|
138
138
|
* readable store of the field's state.
|
|
139
139
|
*
|
|
140
|
-
* @param form - Form controller
|
|
141
|
-
* @param name - Field name
|
|
140
|
+
* @param form - Form controller, or a readable store of one
|
|
141
|
+
* @param name - Field name, or a readable store of one
|
|
142
142
|
* @returns A readable store of the field state (value, error, touched, dirty, valid, validating)
|
|
143
143
|
*/
|
|
144
|
-
export declare function useFieldState<T extends FormValuesConstraint<T>>(form: FormController<T
|
|
144
|
+
export declare function useFieldState<T extends FormValuesConstraint<T>>(form: MaybeReadable<FormController<T>>, name: MaybeReadable<keyof T>): Readable<FieldState<T[keyof T]>>;
|
|
145
145
|
//# sourceMappingURL=useForm.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../src/hooks/useForm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAgB,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAEpE,OAAO,KAAK,EACV,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,WAAW,EACX,gBAAgB,EAChB,SAAS,EACT,iBAAiB,EACjB,oBAAoB,EACpB,eAAe,EAChB,MAAM,qBAAqB,CAAA;AAO5B,OAAO,EAAE,KAAK,aAAa,EAAc,MAAM,qBAA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a controller selection and republishes what `read` returns.
|
|
207
|
+
*
|
|
208
|
+
* The controller and the field name are each a plain value or a readable
|
|
209
|
+
* store (React's `rerender` with new props is a store the app sets): a change
|
|
210
|
+
* re-subscribes the new controller — the old subscription is ended, so an
|
|
211
|
+
* update to a controller or field that is no longer selected is not read.
|
|
212
|
+
*
|
|
213
|
+
* @param form - The controller, or a store of it.
|
|
214
|
+
* @param name - The field name, or a store of it.
|
|
215
|
+
* @param read - Reads the selected field's published value from the controller.
|
|
216
|
+
* @returns The selected value as a readable store.
|
|
217
|
+
*/
|
|
218
|
+
function followField(form, name, read) {
|
|
219
|
+
const formSource = toReadable(form);
|
|
220
|
+
const nameSource = toReadable(name);
|
|
221
|
+
const state = writable(read(get(formSource), get(nameSource)));
|
|
222
|
+
let subscribed = null;
|
|
223
|
+
let stopController = null;
|
|
224
|
+
const follow = () => {
|
|
225
|
+
const controller = get(formSource);
|
|
226
|
+
const field = get(nameSource);
|
|
227
|
+
if (subscribed !== null && subscribed.controller === controller && subscribed.field === field) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
stopController?.();
|
|
231
|
+
subscribed = { controller, field };
|
|
232
|
+
stopController = controller.subscribe(() => {
|
|
233
|
+
state.set(read(controller, field));
|
|
234
|
+
});
|
|
235
|
+
// A controller can change after the first read but before the subscription
|
|
236
|
+
// settles (React's refresh right after subscribing).
|
|
237
|
+
state.set(read(controller, field));
|
|
238
|
+
};
|
|
239
|
+
// A store calls back at once with its current value; a plain value arrives as
|
|
240
|
+
// a constant store, so the first callback is the creation read's follow-up.
|
|
241
|
+
const stopForm = formSource.subscribe(follow);
|
|
242
|
+
const stopName = nameSource.subscribe(follow);
|
|
243
|
+
onDestroy(() => {
|
|
244
|
+
stopForm();
|
|
245
|
+
stopName();
|
|
246
|
+
stopController?.();
|
|
247
|
+
});
|
|
248
|
+
return { subscribe: state.subscribe };
|
|
249
|
+
}
|
|
205
250
|
/**
|
|
206
251
|
* Hook to watch a specific field value, React's `useWatch` in Svelte's idiom:
|
|
207
252
|
* a readable store of the field's current value.
|
|
208
253
|
*
|
|
209
|
-
* @param form - Form controller
|
|
210
|
-
* @param name - Field name to watch
|
|
254
|
+
* @param form - Form controller, or a readable store of one
|
|
255
|
+
* @param name - Field name to watch, or a readable store of one
|
|
211
256
|
* @returns A readable store of the current field value
|
|
212
257
|
* @example
|
|
213
258
|
* ```svelte
|
|
@@ -221,33 +266,16 @@ export function useForm(options = {}) {
|
|
|
221
266
|
* ```
|
|
222
267
|
*/
|
|
223
268
|
export function useWatch(form, name) {
|
|
224
|
-
|
|
225
|
-
// getValue), one read at creation, one re-read after subscribing, one per
|
|
226
|
-
// notification.
|
|
227
|
-
const state = writable(form.getState());
|
|
228
|
-
const refresh = () => {
|
|
229
|
-
state.set(form.getState());
|
|
230
|
-
};
|
|
231
|
-
const unsubscribe = form.subscribe(refresh);
|
|
232
|
-
refresh();
|
|
233
|
-
onDestroy(unsubscribe);
|
|
234
|
-
return derived(state, (current) => current.values[name]);
|
|
269
|
+
return followField(form, name, (controller, field) => controller.getState().values[field]);
|
|
235
270
|
}
|
|
236
271
|
/**
|
|
237
272
|
* Hook to get field-level state, React's `useFieldState` in Svelte's idiom: a
|
|
238
273
|
* readable store of the field's state.
|
|
239
274
|
*
|
|
240
|
-
* @param form - Form controller
|
|
241
|
-
* @param name - Field name
|
|
275
|
+
* @param form - Form controller, or a readable store of one
|
|
276
|
+
* @param name - Field name, or a readable store of one
|
|
242
277
|
* @returns A readable store of the field state (value, error, touched, dirty, valid, validating)
|
|
243
278
|
*/
|
|
244
279
|
export function useFieldState(form, name) {
|
|
245
|
-
|
|
246
|
-
const refresh = () => {
|
|
247
|
-
state.set(form.getFieldState(name));
|
|
248
|
-
};
|
|
249
|
-
const unsubscribe = form.subscribe(refresh);
|
|
250
|
-
refresh();
|
|
251
|
-
onDestroy(unsubscribe);
|
|
252
|
-
return { subscribe: state.subscribe };
|
|
280
|
+
return followField(form, name, (controller, field) => controller.getFieldState(field));
|
|
253
281
|
}
|
package/dist/hooks/useStore.d.ts
CHANGED
|
@@ -13,6 +13,14 @@ export interface UseStoreOptions<T, S> {
|
|
|
13
13
|
/** Compares two selected values; equal keeps the previous result. */
|
|
14
14
|
equalityFn?: (a: S, b: S) => boolean;
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Options for {@link useStore} that pick part of the store: a selector is
|
|
18
|
+
* required, so the selected type `S` always comes from it.
|
|
19
|
+
*/
|
|
20
|
+
export interface SelectingStoreOptions<T, S> extends UseStoreOptions<T, S> {
|
|
21
|
+
/** Picks the value the hook returns from the store's state. */
|
|
22
|
+
selector: (state: T) => S;
|
|
23
|
+
}
|
|
16
24
|
/**
|
|
17
25
|
* Subscribes a store with an optional selector and equality function, React's
|
|
18
26
|
* `useStore` in Svelte's idiom: a readable store of the selected value.
|
|
@@ -46,7 +54,16 @@ export interface UseStoreOptions<T, S> {
|
|
|
46
54
|
* <p>{$count} / {$doubled}</p>
|
|
47
55
|
* ```
|
|
48
56
|
*/
|
|
49
|
-
export declare function useStore<T
|
|
57
|
+
export declare function useStore<T>(store: MaybeReadable<Store<T>>, options?: MaybeReadable<UseStoreOptions<T, T> | undefined>): Readable<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Subscribes to the part of a store a selector picks.
|
|
60
|
+
*
|
|
61
|
+
* @param store - The store to subscribe to (or a readable store of it).
|
|
62
|
+
* @param options - The selector, and an optional equality function for its
|
|
63
|
+
* results, or a store of them.
|
|
64
|
+
* @returns A readable store of the selected value.
|
|
65
|
+
*/
|
|
66
|
+
export declare function useStore<T, S>(store: MaybeReadable<Store<T>>, options: MaybeReadable<SelectingStoreOptions<T, S>>): Readable<S>;
|
|
50
67
|
/**
|
|
51
68
|
* The store's setState function, React's `useSetStore` in Svelte's idiom.
|
|
52
69
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStore.d.ts","sourceRoot":"","sources":["../../src/hooks/useStore.ts"],"names":[],"mappings":"AAA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AAQ,CAAC,CAAC,EACxB,KAAK,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAC9B,OAAO,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,GACzD,QAAQ,CAAC,CAAC,CAAC,CAAA;AACd;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAC3B,KAAK,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAC9B,OAAO,EAAE,aAAa,CAAC,qBAAqB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAClD,QAAQ,CAAC,CAAC,CAAC,CAAA;AA+Dd;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAEpE;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,IAAI,SAAS,OAAO,EAAE,EAAE,CAAC,EACzD,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EACf,MAAM,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,IAAI,EAAE,IAAI,KAAK,CAAC,GAC/F,CAAC,GAAG,IAAI,EAAE,IAAI,KAAK,CAAC,CAGtB"}
|
package/dist/hooks/useStore.js
CHANGED
|
@@ -7,37 +7,12 @@ import { get } from 'svelte/store';
|
|
|
7
7
|
import { useSyncExternalStore } from './external-store.js';
|
|
8
8
|
import { toReadable } from './maybe-readable.js';
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* The store argument may be the store itself (a molecule store has `getState`
|
|
14
|
-
* AND `subscribe`, and is always a VALUE, never a source) or a readable store
|
|
15
|
-
* OF a store: setting it re-subscribes to the new store and re-reads its
|
|
16
|
-
* snapshot on the same component, the way React's re-render does. The options
|
|
17
|
-
* (or a store of them) are read whenever the snapshot is.
|
|
18
|
-
*
|
|
19
|
-
* The store is the argument, so this hook needs NO state provider: it reads
|
|
20
|
-
* `store.getState()` and subscribes with `store.subscribe()`. When `equalityFn`
|
|
21
|
-
* says a new selected value equals the previous one, the previous result is
|
|
22
|
-
* kept (the same value, not a fresh object).
|
|
23
|
-
*
|
|
24
|
-
* The selector and equality function run with the same cadence as React's
|
|
25
|
-
* `useSyncExternalStore` binding, because React re-reads its snapshot around
|
|
26
|
-
* every render; {@link useSyncExternalStore} documents the cadence.
|
|
10
|
+
* The implementation both signatures share: without a selector the selected
|
|
11
|
+
* value is the state itself, which the first signature types as `T`.
|
|
27
12
|
*
|
|
28
13
|
* @param store - The store to subscribe to (or a readable store of it).
|
|
29
14
|
* @param options - Hook options (selector, equalityFn), or a store of them.
|
|
30
|
-
* @returns A readable store of the selected
|
|
31
|
-
* @example
|
|
32
|
-
* ```svelte
|
|
33
|
-
* <script>
|
|
34
|
-
* import { useStore } from '@molecule/app-svelte'
|
|
35
|
-
* const count = useStore(counterStore, { selector: (s) => s.count })
|
|
36
|
-
* const doubled = useStore(counterStore, { selector: (s) => s.count * 2 })
|
|
37
|
-
* </script>
|
|
38
|
-
*
|
|
39
|
-
* <p>{$count} / {$doubled}</p>
|
|
40
|
-
* ```
|
|
15
|
+
* @returns A readable store of the selected value.
|
|
41
16
|
*/
|
|
42
17
|
export function useStore(store, options) {
|
|
43
18
|
const storeStore = toReadable(store);
|
|
@@ -53,6 +28,7 @@ export function useStore(store, options) {
|
|
|
53
28
|
const getSnapshot = () => {
|
|
54
29
|
const { selector, equalityFn } = get(optionsStore) ?? {};
|
|
55
30
|
const state = currentStore.getState();
|
|
31
|
+
// Without a selector the selection IS the store's state.
|
|
56
32
|
const selected = selector ? selector(state) : state;
|
|
57
33
|
// With an equality function and a previous value, an equal selection
|
|
58
34
|
// keeps the previous result — the same value, not a fresh object.
|
|
@@ -68,8 +44,7 @@ export function useStore(store, options) {
|
|
|
68
44
|
// A new store in the source re-subscribes and re-checks the snapshot, the
|
|
69
45
|
// way React's useSyncExternalStore does when its subscribe function
|
|
70
46
|
// changes. The store calls back at once with the store already read.
|
|
71
|
-
stopSource = storeStore.subscribe((
|
|
72
|
-
const incoming = next;
|
|
47
|
+
stopSource = storeStore.subscribe((incoming) => {
|
|
73
48
|
if (Object.is(incoming, currentStore))
|
|
74
49
|
return;
|
|
75
50
|
currentStore = incoming;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTheme.d.ts","sourceRoot":"","sources":["../../src/hooks/useTheme.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"useTheme.d.ts","sourceRoot":"","sources":["../../src/hooks/useTheme.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAW,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAErD,OAAO,KAAK,EAAE,KAAK,EAAiB,MAAM,qBAAqB,CAAA;AAK/D,qCAAqC;AACrC,MAAM,WAAW,cAAc;IAC7B,gCAAgC;IAChC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;IACtB,gCAAgC;IAChC,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;IAC3B,yCAAyC;IACzC,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAChC,2EAA2E;IAC3E,WAAW,EAAE,MAAM,IAAI,CAAA;IACvB,2CAA2C;IAC3C,IAAI,EAAE,QAAQ,CAAC,OAAO,GAAG,MAAM,CAAC,CAAA;CACjC;AAyCD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,IAAI,cAAc,CA6CzC;AAED;;;;GAIG;AACH,wBAAgB,eAAe,IAAI,QAAQ,CAAC,KAAK,CAAC,CAGjD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,QAAQ,CAAC,OAAO,GAAG,MAAM,CAAC,CAGzD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,IAAI,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAG1D"}
|
package/dist/hooks/useTheme.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
+
import { onMount } from 'svelte';
|
|
6
7
|
import { derived } from 'svelte/store';
|
|
7
8
|
import { useThemeProvider } from '../context.js';
|
|
8
9
|
import { fieldStore, liveStore } from './live-store.js';
|
|
@@ -16,13 +17,34 @@ import { fieldStore, liveStore } from './live-store.js';
|
|
|
16
17
|
*/
|
|
17
18
|
function subscribeToThemeProvider(provider, callback) {
|
|
18
19
|
if (typeof provider.subscribe === 'function') {
|
|
20
|
+
// A listener that takes no argument satisfies one that receives the theme.
|
|
19
21
|
return provider.subscribe(callback);
|
|
20
22
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
// `onThemeChange` is not part of `ThemeProvider`; a provider that predates
|
|
24
|
+
// `subscribe` may still expose it. Read it as an unknown function rather
|
|
25
|
+
// than asserting the provider's shape.
|
|
26
|
+
const legacy = Reflect.get(provider, 'onThemeChange');
|
|
27
|
+
if (typeof legacy !== 'function')
|
|
28
|
+
return () => { };
|
|
29
|
+
const stop = legacy(callback);
|
|
30
|
+
return typeof stop === 'function' ? () => stop() : () => { };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The active theme's name. `getThemeName()` is not part of `ThemeProvider`;
|
|
34
|
+
* probe it the way React does, so a provider that predates the theme object's
|
|
35
|
+
* `name` still works.
|
|
36
|
+
*
|
|
37
|
+
* @param provider - The theme provider to read.
|
|
38
|
+
* @returns The active theme's name.
|
|
39
|
+
*/
|
|
40
|
+
function readThemeName(provider) {
|
|
41
|
+
const named = Reflect.get(provider, 'getThemeName');
|
|
42
|
+
if (typeof named === 'function') {
|
|
43
|
+
const name = named();
|
|
44
|
+
if (typeof name === 'string')
|
|
45
|
+
return name;
|
|
24
46
|
}
|
|
25
|
-
return ()
|
|
47
|
+
return provider.getTheme().name;
|
|
26
48
|
}
|
|
27
49
|
/**
|
|
28
50
|
* Theme state and actions, React's `useTheme` in Svelte's idiom: call it
|
|
@@ -41,13 +63,20 @@ function subscribeToThemeProvider(provider, callback) {
|
|
|
41
63
|
*/
|
|
42
64
|
export function useTheme() {
|
|
43
65
|
const provider = useThemeProvider();
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
66
|
+
const readName = () => readThemeName(provider);
|
|
67
|
+
const readTheme = () => ({
|
|
68
|
+
theme: provider.getTheme(),
|
|
69
|
+
name: readName(),
|
|
70
|
+
});
|
|
71
|
+
// A static site prerenders on a server that reads no storage and no OS
|
|
72
|
+
// preference, then hydrates in a browser that reads both. The first value is
|
|
73
|
+
// the theme that markup was rendered with (`getServerTheme()` when the
|
|
74
|
+
// provider has one), so hydration matches; the mount below switches to the
|
|
75
|
+
// live theme, as React's binding does right after hydration. A server render
|
|
76
|
+
// never mounts, so it keeps the server theme.
|
|
77
|
+
const serverTheme = provider.getServerTheme?.();
|
|
78
|
+
const state = liveStore(readTheme, (notify) => subscribeToThemeProvider(provider, notify), serverTheme === undefined ? undefined : { theme: serverTheme, name: serverTheme.name });
|
|
79
|
+
onMount(() => state.refresh());
|
|
51
80
|
const toggleTheme = () => {
|
|
52
81
|
// Prefer the provider's purpose-built light/dark toggle — unlike
|
|
53
82
|
// `getThemes()`, which is optional, so the index-cycle below silently
|
|
@@ -61,9 +90,9 @@ export function useTheme() {
|
|
|
61
90
|
if (themes.length === 0)
|
|
62
91
|
return;
|
|
63
92
|
const currentName = readName();
|
|
64
|
-
const currentIndex = themes.findIndex((entry) =>
|
|
93
|
+
const currentIndex = themes.findIndex((entry) => entry.name === currentName);
|
|
65
94
|
const next = themes[(currentIndex + 1) % themes.length];
|
|
66
|
-
provider.setTheme(
|
|
95
|
+
provider.setTheme(next.name);
|
|
67
96
|
};
|
|
68
97
|
return {
|
|
69
98
|
theme: fieldStore(state, 'theme'),
|
package/dist/index.d.ts
CHANGED
|
@@ -41,6 +41,85 @@
|
|
|
41
41
|
* {/if}
|
|
42
42
|
* ```
|
|
43
43
|
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```svelte
|
|
46
|
+
* <!-- Dashboard.svelte — the session, the theme and translations as stores -->
|
|
47
|
+
* <script lang="ts">
|
|
48
|
+
* import { useAuth, useTheme, useTranslation } from '@molecule/app-svelte'
|
|
49
|
+
*
|
|
50
|
+
* const { user, status, logout } = useAuth<{ name?: string }>()
|
|
51
|
+
* const { t } = useTranslation()
|
|
52
|
+
* const { theme, toggleTheme } = useTheme()
|
|
53
|
+
* </script>
|
|
54
|
+
*
|
|
55
|
+
* {#if $status === 'loading'}
|
|
56
|
+
* <p>{t('auth.restoring', undefined, { defaultValue: 'Restoring session...' })}</p>
|
|
57
|
+
* {:else if $status === 'unauthenticated'}
|
|
58
|
+
* <p>{t('auth.required', undefined, { defaultValue: 'Please log in.' })}</p>
|
|
59
|
+
* {:else}
|
|
60
|
+
* <div style:background={$theme.colors.background}>
|
|
61
|
+
* <h1>
|
|
62
|
+
* {t('greeting.welcome', { name: $user?.name ?? '' }, { defaultValue: 'Welcome, {{name}}!' })}
|
|
63
|
+
* </h1>
|
|
64
|
+
* <button onclick={toggleTheme}>
|
|
65
|
+
* {t('theme.toggle', undefined, { defaultValue: 'Toggle theme' })}
|
|
66
|
+
* </button>
|
|
67
|
+
* <button onclick={() => logout()}>
|
|
68
|
+
* {t('auth.logout', undefined, { defaultValue: 'Log out' })}
|
|
69
|
+
* </button>
|
|
70
|
+
* </div>
|
|
71
|
+
* {/if}
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```svelte
|
|
76
|
+
* <!-- FavoriteButton.svelte — a server-backed favorite that changes at once -->
|
|
77
|
+
* <script lang="ts">
|
|
78
|
+
* import { useHttpClient, useOptimisticValue, useTranslation } from '@molecule/app-svelte'
|
|
79
|
+
*
|
|
80
|
+
* interface FavoriteRecord {
|
|
81
|
+
* slug: string
|
|
82
|
+
* favorited: boolean
|
|
83
|
+
* favoritesCount: number
|
|
84
|
+
* }
|
|
85
|
+
*
|
|
86
|
+
* let { article, onChanged }: {
|
|
87
|
+
* article: FavoriteRecord
|
|
88
|
+
* onChanged?: (saved: FavoriteRecord) => void
|
|
89
|
+
* } = $props()
|
|
90
|
+
*
|
|
91
|
+
* const http = useHttpClient()
|
|
92
|
+
* const { t } = useTranslation()
|
|
93
|
+
* const { value: favorite, update } = useOptimisticValue({
|
|
94
|
+
* value: { favorited: article.favorited, count: article.favoritesCount },
|
|
95
|
+
* commit: async (next) => {
|
|
96
|
+
* const path = `/articles/${article.slug}/favorite`
|
|
97
|
+
* const response = next.favorited
|
|
98
|
+
* ? await http.post<{ article: FavoriteRecord }>(path)
|
|
99
|
+
* : await http.delete<{ article: FavoriteRecord }>(path)
|
|
100
|
+
* const saved = response.data.article
|
|
101
|
+
* onChanged?.(saved)
|
|
102
|
+
* return { favorited: saved.favorited, count: saved.favoritesCount }
|
|
103
|
+
* },
|
|
104
|
+
* })
|
|
105
|
+
* </script>
|
|
106
|
+
*
|
|
107
|
+
* <button
|
|
108
|
+
* aria-pressed={$favorite.favorited}
|
|
109
|
+
* aria-label={t('article.favorite', undefined, { defaultValue: 'Favorite article' })}
|
|
110
|
+
* onclick={() => update((current) => ({
|
|
111
|
+
* favorited: !current.favorited,
|
|
112
|
+
* count: current.count + (current.favorited ? -1 : 1),
|
|
113
|
+
* }))}
|
|
114
|
+
* >{$favorite.count}</button>
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* Session state is a three-way decision, not a boolean: `status === 'loading'`
|
|
118
|
+
* is undecided, so wait for it and redirect only on
|
|
119
|
+
* `status === 'unauthenticated'`; `'unavailable'` keeps the session candidate
|
|
120
|
+
* for retry, so a restore that failed with anything other than a 401/403 never
|
|
121
|
+
* signs the user out.
|
|
122
|
+
*
|
|
44
123
|
* @remarks
|
|
45
124
|
* - **`setMoleculeContext` and every `create*Stores` factory use Svelte context — they are only
|
|
46
125
|
* legal during component initialization** (the top level of a component `<script>`), never in
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyIG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,cAAc,CAAA;AAC5B,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,0BAA0B,CAAA;AACxC,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -41,6 +41,85 @@
|
|
|
41
41
|
* {/if}
|
|
42
42
|
* ```
|
|
43
43
|
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```svelte
|
|
46
|
+
* <!-- Dashboard.svelte — the session, the theme and translations as stores -->
|
|
47
|
+
* <script lang="ts">
|
|
48
|
+
* import { useAuth, useTheme, useTranslation } from '@molecule/app-svelte'
|
|
49
|
+
*
|
|
50
|
+
* const { user, status, logout } = useAuth<{ name?: string }>()
|
|
51
|
+
* const { t } = useTranslation()
|
|
52
|
+
* const { theme, toggleTheme } = useTheme()
|
|
53
|
+
* </script>
|
|
54
|
+
*
|
|
55
|
+
* {#if $status === 'loading'}
|
|
56
|
+
* <p>{t('auth.restoring', undefined, { defaultValue: 'Restoring session...' })}</p>
|
|
57
|
+
* {:else if $status === 'unauthenticated'}
|
|
58
|
+
* <p>{t('auth.required', undefined, { defaultValue: 'Please log in.' })}</p>
|
|
59
|
+
* {:else}
|
|
60
|
+
* <div style:background={$theme.colors.background}>
|
|
61
|
+
* <h1>
|
|
62
|
+
* {t('greeting.welcome', { name: $user?.name ?? '' }, { defaultValue: 'Welcome, {{name}}!' })}
|
|
63
|
+
* </h1>
|
|
64
|
+
* <button onclick={toggleTheme}>
|
|
65
|
+
* {t('theme.toggle', undefined, { defaultValue: 'Toggle theme' })}
|
|
66
|
+
* </button>
|
|
67
|
+
* <button onclick={() => logout()}>
|
|
68
|
+
* {t('auth.logout', undefined, { defaultValue: 'Log out' })}
|
|
69
|
+
* </button>
|
|
70
|
+
* </div>
|
|
71
|
+
* {/if}
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```svelte
|
|
76
|
+
* <!-- FavoriteButton.svelte — a server-backed favorite that changes at once -->
|
|
77
|
+
* <script lang="ts">
|
|
78
|
+
* import { useHttpClient, useOptimisticValue, useTranslation } from '@molecule/app-svelte'
|
|
79
|
+
*
|
|
80
|
+
* interface FavoriteRecord {
|
|
81
|
+
* slug: string
|
|
82
|
+
* favorited: boolean
|
|
83
|
+
* favoritesCount: number
|
|
84
|
+
* }
|
|
85
|
+
*
|
|
86
|
+
* let { article, onChanged }: {
|
|
87
|
+
* article: FavoriteRecord
|
|
88
|
+
* onChanged?: (saved: FavoriteRecord) => void
|
|
89
|
+
* } = $props()
|
|
90
|
+
*
|
|
91
|
+
* const http = useHttpClient()
|
|
92
|
+
* const { t } = useTranslation()
|
|
93
|
+
* const { value: favorite, update } = useOptimisticValue({
|
|
94
|
+
* value: { favorited: article.favorited, count: article.favoritesCount },
|
|
95
|
+
* commit: async (next) => {
|
|
96
|
+
* const path = `/articles/${article.slug}/favorite`
|
|
97
|
+
* const response = next.favorited
|
|
98
|
+
* ? await http.post<{ article: FavoriteRecord }>(path)
|
|
99
|
+
* : await http.delete<{ article: FavoriteRecord }>(path)
|
|
100
|
+
* const saved = response.data.article
|
|
101
|
+
* onChanged?.(saved)
|
|
102
|
+
* return { favorited: saved.favorited, count: saved.favoritesCount }
|
|
103
|
+
* },
|
|
104
|
+
* })
|
|
105
|
+
* </script>
|
|
106
|
+
*
|
|
107
|
+
* <button
|
|
108
|
+
* aria-pressed={$favorite.favorited}
|
|
109
|
+
* aria-label={t('article.favorite', undefined, { defaultValue: 'Favorite article' })}
|
|
110
|
+
* onclick={() => update((current) => ({
|
|
111
|
+
* favorited: !current.favorited,
|
|
112
|
+
* count: current.count + (current.favorited ? -1 : 1),
|
|
113
|
+
* }))}
|
|
114
|
+
* >{$favorite.count}</button>
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* Session state is a three-way decision, not a boolean: `status === 'loading'`
|
|
118
|
+
* is undecided, so wait for it and redirect only on
|
|
119
|
+
* `status === 'unauthenticated'`; `'unavailable'` keeps the session candidate
|
|
120
|
+
* for retry, so a restore that failed with anything other than a 401/403 never
|
|
121
|
+
* signs the user out.
|
|
122
|
+
*
|
|
44
123
|
* @remarks
|
|
45
124
|
* - **`setMoleculeContext` and every `create*Stores` factory use Svelte context — they are only
|
|
46
125
|
* legal during component initialization** (the top level of a component `<script>`), never in
|