1
1
import {
2
2
DestroyRef ,
3
- ENVIRONMENT_INITIALIZER ,
4
3
Injector ,
5
4
PLATFORM_ID ,
6
5
computed ,
7
6
effect ,
8
7
inject ,
9
8
makeEnvironmentProviders ,
9
+ provideEnvironmentInitializer ,
10
10
runInInjectionContext ,
11
11
} from '@angular/core'
12
12
import { QueryClient , onlineManager } from '@tanstack/query-core'
@@ -21,6 +21,8 @@ import type {
21
21
TanstackQueryDevtools ,
22
22
} from '@tanstack/query-devtools'
23
23
24
+ type Providers = Provider | EnvironmentProviders
25
+
24
26
/**
25
27
* Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
26
28
* {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
@@ -98,15 +100,11 @@ export function provideTanStackQuery(
98
100
) : EnvironmentProviders {
99
101
return makeEnvironmentProviders ( [
100
102
provideQueryClient ( queryClient ) ,
101
- {
102
- provide : ENVIRONMENT_INITIALIZER ,
103
- multi : true ,
104
- useValue : ( ) => {
105
- queryClient . mount ( )
106
- // Unmount the query client on application destroy
107
- inject ( DestroyRef ) . onDestroy ( ( ) => queryClient . unmount ( ) )
108
- } ,
109
- } ,
103
+ provideEnvironmentInitializer ( ( ) => {
104
+ queryClient . mount ( )
105
+ // Unmount the query client on application destroy
106
+ inject ( DestroyRef ) . onDestroy ( ( ) => queryClient . unmount ( ) )
107
+ } ) ,
110
108
features . map ( ( feature ) => feature . ɵproviders ) ,
111
109
] )
112
110
}
@@ -132,7 +130,7 @@ export function provideAngularQuery(
132
130
*/
133
131
export interface QueryFeature < TFeatureKind extends QueryFeatureKind > {
134
132
ɵkind : TFeatureKind
135
- ɵproviders : Array < Provider >
133
+ ɵproviders : Providers
136
134
}
137
135
138
136
/**
@@ -143,7 +141,7 @@ export interface QueryFeature<TFeatureKind extends QueryFeatureKind> {
143
141
*/
144
142
export function queryFeature < TFeatureKind extends QueryFeatureKind > (
145
143
kind : TFeatureKind ,
146
- providers : Array < Provider > ,
144
+ providers : Providers ,
147
145
) : QueryFeature < TFeatureKind > {
148
146
return { ɵkind : kind , ɵproviders : providers }
149
147
}
@@ -250,95 +248,89 @@ export interface DevtoolsOptions {
250
248
export function withDevtools (
251
249
optionsFn ?: ( ) => DevtoolsOptions ,
252
250
) : DeveloperToolsFeature {
253
- let providers : Array < Provider > = [ ]
251
+ let providers : Providers = [ ]
254
252
if ( ! isDevMode ( ) && ! optionsFn ) {
255
253
providers = [ ]
256
254
} else {
257
255
providers = [
258
- {
259
- provide : ENVIRONMENT_INITIALIZER ,
260
- multi : true ,
261
- useFactory : ( ) => {
262
- if ( ! isPlatformBrowser ( inject ( PLATFORM_ID ) ) ) return noop
263
- const injector = inject ( Injector )
264
- const options = computed ( ( ) =>
265
- runInInjectionContext ( injector , ( ) => optionsFn ?.( ) ?? { } ) ,
266
- )
256
+ provideEnvironmentInitializer ( ( ) => {
257
+ if ( ! isPlatformBrowser ( inject ( PLATFORM_ID ) ) ) return noop
258
+ const injector = inject ( Injector )
259
+ const options = computed ( ( ) =>
260
+ runInInjectionContext ( injector , ( ) => optionsFn ?.( ) ?? { } ) ,
261
+ )
267
262
268
- let devtools : TanstackQueryDevtools | null = null
269
- let el : HTMLElement | null = null
263
+ let devtools : TanstackQueryDevtools | null = null
264
+ let el : HTMLElement | null = null
270
265
271
- const shouldLoadToolsSignal = computed ( ( ) => {
272
- const { loadDevtools } = options ( )
273
- return typeof loadDevtools === 'boolean'
274
- ? loadDevtools
275
- : isDevMode ( )
276
- } )
266
+ const shouldLoadToolsSignal = computed ( ( ) => {
267
+ const { loadDevtools } = options ( )
268
+ return typeof loadDevtools === 'boolean' ? loadDevtools : isDevMode ( )
269
+ } )
277
270
278
- const destroyRef = inject ( DestroyRef )
271
+ const destroyRef = inject ( DestroyRef )
279
272
280
- const getResolvedQueryClient = ( ) => {
281
- const injectedClient = injector . get ( QueryClient , null )
282
- const client = options ( ) . client ?? injectedClient
283
- if ( ! client ) {
284
- throw new Error ( 'No QueryClient found' )
285
- }
286
- return client
273
+ const getResolvedQueryClient = ( ) => {
274
+ const injectedClient = injector . get ( QueryClient , null )
275
+ const client = options ( ) . client ?? injectedClient
276
+ if ( ! client ) {
277
+ throw new Error ( 'No QueryClient found' )
287
278
}
279
+ return client
280
+ }
288
281
289
- const destroyDevtools = ( ) => {
290
- devtools ?. unmount ( )
291
- el ?. remove ( )
292
- devtools = null
293
- }
282
+ const destroyDevtools = ( ) => {
283
+ devtools ?. unmount ( )
284
+ el ?. remove ( )
285
+ devtools = null
286
+ }
294
287
295
- return ( ) =>
296
- effect ( ( ) => {
297
- const shouldLoadTools = shouldLoadToolsSignal ( )
298
- const {
299
- client,
300
- position,
301
- errorTypes,
302
- buttonPosition,
303
- initialIsOpen,
304
- } = options ( )
288
+ return ( ) =>
289
+ effect ( ( ) => {
290
+ const shouldLoadTools = shouldLoadToolsSignal ( )
291
+ const {
292
+ client,
293
+ position,
294
+ errorTypes,
295
+ buttonPosition,
296
+ initialIsOpen,
297
+ } = options ( )
305
298
306
- if ( devtools && ! shouldLoadTools ) {
307
- destroyDevtools ( )
308
- return
309
- } else if ( devtools && shouldLoadTools ) {
310
- client && devtools . setClient ( client )
311
- position && devtools . setPosition ( position )
312
- errorTypes && devtools . setErrorTypes ( errorTypes )
313
- buttonPosition && devtools . setButtonPosition ( buttonPosition )
314
- initialIsOpen && devtools . setInitialIsOpen ( initialIsOpen )
315
- return
316
- } else if ( ! shouldLoadTools ) {
317
- return
318
- }
299
+ if ( devtools && ! shouldLoadTools ) {
300
+ destroyDevtools ( )
301
+ return
302
+ } else if ( devtools && shouldLoadTools ) {
303
+ client && devtools . setClient ( client )
304
+ position && devtools . setPosition ( position )
305
+ errorTypes && devtools . setErrorTypes ( errorTypes )
306
+ buttonPosition && devtools . setButtonPosition ( buttonPosition )
307
+ initialIsOpen && devtools . setInitialIsOpen ( initialIsOpen )
308
+ return
309
+ } else if ( ! shouldLoadTools ) {
310
+ return
311
+ }
319
312
320
- el = document . body . appendChild ( document . createElement ( 'div' ) )
321
- el . classList . add ( 'tsqd-parent-container' )
313
+ el = document . body . appendChild ( document . createElement ( 'div' ) )
314
+ el . classList . add ( 'tsqd-parent-container' )
322
315
323
- import ( '@tanstack/query-devtools' ) . then ( ( queryDevtools ) =>
324
- runInInjectionContext ( injector , ( ) => {
325
- devtools = new queryDevtools . TanstackQueryDevtools ( {
326
- ...options ( ) ,
327
- client : getResolvedQueryClient ( ) ,
328
- queryFlavor : 'Angular Query' ,
329
- version : '5' ,
330
- onlineManager,
331
- } )
316
+ import ( '@tanstack/query-devtools' ) . then ( ( queryDevtools ) =>
317
+ runInInjectionContext ( injector , ( ) => {
318
+ devtools = new queryDevtools . TanstackQueryDevtools ( {
319
+ ...options ( ) ,
320
+ client : getResolvedQueryClient ( ) ,
321
+ queryFlavor : 'Angular Query' ,
322
+ version : '5' ,
323
+ onlineManager,
324
+ } )
332
325
333
- el && devtools . mount ( el )
326
+ el && devtools . mount ( el )
334
327
335
- // Unmount the devtools on application destroy
336
- destroyRef . onDestroy ( destroyDevtools )
337
- } ) ,
338
- )
339
- } )
340
- } ,
341
- } ,
328
+ // Unmount the devtools on application destroy
329
+ destroyRef . onDestroy ( destroyDevtools )
330
+ } ) ,
331
+ )
332
+ } )
333
+ } ) ,
342
334
]
343
335
}
344
336
return queryFeature ( 'DeveloperTools' , providers )
0 commit comments