From 6157a43047327e5c075133e29dcf8dd72cb25965 Mon Sep 17 00:00:00 2001 From: Arnoud de Vries <6420061+arnoud-dv@users.noreply.github.com> Date: Mon, 5 May 2025 17:38:13 +0200 Subject: [PATCH] refactor(angular-query): use factory for IS_RESTORING default value --- .../src/inject-is-restoring.ts | 24 ++++++++++--------- .../src/with-persist-query-client.ts | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/angular-query-experimental/src/inject-is-restoring.ts b/packages/angular-query-experimental/src/inject-is-restoring.ts index c57a15fbb71..92e50d4342f 100644 --- a/packages/angular-query-experimental/src/inject-is-restoring.ts +++ b/packages/angular-query-experimental/src/inject-is-restoring.ts @@ -2,12 +2,20 @@ import { InjectionToken, Injector, assertInInjectionContext, - computed, inject, + signal, } from '@angular/core' import type { Provider, Signal } from '@angular/core' -const IS_RESTORING = new InjectionToken>('') +const IS_RESTORING = new InjectionToken( + typeof ngDevMode === 'undefined' || ngDevMode + ? 'TANSTACK_QUERY_IS_RESTORING' + : '', + { + // Default value when not provided + factory: () => signal(false).asReadonly(), + }, +) /** * The `Injector` in which to create the isRestoring signal. @@ -19,21 +27,15 @@ interface InjectIsRestoringOptions { } /** - * 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. + * 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. * @param options - Options for injectIsRestoring. * @returns signal with boolean that indicates whether a restore is in progress. * @public */ -export function injectIsRestoring( - options?: InjectIsRestoringOptions, -): Signal { +export function injectIsRestoring(options?: InjectIsRestoringOptions) { !options?.injector && assertInInjectionContext(injectIsRestoring) const injector = options?.injector ?? inject(Injector) - return injector.get( - IS_RESTORING, - computed(() => false), - { optional: true }, - ) + return injector.get(IS_RESTORING) } /** diff --git a/packages/angular-query-persist-client/src/with-persist-query-client.ts b/packages/angular-query-persist-client/src/with-persist-query-client.ts index 2896cbc7b27..ceeeed01cd4 100644 --- a/packages/angular-query-persist-client/src/with-persist-query-client.ts +++ b/packages/angular-query-persist-client/src/with-persist-query-client.ts @@ -55,10 +55,11 @@ type PersistQueryClientOptions = { export function withPersistQueryClient( persistQueryClientOptions: PersistQueryClientOptions, ): PersistQueryClientFeature { - const isRestoring = signal(false) + const isRestoring = signal(true) const providers = [ provideIsRestoring(isRestoring.asReadonly()), { + // Do not use provideEnvironmentInitializer while Angular < v19 is supported provide: ENVIRONMENT_INITIALIZER, multi: true, useValue: () => { @@ -66,7 +67,6 @@ export function withPersistQueryClient( const destroyRef = inject(DestroyRef) const queryClient = inject(QueryClient) - isRestoring.set(true) const { onSuccess, onError, persistOptions } = persistQueryClientOptions const options = { queryClient, ...persistOptions } persistQueryClientRestore(options)