11import {
22 DestroyRef ,
33 ENVIRONMENT_INITIALIZER ,
4+ InjectionToken ,
45 Injector ,
56 PLATFORM_ID ,
7+ computed ,
68 inject ,
79 makeEnvironmentProviders ,
810} from '@angular/core'
911import { QueryClient } from '@tanstack/query-core'
1012import { isPlatformBrowser } from '@angular/common'
1113import { noop } from './util'
12- import type { EnvironmentProviders , Provider } from '@angular/core'
14+ import type { EnvironmentProviders , Provider , Signal } from '@angular/core'
1315import type {
1416 DevtoolsButtonPosition ,
1517 DevtoolsErrorType ,
@@ -18,6 +20,13 @@ import type {
1820
1921declare const ngDevMode : unknown
2022
23+ /**
24+ * @internal
25+ */
26+ const DEVTOOLS_OPTIONS_SIGNAL = new InjectionToken < Signal < DevtoolsOptions > > (
27+ 'devtools options signal' ,
28+ )
29+
2130/**
2231 * Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
2332 * {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
@@ -163,6 +172,36 @@ export type DeveloperToolsFeature =
163172export type PersistQueryClientFeature =
164173 QueryFeature < QueryFeatureKind . PersistQueryClient >
165174
175+ /**
176+ * Options for configuring withDevtools.
177+ * @public
178+ */
179+ export interface WithDevtoolsOptions {
180+ /**
181+ * An array of dependencies to be injected and passed to the `withDevtoolsFn` function.
182+ *
183+ * **Example**
184+ * ```ts
185+ * export const appConfig: ApplicationConfig = {
186+ * providers: [
187+ * provideTanStackQuery(
188+ * new QueryClient(),
189+ * withDevtools(
190+ * (devToolsOptionsManager: DevtoolsOptionsManager) => ({
191+ * loadDevtools: devToolsOptionsManager.loadDevtools(),
192+ * }),
193+ * {
194+ * deps: [DevtoolsOptionsManager],
195+ * },
196+ * ),
197+ * ),
198+ * ],
199+ * }
200+ * ```
201+ */
202+ deps ?: Array < any >
203+ }
204+
166205/**
167206 * Options for configuring the TanStack Query devtools.
168207 * @public
@@ -242,39 +281,47 @@ export interface DevtoolsOptions {
242281 *
243282 * If you need more control over where devtools are rendered, consider `injectDevtoolsPanel`. This allows rendering devtools inside your own devtools for example.
244283 * @param withDevtoolsFn - A function that returns `DevtoolsOptions`.
284+ * @param options - Additional options for configuring `withDevtools`.
245285 * @returns A set of providers for use with `provideTanStackQuery`.
246286 * @public
247287 * @see {@link provideTanStackQuery }
248288 * @see {@link DevtoolsOptions }
249289 */
250290export function withDevtools (
251- withDevtoolsFn ?: ( ) => DevtoolsOptions ,
291+ withDevtoolsFn ?: ( ...deps : Array < any > ) => DevtoolsOptions ,
292+ options : WithDevtoolsOptions = { } ,
252293) : DeveloperToolsFeature {
294+ let providers : Array < Provider > = [ ]
253295 if ( withDevtoolsFn === undefined && typeof ngDevMode === 'undefined' ) {
254- return queryFeature ( QueryFeatureKind . DeveloperTools , [ ] )
296+ return queryFeature ( QueryFeatureKind . DeveloperTools , providers )
255297 } else {
256- return createDevtoolsFeature ( withDevtoolsFn )
257- }
258- }
259-
260- function createDevtoolsFeature ( withDevtoolsFn ?: ( ) => DevtoolsOptions ) {
261- const providers = [
262- {
263- provide : ENVIRONMENT_INITIALIZER ,
264- multi : true ,
265- useFactory : ( ) => {
266- if ( ! isPlatformBrowser ( inject ( PLATFORM_ID ) ) ) return noop
267- let destroyed = false
268- const injector = inject ( Injector )
269- inject ( DestroyRef ) . onDestroy ( ( ) => ( destroyed = true ) )
298+ providers = [
299+ {
300+ provide : DEVTOOLS_OPTIONS_SIGNAL ,
301+ useFactory : ( ...deps : Array < any > ) =>
302+ computed ( ( ) => withDevtoolsFn ?.( ...deps ) ?? { } ) ,
303+ deps : options . deps || [ ] ,
304+ } ,
305+ {
306+ // Do not use provideEnvironmentInitializer while Angular < v19 is supported
307+ provide : ENVIRONMENT_INITIALIZER ,
308+ multi : true ,
309+ useFactory : ( devtoolsOptionsSignal : Signal < DevtoolsOptions > ) => {
310+ if ( ! isPlatformBrowser ( inject ( PLATFORM_ID ) ) ) return noop
311+ let destroyed = false
312+ const injector = inject ( Injector )
313+ inject ( DestroyRef ) . onDestroy ( ( ) => ( destroyed = true ) )
270314
271- return ( ) =>
272- import ( './devtools-setup' ) . then ( ( module ) => {
273- ! destroyed && module . setupDevtools ( injector , withDevtoolsFn )
274- } )
315+ return ( ) =>
316+ import ( './devtools-setup' ) . then ( ( module ) => {
317+ ! destroyed &&
318+ module . setupDevtools ( injector , devtoolsOptionsSignal )
319+ } )
320+ } ,
321+ deps : [ DEVTOOLS_OPTIONS_SIGNAL ] ,
275322 } ,
276- } ,
277- ]
323+ ]
324+ }
278325 return queryFeature ( QueryFeatureKind . DeveloperTools , providers )
279326}
280327
0 commit comments