@tanstack/angular-query-experimental
Version:
Signals for managing, caching and syncing asynchronous and remote data in Angular
110 lines (109 loc) • 3.93 kB
JavaScript
import { InjectionToken, inject, DestroyRef, isDevMode, ENVIRONMENT_INITIALIZER, PLATFORM_ID, computed, effect } from "@angular/core";
import { QueryClient, noop, onlineManager } from "@tanstack/query-core";
import { isPlatformBrowser } from "@angular/common";
function provideQueryClient(queryClient) {
return {
provide: QueryClient,
useFactory: () => {
const client = queryClient instanceof InjectionToken ? inject(queryClient) : queryClient;
inject(DestroyRef).onDestroy(() => client.unmount());
client.mount();
return client;
}
};
}
function provideTanStackQuery(queryClient, ...features) {
return [
provideQueryClient(queryClient),
features.map((feature) => feature.ɵproviders)
];
}
function queryFeature(kind, providers) {
return { ɵkind: kind, ɵproviders: providers };
}
function withDevtools(withDevtoolsFn) {
let providers = [];
if (!isDevMode() && !withDevtoolsFn) {
providers = [];
} else {
providers = [
{
// Do not use provideEnvironmentInitializer while Angular < v19 is supported
provide: ENVIRONMENT_INITIALIZER,
multi: true,
useFactory: () => {
if (!isPlatformBrowser(inject(PLATFORM_ID))) return noop;
const injectedClient = inject(QueryClient, {
optional: true
});
const destroyRef = inject(DestroyRef);
const options = computed(() => (withDevtoolsFn == null ? void 0 : withDevtoolsFn()) ?? {});
let devtools = null;
let el = null;
const shouldLoadToolsSignal = computed(() => {
const { loadDevtools } = options();
return typeof loadDevtools === "boolean" ? loadDevtools : isDevMode();
});
const getResolvedQueryClient = () => {
const client = options().client ?? injectedClient;
if (!client) {
throw new Error("No QueryClient found");
}
return client;
};
const destroyDevtools = () => {
devtools == null ? void 0 : devtools.unmount();
el == null ? void 0 : el.remove();
devtools = null;
};
return () => effect(() => {
const shouldLoadTools = shouldLoadToolsSignal();
const {
client,
position,
errorTypes,
buttonPosition,
initialIsOpen
} = options();
if (devtools && !shouldLoadTools) {
destroyDevtools();
return;
} else if (devtools && shouldLoadTools) {
client && devtools.setClient(client);
position && devtools.setPosition(position);
errorTypes && devtools.setErrorTypes(errorTypes);
buttonPosition && devtools.setButtonPosition(buttonPosition);
initialIsOpen && devtools.setInitialIsOpen(initialIsOpen);
return;
} else if (!shouldLoadTools) {
return;
}
el = document.body.appendChild(document.createElement("div"));
el.classList.add("tsqd-parent-container");
import("@tanstack/query-devtools").then((queryDevtools) => {
devtools = new queryDevtools.TanstackQueryDevtools({
...options(),
client: getResolvedQueryClient(),
queryFlavor: "Angular Query",
version: "5",
onlineManager
});
el && devtools.mount(el);
destroyRef.onDestroy(destroyDevtools);
});
});
}
}
];
}
return queryFeature("DeveloperTools", providers);
}
const queryFeatures = ["DeveloperTools", "PersistQueryClient"];
export {
provideQueryClient,
provideTanStackQuery,
queryFeature,
queryFeatures,
withDevtools
};
//# sourceMappingURL=providers.mjs.map