@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
package/dist/hooks/useStorage.js
CHANGED
|
@@ -4,21 +4,29 @@
|
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
import { onDestroy } from 'svelte';
|
|
7
|
-
import { writable } from 'svelte/store';
|
|
7
|
+
import { get, writable } from 'svelte/store';
|
|
8
8
|
import { getPreferenceStore } from '@molecule/app-storage';
|
|
9
9
|
import { useStorageProvider } from '../context.js';
|
|
10
|
+
import { toReadable } from './maybe-readable.js';
|
|
10
11
|
/**
|
|
11
12
|
* One storage key as Svelte stores, React's `useStorageValue` in Svelte's
|
|
12
13
|
* idiom: call it during component init under `setMoleculeContext({ storage })`.
|
|
13
14
|
*
|
|
15
|
+
* Both arguments may be a plain value or a readable store of one (a store
|
|
16
|
+
* holding `undefined` means the argument was omitted): setting the `key` store
|
|
17
|
+
* starts the new key over — its value is unknown until its read lands, so the
|
|
18
|
+
* default shows and `loading` is true, exactly as a fresh mount — while writes
|
|
19
|
+
* go to the key the hook holds at the moment they run. The latest
|
|
20
|
+
* `defaultValue` and `sync` always apply.
|
|
21
|
+
*
|
|
14
22
|
* A read that started before a newer local write keeps that write's value (it
|
|
15
23
|
* still ends `loading`), and with `sync` an outside write reaches the hook
|
|
16
24
|
* through the provider's own subscription — so a non-browser backend
|
|
17
25
|
* (native, encrypted, memory) propagates the same way, not just the browser
|
|
18
26
|
* `storage` event.
|
|
19
27
|
*
|
|
20
|
-
* @param key - Storage key
|
|
21
|
-
* @param options - Hook options
|
|
28
|
+
* @param key - Storage key (or a store of it).
|
|
29
|
+
* @param options - Hook options (or a store of them).
|
|
22
30
|
* @returns Storage value state (value, loading, error) and mutators (setValue, removeValue).
|
|
23
31
|
* @example
|
|
24
32
|
* ```svelte
|
|
@@ -35,61 +43,95 @@ import { useStorageProvider } from '../context.js';
|
|
|
35
43
|
*/
|
|
36
44
|
export function useStorageValue(key, options) {
|
|
37
45
|
const storage = useStorageProvider();
|
|
38
|
-
const
|
|
39
|
-
const
|
|
46
|
+
const keyStore = toReadable(key);
|
|
47
|
+
const optionsStore = toReadable(options);
|
|
48
|
+
const defaultOf = () => get(optionsStore)?.defaultValue;
|
|
49
|
+
const syncEnabled = () => get(optionsStore)?.sync ?? false;
|
|
50
|
+
const value = writable(defaultOf());
|
|
40
51
|
const loading = writable(true);
|
|
41
52
|
const error = writable(null);
|
|
42
53
|
// Incremented by every local write. A read that started before it must not
|
|
43
54
|
// replace the newer value — the user editing before the first read lands is
|
|
44
55
|
// the common case on a slow (native, encrypted) backend.
|
|
45
56
|
let writeVersion = 0;
|
|
46
|
-
// The
|
|
47
|
-
//
|
|
48
|
-
|
|
57
|
+
// The key whose read is current: a key change kills the old read (React's
|
|
58
|
+
// per-effect `mounted` flag), so a late landing does nothing at all.
|
|
59
|
+
let readEpoch = 0;
|
|
49
60
|
let mounted = true;
|
|
50
61
|
onDestroy(() => {
|
|
51
62
|
mounted = false;
|
|
52
63
|
});
|
|
53
|
-
// Load
|
|
54
|
-
const
|
|
64
|
+
// Load one key's value (React's load effect, keyed on the key).
|
|
65
|
+
const loadKey = (keyToLoad) => {
|
|
66
|
+
// A new key's value is unknown until its read lands: show what a fresh
|
|
67
|
+
// mount shows (React's render-time reset for a changed key).
|
|
68
|
+
value.set(defaultOf());
|
|
69
|
+
loading.set(true);
|
|
70
|
+
error.set(null);
|
|
71
|
+
const epoch = ++readEpoch;
|
|
55
72
|
const version = writeVersion;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
73
|
+
void storage
|
|
74
|
+
.get(keyToLoad)
|
|
75
|
+
.then((stored) => {
|
|
76
|
+
if (!mounted || epoch !== readEpoch)
|
|
59
77
|
return;
|
|
60
78
|
// A write made after this read started holds the newer value; the read still ends loading.
|
|
61
79
|
if (writeVersion === version)
|
|
62
|
-
value.set((stored ??
|
|
80
|
+
value.set((stored ?? defaultOf()));
|
|
63
81
|
loading.set(false);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (!mounted)
|
|
82
|
+
})
|
|
83
|
+
.catch((err) => {
|
|
84
|
+
if (!mounted || epoch !== readEpoch)
|
|
67
85
|
return;
|
|
68
86
|
if (writeVersion === version) {
|
|
69
87
|
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
70
88
|
}
|
|
71
89
|
loading.set(false);
|
|
72
|
-
}
|
|
90
|
+
});
|
|
73
91
|
};
|
|
74
|
-
|
|
92
|
+
loadKey(get(keyStore));
|
|
75
93
|
// Sync across tabs if supported — through the PROVIDER's own subscription, so
|
|
76
94
|
// a non-browser backend (native, encrypted, memory) propagates the same way
|
|
77
|
-
// instead of being limited to the browser `storage` event.
|
|
95
|
+
// instead of being limited to the browser `storage` event. Re-armed whenever
|
|
96
|
+
// the key or the `sync` option changes (React's [key, sync, storage] effect).
|
|
78
97
|
let stopSync = null;
|
|
79
|
-
|
|
80
|
-
stopSync
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
}
|
|
98
|
+
const armSync = () => {
|
|
99
|
+
stopSync?.();
|
|
100
|
+
stopSync = null;
|
|
101
|
+
if (syncEnabled() && storage.subscribe) {
|
|
102
|
+
stopSync = storage.subscribe(get(keyStore), (next) => {
|
|
103
|
+
writeVersion += 1;
|
|
104
|
+
value.set(next === null ? defaultOf() : next);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
armSync();
|
|
109
|
+
onDestroy(() => {
|
|
110
|
+
stopSync?.();
|
|
111
|
+
stopSync = null;
|
|
112
|
+
});
|
|
113
|
+
// Follow the key: a changed key loads its own value; a changed `sync`
|
|
114
|
+
// re-arms the subscription. The stores call back at once with the values
|
|
115
|
+
// already read, which changes nothing.
|
|
116
|
+
let currentKey = get(keyStore);
|
|
117
|
+
let currentSync = syncEnabled();
|
|
118
|
+
const stopKey = keyStore.subscribe((next) => {
|
|
119
|
+
const syncNow = syncEnabled();
|
|
120
|
+
if (Object.is(next, currentKey) && Object.is(syncNow, currentSync))
|
|
121
|
+
return;
|
|
122
|
+
const keyChanged = !Object.is(next, currentKey);
|
|
123
|
+
currentKey = next;
|
|
124
|
+
currentSync = syncNow;
|
|
125
|
+
if (keyChanged)
|
|
126
|
+
loadKey(next);
|
|
127
|
+
else
|
|
128
|
+
armSync();
|
|
129
|
+
});
|
|
130
|
+
onDestroy(stopKey);
|
|
89
131
|
const setValue = async (newValue) => {
|
|
90
132
|
writeVersion += 1;
|
|
91
133
|
try {
|
|
92
|
-
await storage.set(
|
|
134
|
+
await storage.set(get(keyStore), newValue);
|
|
93
135
|
value.set(newValue);
|
|
94
136
|
error.set(null);
|
|
95
137
|
}
|
|
@@ -101,8 +143,8 @@ export function useStorageValue(key, options) {
|
|
|
101
143
|
const removeValue = async () => {
|
|
102
144
|
writeVersion += 1;
|
|
103
145
|
try {
|
|
104
|
-
await storage.remove(
|
|
105
|
-
value.set(
|
|
146
|
+
await storage.remove(get(keyStore));
|
|
147
|
+
value.set(defaultOf());
|
|
106
148
|
error.set(null);
|
|
107
149
|
}
|
|
108
150
|
catch (err) {
|
|
@@ -129,12 +171,17 @@ export function useStorageValue(key, options) {
|
|
|
129
171
|
* started before a newer write, remove or external change is discarded, so a
|
|
130
172
|
* slow hydration never overwrites an edit the user already made.
|
|
131
173
|
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
174
|
+
* Both arguments may be a plain value or a readable store of one (a store
|
|
175
|
+
* holding `undefined` means the argument was omitted): setting the `key` store
|
|
176
|
+
* re-reads, re-subscribes and re-hydrates the new key on the same component
|
|
177
|
+
* (a `null`/`undefined` key is local state around the default and persists
|
|
178
|
+
* nothing), and writes go to the key the hook holds at the moment they run.
|
|
179
|
+
* The latest `defaultValue` always applies.
|
|
135
180
|
*
|
|
136
|
-
* @param key - The storage key, or `null` when there is nothing to persist
|
|
137
|
-
*
|
|
181
|
+
* @param key - The storage key, or `null` when there is nothing to persist
|
|
182
|
+
* (or a store of either).
|
|
183
|
+
* @param options - `defaultValue`, reported until the provider supplies one
|
|
184
|
+
* (or a store of them).
|
|
138
185
|
* @returns The current value, its mutators, and whether it is definite yet.
|
|
139
186
|
* @example
|
|
140
187
|
* ```svelte
|
|
@@ -148,60 +195,90 @@ export function useStorageValue(key, options) {
|
|
|
148
195
|
*/
|
|
149
196
|
export function useStoragePreference(key, options) {
|
|
150
197
|
const store = getPreferenceStore();
|
|
151
|
-
const
|
|
198
|
+
const keyStore = toReadable(key);
|
|
199
|
+
const optionsStore = toReadable(options);
|
|
200
|
+
const defaultOf = () => get(optionsStore)?.defaultValue;
|
|
201
|
+
const currentKey = () => get(keyStore);
|
|
152
202
|
const read = () => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
203
|
+
const keyNow = currentKey();
|
|
204
|
+
if (!keyNow)
|
|
205
|
+
return defaultOf();
|
|
206
|
+
const stored = store.get(keyNow);
|
|
207
|
+
return stored === null ? defaultOf() : stored;
|
|
157
208
|
};
|
|
158
209
|
const initial = read();
|
|
159
210
|
const value = writable(initial);
|
|
160
|
-
const ready = writable(!
|
|
211
|
+
const ready = writable(!currentKey());
|
|
161
212
|
// The value `setValue`'s functional form receives, kept beside the store.
|
|
162
213
|
let currentValue = initial;
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
214
|
+
// React's [store, key, read] effect for one key: seed from whatever the
|
|
215
|
+
// store already knows, then let hydration converge it. With a synchronous
|
|
216
|
+
// provider the seed is a no-op change. A replaced key tears the old one
|
|
217
|
+
// down first, so its late hydration is discarded.
|
|
218
|
+
let stopStore = null;
|
|
219
|
+
let cancelled = false;
|
|
220
|
+
const followKey = (keyToFollow) => {
|
|
221
|
+
stopStore?.();
|
|
222
|
+
stopStore = null;
|
|
223
|
+
cancelled = true;
|
|
224
|
+
if (!keyToFollow) {
|
|
225
|
+
ready.set(true);
|
|
226
|
+
const fallback = defaultOf();
|
|
227
|
+
value.set(fallback);
|
|
228
|
+
currentValue = fallback;
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
cancelled = false;
|
|
170
232
|
const sync = () => {
|
|
171
|
-
if (
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
233
|
+
if (cancelled)
|
|
234
|
+
return;
|
|
235
|
+
const next = read();
|
|
236
|
+
value.set(next);
|
|
237
|
+
currentValue = next;
|
|
176
238
|
};
|
|
177
239
|
// Seed from whatever the store already knows, then let hydration converge
|
|
178
240
|
// it. With a synchronous provider this is a no-op change.
|
|
179
241
|
sync();
|
|
180
242
|
ready.set(false);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
cancelled = true;
|
|
184
|
-
stop();
|
|
185
|
-
});
|
|
186
|
-
void store.hydrate([key]).then(() => {
|
|
243
|
+
stopStore = store.subscribe(keyToFollow, sync);
|
|
244
|
+
void store.hydrate([keyToFollow]).then(() => {
|
|
187
245
|
if (cancelled)
|
|
188
246
|
return;
|
|
189
247
|
sync();
|
|
190
248
|
ready.set(true);
|
|
191
249
|
});
|
|
192
|
-
}
|
|
250
|
+
};
|
|
251
|
+
followKey(currentKey());
|
|
252
|
+
onDestroy(() => {
|
|
253
|
+
cancelled = true;
|
|
254
|
+
stopStore?.();
|
|
255
|
+
stopStore = null;
|
|
256
|
+
});
|
|
257
|
+
// Follow the key source: the store calls back at once with the key already
|
|
258
|
+
// read, which changes nothing.
|
|
259
|
+
let followedKey = currentKey();
|
|
260
|
+
const stopKey = keyStore.subscribe((next) => {
|
|
261
|
+
if (Object.is(next, followedKey))
|
|
262
|
+
return;
|
|
263
|
+
followedKey = next;
|
|
264
|
+
followKey(next);
|
|
265
|
+
});
|
|
266
|
+
onDestroy(stopKey);
|
|
193
267
|
const setValue = (next) => {
|
|
194
268
|
const resolved = typeof next === 'function' ? next(currentValue) : next;
|
|
195
269
|
currentValue = resolved;
|
|
196
270
|
value.set(resolved);
|
|
197
|
-
|
|
198
|
-
|
|
271
|
+
const keyNow = currentKey();
|
|
272
|
+
if (keyNow)
|
|
273
|
+
store.set(keyNow, resolved);
|
|
199
274
|
};
|
|
200
275
|
const removeValue = () => {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
276
|
+
const fallback = defaultOf();
|
|
277
|
+
currentValue = fallback;
|
|
278
|
+
value.set(fallback);
|
|
279
|
+
const keyNow = currentKey();
|
|
280
|
+
if (keyNow)
|
|
281
|
+
store.remove(keyNow);
|
|
205
282
|
};
|
|
206
283
|
return {
|
|
207
284
|
value: { subscribe: value.subscribe },
|
package/dist/hooks/useStore.d.ts
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
import type
|
|
6
|
+
import { type Readable } from 'svelte/store';
|
|
7
7
|
import type { Store } from '@molecule/app-state';
|
|
8
|
+
import { type MaybeReadable } from './maybe-readable.js';
|
|
8
9
|
/** Options for {@link useStore}. */
|
|
9
10
|
export interface UseStoreOptions<T, S> {
|
|
10
11
|
/** Selects part of the state. */
|
|
@@ -16,17 +17,23 @@ export interface UseStoreOptions<T, S> {
|
|
|
16
17
|
* Subscribes a store with an optional selector and equality function, React's
|
|
17
18
|
* `useStore` in Svelte's idiom: a readable store of the selected value.
|
|
18
19
|
*
|
|
20
|
+
* The store argument may be the store itself (a molecule store has `getState`
|
|
21
|
+
* AND `subscribe`, and is always a VALUE, never a source) or a readable store
|
|
22
|
+
* OF a store: setting it re-subscribes to the new store and re-reads its
|
|
23
|
+
* snapshot on the same component, the way React's re-render does. The options
|
|
24
|
+
* (or a store of them) are read whenever the snapshot is.
|
|
25
|
+
*
|
|
19
26
|
* The store is the argument, so this hook needs NO state provider: it reads
|
|
20
|
-
* `store.getState()` and subscribes with `store.subscribe()`. When
|
|
21
|
-
*
|
|
22
|
-
*
|
|
27
|
+
* `store.getState()` and subscribes with `store.subscribe()`. When `equalityFn`
|
|
28
|
+
* says a new selected value equals the previous one, the previous result is
|
|
29
|
+
* kept (the same value, not a fresh object).
|
|
23
30
|
*
|
|
24
31
|
* The selector and equality function run with the same cadence as React's
|
|
25
32
|
* `useSyncExternalStore` binding, because React re-reads its snapshot around
|
|
26
33
|
* every render; {@link useSyncExternalStore} documents the cadence.
|
|
27
34
|
*
|
|
28
|
-
* @param store - The store to subscribe to.
|
|
29
|
-
* @param options - Hook options (selector, equalityFn).
|
|
35
|
+
* @param store - The store to subscribe to (or a readable store of it).
|
|
36
|
+
* @param options - Hook options (selector, equalityFn), or a store of them.
|
|
30
37
|
* @returns A readable store of the selected state.
|
|
31
38
|
* @example
|
|
32
39
|
* ```svelte
|
|
@@ -39,7 +46,7 @@ export interface UseStoreOptions<T, S> {
|
|
|
39
46
|
* <p>{$count} / {$doubled}</p>
|
|
40
47
|
* ```
|
|
41
48
|
*/
|
|
42
|
-
export declare function useStore<T, S = T>(store: Store<T
|
|
49
|
+
export declare function useStore<T, S = T>(store: MaybeReadable<Store<T>>, options?: MaybeReadable<UseStoreOptions<T, S> | undefined>): Readable<S>;
|
|
43
50
|
/**
|
|
44
51
|
* The store's setState function, React's `useSetStore` in Svelte's idiom.
|
|
45
52
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStore.d.ts","sourceRoot":"","sources":["../../src/hooks/useStore.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"useStore.d.ts","sourceRoot":"","sources":["../../src/hooks/useStore.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAO,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAGhD,OAAO,EAAE,KAAK,aAAa,EAAc,MAAM,qBAAqB,CAAA;AAEpE,oCAAoC;AACpC,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC;IACnC,iCAAiC;IACjC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAA;IAC1B,qEAAqE;IACrE,UAAU,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAA;CACrC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAC/B,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,CAiDb;AAED;;;;;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
|
@@ -3,22 +3,30 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
+
import { get } from 'svelte/store';
|
|
6
7
|
import { useSyncExternalStore } from './external-store.js';
|
|
8
|
+
import { toReadable } from './maybe-readable.js';
|
|
7
9
|
/**
|
|
8
10
|
* Subscribes a store with an optional selector and equality function, React's
|
|
9
11
|
* `useStore` in Svelte's idiom: a readable store of the selected value.
|
|
10
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
|
+
*
|
|
11
19
|
* The store is the argument, so this hook needs NO state provider: it reads
|
|
12
|
-
* `store.getState()` and subscribes with `store.subscribe()`. When
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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).
|
|
15
23
|
*
|
|
16
24
|
* The selector and equality function run with the same cadence as React's
|
|
17
25
|
* `useSyncExternalStore` binding, because React re-reads its snapshot around
|
|
18
26
|
* every render; {@link useSyncExternalStore} documents the cadence.
|
|
19
27
|
*
|
|
20
|
-
* @param store - The store to subscribe to.
|
|
21
|
-
* @param options - Hook options (selector, equalityFn).
|
|
28
|
+
* @param store - The store to subscribe to (or a readable store of it).
|
|
29
|
+
* @param options - Hook options (selector, equalityFn), or a store of them.
|
|
22
30
|
* @returns A readable store of the selected state.
|
|
23
31
|
* @example
|
|
24
32
|
* ```svelte
|
|
@@ -32,12 +40,19 @@ import { useSyncExternalStore } from './external-store.js';
|
|
|
32
40
|
* ```
|
|
33
41
|
*/
|
|
34
42
|
export function useStore(store, options) {
|
|
35
|
-
const
|
|
43
|
+
const storeStore = toReadable(store);
|
|
44
|
+
const optionsStore = toReadable(options);
|
|
45
|
+
// The store currently subscribed: setting the source re-subscribes, as
|
|
46
|
+
// React's subscribe callback (keyed on the store) does on a re-render.
|
|
47
|
+
let currentStore = get(storeStore);
|
|
48
|
+
let stopStore = null;
|
|
49
|
+
let stopSource = null;
|
|
36
50
|
// The previous selected value (React's prevSelectedRef): while an equality
|
|
37
51
|
// function accepts it, it stays the result, so its identity is stable.
|
|
38
52
|
let previous;
|
|
39
|
-
|
|
40
|
-
const
|
|
53
|
+
const getSnapshot = () => {
|
|
54
|
+
const { selector, equalityFn } = get(optionsStore) ?? {};
|
|
55
|
+
const state = currentStore.getState();
|
|
41
56
|
const selected = selector ? selector(state) : state;
|
|
42
57
|
// With an equality function and a previous value, an equal selection
|
|
43
58
|
// keeps the previous result — the same value, not a fresh object.
|
|
@@ -47,7 +62,29 @@ export function useStore(store, options) {
|
|
|
47
62
|
}
|
|
48
63
|
previous = selected;
|
|
49
64
|
return selected;
|
|
50
|
-
}
|
|
65
|
+
};
|
|
66
|
+
const subscribe = (onChange) => {
|
|
67
|
+
stopStore = currentStore.subscribe(onChange);
|
|
68
|
+
// A new store in the source re-subscribes and re-checks the snapshot, the
|
|
69
|
+
// way React's useSyncExternalStore does when its subscribe function
|
|
70
|
+
// changes. The store calls back at once with the store already read.
|
|
71
|
+
stopSource = storeStore.subscribe((next) => {
|
|
72
|
+
const incoming = next;
|
|
73
|
+
if (Object.is(incoming, currentStore))
|
|
74
|
+
return;
|
|
75
|
+
currentStore = incoming;
|
|
76
|
+
stopStore?.();
|
|
77
|
+
stopStore = currentStore.subscribe(onChange);
|
|
78
|
+
onChange();
|
|
79
|
+
});
|
|
80
|
+
return () => {
|
|
81
|
+
stopSource?.();
|
|
82
|
+
stopStore?.();
|
|
83
|
+
stopSource = null;
|
|
84
|
+
stopStore = null;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
return useSyncExternalStore(subscribe, getSnapshot);
|
|
51
88
|
}
|
|
52
89
|
/**
|
|
53
90
|
* The store's setState function, React's `useSetStore` in Svelte's idiom.
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { type Readable } from 'svelte/store';
|
|
9
9
|
import type { I18nProvider, TranslateOptions } from '@molecule/app-i18n';
|
|
10
|
+
import { type MaybeReadable } from './maybe-readable.js';
|
|
10
11
|
/**
|
|
11
12
|
* Options for the shorthand form of {@link UseTFunction}: the fallback text
|
|
12
13
|
* is passed positionally, so only `count` remains here.
|
|
@@ -99,8 +100,11 @@ export declare function useDirection(): Readable<'ltr' | 'rtl'>;
|
|
|
99
100
|
* using the current locale. For plain `Error` instances, `error.message` is
|
|
100
101
|
* returned unchanged.
|
|
101
102
|
*
|
|
102
|
-
*
|
|
103
|
+
* The error may be a plain value or a readable store of one: setting the store
|
|
104
|
+
* replaces the message on the same component, the way React's re-render does.
|
|
105
|
+
*
|
|
106
|
+
* @param error - The error to translate, or `null`/`undefined` (or a store of either).
|
|
103
107
|
* @returns A readable store of the translated error string, or null.
|
|
104
108
|
*/
|
|
105
|
-
export declare function useI18nError(error: Error | null | undefined): Readable<string | null>;
|
|
109
|
+
export declare function useI18nError(error: MaybeReadable<Error | null | undefined>): Readable<string | null>;
|
|
106
110
|
//# sourceMappingURL=useTranslation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTranslation.d.ts","sourceRoot":"","sources":["../../src/hooks/useTranslation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAsB,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAE1E,OAAO,KAAK,EAEV,YAAY,EAGZ,gBAAgB,EACjB,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"useTranslation.d.ts","sourceRoot":"","sources":["../../src/hooks/useTranslation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAsB,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAE1E,OAAO,KAAK,EAEV,YAAY,EAGZ,gBAAgB,EACjB,MAAM,oBAAoB,CAAA;AAI3B,OAAO,EAAE,KAAK,aAAa,EAAc,MAAM,qBAAqB,CAAA;AAEpE;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,gBAAgB,EAAE,cAAc,CAAC,CAAA;AAEzE;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,GAAG,YAAY,CAAC,GAAG,CAAC,GAAG;IAC7C,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,MAAM,CAAA;CACxE,CAAA;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;;;OAKG;IACH,CAAC,EAAE,YAAY,CAAC,GAAG,CAAC,CAAA;IACpB,8BAA8B;IAC9B,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;IACxB,kCAAkC;IAClC,SAAS,EAAE,YAAY,CAAC,WAAW,CAAC,CAAA;IACpC,8BAA8B;IAC9B,OAAO,EAAE,QAAQ,CAAC,UAAU,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC,CAAA;IACzD,8CAA8C;IAC9C,YAAY,EAAE,YAAY,CAAC,cAAc,CAAC,CAAA;IAC1C,4CAA4C;IAC5C,UAAU,EAAE,YAAY,CAAC,YAAY,CAAC,CAAA;IACtC,0CAA0C;IAC1C,SAAS,EAAE,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC,CAAA;CACnC;AAqDD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,IAAI,oBAAoB,CAmBrD;AAED;;;;;;;;;GASG;AACH,wBAAgB,IAAI,IAAI,YAAY,CAenC;AAED;;;;GAIG;AACH,wBAAgB,SAAS,IAAI,QAAQ,CAAC,MAAM,CAAC,CAG5C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC,CAGtD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,aAAa,CAAC,KAAK,GAAG,IAAI,GAAG,SAAS,CAAC,GAC7C,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,CAazB"}
|
|
@@ -9,6 +9,7 @@ import { onDestroy } from 'svelte';
|
|
|
9
9
|
import { derived, fromStore, writable } from 'svelte/store';
|
|
10
10
|
import { I18nError } from '@molecule/app-i18n';
|
|
11
11
|
import { useI18nProvider } from '../context.js';
|
|
12
|
+
import { toReadable } from './maybe-readable.js';
|
|
12
13
|
/**
|
|
13
14
|
* Subscribes the calling component to the provider's locale notifications,
|
|
14
15
|
* like React's `useLocaleSnapshot`.
|
|
@@ -140,20 +141,24 @@ export function useDirection() {
|
|
|
140
141
|
* using the current locale. For plain `Error` instances, `error.message` is
|
|
141
142
|
* returned unchanged.
|
|
142
143
|
*
|
|
143
|
-
*
|
|
144
|
+
* The error may be a plain value or a readable store of one: setting the store
|
|
145
|
+
* replaces the message on the same component, the way React's re-render does.
|
|
146
|
+
*
|
|
147
|
+
* @param error - The error to translate, or `null`/`undefined` (or a store of either).
|
|
144
148
|
* @returns A readable store of the translated error string, or null.
|
|
145
149
|
*/
|
|
146
150
|
export function useI18nError(error) {
|
|
147
151
|
const { t, locale } = useTranslation();
|
|
148
|
-
|
|
149
|
-
|
|
152
|
+
const errorStore = toReadable(error);
|
|
153
|
+
return derived([errorStore, locale], ([current]) => {
|
|
154
|
+
if (!current)
|
|
150
155
|
return null;
|
|
151
156
|
// defaultValue is the I18nError's own message — the fallback text its
|
|
152
157
|
// throw site provided. Without it, a key the CLIENT dictionary lacks
|
|
153
158
|
// renders as the raw key instead of a message.
|
|
154
|
-
if (
|
|
155
|
-
return t(
|
|
159
|
+
if (current instanceof I18nError) {
|
|
160
|
+
return t(current.i18nKey, current.i18nValues, { defaultValue: current.message });
|
|
156
161
|
}
|
|
157
|
-
return
|
|
162
|
+
return current.message;
|
|
158
163
|
});
|
|
159
164
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -51,12 +51,16 @@
|
|
|
51
51
|
* config; don't wrap in try/catch.
|
|
52
52
|
* - Results are Svelte stores — read them with the `$` prefix (`$isAuthenticated`, `$theme`);
|
|
53
53
|
* actions (`logout`, `toggleTheme`, `setTheme`) are plain functions.
|
|
54
|
+
* - In-app links are `Link` (`<Link to="/settings">`): it navigates through the router with
|
|
55
|
+
* react-router `<Link>`'s click rules. A hand-written click handler that calls `navigate()`
|
|
56
|
+
* breaks ctrl/cmd-click (new tab) unless it repeats those rules.
|
|
54
57
|
*
|
|
55
58
|
* @module
|
|
56
59
|
*/
|
|
57
60
|
export * from './agent-identity.js';
|
|
58
61
|
export * from './context.js';
|
|
59
62
|
export * from './hooks/index.js';
|
|
63
|
+
export * from './link/index.js';
|
|
60
64
|
export * from './session-ready/index.js';
|
|
61
65
|
export * from './stores/index.js';
|
|
62
66
|
export * from './types.js';
|
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;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
|
@@ -51,12 +51,16 @@
|
|
|
51
51
|
* config; don't wrap in try/catch.
|
|
52
52
|
* - Results are Svelte stores — read them with the `$` prefix (`$isAuthenticated`, `$theme`);
|
|
53
53
|
* actions (`logout`, `toggleTheme`, `setTheme`) are plain functions.
|
|
54
|
+
* - In-app links are `Link` (`<Link to="/settings">`): it navigates through the router with
|
|
55
|
+
* react-router `<Link>`'s click rules. A hand-written click handler that calls `navigate()`
|
|
56
|
+
* breaks ctrl/cmd-click (new tab) unless it repeats those rules.
|
|
54
57
|
*
|
|
55
58
|
* @module
|
|
56
59
|
*/
|
|
57
60
|
export * from './agent-identity.js';
|
|
58
61
|
export * from './context.js';
|
|
59
62
|
export * from './hooks/index.js';
|
|
63
|
+
export * from './link/index.js';
|
|
60
64
|
export * from './session-ready/index.js';
|
|
61
65
|
export * from './stores/index.js';
|
|
62
66
|
export * from './types.js';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
An `<a href={to}>` that navigates in-app through the molecule router, with
|
|
4
|
+
react-router `<Link>`'s rules (`followRouterLink` from `@molecule/app-routing`):
|
|
5
|
+
a plain primary click navigates without a page load; a ctrl/cmd, shift or alt
|
|
6
|
+
click, a `target` other than `_self`, a click an `onclick` already prevented,
|
|
7
|
+
and a URL on another origin are left to the browser.
|
|
8
|
+
-->
|
|
9
|
+
<script lang="ts">
|
|
10
|
+
import { followRouterLink } from '@molecule/app-routing'
|
|
11
|
+
|
|
12
|
+
import { useRouterInstance } from '../context.js'
|
|
13
|
+
import type { LinkProps } from './types.js'
|
|
14
|
+
|
|
15
|
+
let { to, replace, state, children, onclick, ...anchor }: LinkProps = $props()
|
|
16
|
+
|
|
17
|
+
const router = useRouterInstance()
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Runs the caller's `onclick`, then navigates in-app for a click the router
|
|
21
|
+
* should take.
|
|
22
|
+
*
|
|
23
|
+
* @param event - The anchor's click event.
|
|
24
|
+
*/
|
|
25
|
+
function handleClick(
|
|
26
|
+
event: MouseEvent & { currentTarget: EventTarget & HTMLAnchorElement },
|
|
27
|
+
): void {
|
|
28
|
+
onclick?.(event)
|
|
29
|
+
followRouterLink(event, to, router, { replace, state })
|
|
30
|
+
}
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
<a {...anchor} href={to} onclick={handleClick}>{@render children?.()}</a>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { LinkProps } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* An `<a href={to}>` that navigates in-app through the molecule router, with
|
|
4
|
+
* react-router `<Link>`'s rules (`followRouterLink` from `@molecule/app-routing`):
|
|
5
|
+
* a plain primary click navigates without a page load; a ctrl/cmd, shift or alt
|
|
6
|
+
* click, a `target` other than `_self`, a click an `onclick` already prevented,
|
|
7
|
+
* and a URL on another origin are left to the browser.
|
|
8
|
+
*/
|
|
9
|
+
declare const Link: import("svelte").Component<LinkProps, {}, "">;
|
|
10
|
+
type Link = ReturnType<typeof Link>;
|
|
11
|
+
export default Link;
|
|
12
|
+
//# sourceMappingURL=Link.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Link.svelte.d.ts","sourceRoot":"","sources":["../../src/link/Link.svelte"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AA+B5C;;;;;;GAMG;AACH,QAAA,MAAM,IAAI,+CAAwC,CAAC;AACnD,KAAK,IAAI,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;AACpC,eAAe,IAAI,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `Link`: an in-app anchor for Svelte, the counterpart of react-router's `<Link>`.
|
|
3
|
+
*
|
|
4
|
+
* Use it for every internal link instead of an anchor with a click handler that
|
|
5
|
+
* calls `navigate()`. It reads the router from `setMoleculeContext`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```svelte
|
|
9
|
+
* <script lang="ts">
|
|
10
|
+
* import { Link } from '@molecule/app-svelte'
|
|
11
|
+
* </script>
|
|
12
|
+
*
|
|
13
|
+
* <nav>
|
|
14
|
+
* <Link to="/projects" class="nav-link">Projects</Link>
|
|
15
|
+
* <Link to="/login" replace state={{ from: '/projects' }}>Log in</Link>
|
|
16
|
+
* </nav>
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
export { default as Link } from './Link.svelte';
|
|
22
|
+
export * from './types.js';
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/link/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,eAAe,CAAA;AAC/C,cAAc,YAAY,CAAA"}
|