Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/angular-query-experimental/src/inject-is-restoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
InjectionToken,
Injector,
assertInInjectionContext,
computed,
inject,
signal,
} from '@angular/core'
import type { Provider, Signal } from '@angular/core'

const IS_RESTORING = new InjectionToken<Signal<boolean>>('')
const IS_RESTORING = new InjectionToken(
typeof ngDevMode === 'undefined' || ngDevMode
? 'TANSTACK_QUERY_IS_RESTORING'
: '',

Check warning on line 13 in packages/angular-query-experimental/src/inject-is-restoring.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L13 was not covered by tests
{
// Default value when not provided
factory: () => signal(false).asReadonly(),
},
)

/**
* The `Injector` in which to create the isRestoring signal.
Expand All @@ -19,21 +27,15 @@
}

/**
* 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<boolean> {
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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ 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: () => {
if (!isPlatformBrowser(inject(PLATFORM_ID))) return
const destroyRef = inject(DestroyRef)
const queryClient = inject(QueryClient)

isRestoring.set(true)
const { onSuccess, onError, persistOptions } = persistQueryClientOptions
const options = { queryClient, ...persistOptions }
persistQueryClientRestore(options)
Expand Down