@molecule/app-angular 3.3.0 → 3.5.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 +3889 -1267
- 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 +46 -0
- package/dist/hooks/use-ai-models.d.ts.map +1 -0
- package/dist/hooks/use-ai-models.js +142 -0
- package/dist/hooks/use-ai-models.js.map +1 -0
- package/dist/hooks/use-async.d.ts +125 -0
- package/dist/hooks/use-async.d.ts.map +1 -0
- package/dist/hooks/use-async.js +189 -0
- package/dist/hooks/use-async.js.map +1 -0
- package/dist/hooks/use-auth-flows.d.ts +207 -0
- package/dist/hooks/use-auth-flows.d.ts.map +1 -0
- package/dist/hooks/use-auth-flows.js +460 -0
- package/dist/hooks/use-auth-flows.js.map +1 -0
- package/dist/hooks/use-auth.d.ts +80 -0
- package/dist/hooks/use-auth.d.ts.map +1 -0
- package/dist/hooks/use-auth.js +89 -0
- package/dist/hooks/use-auth.js.map +1 -0
- package/dist/hooks/use-forms.d.ts +177 -0
- package/dist/hooks/use-forms.d.ts.map +1 -0
- package/dist/hooks/use-forms.js +185 -0
- package/dist/hooks/use-forms.js.map +1 -0
- package/dist/hooks/use-http.d.ts +155 -0
- package/dist/hooks/use-http.d.ts.map +1 -0
- package/dist/hooks/use-http.js +212 -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 +213 -0
- package/dist/hooks/use-native.d.ts.map +1 -0
- package/dist/hooks/use-native.js +158 -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 +212 -0
- package/dist/hooks/use-router.d.ts.map +1 -0
- package/dist/hooks/use-router.js +251 -0
- package/dist/hooks/use-router.js.map +1 -0
- package/dist/hooks/use-storage.d.ts +128 -0
- package/dist/hooks/use-storage.d.ts.map +1 -0
- package/dist/hooks/use-storage.js +217 -0
- package/dist/hooks/use-storage.js.map +1 -0
- package/dist/hooks/use-store.d.ts +74 -0
- package/dist/hooks/use-store.d.ts.map +1 -0
- package/dist/hooks/use-store.js +103 -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 +86 -0
- package/dist/hooks/use-translation.d.ts.map +1 -0
- package/dist/hooks/use-translation.js +146 -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 +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -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/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/services/change-password.service.d.ts +4 -1
- package/dist/services/change-password.service.d.ts.map +1 -1
- package/dist/services/change-password.service.js +3 -0
- package/dist/services/change-password.service.js.map +1 -1
- package/dist/services/password-reset.service.d.ts +4 -1
- package/dist/services/password-reset.service.d.ts.map +1 -1
- package/dist/services/password-reset.service.js +3 -0
- package/dist/services/password-reset.service.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/site.d.ts +51 -0
- package/dist/site.d.ts.map +1 -0
- package/dist/site.js +69 -0
- package/dist/site.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 +6 -6
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for routing, mirroring `useRouter`, `useRouterInstance`,
|
|
3
|
+
* `useLocation`, `useParams`, `useQuery`, `useNavigate`, `useIsActive`,
|
|
4
|
+
* `useListRoute` and `useConsumedSearchParam` from `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { afterNextRender, computed, DestroyRef, effect, inject, isSignal, signal, } from '@angular/core';
|
|
9
|
+
import { createConsumedSearchParam, listRouteRedirect, resolveListRoute, } from '@molecule/app-routing';
|
|
10
|
+
import { injectProvider } from '../inject.js';
|
|
11
|
+
import { ROUTER } from '../tokens.js';
|
|
12
|
+
/**
|
|
13
|
+
* The router registered by `provideMolecule({ router })` or `provideRouter`.
|
|
14
|
+
* Call it in an injection context.
|
|
15
|
+
*
|
|
16
|
+
* @returns The router instance.
|
|
17
|
+
* @throws {Error} When no router is registered.
|
|
18
|
+
*/
|
|
19
|
+
export function useRouterInstance() {
|
|
20
|
+
return injectProvider(ROUTER, 'useRouterOutsideProvider', 'useRouterInstance must be used within a RouterProvider');
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Subscribes to the router once and mirrors location, params and query into
|
|
24
|
+
* signals.
|
|
25
|
+
*
|
|
26
|
+
* @param router - The router to follow.
|
|
27
|
+
* @returns The three location signals.
|
|
28
|
+
*/
|
|
29
|
+
function followRouter(router) {
|
|
30
|
+
const location = signal(router.getLocation(), /* @ts-ignore */
|
|
31
|
+
...(ngDevMode ? [{ debugName: "location" }] : /* istanbul ignore next */ []));
|
|
32
|
+
const params = signal(router.getParams(), /* @ts-ignore */
|
|
33
|
+
...(ngDevMode ? [{ debugName: "params" }] : /* istanbul ignore next */ []));
|
|
34
|
+
const query = signal(router.getQuery(), /* @ts-ignore */
|
|
35
|
+
...(ngDevMode ? [{ debugName: "query" }] : /* istanbul ignore next */ []));
|
|
36
|
+
const stop = router.subscribe(() => {
|
|
37
|
+
location.set(router.getLocation());
|
|
38
|
+
params.set(router.getParams());
|
|
39
|
+
query.set(router.getQuery());
|
|
40
|
+
});
|
|
41
|
+
inject(DestroyRef).onDestroy(stop);
|
|
42
|
+
return {
|
|
43
|
+
location: location.asReadonly(),
|
|
44
|
+
params: params.asReadonly(),
|
|
45
|
+
query: query.asReadonly(),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Routing state and actions. Call it in an injection context (a component
|
|
50
|
+
* field initializer or constructor).
|
|
51
|
+
*
|
|
52
|
+
* @returns The location, params and query as signals, and the navigation actions.
|
|
53
|
+
*/
|
|
54
|
+
export function useRouter() {
|
|
55
|
+
const router = useRouterInstance();
|
|
56
|
+
const { location, params, query } = followRouter(router);
|
|
57
|
+
const navigate = (path, options) => {
|
|
58
|
+
router.navigate(path, options);
|
|
59
|
+
};
|
|
60
|
+
const navigateTo = (name, routeParams, query, options) => {
|
|
61
|
+
router.navigateTo(name, routeParams, query, options);
|
|
62
|
+
};
|
|
63
|
+
const back = () => {
|
|
64
|
+
router.back();
|
|
65
|
+
};
|
|
66
|
+
const forward = () => {
|
|
67
|
+
router.forward();
|
|
68
|
+
};
|
|
69
|
+
const isActive = (path, exact) => router.isActive(path, exact);
|
|
70
|
+
return { location, params, query, navigate, navigateTo, back, forward, isActive };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The current location alone. Call it in an injection context.
|
|
74
|
+
*
|
|
75
|
+
* @returns A signal of the current route location.
|
|
76
|
+
*/
|
|
77
|
+
export function useLocation() {
|
|
78
|
+
return followRouter(useRouterInstance()).location;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The current path parameters alone. Call it in an injection context.
|
|
82
|
+
*
|
|
83
|
+
* @returns A signal of the current path parameters.
|
|
84
|
+
*/
|
|
85
|
+
export function useParams() {
|
|
86
|
+
return followRouter(useRouterInstance()).params;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The current query parameters alone. Call it in an injection context.
|
|
90
|
+
*
|
|
91
|
+
* @returns A signal of the current query parameters.
|
|
92
|
+
*/
|
|
93
|
+
export function useQuery() {
|
|
94
|
+
return followRouter(useRouterInstance()).query;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A navigate function alone. Call it in an injection context.
|
|
98
|
+
*
|
|
99
|
+
* @returns The navigate function.
|
|
100
|
+
*/
|
|
101
|
+
export function useNavigate() {
|
|
102
|
+
const router = useRouterInstance();
|
|
103
|
+
return (path, options) => {
|
|
104
|
+
router.navigate(path, options);
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Whether a path is active, re-evaluated on every route change. Call it in an
|
|
109
|
+
* injection context.
|
|
110
|
+
*
|
|
111
|
+
* @param path - The path to check.
|
|
112
|
+
* @param exact - Whether to match exactly (default: `false`).
|
|
113
|
+
* @returns A signal of whether the path is active.
|
|
114
|
+
*/
|
|
115
|
+
export function useIsActive(path, exact = false) {
|
|
116
|
+
const router = useRouterInstance();
|
|
117
|
+
const active = signal(router.isActive(path, exact), /* @ts-ignore */
|
|
118
|
+
...(ngDevMode ? [{ debugName: "active" }] : /* istanbul ignore next */ []));
|
|
119
|
+
const stop = router.subscribe(() => active.set(router.isActive(path, exact)));
|
|
120
|
+
inject(DestroyRef).onDestroy(stop);
|
|
121
|
+
return active.asReadonly();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* URL-backed pagination, filters and selected views, through whichever routing
|
|
125
|
+
* bond the app wired: the page, the filters and the view live in the URL only,
|
|
126
|
+
* so a reload, a shared link and Back all show the same list. Built on
|
|
127
|
+
* `resolveListRoute` from `@molecule/app-routing`, so every framework binding
|
|
128
|
+
* returns the same result. Call it in an injection context (a component field
|
|
129
|
+
* initializer or constructor).
|
|
130
|
+
*
|
|
131
|
+
* The options may be a signal: an auth status going from `loading` to
|
|
132
|
+
* `unauthenticated` reaches the list on the same component, the way React
|
|
133
|
+
* re-renders `useListRoute` with new options.
|
|
134
|
+
*
|
|
135
|
+
* A signed-out visit to a view with `requiresAuth` replaces the entry with
|
|
136
|
+
* `options.auth.redirectTo`, carrying `state.returnTo`; until auth is confirmed
|
|
137
|
+
* `canLoad` is `false`.
|
|
138
|
+
*
|
|
139
|
+
* @param options - Filter sources, view paths, pagination and optional auth
|
|
140
|
+
* readiness (or a signal of them).
|
|
141
|
+
* @returns The list state (`page`, `offset`, `filters`, `selection`, `canLoad`)
|
|
142
|
+
* as signals, and the list's link builder and navigation actions.
|
|
143
|
+
* @throws {Error} When `pageSize` is not a positive safe integer, or a protected
|
|
144
|
+
* view has no `auth`.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* @Component({
|
|
149
|
+
* selector: 'app-articles',
|
|
150
|
+
* template: `
|
|
151
|
+
* <a [href]="list.href({ page: list.page() + 1 })">{{ i18n.t('list.nextPage') }}</a>
|
|
152
|
+
* `,
|
|
153
|
+
* })
|
|
154
|
+
* export class ArticlesComponent {
|
|
155
|
+
* readonly i18n = useTranslation()
|
|
156
|
+
* // Load items from list.offset(); the page and filters come from the URL.
|
|
157
|
+
* readonly list = useListRoute({ pageSize: 10, filters: { tag: {} } })
|
|
158
|
+
* }
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
export function useListRoute(options = {}) {
|
|
162
|
+
const { location, params, navigate } = useRouter();
|
|
163
|
+
// The options may be a signal; a plain object never changes on this component.
|
|
164
|
+
const readOptions = () => isSignal(options) ? options() : options;
|
|
165
|
+
// Re-resolved from the current location alone, exactly as React re-renders:
|
|
166
|
+
// a navigation (or an options change) recomputes every field.
|
|
167
|
+
const list = computed(() => resolveListRoute({ location: location(), routeParams: params() }, readOptions(), navigate), /* @ts-ignore */
|
|
168
|
+
...(ngDevMode ? [{ debugName: "list" }] : /* istanbul ignore next */ []));
|
|
169
|
+
// Resolve once at the call site: an invalid pageSize or a protected selection
|
|
170
|
+
// without auth throws here, the way React's render throws (a lazy computed
|
|
171
|
+
// would defer the throw to the first template read).
|
|
172
|
+
list();
|
|
173
|
+
// React performs the redirect after the render that reported it; this effect
|
|
174
|
+
// runs it at the same point, and re-runs while the redirect target moves.
|
|
175
|
+
effect(() => {
|
|
176
|
+
const redirect = listRouteRedirect(list(), location());
|
|
177
|
+
if (redirect === undefined)
|
|
178
|
+
return;
|
|
179
|
+
navigate(redirect.to, redirect.options);
|
|
180
|
+
});
|
|
181
|
+
return {
|
|
182
|
+
page: computed(() => list().page),
|
|
183
|
+
pageSize: computed(() => list().pageSize),
|
|
184
|
+
offset: computed(() => list().offset),
|
|
185
|
+
filters: computed(() => list().filters),
|
|
186
|
+
selection: computed(() => list().selection),
|
|
187
|
+
params: computed(() => list().params),
|
|
188
|
+
canLoad: computed(() => list().canLoad),
|
|
189
|
+
status: computed(() => list().status),
|
|
190
|
+
redirectTo: computed(() => list().redirectTo),
|
|
191
|
+
href: (change) => list().href(change),
|
|
192
|
+
navigate: (change, navOptions) => list().navigate(change, navOptions),
|
|
193
|
+
setPage: (page, navOptions) => list().setPage(page, navOptions),
|
|
194
|
+
setFilters: (filters, navOptions) => list().setFilters(filters, navOptions),
|
|
195
|
+
select: (selection, navOptions) => list().select(selection, navOptions),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Takes a one-time credential (a reset `token`, an OAuth `code`) out of the URL
|
|
200
|
+
* and keeps it in memory, through whichever routing bond the app wired. The
|
|
201
|
+
* parameter is removed with a replace navigation that keeps the path, the hash,
|
|
202
|
+
* the other query values and the location state; the value survives the
|
|
203
|
+
* component's lifetime, and Back to an entry restores the value consumed there.
|
|
204
|
+
* Call it in an injection context (a component field initializer or
|
|
205
|
+
* constructor).
|
|
206
|
+
*
|
|
207
|
+
* @param name - The query key to consume, for example `token` or `code`.
|
|
208
|
+
* @returns The consumed value (`''` before the parameter was ever present) and
|
|
209
|
+
* a setter (a form clears it after a successful submit).
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* @Component({
|
|
214
|
+
* selector: 'app-reset',
|
|
215
|
+
* template: `<input [placeholder]="i18n.t('auth.resetPassword.token')" />`,
|
|
216
|
+
* })
|
|
217
|
+
* export class ResetComponent {
|
|
218
|
+
* readonly i18n = useTranslation()
|
|
219
|
+
* readonly token = useConsumedSearchParam('token')
|
|
220
|
+
* submit(password: string): void {
|
|
221
|
+
* // token[0] holds the value after the address no longer contains it;
|
|
222
|
+
* // token[1]('') clears it after a successful submit.
|
|
223
|
+
* }
|
|
224
|
+
* }
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
export function useConsumedSearchParam(name) {
|
|
228
|
+
const router = useRouterInstance();
|
|
229
|
+
const param = createConsumedSearchParam(router, name);
|
|
230
|
+
// The tuple is the mounted result for this component's whole lifetime and its
|
|
231
|
+
// state member is updated in place, exactly like `usePromise`/`useAsyncState`
|
|
232
|
+
// (see use-async.ts): React re-renders a new value into the same tuple, this
|
|
233
|
+
// port writes the new value into the same tuple's member.
|
|
234
|
+
const tuple = [param.get(), (value) => param.set(value)];
|
|
235
|
+
const stopFollowing = param.subscribe((value) => {
|
|
236
|
+
tuple[0] = value;
|
|
237
|
+
});
|
|
238
|
+
inject(DestroyRef).onDestroy(stopFollowing);
|
|
239
|
+
// Like React's effect, the parameter is consumed once the component has
|
|
240
|
+
// rendered (and never during server rendering).
|
|
241
|
+
let disconnect = null;
|
|
242
|
+
afterNextRender(() => {
|
|
243
|
+
disconnect = param.connect();
|
|
244
|
+
});
|
|
245
|
+
inject(DestroyRef).onDestroy(() => {
|
|
246
|
+
disconnect?.();
|
|
247
|
+
disconnect = null;
|
|
248
|
+
});
|
|
249
|
+
return tuple;
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=use-router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-router.js","sourceRoot":"","sources":["../../src/hooks/use-router.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,eAAe,EACf,QAAQ,EACR,UAAU,EACV,MAAM,EACN,MAAM,EACN,QAAQ,EAER,MAAM,GACP,MAAM,eAAe,CAAA;AAYtB,OAAO,EACL,yBAAyB,EACzB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,uBAAuB,CAAA;AAE9B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB;IAC/B,OAAO,cAAc,CACnB,MAAM,EAC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AE,CAAC,SAAS,EAAE,UAAU,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC;KACxE,CAAA;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAAY;IACjD,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAA;IAClC,MAAM,KAAK,GAAG,yBAAyB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IACrD,8EAA8E;IAC9E,8EAA8E;IAC9E,6EAA6E;IAC7E,0DAA0D;IAC1D,MAAM,KAAK,GAAsC,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAA;IAC3F,MAAM,aAAa,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;QAC9C,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAA;IAClB,CAAC,CAAC,CAAA;IACF,MAAM,CAAC,UAAU,CAAC,CAAC,SAAS,CAAC,aAAa,CAAC,CAAA;IAC3C,wEAAwE;IACxE,gDAAgD;IAChD,IAAI,UAAU,GAAwB,IAAI,CAAA;IAC1C,eAAe,CAAC,GAAG,EAAE;QACnB,UAAU,GAAG,KAAK,CAAC,OAAO,EAAE,CAAA;IAC9B,CAAC,CAAC,CAAA;IACF,MAAM,CAAC,UAAU,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE;QAChC,UAAU,EAAE,EAAE,CAAA;QACd,UAAU,GAAG,IAAI,CAAA;IACnB,CAAC,CAAC,CAAA;IACF,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for storage, mirroring `useStorageProvider`,
|
|
3
|
+
* `useStorageValue`, `useStoragePreference` and `useStorage` from
|
|
4
|
+
* `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { type Signal } from '@angular/core';
|
|
9
|
+
import { type StorageProvider } from '@molecule/app-storage';
|
|
10
|
+
/**
|
|
11
|
+
* The storage provider registered by `provideMolecule({ storage })` or
|
|
12
|
+
* `provideStorage`. Call it in an injection context.
|
|
13
|
+
*
|
|
14
|
+
* @returns The storage provider.
|
|
15
|
+
* @throws {Error} When no storage provider is registered.
|
|
16
|
+
*/
|
|
17
|
+
export declare function useStorageProvider(): StorageProvider;
|
|
18
|
+
/**
|
|
19
|
+
* Options for {@link useStorageValue}.
|
|
20
|
+
*
|
|
21
|
+
* @template T - The value's shape.
|
|
22
|
+
*/
|
|
23
|
+
export interface UseStorageValueOptions<T> {
|
|
24
|
+
/** Default value if the key doesn't exist. */
|
|
25
|
+
defaultValue?: T;
|
|
26
|
+
/** Whether to sync across tabs/windows (if supported by storage provider). */
|
|
27
|
+
sync?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Result of {@link useStorageValue}: every state field as a signal and the
|
|
31
|
+
* mutators as plain functions.
|
|
32
|
+
*
|
|
33
|
+
* @template T - The value's shape.
|
|
34
|
+
*/
|
|
35
|
+
export interface UseStorageValueResult<T> {
|
|
36
|
+
/** The stored value, the default while absent, `undefined` while unknown. */
|
|
37
|
+
readonly value: Signal<T | undefined>;
|
|
38
|
+
/** Writes the value through the provider and publishes it. */
|
|
39
|
+
setValue: (value: T) => Promise<void>;
|
|
40
|
+
/** Removes the key; the value falls back to the default. */
|
|
41
|
+
removeValue: () => Promise<void>;
|
|
42
|
+
/** Whether the first read for the key is still in flight. */
|
|
43
|
+
readonly loading: Signal<boolean>;
|
|
44
|
+
/** The read or write failure, cleared by the next successful operation. */
|
|
45
|
+
readonly error: Signal<Error | null>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Manages one storage value. Call it in an injection context (a component
|
|
49
|
+
* field initializer or constructor).
|
|
50
|
+
*
|
|
51
|
+
* A write bumps a version counter, so a read that started before it never
|
|
52
|
+
* replaces the newer value; a read that started before a write still ends
|
|
53
|
+
* loading. `sync` subscribes through the PROVIDER's own change subscription, so
|
|
54
|
+
* a non-browser backend propagates the same way instead of being limited to
|
|
55
|
+
* the browser `storage` event.
|
|
56
|
+
*
|
|
57
|
+
* @param key - Storage key.
|
|
58
|
+
* @param options - Hook options (`defaultValue`, `sync`).
|
|
59
|
+
* @returns The value, its mutators, and the loading/error state as signals.
|
|
60
|
+
*/
|
|
61
|
+
export declare function useStorageValue<T>(key: string, options?: UseStorageValueOptions<T>): UseStorageValueResult<T>;
|
|
62
|
+
/**
|
|
63
|
+
* Options for {@link useStoragePreference}.
|
|
64
|
+
*
|
|
65
|
+
* @template T - The value's shape.
|
|
66
|
+
*/
|
|
67
|
+
export interface UseStoragePreferenceOptions<T> {
|
|
68
|
+
/**
|
|
69
|
+
* Value reported until the provider supplies one. It is NEVER written to
|
|
70
|
+
* storage: a default is a display fallback, not a decision the user made.
|
|
71
|
+
*/
|
|
72
|
+
defaultValue?: T;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Result of {@link useStoragePreference}.
|
|
76
|
+
*
|
|
77
|
+
* @template T - The value's shape.
|
|
78
|
+
*/
|
|
79
|
+
export interface UseStoragePreferenceResult<T> {
|
|
80
|
+
/** The persisted value, or `defaultValue` while none is known. */
|
|
81
|
+
readonly value: Signal<T | undefined>;
|
|
82
|
+
/**
|
|
83
|
+
* Writes the value through the preference store: subscribers update
|
|
84
|
+
* immediately and persistence is best-effort.
|
|
85
|
+
*/
|
|
86
|
+
setValue: (next: T | ((previous: T | undefined) => T)) => void;
|
|
87
|
+
/** Removes the key through the preference store. */
|
|
88
|
+
removeValue: () => void;
|
|
89
|
+
/**
|
|
90
|
+
* `true` once the store has a definite answer — the synchronous snapshot on
|
|
91
|
+
* a synchronous provider, or the hydration result on an asynchronous one.
|
|
92
|
+
*/
|
|
93
|
+
readonly ready: Signal<boolean>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Subscribes to ONE persisted preference through `getPreferenceStore()`. Call
|
|
97
|
+
* it in an injection context (a component field initializer or constructor).
|
|
98
|
+
*
|
|
99
|
+
* This is the component-side half of `getPreferenceStore()`: a value read at
|
|
100
|
+
* injection time is available immediately when the bonded provider can answer
|
|
101
|
+
* synchronously, and an asynchronous provider's hydration CONVERGES onto the
|
|
102
|
+
* same component without a remount. Ordering is the store's: a read that
|
|
103
|
+
* started before a newer write, remove or external change is discarded, so a
|
|
104
|
+
* slow hydration never overwrites an edit the user already made.
|
|
105
|
+
*
|
|
106
|
+
* Pass `null` for `key` while the real key is unknown (no project selected
|
|
107
|
+
* yet): the hook then behaves as local state around `defaultValue` and
|
|
108
|
+
* persists nothing.
|
|
109
|
+
*
|
|
110
|
+
* @param key - The storage key, or `null` when there is nothing to persist.
|
|
111
|
+
* @param options - `defaultValue`, reported until the provider supplies one.
|
|
112
|
+
* @returns The current value, its mutators, and whether it is definite yet.
|
|
113
|
+
*/
|
|
114
|
+
export declare function useStoragePreference<T = unknown>(key: string | null | undefined, options?: UseStoragePreferenceOptions<T>): UseStoragePreferenceResult<T>;
|
|
115
|
+
/**
|
|
116
|
+
* The registered provider's operations as stable functions, no reactive state.
|
|
117
|
+
* Call it in an injection context.
|
|
118
|
+
*
|
|
119
|
+
* @returns The get/set/remove/clear/keys functions.
|
|
120
|
+
*/
|
|
121
|
+
export declare function useStorage(): {
|
|
122
|
+
get: <T>(key: string) => Promise<T | null>;
|
|
123
|
+
set: <T>(key: string, value: T) => Promise<void>;
|
|
124
|
+
remove: (key: string) => Promise<void>;
|
|
125
|
+
clear: () => Promise<void>;
|
|
126
|
+
keys: () => Promise<string[]>;
|
|
127
|
+
};
|
|
128
|
+
//# sourceMappingURL=use-storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-storage.d.ts","sourceRoot":"","sources":["../../src/hooks/use-storage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AM,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA;IACrC;;;OAGG;IACH,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC,GAAG,SAAS,KAAK,CAAC,CAAC,KAAK,IAAI,CAAA;IAC9D,oDAAoD;IACpD,WAAW,EAAE,MAAM,IAAI,CAAA;IACvB;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;CAChC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,GAAG,OAAO,EAC9C,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC9B,OAAO,CAAC,EAAE,2BAA2B,CAAC,CAAC,CAAC,GACvC,0BAA0B,CAAC,CAAC,CAAC,CA4D/B;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,IAAI;IAC5B,GAAG,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;IAC1C,GAAG,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAChD,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACtC,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1B,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;CAC9B,CAUA"}
|
|
@@ -0,0 +1,217 @@
|
|
|
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, inject, signal } from '@angular/core';
|
|
9
|
+
import { getPreferenceStore } from '@molecule/app-storage';
|
|
10
|
+
import { injectProvider } from '../inject.js';
|
|
11
|
+
import { STORAGE_PROVIDER } from '../tokens.js';
|
|
12
|
+
/**
|
|
13
|
+
* The storage provider registered by `provideMolecule({ storage })` or
|
|
14
|
+
* `provideStorage`. Call it in an injection context.
|
|
15
|
+
*
|
|
16
|
+
* @returns The storage provider.
|
|
17
|
+
* @throws {Error} When no storage provider is registered.
|
|
18
|
+
*/
|
|
19
|
+
export function useStorageProvider() {
|
|
20
|
+
return injectProvider(STORAGE_PROVIDER, 'useStorageOutsideProvider', 'useStorageProvider must be used within a StorageProvider');
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Manages one storage value. Call it in an injection context (a component
|
|
24
|
+
* field initializer or constructor).
|
|
25
|
+
*
|
|
26
|
+
* A write bumps a version counter, so a read that started before it never
|
|
27
|
+
* replaces the newer value; a read that started before a write still ends
|
|
28
|
+
* loading. `sync` subscribes through the PROVIDER's own change subscription, so
|
|
29
|
+
* a non-browser backend propagates the same way instead of being limited to
|
|
30
|
+
* the browser `storage` event.
|
|
31
|
+
*
|
|
32
|
+
* @param key - Storage key.
|
|
33
|
+
* @param options - Hook options (`defaultValue`, `sync`).
|
|
34
|
+
* @returns The value, its mutators, and the loading/error state as signals.
|
|
35
|
+
*/
|
|
36
|
+
export function useStorageValue(key, options) {
|
|
37
|
+
const storage = useStorageProvider();
|
|
38
|
+
const { defaultValue, sync = false } = options ?? {};
|
|
39
|
+
const value = signal(defaultValue, /* @ts-ignore */
|
|
40
|
+
...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
|
|
41
|
+
const loading = signal(true, /* @ts-ignore */
|
|
42
|
+
...(ngDevMode ? [{ debugName: "loading" }] : /* istanbul ignore next */ []));
|
|
43
|
+
const error = signal(null, /* @ts-ignore */
|
|
44
|
+
...(ngDevMode ? [{ debugName: "error" }] : /* istanbul ignore next */ []));
|
|
45
|
+
/**
|
|
46
|
+
* Incremented by every local write. A read that started before it must not
|
|
47
|
+
* replace the newer value — the user editing before the first read lands is
|
|
48
|
+
* the common case on a slow (native, encrypted) backend.
|
|
49
|
+
*/
|
|
50
|
+
let writeVersion = 0;
|
|
51
|
+
const destroyRef = inject(DestroyRef);
|
|
52
|
+
let mounted = true;
|
|
53
|
+
destroyRef.onDestroy(() => {
|
|
54
|
+
mounted = false;
|
|
55
|
+
});
|
|
56
|
+
// Load the key's value.
|
|
57
|
+
const version = writeVersion;
|
|
58
|
+
const loadValue = async () => {
|
|
59
|
+
try {
|
|
60
|
+
const stored = await storage.get(key);
|
|
61
|
+
if (!mounted)
|
|
62
|
+
return;
|
|
63
|
+
// A write made after this read started holds the newer value; the read still ends loading.
|
|
64
|
+
if (writeVersion === version)
|
|
65
|
+
value.set(stored ?? defaultValue);
|
|
66
|
+
loading.set(false);
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
if (!mounted)
|
|
70
|
+
return;
|
|
71
|
+
if (writeVersion === version) {
|
|
72
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
73
|
+
}
|
|
74
|
+
loading.set(false);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
void loadValue();
|
|
78
|
+
// Sync across tabs if supported — through the PROVIDER's own subscription, so
|
|
79
|
+
// a non-browser backend (native, encrypted, memory) propagates the same way
|
|
80
|
+
// instead of being limited to the browser `storage` event.
|
|
81
|
+
if (sync && storage.subscribe) {
|
|
82
|
+
const stop = storage.subscribe(key, (next) => {
|
|
83
|
+
writeVersion += 1;
|
|
84
|
+
value.set(next === null ? defaultValue : next);
|
|
85
|
+
});
|
|
86
|
+
destroyRef.onDestroy(stop);
|
|
87
|
+
}
|
|
88
|
+
const setValue = async (newValue) => {
|
|
89
|
+
writeVersion += 1;
|
|
90
|
+
try {
|
|
91
|
+
await storage.set(key, newValue);
|
|
92
|
+
value.set(newValue);
|
|
93
|
+
error.set(null);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const removeValue = async () => {
|
|
101
|
+
writeVersion += 1;
|
|
102
|
+
try {
|
|
103
|
+
await storage.remove(key);
|
|
104
|
+
value.set(defaultValue);
|
|
105
|
+
error.set(null);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
value: value.asReadonly(),
|
|
114
|
+
setValue,
|
|
115
|
+
removeValue,
|
|
116
|
+
loading: loading.asReadonly(),
|
|
117
|
+
error: error.asReadonly(),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Subscribes to ONE persisted preference through `getPreferenceStore()`. Call
|
|
122
|
+
* it in an injection context (a component field initializer or constructor).
|
|
123
|
+
*
|
|
124
|
+
* This is the component-side half of `getPreferenceStore()`: a value read at
|
|
125
|
+
* injection time is available immediately when the bonded provider can answer
|
|
126
|
+
* synchronously, and an asynchronous provider's hydration CONVERGES onto the
|
|
127
|
+
* same component without a remount. Ordering is the store's: a read that
|
|
128
|
+
* started before a newer write, remove or external change is discarded, so a
|
|
129
|
+
* slow hydration never overwrites an edit the user already made.
|
|
130
|
+
*
|
|
131
|
+
* Pass `null` for `key` while the real key is unknown (no project selected
|
|
132
|
+
* yet): the hook then behaves as local state around `defaultValue` and
|
|
133
|
+
* persists nothing.
|
|
134
|
+
*
|
|
135
|
+
* @param key - The storage key, or `null` when there is nothing to persist.
|
|
136
|
+
* @param options - `defaultValue`, reported until the provider supplies one.
|
|
137
|
+
* @returns The current value, its mutators, and whether it is definite yet.
|
|
138
|
+
*/
|
|
139
|
+
export function useStoragePreference(key, options) {
|
|
140
|
+
const store = getPreferenceStore();
|
|
141
|
+
const defaultValue = options?.defaultValue;
|
|
142
|
+
const read = () => {
|
|
143
|
+
if (!key)
|
|
144
|
+
return defaultValue;
|
|
145
|
+
const stored = store.get(key);
|
|
146
|
+
return stored === null ? defaultValue : stored;
|
|
147
|
+
};
|
|
148
|
+
const value = signal(read(), /* @ts-ignore */
|
|
149
|
+
...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
|
|
150
|
+
const ready = signal(!key, /* @ts-ignore */
|
|
151
|
+
...(ngDevMode ? [{ debugName: "ready" }] : /* istanbul ignore next */ []));
|
|
152
|
+
const destroyRef = inject(DestroyRef);
|
|
153
|
+
if (!key) {
|
|
154
|
+
ready.set(true);
|
|
155
|
+
value.set(defaultValue);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
let cancelled = false;
|
|
159
|
+
const sync = () => {
|
|
160
|
+
if (!cancelled)
|
|
161
|
+
value.set(read());
|
|
162
|
+
};
|
|
163
|
+
// Seed from whatever the store already knows, then let hydration converge
|
|
164
|
+
// it. With a synchronous provider this is a no-op change.
|
|
165
|
+
sync();
|
|
166
|
+
ready.set(false);
|
|
167
|
+
const stop = store.subscribe(key, sync);
|
|
168
|
+
destroyRef.onDestroy(() => {
|
|
169
|
+
cancelled = true;
|
|
170
|
+
stop();
|
|
171
|
+
});
|
|
172
|
+
store
|
|
173
|
+
.hydrate([key])
|
|
174
|
+
.then(() => {
|
|
175
|
+
if (cancelled)
|
|
176
|
+
return;
|
|
177
|
+
sync();
|
|
178
|
+
ready.set(true);
|
|
179
|
+
})
|
|
180
|
+
.catch((_error) => {
|
|
181
|
+
// Hydration is best-effort by the store's contract (persistence and
|
|
182
|
+
// read failures are swallowed there, and `hydrate` resolves once every
|
|
183
|
+
// key has been ATTEMPTED); the value keeps the last known snapshot, and
|
|
184
|
+
// `ready` still turns true so the UI is not stuck on a spinner.
|
|
185
|
+
if (!cancelled)
|
|
186
|
+
ready.set(true);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
const setValue = (next) => {
|
|
190
|
+
const resolved = typeof next === 'function' ? next(value()) : next;
|
|
191
|
+
value.set(resolved);
|
|
192
|
+
if (key)
|
|
193
|
+
store.set(key, resolved);
|
|
194
|
+
};
|
|
195
|
+
const removeValue = () => {
|
|
196
|
+
value.set(defaultValue);
|
|
197
|
+
if (key)
|
|
198
|
+
store.remove(key);
|
|
199
|
+
};
|
|
200
|
+
return { value: value.asReadonly(), setValue, removeValue, ready: ready.asReadonly() };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The registered provider's operations as stable functions, no reactive state.
|
|
204
|
+
* Call it in an injection context.
|
|
205
|
+
*
|
|
206
|
+
* @returns The get/set/remove/clear/keys functions.
|
|
207
|
+
*/
|
|
208
|
+
export function useStorage() {
|
|
209
|
+
const storage = useStorageProvider();
|
|
210
|
+
const get = (key) => storage.get(key);
|
|
211
|
+
const set = (key, value) => storage.set(key, value);
|
|
212
|
+
const remove = (key) => storage.remove(key);
|
|
213
|
+
const clear = () => storage.clear();
|
|
214
|
+
const keys = () => storage.keys();
|
|
215
|
+
return { get, set, remove, clear, keys };
|
|
216
|
+
}
|
|
217
|
+
//# 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,CAAA;AAEvE,OAAO,EAAE,kBAAkB,EAAwB,MAAM,uBAAuB,CAAA;AAEhF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,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;AA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