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β
10/**
11 * Internal token used to track isRestoring state, accessible in public API through `injectIsRestoring` and set via `provideIsRestoring`
12 */
13const IS_RESTORING = new InjectionToken('', {
14 // Default value when not provided
15 factory: () => signal(false).asReadonly(),
16})
17β
18interface InjectIsRestoringOptions {
19 /**
20 * The `Injector` to use to get the isRestoring signal.
21 *
22 * If this is not provided, the current injection context will be used instead (via `inject`).
23 */
24 injector?: Injector
25}
26β
27/**
28 * 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.
29 * @param options - Options for injectIsRestoring.
30 * @returns readonly signal with boolean that indicates whether a restore is in progress.
31 */
32export function injectIsRestoring(options?: InjectIsRestoringOptions) {
33 !options?.injector && assertInInjectionContext(injectIsRestoring)
34 const injector = options?.injector ?? inject(Injector)
35 return injector.get(IS_RESTORING)
36}
37β
38/**
39 * Used by TanStack Query Angular persist client plugin to provide the signal that tracks the restore state
40 * @param isRestoring - a readonly signal that returns a boolean
41 * @returns Provider for the `isRestoring` signal
42 */
43export function provideIsRestoring(isRestoring: Signal<boolean>): Provider {
44 return {
45 provide: IS_RESTORING,
46 useValue: isRestoring,
47 }
48}