Skip to content

Commit 8ee4ba7

Browse files
committed
Enable DI via deps options
1 parent d276a83 commit 8ee4ba7

File tree

3 files changed

+86
-36
lines changed

3 files changed

+86
-36
lines changed

docs/framework/angular/devtools.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,23 @@ export const appConfig: ApplicationConfig = {
8181
provideHttpClient(),
8282
provideTanStackQuery(
8383
new QueryClient(),
84-
withDevtools(() => ({
85-
initialIsOpen: true,
86-
loadDevtools: inject(DevtoolsOptionsManager).loadDevtools(),
87-
})),
84+
withDevtools(
85+
(devToolsOptionsManager: DevtoolsOptionsManager) => ({
86+
loadDevtools: devToolsOptionsManager.loadDevtools(),
87+
}),
88+
{
89+
// `deps` can be used to pass one or more injectables as parameters to the `withDevtools` callback.
90+
deps: [DevtoolsOptionsManager],
91+
},
92+
),
8893
),
8994
],
9095
}
9196
```
9297

9398
### Options
9499

95-
Of these options `client`, `position`, `errorTypes`, `buttonPosition`, and `initialIsOpen` support reactivity through signals.
100+
Of these options `loadDevtools`, `client`, `position`, `errorTypes`, `buttonPosition`, and `initialIsOpen` support reactivity through signals.
96101

97102
- `loadDevtools?: 'auto' | boolean`
98103
- Defaults to `auto`: lazily loads devtools when in development mode. Skips loading in production mode.

packages/angular-query-experimental/src/devtools-setup.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DestroyRef, computed, effect } from '@angular/core'
22
import { QueryClient, onlineManager } from '@tanstack/query-core'
3-
import type { Injector } from '@angular/core'
3+
import type { Injector, Signal } from '@angular/core'
44
import type { TanstackQueryDevtools } from '@tanstack/query-devtools'
55
import type { DevtoolsOptions } from './providers'
66

@@ -10,26 +10,24 @@ declare const ngDevMode: unknown
1010
// and to minimize bundle size
1111
export function setupDevtools(
1212
injector: Injector,
13-
withDevtoolsFn?: () => DevtoolsOptions,
13+
devtoolsOptions: Signal<DevtoolsOptions>,
1414
) {
1515
const isDevMode = typeof ngDevMode !== 'undefined' && ngDevMode
1616
const injectedClient = injector.get(QueryClient, {
1717
optional: true,
1818
})
1919
const destroyRef = injector.get(DestroyRef)
2020

21-
const options = computed(() => withDevtoolsFn?.() ?? {})
22-
2321
let devtools: TanstackQueryDevtools | null = null
2422
let el: HTMLElement | null = null
2523

2624
const shouldLoadToolsSignal = computed(() => {
27-
const { loadDevtools } = options()
25+
const { loadDevtools } = devtoolsOptions()
2826
return typeof loadDevtools === 'boolean' ? loadDevtools : isDevMode
2927
})
3028

3129
const getResolvedQueryClient = () => {
32-
const client = options().client ?? injectedClient
30+
const client = devtoolsOptions().client ?? injectedClient
3331
if (!client) {
3432
throw new Error('No QueryClient found')
3533
}
@@ -46,7 +44,7 @@ export function setupDevtools(
4644
() => {
4745
const shouldLoadTools = shouldLoadToolsSignal()
4846
const { client, position, errorTypes, buttonPosition, initialIsOpen } =
49-
options()
47+
devtoolsOptions()
5048

5149
if (devtools && !shouldLoadTools) {
5250
destroyDevtools()
@@ -67,7 +65,7 @@ export function setupDevtools(
6765

6866
import('@tanstack/query-devtools').then((queryDevtools) => {
6967
devtools = new queryDevtools.TanstackQueryDevtools({
70-
...options(),
68+
...devtoolsOptions(),
7169
client: getResolvedQueryClient(),
7270
queryFlavor: 'Angular Query',
7371
version: '5',

packages/angular-query-experimental/src/providers.ts

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import {
22
DestroyRef,
33
ENVIRONMENT_INITIALIZER,
4+
InjectionToken,
45
Injector,
56
PLATFORM_ID,
7+
computed,
68
inject,
79
makeEnvironmentProviders,
810
} from '@angular/core'
911
import { QueryClient } from '@tanstack/query-core'
1012
import { isPlatformBrowser } from '@angular/common'
1113
import { noop } from './util'
12-
import type { EnvironmentProviders, Provider } from '@angular/core'
14+
import type { EnvironmentProviders, Provider, Signal } from '@angular/core'
1315
import type {
1416
DevtoolsButtonPosition,
1517
DevtoolsErrorType,
@@ -18,6 +20,13 @@ import type {
1820

1921
declare 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 =
163172
export 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
*/
250290
export 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

Comments
 (0)