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