@molecule/app-angular 3.3.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 +4049 -1255
- package/dist/chat.d.ts.map +1 -1
- package/dist/chat.js +3 -2
- package/dist/chat.js.map +1 -1
- package/dist/hooks/external-store.d.ts +32 -0
- package/dist/hooks/external-store.d.ts.map +1 -0
- package/dist/hooks/external-store.js +62 -0
- package/dist/hooks/external-store.js.map +1 -0
- package/dist/hooks/index.d.ts +24 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +24 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/use-ai-models.d.ts +53 -0
- package/dist/hooks/use-ai-models.d.ts.map +1 -0
- package/dist/hooks/use-ai-models.js +167 -0
- package/dist/hooks/use-ai-models.js.map +1 -0
- package/dist/hooks/use-async.d.ts +129 -0
- package/dist/hooks/use-async.d.ts.map +1 -0
- package/dist/hooks/use-async.js +194 -0
- package/dist/hooks/use-async.js.map +1 -0
- package/dist/hooks/use-auth-flows.d.ts +226 -0
- package/dist/hooks/use-auth-flows.d.ts.map +1 -0
- package/dist/hooks/use-auth-flows.js +481 -0
- package/dist/hooks/use-auth-flows.js.map +1 -0
- package/dist/hooks/use-auth.d.ts +85 -0
- package/dist/hooks/use-auth.d.ts.map +1 -0
- package/dist/hooks/use-auth.js +104 -0
- package/dist/hooks/use-auth.js.map +1 -0
- package/dist/hooks/use-forms.d.ts +174 -0
- package/dist/hooks/use-forms.d.ts.map +1 -0
- package/dist/hooks/use-forms.js +221 -0
- package/dist/hooks/use-forms.js.map +1 -0
- package/dist/hooks/use-http.d.ts +163 -0
- package/dist/hooks/use-http.d.ts.map +1 -0
- package/dist/hooks/use-http.js +198 -0
- package/dist/hooks/use-http.js.map +1 -0
- package/dist/hooks/use-ide.d.ts +208 -0
- package/dist/hooks/use-ide.d.ts.map +1 -0
- package/dist/hooks/use-ide.js +217 -0
- package/dist/hooks/use-ide.js.map +1 -0
- package/dist/hooks/use-logger.d.ts +40 -0
- package/dist/hooks/use-logger.d.ts.map +1 -0
- package/dist/hooks/use-logger.js +56 -0
- package/dist/hooks/use-logger.js.map +1 -0
- package/dist/hooks/use-native.d.ts +218 -0
- package/dist/hooks/use-native.d.ts.map +1 -0
- package/dist/hooks/use-native.js +173 -0
- package/dist/hooks/use-native.js.map +1 -0
- package/dist/hooks/use-profile-form.d.ts +113 -0
- package/dist/hooks/use-profile-form.d.ts.map +1 -0
- package/dist/hooks/use-profile-form.js +253 -0
- package/dist/hooks/use-profile-form.js.map +1 -0
- package/dist/hooks/use-router.d.ts +219 -0
- package/dist/hooks/use-router.d.ts.map +1 -0
- package/dist/hooks/use-router.js +266 -0
- package/dist/hooks/use-router.js.map +1 -0
- package/dist/hooks/use-storage.d.ts +142 -0
- package/dist/hooks/use-storage.d.ts.map +1 -0
- package/dist/hooks/use-storage.js +299 -0
- package/dist/hooks/use-storage.js.map +1 -0
- package/dist/hooks/use-store.d.ts +81 -0
- package/dist/hooks/use-store.d.ts.map +1 -0
- package/dist/hooks/use-store.js +143 -0
- package/dist/hooks/use-store.js.map +1 -0
- package/dist/hooks/use-theme.d.ts +59 -0
- package/dist/hooks/use-theme.d.ts.map +1 -0
- package/dist/hooks/use-theme.js +117 -0
- package/dist/hooks/use-theme.js.map +1 -0
- package/dist/hooks/use-translation.d.ts +91 -0
- package/dist/hooks/use-translation.d.ts.map +1 -0
- package/dist/hooks/use-translation.js +152 -0
- package/dist/hooks/use-translation.js.map +1 -0
- package/dist/hooks/use-verify-payment-return.d.ts +86 -0
- package/dist/hooks/use-verify-payment-return.d.ts.map +1 -0
- package/dist/hooks/use-verify-payment-return.js +167 -0
- package/dist/hooks/use-verify-payment-return.js.map +1 -0
- package/dist/hooks/use-version.d.ts +49 -0
- package/dist/hooks/use-version.d.ts.map +1 -0
- package/dist/hooks/use-version.js +70 -0
- package/dist/hooks/use-version.js.map +1 -0
- package/dist/i18n-reactive.d.ts +2 -5
- package/dist/i18n-reactive.d.ts.map +1 -1
- package/dist/i18n-reactive.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/inject.d.ts +24 -0
- package/dist/inject.d.ts.map +1 -0
- package/dist/inject.js +33 -0
- package/dist/inject.js.map +1 -0
- package/dist/link.d.ts +63 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +82 -0
- package/dist/link.js.map +1 -0
- package/dist/maybe-signal.d.ts +26 -0
- package/dist/maybe-signal.d.ts.map +1 -0
- package/dist/maybe-signal.js +20 -0
- package/dist/maybe-signal.js.map +1 -0
- package/dist/providers.d.ts +24 -0
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +44 -3
- package/dist/providers.js.map +1 -1
- package/dist/session-ready.d.ts +41 -0
- package/dist/session-ready.d.ts.map +1 -0
- package/dist/session-ready.js +79 -0
- package/dist/session-ready.js.map +1 -0
- package/dist/tokens.d.ts +15 -0
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +12 -0
- package/dist/tokens.js.map +1 -1
- package/dist/types.d.ts +14 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for storage, mirroring `useStorageProvider`,
|
|
3
|
+
* `useStorageValue`, `useStoragePreference` and `useStorage` from
|
|
4
|
+
* `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { DestroyRef, effect, inject, signal, untracked } from '@angular/core';
|
|
9
|
+
import { getPreferenceStore } from '@molecule/app-storage';
|
|
10
|
+
import { injectProvider } from '../inject.js';
|
|
11
|
+
import { readMaybeSignal } from '../maybe-signal.js';
|
|
12
|
+
import { STORAGE_PROVIDER } from '../tokens.js';
|
|
13
|
+
/**
|
|
14
|
+
* The storage provider registered by `provideMolecule({ storage })` or
|
|
15
|
+
* `provideStorage`. Call it in an injection context.
|
|
16
|
+
*
|
|
17
|
+
* @returns The storage provider.
|
|
18
|
+
* @throws {Error} When no storage provider is registered.
|
|
19
|
+
*/
|
|
20
|
+
export function useStorageProvider() {
|
|
21
|
+
return injectProvider(STORAGE_PROVIDER, 'useStorageOutsideProvider', 'useStorageProvider must be used within a StorageProvider');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Manages one storage value. Call it in an injection context (a component
|
|
25
|
+
* field initializer or constructor).
|
|
26
|
+
*
|
|
27
|
+
* A write bumps a version counter, so a read that started before it never
|
|
28
|
+
* replaces the newer value; a read that started before a write still ends
|
|
29
|
+
* loading. `sync` subscribes through the PROVIDER's own change subscription, so
|
|
30
|
+
* a non-browser backend propagates the same way instead of being limited to
|
|
31
|
+
* the browser `storage` event.
|
|
32
|
+
*
|
|
33
|
+
* The arguments may be signals: a new key then starts over on the same
|
|
34
|
+
* component (the value falls back to the default, loading turns true and the
|
|
35
|
+
* new key is read and subscribed), the way React re-renders `useStorageValue`
|
|
36
|
+
* with a new key. The value, its pending write and any error survive the
|
|
37
|
+
* change, as React's state does.
|
|
38
|
+
*
|
|
39
|
+
* @param key - Storage key (or a signal of it).
|
|
40
|
+
* @param options - Hook options (`defaultValue`, `sync`), or a signal of them.
|
|
41
|
+
* @returns The value, its mutators, and the loading/error state as signals.
|
|
42
|
+
*/
|
|
43
|
+
export function useStorageValue(key, options) {
|
|
44
|
+
const storage = useStorageProvider();
|
|
45
|
+
const initialOptions = readMaybeSignal(options) ?? {};
|
|
46
|
+
const value = signal(initialOptions.defaultValue, /* @ts-ignore */
|
|
47
|
+
...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
|
|
48
|
+
const loading = signal(true, /* @ts-ignore */
|
|
49
|
+
...(ngDevMode ? [{ debugName: "loading" }] : /* istanbul ignore next */ []));
|
|
50
|
+
const error = signal(null, /* @ts-ignore */
|
|
51
|
+
...(ngDevMode ? [{ debugName: "error" }] : /* istanbul ignore next */ []));
|
|
52
|
+
/**
|
|
53
|
+
* Incremented by every local write. A read that started before it must not
|
|
54
|
+
* replace the newer value — the user editing before the first read lands is
|
|
55
|
+
* the common case on a slow (native, encrypted) backend.
|
|
56
|
+
*/
|
|
57
|
+
let writeVersion = 0;
|
|
58
|
+
const destroyRef = inject(DestroyRef);
|
|
59
|
+
// Load the key's value. Each load owns a `current` flag: a newer load (a key
|
|
60
|
+
// change) or the component's end supersedes it, exactly like the `mounted`
|
|
61
|
+
// closure of React's read effect. The default is read when the read resolves,
|
|
62
|
+
// like React's `defaultRef.current`.
|
|
63
|
+
const loadValue = (currentKey) => {
|
|
64
|
+
let current = true;
|
|
65
|
+
const version = writeVersion;
|
|
66
|
+
const run = async () => {
|
|
67
|
+
try {
|
|
68
|
+
const stored = await storage.get(currentKey);
|
|
69
|
+
if (!current)
|
|
70
|
+
return;
|
|
71
|
+
// A write made after this read started holds the newer value; the read still ends loading.
|
|
72
|
+
if (writeVersion === version)
|
|
73
|
+
value.set(stored ?? readMaybeSignal(options)?.defaultValue);
|
|
74
|
+
loading.set(false);
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
if (!current)
|
|
78
|
+
return;
|
|
79
|
+
if (writeVersion === version) {
|
|
80
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
81
|
+
}
|
|
82
|
+
loading.set(false);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
void run();
|
|
86
|
+
return () => {
|
|
87
|
+
current = false;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
// Sync across tabs if supported — through the PROVIDER's own subscription, so
|
|
91
|
+
// a non-browser backend (native, encrypted, memory) propagates the same way
|
|
92
|
+
// instead of being limited to the browser `storage` event. Re-keyed when the
|
|
93
|
+
// key or the `sync` option changes, as React's effect's dependency array
|
|
94
|
+
// re-runs it.
|
|
95
|
+
let stopSync = null;
|
|
96
|
+
const syncKey = (currentKey, sync) => {
|
|
97
|
+
stopSync?.();
|
|
98
|
+
stopSync = null;
|
|
99
|
+
if (!sync || !storage.subscribe)
|
|
100
|
+
return;
|
|
101
|
+
stopSync = storage.subscribe(currentKey, (next) => {
|
|
102
|
+
writeVersion += 1;
|
|
103
|
+
value.set(next === null ? readMaybeSignal(options)?.defaultValue : next);
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
let wiredKey = readMaybeSignal(key);
|
|
107
|
+
let wiredSync = initialOptions.sync ?? false;
|
|
108
|
+
let stopLoad = loadValue(wiredKey);
|
|
109
|
+
syncKey(wiredKey, wiredSync);
|
|
110
|
+
destroyRef.onDestroy(() => {
|
|
111
|
+
stopLoad();
|
|
112
|
+
stopSync?.();
|
|
113
|
+
});
|
|
114
|
+
// A new key's value is unknown until its read lands: show what a fresh mount
|
|
115
|
+
// shows, then read and re-subscribe the new key — React does the reset at
|
|
116
|
+
// render time and re-runs its two effects.
|
|
117
|
+
effect(() => {
|
|
118
|
+
const nextKey = readMaybeSignal(key);
|
|
119
|
+
const nextSync = readMaybeSignal(options)?.sync ?? false;
|
|
120
|
+
if (nextKey === wiredKey && nextSync === wiredSync)
|
|
121
|
+
return;
|
|
122
|
+
untracked(() => {
|
|
123
|
+
if (nextKey !== wiredKey) {
|
|
124
|
+
wiredKey = nextKey;
|
|
125
|
+
stopLoad();
|
|
126
|
+
value.set(readMaybeSignal(options)?.defaultValue);
|
|
127
|
+
loading.set(true);
|
|
128
|
+
error.set(null);
|
|
129
|
+
stopLoad = loadValue(nextKey);
|
|
130
|
+
}
|
|
131
|
+
wiredSync = nextSync;
|
|
132
|
+
syncKey(wiredKey, nextSync);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
const setValue = async (newValue) => {
|
|
136
|
+
writeVersion += 1;
|
|
137
|
+
try {
|
|
138
|
+
await storage.set(readMaybeSignal(key), newValue);
|
|
139
|
+
value.set(newValue);
|
|
140
|
+
error.set(null);
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
144
|
+
throw err;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const removeValue = async () => {
|
|
148
|
+
writeVersion += 1;
|
|
149
|
+
try {
|
|
150
|
+
await storage.remove(readMaybeSignal(key));
|
|
151
|
+
value.set(readMaybeSignal(options)?.defaultValue);
|
|
152
|
+
error.set(null);
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
return {
|
|
160
|
+
value: value.asReadonly(),
|
|
161
|
+
setValue,
|
|
162
|
+
removeValue,
|
|
163
|
+
loading: loading.asReadonly(),
|
|
164
|
+
error: error.asReadonly(),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Subscribes to ONE persisted preference through `getPreferenceStore()`. Call
|
|
169
|
+
* it in an injection context (a component field initializer or constructor).
|
|
170
|
+
*
|
|
171
|
+
* This is the component-side half of `getPreferenceStore()`: a value read at
|
|
172
|
+
* injection time is available immediately when the bonded provider can answer
|
|
173
|
+
* synchronously, and an asynchronous provider's hydration CONVERGES onto the
|
|
174
|
+
* same component without a remount. Ordering is the store's: a read that
|
|
175
|
+
* started before a newer write, remove or external change is discarded, so a
|
|
176
|
+
* slow hydration never overwrites an edit the user already made.
|
|
177
|
+
*
|
|
178
|
+
* Pass `null` for `key` while the real key is unknown (no project selected
|
|
179
|
+
* yet): the hook then behaves as local state around `defaultValue` and
|
|
180
|
+
* persists nothing.
|
|
181
|
+
*
|
|
182
|
+
* The arguments may be signals: a new key then re-reads and re-subscribes that
|
|
183
|
+
* key on the same component (its old subscription is dropped, hydration
|
|
184
|
+
* converges it), the way React re-renders `useStoragePreference` with a new
|
|
185
|
+
* key. The shown value survives the change, as React's state does.
|
|
186
|
+
*
|
|
187
|
+
* @param key - The storage key, or `null` when there is nothing to persist (or
|
|
188
|
+
* a signal of it).
|
|
189
|
+
* @param options - `defaultValue`, reported until the provider supplies one
|
|
190
|
+
* (or a signal of them).
|
|
191
|
+
* @returns The current value, its mutators, and whether it is definite yet.
|
|
192
|
+
*/
|
|
193
|
+
export function useStoragePreference(key, options) {
|
|
194
|
+
const store = getPreferenceStore();
|
|
195
|
+
const read = () => {
|
|
196
|
+
const currentKey = readMaybeSignal(key);
|
|
197
|
+
if (!currentKey)
|
|
198
|
+
return readMaybeSignal(options)?.defaultValue;
|
|
199
|
+
const stored = store.get(currentKey);
|
|
200
|
+
return stored === null ? readMaybeSignal(options)?.defaultValue : stored;
|
|
201
|
+
};
|
|
202
|
+
const value = signal(read(), /* @ts-ignore */
|
|
203
|
+
...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
|
|
204
|
+
const ready = signal(!readMaybeSignal(key), /* @ts-ignore */
|
|
205
|
+
...(ngDevMode ? [{ debugName: "ready" }] : /* istanbul ignore next */ []));
|
|
206
|
+
const destroyRef = inject(DestroyRef);
|
|
207
|
+
/**
|
|
208
|
+
* Wires one key, exactly the body of React's subscription effect for that
|
|
209
|
+
* key: seed from whatever the store already knows, then let hydration
|
|
210
|
+
* converge it (a no-op change on a synchronous provider).
|
|
211
|
+
*
|
|
212
|
+
* @param currentKey - The key to wire (`null`/`undefined` is local state).
|
|
213
|
+
* @returns The unwire function: cancels the hydration and unsubscribes.
|
|
214
|
+
*/
|
|
215
|
+
const wire = (currentKey) => {
|
|
216
|
+
if (!currentKey) {
|
|
217
|
+
ready.set(true);
|
|
218
|
+
value.set(readMaybeSignal(options)?.defaultValue);
|
|
219
|
+
return () => { };
|
|
220
|
+
}
|
|
221
|
+
let cancelled = false;
|
|
222
|
+
const sync = () => {
|
|
223
|
+
if (!cancelled)
|
|
224
|
+
value.set(read());
|
|
225
|
+
};
|
|
226
|
+
sync();
|
|
227
|
+
ready.set(false);
|
|
228
|
+
const stop = store.subscribe(currentKey, sync);
|
|
229
|
+
store
|
|
230
|
+
.hydrate([currentKey])
|
|
231
|
+
.then(() => {
|
|
232
|
+
if (cancelled)
|
|
233
|
+
return;
|
|
234
|
+
sync();
|
|
235
|
+
ready.set(true);
|
|
236
|
+
})
|
|
237
|
+
.catch((_error) => {
|
|
238
|
+
// Hydration is best-effort by the store's contract (persistence and
|
|
239
|
+
// read failures are swallowed there, and `hydrate` resolves once every
|
|
240
|
+
// key has been ATTEMPTED); the value keeps the last known snapshot, and
|
|
241
|
+
// `ready` still turns true so the UI is not stuck on a spinner.
|
|
242
|
+
if (!cancelled)
|
|
243
|
+
ready.set(true);
|
|
244
|
+
});
|
|
245
|
+
return () => {
|
|
246
|
+
cancelled = true;
|
|
247
|
+
stop();
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
let wiredKey = readMaybeSignal(key);
|
|
251
|
+
let unwire = wire(wiredKey);
|
|
252
|
+
// Reads the CURRENT unwire, so a key switch re-targets the cleanup too.
|
|
253
|
+
destroyRef.onDestroy(() => {
|
|
254
|
+
unwire();
|
|
255
|
+
});
|
|
256
|
+
// A changed key re-wires on the same component, as React's effect re-runs
|
|
257
|
+
// when its key dependency changes: the old key's subscription is dropped
|
|
258
|
+
// before the new one starts.
|
|
259
|
+
effect(() => {
|
|
260
|
+
const nextKey = readMaybeSignal(key);
|
|
261
|
+
if (nextKey === wiredKey)
|
|
262
|
+
return;
|
|
263
|
+
wiredKey = nextKey;
|
|
264
|
+
untracked(() => {
|
|
265
|
+
unwire();
|
|
266
|
+
unwire = wire(nextKey);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
const setValue = (next) => {
|
|
270
|
+
const resolved = typeof next === 'function' ? next(value()) : next;
|
|
271
|
+
value.set(resolved);
|
|
272
|
+
const currentKey = readMaybeSignal(key);
|
|
273
|
+
if (currentKey)
|
|
274
|
+
store.set(currentKey, resolved);
|
|
275
|
+
};
|
|
276
|
+
const removeValue = () => {
|
|
277
|
+
value.set(readMaybeSignal(options)?.defaultValue);
|
|
278
|
+
const currentKey = readMaybeSignal(key);
|
|
279
|
+
if (currentKey)
|
|
280
|
+
store.remove(currentKey);
|
|
281
|
+
};
|
|
282
|
+
return { value: value.asReadonly(), setValue, removeValue, ready: ready.asReadonly() };
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* The registered provider's operations as stable functions, no reactive state.
|
|
286
|
+
* Call it in an injection context.
|
|
287
|
+
*
|
|
288
|
+
* @returns The get/set/remove/clear/keys functions.
|
|
289
|
+
*/
|
|
290
|
+
export function useStorage() {
|
|
291
|
+
const storage = useStorageProvider();
|
|
292
|
+
const get = (key) => storage.get(key);
|
|
293
|
+
const set = (key, value) => storage.set(key, value);
|
|
294
|
+
const remove = (key) => storage.remove(key);
|
|
295
|
+
const clear = () => storage.clear();
|
|
296
|
+
const keys = () => storage.keys();
|
|
297
|
+
return { get, set, remove, clear, keys };
|
|
298
|
+
}
|
|
299
|
+
//# sourceMappingURL=use-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-storage.js","sourceRoot":"","sources":["../../src/hooks/use-storage.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAe,MAAM,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAE1F,OAAO,EAAE,kBAAkB,EAAwB,MAAM,uBAAuB,CAAA;AAEhF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAoB,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAE/C;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB;IAChC,OAAO,cAAc,CACnB,gBAAgB,EAChB,2BAA2B,EAC3B,0DAA0D,CAC3D,CAAA;AACH,CAAC;AAiCD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,eAAe,CAC7B,GA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UAAU,CAAC,CAAA;IAC1C,CAAC,CAAA;IAED,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,UAAU,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,CAAC,UAAU,EAAE,EAAE,CAAA;AACxF,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU;IAOxB,MAAM,OAAO,GAAG,kBAAkB,EAAE,CAAA;IAEpC,MAAM,GAAG,GAAG,CAAI,GAAW,EAAqB,EAAE,CAAC,OAAO,CAAC,GAAG,CAAI,GAAG,CAAC,CAAA;IACtE,MAAM,GAAG,GAAG,CAAI,GAAW,EAAE,KAAQ,EAAiB,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IAChF,MAAM,MAAM,GAAG,CAAC,GAAW,EAAiB,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAClE,MAAM,KAAK,GAAG,GAAkB,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;IAClD,MAAM,IAAI,GAAG,GAAsB,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;IAEpD,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;AAC1C,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for state stores, mirroring `useStateProvider`, `useStore`,
|
|
3
|
+
* `useSetStore` and `useStoreAction` from `@molecule/app-react`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { type Signal } from '@angular/core';
|
|
8
|
+
import type { StateProvider, Store } from '@molecule/app-state';
|
|
9
|
+
import { type MaybeSignal } from '../maybe-signal.js';
|
|
10
|
+
/**
|
|
11
|
+
* Options for {@link useStore}.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The store's state shape.
|
|
14
|
+
* @template S - The selected value's shape.
|
|
15
|
+
*/
|
|
16
|
+
export interface UseStoreOptions<T, S = T> {
|
|
17
|
+
/**
|
|
18
|
+
* Derives the hook's value from the whole state. Receives the full state on
|
|
19
|
+
* every read.
|
|
20
|
+
*/
|
|
21
|
+
selector?: (state: T) => S;
|
|
22
|
+
/**
|
|
23
|
+
* Compares the previous selected value with the next one; an equal pair keeps
|
|
24
|
+
* the previous result. Without it, identity decides.
|
|
25
|
+
*/
|
|
26
|
+
equalityFn?: (previous: S, next: S) => boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The state provider registered by `provideMolecule({ state })` (by default the
|
|
30
|
+
* provider bonded under `@molecule/app-state`). Call it in an injection context.
|
|
31
|
+
*
|
|
32
|
+
* Only hooks that read the registration call this; `useStore(store)` takes its
|
|
33
|
+
* store as an argument and works with no provider at all.
|
|
34
|
+
*
|
|
35
|
+
* @returns The state provider.
|
|
36
|
+
* @throws {Error} When no state provider is registered — the message names the
|
|
37
|
+
* fix (which registration to add, and the call for it).
|
|
38
|
+
*/
|
|
39
|
+
export declare function useStateProvider(): StateProvider;
|
|
40
|
+
/**
|
|
41
|
+
* Subscribes to a store with an optional selector and equality function.
|
|
42
|
+
* Call it in an injection context.
|
|
43
|
+
*
|
|
44
|
+
* The store is the argument, so this function needs NO registration: it reads
|
|
45
|
+
* `store.getState()` and subscribes with `store.subscribe()`, and never touches
|
|
46
|
+
* the state registration. A store minted outside the `state` bond — the direct
|
|
47
|
+
* `createStoreWithActions()` / `createStore()` a provider package exports — is
|
|
48
|
+
* therefore as valid here as one `@molecule/app-state`'s `createStore()` made.
|
|
49
|
+
*
|
|
50
|
+
* The arguments may be signals: a new store is subscribed in place of the old
|
|
51
|
+
* one (the old subscription is dropped first) and new options are applied on
|
|
52
|
+
* the next read, the way React re-renders `useStore` with new arguments. The
|
|
53
|
+
* previous selected value the equality function compares survives the change,
|
|
54
|
+
* as React's ref does.
|
|
55
|
+
*
|
|
56
|
+
* @param store - The store to subscribe to (or a signal of it).
|
|
57
|
+
* @param options - Hook options (`selector`, `equalityFn`), or a signal of them.
|
|
58
|
+
* @returns A signal of the selected state.
|
|
59
|
+
*/
|
|
60
|
+
export declare function useStore<T, S = T>(store: MaybeSignal<Store<T>>, options?: MaybeSignal<UseStoreOptions<T, S>>): Signal<S>;
|
|
61
|
+
/**
|
|
62
|
+
* The store's `setState` function. Call it in an injection context.
|
|
63
|
+
*
|
|
64
|
+
* @param store - The store to get `setState` from.
|
|
65
|
+
* @returns The `setState` function.
|
|
66
|
+
*/
|
|
67
|
+
export declare function useSetStore<T>(store: Store<T>): Store<T>['setState'];
|
|
68
|
+
/**
|
|
69
|
+
* A function bound to a store's `setState` and `getState`. Call it in an
|
|
70
|
+
* injection context.
|
|
71
|
+
*
|
|
72
|
+
* The action receives the store's `setState` and `getState` on every call, so
|
|
73
|
+
* it reads fresh state each time (like React's, which re-runs
|
|
74
|
+
* `action(setState, getState)` per call).
|
|
75
|
+
*
|
|
76
|
+
* @param store - The store to bind to.
|
|
77
|
+
* @param action - The action function that receives `setState` and `getState`.
|
|
78
|
+
* @returns A bound action function.
|
|
79
|
+
*/
|
|
80
|
+
export declare function useStoreAction<T, Args extends unknown[], R>(store: Store<T>, action: (setState: Store<T>['setState'], getState: Store<T>['getState']) => (...args: Args) => R): (...args: Args) => R;
|
|
81
|
+
//# sourceMappingURL=use-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-store.d.ts","sourceRoot":"","sources":["../../src/hooks/use-store.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAkB,KAAK,MAAM,EAAa,MAAM,eAAe,CAAA;AAGtE,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAE/D,OAAO,EAAE,KAAK,WAAW,EAAmB,MAAM,oBAAoB,CAAA;AAItE;;;;;GAKG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,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|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { effect, inject, untracked } from '@angular/core';
|
|
8
|
+
import { t } from '@molecule/app-i18n';
|
|
9
|
+
import { readMaybeSignal } from '../maybe-signal.js';
|
|
10
|
+
import { STATE_PROVIDER } from '../tokens.js';
|
|
11
|
+
import { useExternalStore } from './external-store.js';
|
|
12
|
+
/**
|
|
13
|
+
* The fix, appended to the error below — this function is the one place that
|
|
14
|
+
* READS the token, so it is the one place that has to say where the provider
|
|
15
|
+
* comes from. The sentences are code (a function call, a bootstrap array), so
|
|
16
|
+
* they are appended in English rather than translated, exactly like React's.
|
|
17
|
+
*/
|
|
18
|
+
const STATE_PROVIDER_FIX = ' To fix: register the provider before this component injects it — ' +
|
|
19
|
+
"`import { provideMolecule } from '@molecule/app-angular'`; " +
|
|
20
|
+
'provideMolecule passes the provider bonded under `@molecule/app-state` ' +
|
|
21
|
+
'(bond one with `setProvider(provider)` from `@molecule/app-state`), and ' +
|
|
22
|
+
'provideMolecule({ state: provider }) passes one you hold. ' +
|
|
23
|
+
'To read a store you already have, call useStore(store) instead — it subscribes to the ' +
|
|
24
|
+
'store it is given and needs no provider.';
|
|
25
|
+
/**
|
|
26
|
+
* The state provider registered by `provideMolecule({ state })` (by default the
|
|
27
|
+
* provider bonded under `@molecule/app-state`). Call it in an injection context.
|
|
28
|
+
*
|
|
29
|
+
* Only hooks that read the registration call this; `useStore(store)` takes its
|
|
30
|
+
* store as an argument and works with no provider at all.
|
|
31
|
+
*
|
|
32
|
+
* @returns The state provider.
|
|
33
|
+
* @throws {Error} When no state provider is registered — the message names the
|
|
34
|
+
* fix (which registration to add, and the call for it).
|
|
35
|
+
*/
|
|
36
|
+
export function useStateProvider() {
|
|
37
|
+
const provider = inject(STATE_PROVIDER, { optional: true });
|
|
38
|
+
if (!provider) {
|
|
39
|
+
throw new Error(t('angular.error.useStoreOutsideProvider', undefined, {
|
|
40
|
+
defaultValue: 'useStateProvider must be used within a StateProvider',
|
|
41
|
+
}) + STATE_PROVIDER_FIX);
|
|
42
|
+
}
|
|
43
|
+
return provider;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Subscribes to a store with an optional selector and equality function.
|
|
47
|
+
* Call it in an injection context.
|
|
48
|
+
*
|
|
49
|
+
* The store is the argument, so this function needs NO registration: it reads
|
|
50
|
+
* `store.getState()` and subscribes with `store.subscribe()`, and never touches
|
|
51
|
+
* the state registration. A store minted outside the `state` bond — the direct
|
|
52
|
+
* `createStoreWithActions()` / `createStore()` a provider package exports — is
|
|
53
|
+
* therefore as valid here as one `@molecule/app-state`'s `createStore()` made.
|
|
54
|
+
*
|
|
55
|
+
* The arguments may be signals: a new store is subscribed in place of the old
|
|
56
|
+
* one (the old subscription is dropped first) and new options are applied on
|
|
57
|
+
* the next read, the way React re-renders `useStore` with new arguments. The
|
|
58
|
+
* previous selected value the equality function compares survives the change,
|
|
59
|
+
* as React's ref does.
|
|
60
|
+
*
|
|
61
|
+
* @param store - The store to subscribe to (or a signal of it).
|
|
62
|
+
* @param options - Hook options (`selector`, `equalityFn`), or a signal of them.
|
|
63
|
+
* @returns A signal of the selected state.
|
|
64
|
+
*/
|
|
65
|
+
export function useStore(store, options) {
|
|
66
|
+
const initialOptions = readMaybeSignal(options);
|
|
67
|
+
let currentStore = readMaybeSignal(store);
|
|
68
|
+
let currentSelector = initialOptions?.selector;
|
|
69
|
+
let currentEquality = initialOptions?.equalityFn;
|
|
70
|
+
// The previous selected value for the equality comparison, exactly like
|
|
71
|
+
// React's ref: an equal selection keeps the previous result (its identity).
|
|
72
|
+
let prevSelected;
|
|
73
|
+
const read = () => {
|
|
74
|
+
const state = currentStore.getState();
|
|
75
|
+
const selected = currentSelector ? currentSelector(state) : state;
|
|
76
|
+
if (currentEquality && prevSelected !== undefined) {
|
|
77
|
+
if (currentEquality(prevSelected, selected)) {
|
|
78
|
+
return prevSelected;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
prevSelected = selected;
|
|
82
|
+
return selected;
|
|
83
|
+
};
|
|
84
|
+
// The store's notifications feed the external-store bridge's own callback
|
|
85
|
+
// (its read discipline); the subscription is re-keyed when the store source
|
|
86
|
+
// changes, as React's useSyncExternalStore re-subscribes when its store
|
|
87
|
+
// does.
|
|
88
|
+
let notifyStore = null;
|
|
89
|
+
let stop = null;
|
|
90
|
+
const state = useExternalStore(read, (notify) => {
|
|
91
|
+
notifyStore = notify;
|
|
92
|
+
stop = currentStore.subscribe(() => notify());
|
|
93
|
+
return () => stop?.();
|
|
94
|
+
});
|
|
95
|
+
effect(() => {
|
|
96
|
+
const nextStore = readMaybeSignal(store);
|
|
97
|
+
const nextOptions = readMaybeSignal(options);
|
|
98
|
+
const nextSelector = nextOptions?.selector;
|
|
99
|
+
const nextEquality = nextOptions?.equalityFn;
|
|
100
|
+
if (nextStore === currentStore &&
|
|
101
|
+
nextSelector === currentSelector &&
|
|
102
|
+
nextEquality === currentEquality) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
currentStore = nextStore;
|
|
106
|
+
currentSelector = nextSelector;
|
|
107
|
+
currentEquality = nextEquality;
|
|
108
|
+
untracked(() => {
|
|
109
|
+
stop?.();
|
|
110
|
+
stop = currentStore.subscribe(() => notifyStore?.());
|
|
111
|
+
notifyStore?.();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
return state;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The store's `setState` function. Call it in an injection context.
|
|
118
|
+
*
|
|
119
|
+
* @param store - The store to get `setState` from.
|
|
120
|
+
* @returns The `setState` function.
|
|
121
|
+
*/
|
|
122
|
+
export function useSetStore(store) {
|
|
123
|
+
return store.setState;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* A function bound to a store's `setState` and `getState`. Call it in an
|
|
127
|
+
* injection context.
|
|
128
|
+
*
|
|
129
|
+
* The action receives the store's `setState` and `getState` on every call, so
|
|
130
|
+
* it reads fresh state each time (like React's, which re-runs
|
|
131
|
+
* `action(setState, getState)` per call).
|
|
132
|
+
*
|
|
133
|
+
* @param store - The store to bind to.
|
|
134
|
+
* @param action - The action function that receives `setState` and `getState`.
|
|
135
|
+
* @returns A bound action function.
|
|
136
|
+
*/
|
|
137
|
+
export function useStoreAction(store, action) {
|
|
138
|
+
// The action receives the store's setState and getState on every CALL, so it
|
|
139
|
+
// reads fresh state each time (React re-runs action(setState, getState) per
|
|
140
|
+
// call through its useCallback for the same reason).
|
|
141
|
+
return (...args) => action(store.setState, store.getState)(...args);
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=use-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-store.js","sourceRoot":"","sources":["../../src/hooks/use-store.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAe,SAAS,EAAE,MAAM,eAAe,CAAA;AAEtE,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AAGtC,OAAO,EAAoB,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAqBtD;;;;;GAKG;AACH,MAAM,kBAAkB,GACtB,oEAAoE;IACpE,6DAA6D;IAC7D,yEAAyE;IACzE,0EAA0E;IAC1E,4DAA4D;IAC5D,wFAAwF;IACxF,0CAA0C,CAAA;AAE5C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB;IAC9B,MAAM,QAAQ,GAAG,MAAM,CAAC,cAAc,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAC3D,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,CAAC,CAAC,uCAAuC,EAAE,SAAS,EAAE;YACpD,YAAY,EAAE,sDAAsD;SACrE,CAAC,GAAG,kBAAkB,CACxB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
|
4
|
+
* `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { type Signal } from '@angular/core';
|
|
9
|
+
import type { Theme, ThemeProvider } from '@molecule/app-theme';
|
|
10
|
+
/**
|
|
11
|
+
* The theme provider registered by `provideMolecule({ theme })` or
|
|
12
|
+
* `provideTheme`. Call it in an injection context.
|
|
13
|
+
*
|
|
14
|
+
* @returns The theme provider.
|
|
15
|
+
* @throws {Error} When no theme provider is registered.
|
|
16
|
+
*/
|
|
17
|
+
export declare function useThemeProvider(): ThemeProvider;
|
|
18
|
+
/**
|
|
19
|
+
* What {@link useTheme} returns: the current theme, its name and mode as
|
|
20
|
+
* signals, and the actions as plain functions.
|
|
21
|
+
*/
|
|
22
|
+
export interface UseThemeResult {
|
|
23
|
+
/** The current theme object. */
|
|
24
|
+
readonly theme: Signal<Theme>;
|
|
25
|
+
/** The current theme's name. */
|
|
26
|
+
readonly themeName: Signal<string>;
|
|
27
|
+
/** The current theme's mode (`'light'` or `'dark'`). */
|
|
28
|
+
readonly mode: Signal<'light' | 'dark'>;
|
|
29
|
+
/** Sets the theme by name. */
|
|
30
|
+
setTheme: (name: string) => void;
|
|
31
|
+
/** Toggles the theme mode, preferring the provider's `toggleMode()`. */
|
|
32
|
+
toggleTheme: () => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Theme state and actions. Call it in an injection context (a component field
|
|
36
|
+
* initializer or constructor).
|
|
37
|
+
*
|
|
38
|
+
* @returns The theme, its name and mode as signals, and the actions.
|
|
39
|
+
*/
|
|
40
|
+
export declare function useTheme(): UseThemeResult;
|
|
41
|
+
/**
|
|
42
|
+
* The current theme object alone. Call it in an injection context.
|
|
43
|
+
*
|
|
44
|
+
* @returns A signal of the current theme.
|
|
45
|
+
*/
|
|
46
|
+
export declare function useCurrentTheme(): Signal<Theme>;
|
|
47
|
+
/**
|
|
48
|
+
* The current theme mode alone. Call it in an injection context.
|
|
49
|
+
*
|
|
50
|
+
* @returns A signal of `'light'` or `'dark'`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function useThemeMode(): Signal<'light' | 'dark'>;
|
|
53
|
+
/**
|
|
54
|
+
* The current theme's color palette. Call it in an injection context.
|
|
55
|
+
*
|
|
56
|
+
* @returns A signal of the current theme's colors.
|
|
57
|
+
*/
|
|
58
|
+
export declare function useThemeColors(): Signal<Theme['colors']>;
|
|
59
|
+
//# sourceMappingURL=use-theme.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-theme.d.ts","sourceRoot":"","sources":["../../src/hooks/use-theme.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAgC,KAAK,MAAM,EAAU,MAAM,eAAe,CAAA;AAEjF,OAAO,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAK/D;;;;;;GAMG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,CAMhD;AAgCD;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,gCAAgC;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;IAC7B,gCAAgC;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;IAClC,wDAAwD;IACxD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,CAAA;IACvC,8BAA8B;IAC9B,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAChC,wEAAwE;IACxE,WAAW,EAAE,MAAM,IAAI,CAAA;CACxB;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,IAAI,cAAc,CA4CzC;AAED;;;;GAIG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAAC,KAAK,CAAC,CAE/C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,CAEvD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAGxD"}
|