Open History to see the last commit
1import {
2 InjectionToken,
3 Injector,
4 assertInInjectionContext,
5 inject,
6 signal,
7} from '@angular/core'
8import type { Provider, Signal } from '@angular/core'
9β
10const IS_RESTORING = new InjectionToken(
11 typeof ngDevMode === 'undefined' || ngDevMode
12 ? 'TANSTACK_QUERY_IS_RESTORING'
13 : '',
14 {
15 // Default value when not provided
16 factory: () => signal(false).asReadonly(),
17 },
18)
19β
20/**
21 * The `Injector` in which to create the isRestoring signal.
22 *
23 * If this is not provided, the current injection context will be used instead (via `inject`).
24 */
25interface InjectIsRestoringOptions {
26 injector?: Injector
27}
28β
29/**
30 * Injects a signal that tracks whether a restore is currently in progress. {@link injectQuery} and friends also check this internally to avoid race conditions between the restore and initializing queries.
31 * @param options - Options for injectIsRestoring.
32 * @returns signal with boolean that indicates whether a restore is in progress.
33 * @public
34 */
35export function injectIsRestoring(options?: InjectIsRestoringOptions) {
36 !options?.injector && assertInInjectionContext(injectIsRestoring)
37 const injector = options?.injector ?? inject(Injector)
38 return injector.get(IS_RESTORING)
39}
40β
41/**
42 * Used by TanStack Query Angular persist client plugin to provide the signal that tracks the restore state
43 * @param isRestoring - a readonly signal that returns a boolean
44 * @returns Provider for the `isRestoring` signal
45 * @public
46 */
47export function provideIsRestoring(isRestoring: Signal<boolean>): Provider {
48 return {
49 provide: IS_RESTORING,
50 useValue: isRestoring,
51 }
52}