Skip to content

Commit d82fb69

Browse files
authored
refactor(angular-query): use factory for IS_RESTORING default value (#9107)
1 parent 2121836 commit d82fb69

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

packages/angular-query-experimental/src/inject-is-restoring.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import {
22
InjectionToken,
33
Injector,
44
assertInInjectionContext,
5-
computed,
65
inject,
6+
signal,
77
} from '@angular/core'
88
import type { Provider, Signal } from '@angular/core'
99

10-
const IS_RESTORING = new InjectionToken<Signal<boolean>>('')
10+
const 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+
)
1119

1220
/**
1321
* The `Injector` in which to create the isRestoring signal.
@@ -19,21 +27,15 @@ interface InjectIsRestoringOptions {
1927
}
2028

2129
/**
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.
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.
2331
* @param options - Options for injectIsRestoring.
2432
* @returns signal with boolean that indicates whether a restore is in progress.
2533
* @public
2634
*/
27-
export function injectIsRestoring(
28-
options?: InjectIsRestoringOptions,
29-
): Signal<boolean> {
35+
export function injectIsRestoring(options?: InjectIsRestoringOptions) {
3036
!options?.injector && assertInInjectionContext(injectIsRestoring)
3137
const injector = options?.injector ?? inject(Injector)
32-
return injector.get(
33-
IS_RESTORING,
34-
computed(() => false),
35-
{ optional: true },
36-
)
38+
return injector.get(IS_RESTORING)
3739
}
3840

3941
/**

packages/angular-query-persist-client/src/with-persist-query-client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ type PersistQueryClientOptions = {
5555
export function withPersistQueryClient(
5656
persistQueryClientOptions: PersistQueryClientOptions,
5757
): PersistQueryClientFeature {
58-
const isRestoring = signal(false)
58+
const isRestoring = signal(true)
5959
const providers = [
6060
provideIsRestoring(isRestoring.asReadonly()),
6161
{
62+
// Do not use provideEnvironmentInitializer while Angular < v19 is supported
6263
provide: ENVIRONMENT_INITIALIZER,
6364
multi: true,
6465
useValue: () => {
6566
if (!isPlatformBrowser(inject(PLATFORM_ID))) return
6667
const destroyRef = inject(DestroyRef)
6768
const queryClient = inject(QueryClient)
6869

69-
isRestoring.set(true)
7070
const { onSuccess, onError, persistOptions } = persistQueryClientOptions
7171
const options = { queryClient, ...persistOptions }
7272
persistQueryClientRestore(options)

0 commit comments

Comments
 (0)