1
1
import {
2
2
DestroyRef ,
3
3
ENVIRONMENT_INITIALIZER ,
4
- Injector ,
5
4
PLATFORM_ID ,
6
5
computed ,
7
6
effect ,
8
7
inject ,
9
8
makeEnvironmentProviders ,
10
- runInInjectionContext ,
11
9
} from '@angular/core'
12
10
import { QueryClient , onlineManager } from '@tanstack/query-core'
13
11
import { isPlatformBrowser } from '@angular/common'
@@ -99,7 +97,7 @@ export function provideTanStackQuery(
99
97
return makeEnvironmentProviders ( [
100
98
provideQueryClient ( queryClient ) ,
101
99
{
102
- // Do not use provideEnvironmentInitializer to support Angular < v19
100
+ // Do not use provideEnvironmentInitializer while Angular < v19 is supported
103
101
provide : ENVIRONMENT_INITIALIZER ,
104
102
multi : true ,
105
103
useValue : ( ) => {
@@ -250,14 +248,17 @@ export function withDevtools(
250
248
} else {
251
249
providers = [
252
250
{
251
+ // Do not use provideEnvironmentInitializer while Angular < v19 is supported
253
252
provide : ENVIRONMENT_INITIALIZER ,
254
253
multi : true ,
255
254
useFactory : ( ) => {
256
255
if ( ! isPlatformBrowser ( inject ( PLATFORM_ID ) ) ) return noop
257
- const injector = inject ( Injector )
258
- const options = computed ( ( ) =>
259
- runInInjectionContext ( injector , ( ) => optionsFn ?.( ) ?? { } ) ,
260
- )
256
+ const injectedClient = inject ( QueryClient , {
257
+ optional : true ,
258
+ } )
259
+ const destroyRef = inject ( DestroyRef )
260
+
261
+ const options = computed ( ( ) => optionsFn ?.( ) ?? { } )
261
262
262
263
let devtools : TanstackQueryDevtools | null = null
263
264
let el : HTMLElement | null = null
@@ -269,10 +270,7 @@ export function withDevtools(
269
270
: isDevMode ( )
270
271
} )
271
272
272
- const destroyRef = inject ( DestroyRef )
273
-
274
273
const getResolvedQueryClient = ( ) => {
275
- const injectedClient = injector . get ( QueryClient , null )
276
274
const client = options ( ) . client ?? injectedClient
277
275
if ( ! client ) {
278
276
throw new Error ( 'No QueryClient found' )
@@ -314,22 +312,20 @@ export function withDevtools(
314
312
el = document . body . appendChild ( document . createElement ( 'div' ) )
315
313
el . classList . add ( 'tsqd-parent-container' )
316
314
317
- import ( '@tanstack/query-devtools' ) . then ( ( queryDevtools ) =>
318
- runInInjectionContext ( injector , ( ) => {
319
- devtools = new queryDevtools . TanstackQueryDevtools ( {
320
- ...options ( ) ,
321
- client : getResolvedQueryClient ( ) ,
322
- queryFlavor : 'Angular Query' ,
323
- version : '5' ,
324
- onlineManager,
325
- } )
315
+ import ( '@tanstack/query-devtools' ) . then ( ( queryDevtools ) => {
316
+ devtools = new queryDevtools . TanstackQueryDevtools ( {
317
+ ...options ( ) ,
318
+ client : getResolvedQueryClient ( ) ,
319
+ queryFlavor : 'Angular Query' ,
320
+ version : '5' ,
321
+ onlineManager,
322
+ } )
326
323
327
- el && devtools . mount ( el )
324
+ el && devtools . mount ( el )
328
325
329
- // Unmount the devtools on application destroy
330
- destroyRef . onDestroy ( destroyDevtools )
331
- } ) ,
332
- )
326
+ // Unmount the devtools on application destroy
327
+ destroyRef . onDestroy ( destroyDevtools )
328
+ } )
333
329
} )
334
330
} ,
335
331
} ,
0 commit comments