@trpc/tanstack-react-query
Version:
TanStack React Query Integration for tRPC
194 lines (193 loc) • 6.89 kB
JavaScript
import { n as unstable_createHydrateClient, r as createTRPCOptionsProxy } from "./HydrateClient-CWZqtji9.mjs";
import { c as _objectSpread2 } from "./utils-DTVvWT-Z.mjs";
import * as React from "react";
import { hashKey } from "@tanstack/react-query";
// src/internals/Context.tsx
/**
* Creates a `TRPCProvider` and the `useTRPC` and `useTRPCClient` hooks for
* your router.
*
* @example
* ```ts
* export const { TRPCProvider, useTRPC, useTRPCClient } =
* createTRPCContext<AppRouter>();
* ```
*
* @see https://trpc.io/docs/client/tanstack-react-query/setup#3a-set-up-the-trpc-context-provider
*/
function createTRPCContext() {
const TRPCClientContext = React.createContext(null);
const TRPCContext = React.createContext(null);
const TRPCProvider = (props) => {
const value = React.useMemo(() => createTRPCOptionsProxy({
client: props.trpcClient,
queryClient: props.queryClient,
keyPrefix: props.keyPrefix
}), [
props.trpcClient,
props.queryClient,
props.keyPrefix
]);
return /* @__PURE__ */ React.createElement(TRPCClientContext.Provider, { value: props.trpcClient }, /* @__PURE__ */ React.createElement(TRPCContext.Provider, { value }, props.children));
};
TRPCProvider.displayName = "TRPCProvider";
function useTRPC() {
const utils = React.useContext(TRPCContext);
if (!utils) throw new Error("useTRPC() can only be used inside of a <TRPCProvider>");
return utils;
}
function useTRPCClient() {
const client = React.useContext(TRPCClientContext);
if (!client) throw new Error("useTRPCClient() can only be used inside of a <TRPCProvider>");
return client;
}
return {
TRPCProvider,
useTRPC,
useTRPCClient
};
}
// src/internals/useSubscription.ts
/**
* Runs a subscription while the component is mounted.
*
* @example
* ```ts
* const trpc = useTRPC();
* const sub = useSubscription(
* trpc.chat.onMessage.subscriptionOptions({ roomId }),
* );
* ```
*
* @see https://trpc.io/docs/client/tanstack-react-query/usage#subscriptionOptions
*/
function useSubscription(opts) {
const optsRef = React.useRef(opts);
optsRef.current = opts;
const trackedProps = React.useRef(/* @__PURE__ */ new Set([]));
const addTrackedProp = React.useCallback((key) => {
trackedProps.current.add(key);
}, []);
const currentSubscriptionRef = React.useRef(() => {});
const reset = React.useCallback(() => {
var _currentSubscriptionR;
(_currentSubscriptionR = currentSubscriptionRef.current) === null || _currentSubscriptionR === void 0 || _currentSubscriptionR.call(currentSubscriptionRef);
updateState(getInitialState);
if (!opts.enabled) return;
let isUnsubscribed = false;
let isEnded = false;
const setEnded = () => {
if (isEnded) return;
isEnded = true;
updateState((prev) => _objectSpread2(_objectSpread2({}, prev), {}, {
status: "idle",
data: void 0,
error: null
}));
};
const subscription = opts.subscribe({
onStarted: () => {
var _optsRef$current$onSt, _optsRef$current;
(_optsRef$current$onSt = (_optsRef$current = optsRef.current).onStarted) === null || _optsRef$current$onSt === void 0 || _optsRef$current$onSt.call(_optsRef$current);
updateState((prev) => _objectSpread2(_objectSpread2({}, prev), {}, {
status: "pending",
error: null
}));
},
onData: (data) => {
var _optsRef$current$onDa, _optsRef$current2;
(_optsRef$current$onDa = (_optsRef$current2 = optsRef.current).onData) === null || _optsRef$current$onDa === void 0 || _optsRef$current$onDa.call(_optsRef$current2, data);
updateState((prev) => _objectSpread2(_objectSpread2({}, prev), {}, {
status: "pending",
data,
error: null
}));
},
onError: (error) => {
var _optsRef$current$onEr, _optsRef$current3;
(_optsRef$current$onEr = (_optsRef$current3 = optsRef.current).onError) === null || _optsRef$current$onEr === void 0 || _optsRef$current$onEr.call(_optsRef$current3, error);
updateState((prev) => _objectSpread2(_objectSpread2({}, prev), {}, {
status: "error",
error
}));
},
onStopped: () => {
var _optsRef$current$onSt2, _optsRef$current4;
if (isUnsubscribed) return;
setEnded();
(_optsRef$current$onSt2 = (_optsRef$current4 = optsRef.current).onStopped) === null || _optsRef$current$onSt2 === void 0 || _optsRef$current$onSt2.call(_optsRef$current4);
},
onComplete: () => {
var _optsRef$current$onCo, _optsRef$current5;
if (isUnsubscribed) return;
setEnded();
(_optsRef$current$onCo = (_optsRef$current5 = optsRef.current).onComplete) === null || _optsRef$current$onCo === void 0 || _optsRef$current$onCo.call(_optsRef$current5);
},
onConnectionStateChange: (result) => {
var _optsRef$current$onCo2, _optsRef$current6;
(_optsRef$current$onCo2 = (_optsRef$current6 = optsRef.current).onConnectionStateChange) === null || _optsRef$current$onCo2 === void 0 || _optsRef$current$onCo2.call(_optsRef$current6, result);
updateState((prev) => {
switch (result.state) {
case "connecting": return _objectSpread2(_objectSpread2({}, prev), {}, {
status: "connecting",
error: result.error
});
case "pending": return prev;
case "idle": return _objectSpread2(_objectSpread2({}, prev), {}, {
status: "idle",
data: void 0,
error: null
});
}
});
}
});
currentSubscriptionRef.current = () => {
isUnsubscribed = true;
subscription.unsubscribe();
};
}, [hashKey(opts.queryKey), opts.enabled]);
const getInitialState = React.useCallback(() => {
return opts.enabled ? {
data: void 0,
error: null,
status: "connecting",
reset
} : {
data: void 0,
error: null,
status: "idle",
reset
};
}, [opts.enabled, reset]);
const resultRef = React.useRef(getInitialState());
const [state, setState] = React.useState(trackResult(resultRef, addTrackedProp));
state.reset = reset;
const updateState = React.useCallback((callback) => {
const prev = resultRef.current;
const next = resultRef.current = callback(prev);
let shouldUpdate = false;
for (const key of trackedProps.current) if (prev[key] !== next[key]) {
shouldUpdate = true;
break;
}
if (shouldUpdate) setState(trackResult(resultRef, addTrackedProp));
}, [addTrackedProp]);
React.useEffect(() => {
if (!opts.enabled) return;
reset();
return () => {
var _currentSubscriptionR2;
(_currentSubscriptionR2 = currentSubscriptionRef.current) === null || _currentSubscriptionR2 === void 0 || _currentSubscriptionR2.call(currentSubscriptionRef);
};
}, [reset, opts.enabled]);
return state;
}
function trackResult(result, onTrackResult) {
return new Proxy(result.current, { get(_target, prop) {
onTrackResult(prop);
return result.current[prop];
} });
}
export { createTRPCContext, createTRPCOptionsProxy, unstable_createHydrateClient, useSubscription };
//# sourceMappingURL=index.mjs.map