UNPKG

@preact-signals/query

Version:

A reactive utility for React/Preact that simplifies the handling of data fetching and state management. Powered by Preact Signals, it provides hooks and functions to create reactive resources and manage their state seamlessly.

84 lines 3.76 kB
"use strict"; "use client"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryClientProvider = exports.useQueryClient$ = exports.useQueryClient = exports.defaultContext = void 0; const React = __importStar(require("react")); const hooks_1 = require("@preact-signals/utils/hooks"); exports.defaultContext = React.createContext(undefined); // export const signalContext = React.createContext< // ReadonlySignal<QueryClient> | undefined // >(undefined); const QueryClientSharingContext = React.createContext(false); // If we are given a context, we will use it. // Otherwise, if contextSharing is on, we share the first and at least one // instance of the context across the window // to ensure that if React Query is used across // different bundles or microfrontends they will // all use the same **instance** of context, regardless // of module scoping. function getQueryClientContext(context, contextSharing) { if (context) { return context; } if (contextSharing && typeof window !== "undefined") { if (!window.ReactQueryClientContext) { window.ReactQueryClientContext = exports.defaultContext; } return window.ReactQueryClientContext; } return exports.defaultContext; } const useQueryClient = ({ context } = {}) => { const queryClient = React.useContext(getQueryClientContext(context, React.useContext(QueryClientSharingContext))); if (!queryClient) { throw new Error("No QueryClient set, use QueryClientProvider to set one"); } return queryClient; }; exports.useQueryClient = useQueryClient; const useQueryClient$ = (options) => (0, hooks_1.useSignalOfState)((0, exports.useQueryClient)(options)); exports.useQueryClient$ = useQueryClient$; const QueryClientProvider = ({ client, children, context, contextSharing = false, }) => { React.useEffect(() => { client.mount(); return () => { client.unmount(); }; }, [client]); if (process.env.NODE_ENV !== "production" && contextSharing) { client .getLogger() .error(`The contextSharing option has been deprecated and will be removed in the next major version`); } // const $client = useSignalOfState(client); // const $context = useSignalOfState(context); // const $contextSharing = useSignalOfState(contextSharing); const Context = getQueryClientContext(context, contextSharing); return (React.createElement(QueryClientSharingContext.Provider, { value: !context && contextSharing }, React.createElement(Context.Provider, { value: client }, children))); }; exports.QueryClientProvider = QueryClientProvider; //# sourceMappingURL=QueryClientProvider.js.map