@tanstack/query-broadcast-client-experimental 5.104.0 → 5.104.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/index.cjs +8 -0
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +17 -0
- package/build/legacy/index.d.cts.map +1 -1
- package/build/legacy/index.d.ts +17 -0
- package/build/legacy/index.d.ts.map +1 -1
- package/build/legacy/index.js +8 -0
- package/build/legacy/index.js.map +1 -1
- package/build/modern/index.cjs +8 -0
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +17 -0
- package/build/modern/index.d.cts.map +1 -1
- package/build/modern/index.d.ts +17 -0
- package/build/modern/index.d.ts.map +1 -1
- package/build/modern/index.js +8 -0
- package/build/modern/index.js.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +17 -0
package/build/legacy/index.cjs
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let broadcast_channel = require("broadcast-channel");
|
|
3
3
|
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
6
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
7
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
8
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
9
|
+
* and the `onBroadcastError` callback.
|
|
10
|
+
* @returns A function that stops syncing and closes the channel.
|
|
11
|
+
*/
|
|
4
12
|
function broadcastQueryClient({ queryClient, broadcastChannel = "tanstack-query", options, onBroadcastError }) {
|
|
5
13
|
let transaction = false;
|
|
6
14
|
const tx = (cb) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["BroadcastChannel"],"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient, QueryKey } from '@tanstack/query-core'\n\n/**\n * Metadata describing a broadcast that failed to be delivered to other tabs.\n * Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers\n * can correlate failures with the originating query.\n */\nexport interface BroadcastErrorEvent {\n type: 'updated' | 'removed' | 'added'\n queryHash: string\n queryKey: QueryKey\n}\n\ntype BroadcastMessage =\n | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown }\n | { type: 'removed'; queryHash: string; queryKey: QueryKey }\n | { type: 'added'; queryHash: string; queryKey: QueryKey; state: unknown }\n\ninterface BroadcastQueryClientOptions {\n /** The QueryClient to sync. */\n queryClient: QueryClient\n /**\n * Unique channel name used to communicate between tabs and windows.\n * @default 'tanstack-query'\n */\n broadcastChannel?: string\n /** Options forwarded to the underlying `BroadcastChannel`. */\n options?: BroadcastChannelOptions\n /**\n * Called when a query event fails to broadcast to other tabs — most\n * commonly because the query's `state.data`, `state.error`, or `queryKey`\n * contains a value the structured-clone algorithm cannot serialize\n * (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).\n *\n * Provide this to route failures to an error tracker. If omitted, a\n * `console.warn` is emitted in development so failures are never silent.\n *\n * May return a `Promise`; any rejection is caught internally so it cannot\n * cause a secondary unhandled rejection.\n */\n onBroadcastError?: (\n error: unknown,\n event: BroadcastErrorEvent,\n ) => void | Promise<void>\n}\n\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n onBroadcastError,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n try {\n cb()\n } finally {\n // Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`\n // triggering a listener that throws while applying an incoming\n // cross-tab message). Without this, `transaction` would stay `true`\n // forever, silently disabling this tab's own broadcasts to other tabs\n // for the rest of the session.\n transaction = false\n }\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const safePost = (message: BroadcastMessage): void => {\n channel.postMessage(message).catch((error: unknown) => {\n const event: BroadcastErrorEvent = {\n type: message.type,\n queryHash: message.queryHash,\n queryKey: message.queryKey,\n }\n\n if (onBroadcastError) {\n const warnCallbackError = (callbackError: unknown) => {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] onBroadcastError threw while handling \"${event.type}\" for query ${event.queryHash}.`,\n callbackError,\n )\n }\n }\n let result: void | Promise<void>\n try {\n result = onBroadcastError(error, event)\n } catch (callbackError) {\n warnCallbackError(callbackError)\n return\n }\n result?.catch(warnCallbackError)\n } else if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] Failed to broadcast \"${event.type}\" event for query ${event.queryHash}. ` +\n 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',\n error,\n )\n }\n })\n }\n\n const unsubscribe = queryCache.subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state, observers },\n } = queryEvent\n\n if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {\n safePost({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n safePost({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n safePost({\n type: 'added',\n queryHash,\n queryKey,\n state,\n })\n }\n })\n\n channel.onmessage = (action) => {\n if (!action?.type) {\n return\n }\n\n tx(() => {\n const { type, queryHash, queryKey, state } = action\n\n const query = queryCache.get(queryHash)\n\n if (type === 'updated') {\n if (query) {\n query.setState(state)\n return\n }\n\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n } else if (type === 'removed') {\n if (query) {\n queryCache.remove(query)\n }\n } else if (type === 'added') {\n if (query) {\n if (query.state.data === undefined) {\n query.setState(state)\n }\n return\n }\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n }\n })\n }\n return () => {\n unsubscribe()\n channel.close()\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["BroadcastChannel"],"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient, QueryKey } from '@tanstack/query-core'\n\n/**\n * Metadata describing a broadcast that failed to be delivered to other tabs.\n * Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers\n * can correlate failures with the originating query.\n */\nexport interface BroadcastErrorEvent {\n /**\n * The kind of cache event that was being broadcast.\n */\n type: 'updated' | 'removed' | 'added'\n /**\n * The hash of the query the broadcast was about.\n */\n queryHash: string\n /**\n * The key of the query the broadcast was about.\n */\n queryKey: QueryKey\n}\n\ntype BroadcastMessage =\n | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown }\n | { type: 'removed'; queryHash: string; queryKey: QueryKey }\n | { type: 'added'; queryHash: string; queryKey: QueryKey; state: unknown }\n\ninterface BroadcastQueryClientOptions {\n /** The QueryClient to sync. */\n queryClient: QueryClient\n /**\n * Unique channel name used to communicate between tabs and windows.\n * @default 'tanstack-query'\n */\n broadcastChannel?: string\n /** Options forwarded to the underlying `BroadcastChannel`. */\n options?: BroadcastChannelOptions\n /**\n * Called when a query event fails to broadcast to other tabs — most\n * commonly because the query's `state.data`, `state.error`, or `queryKey`\n * contains a value the structured-clone algorithm cannot serialize\n * (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).\n *\n * Provide this to route failures to an error tracker. If omitted, a\n * `console.warn` is emitted in development so failures are never silent.\n *\n * May return a `Promise`; any rejection is caught internally so it cannot\n * cause a secondary unhandled rejection.\n */\n onBroadcastError?: (\n error: unknown,\n event: BroadcastErrorEvent,\n ) => void | Promise<void>\n}\n\n/**\n * Syncs a client's query cache with the caches of other tabs and windows through a\n * `BroadcastChannel`: added queries, successful updates, and removals of queries that have\n * observers are broadcast, and those received from other tabs are applied.\n * @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,\n * and the `onBroadcastError` callback.\n * @returns A function that stops syncing and closes the channel.\n */\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n onBroadcastError,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n try {\n cb()\n } finally {\n // Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`\n // triggering a listener that throws while applying an incoming\n // cross-tab message). Without this, `transaction` would stay `true`\n // forever, silently disabling this tab's own broadcasts to other tabs\n // for the rest of the session.\n transaction = false\n }\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const safePost = (message: BroadcastMessage): void => {\n channel.postMessage(message).catch((error: unknown) => {\n const event: BroadcastErrorEvent = {\n type: message.type,\n queryHash: message.queryHash,\n queryKey: message.queryKey,\n }\n\n if (onBroadcastError) {\n const warnCallbackError = (callbackError: unknown) => {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] onBroadcastError threw while handling \"${event.type}\" for query ${event.queryHash}.`,\n callbackError,\n )\n }\n }\n let result: void | Promise<void>\n try {\n result = onBroadcastError(error, event)\n } catch (callbackError) {\n warnCallbackError(callbackError)\n return\n }\n result?.catch(warnCallbackError)\n } else if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] Failed to broadcast \"${event.type}\" event for query ${event.queryHash}. ` +\n 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',\n error,\n )\n }\n })\n }\n\n const unsubscribe = queryCache.subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state, observers },\n } = queryEvent\n\n if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {\n safePost({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n safePost({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n safePost({\n type: 'added',\n queryHash,\n queryKey,\n state,\n })\n }\n })\n\n channel.onmessage = (action) => {\n if (!action?.type) {\n return\n }\n\n tx(() => {\n const { type, queryHash, queryKey, state } = action\n\n const query = queryCache.get(queryHash)\n\n if (type === 'updated') {\n if (query) {\n query.setState(state)\n return\n }\n\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n } else if (type === 'removed') {\n if (query) {\n queryCache.remove(query)\n }\n } else if (type === 'added') {\n if (query) {\n if (query.state.data === undefined) {\n query.setState(state)\n }\n return\n }\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n }\n })\n }\n return () => {\n unsubscribe()\n channel.close()\n }\n}\n"],"mappings":";;;;;;;;;;;AAiEA,SAAgB,qBAAqB,EACnC,aACA,mBAAmB,kBACnB,SACA,oBAC0C;CAC1C,IAAI,cAAc;CAClB,MAAM,MAAM,OAAmB;EAC7B,cAAc;EACd,IAAI;GACF,GAAG;EACL,UAAU;GAMR,cAAc;EAChB;CACF;CAEA,MAAM,UAAU,IAAIA,kBAAAA,iBAAiB,kBAAkB;EACrD,kBAAkB;EAClB,GAAG;CACL,CAAC;CAED,MAAM,aAAa,YAAY,cAAc;CAE7C,MAAM,YAAY,YAAoC;EACpD,QAAQ,YAAY,OAAO,CAAC,CAAC,OAAO,UAAmB;GACrD,MAAM,QAA6B;IACjC,MAAM,QAAQ;IACd,WAAW,QAAQ;IACnB,UAAU,QAAQ;GACpB;GAEA,IAAI,kBAAkB;IACpB,MAAM,qBAAqB,kBAA2B;KACpD,IAAI,QAAQ,IAAI,aAAa,cAC3B,QAAQ,KACN,iEAAiE,MAAM,KAAK,cAAc,MAAM,UAAU,IAC1G,aACF;IAEJ;IACA,IAAI;IACJ,IAAI;KACF,SAAS,iBAAiB,OAAO,KAAK;IACxC,SAAS,eAAe;KACtB,kBAAkB,aAAa;KAC/B;IACF;IACA,WAAA,QAAA,WAAA,KAAA,KAAA,OAAQ,MAAM,iBAAiB;GACjC,OAAO,IAAI,QAAQ,IAAI,aAAa,cAClC,QAAQ,KACN,+CAA+C,MAAM,KAAK,oBAAoB,MAAM,UAAU,+FAE9F,KACF;EAEJ,CAAC;CACH;CAEA,MAAM,cAAc,WAAW,WAAW,eAAe;EACvD,IAAI,aACF;EAGF,MAAM,EACJ,OAAO,EAAE,WAAW,UAAU,OAAO,gBACnC;EAEJ,IAAI,WAAW,SAAS,aAAa,WAAW,OAAO,SAAS,WAC9D,SAAS;GACP,MAAM;GACN;GACA;GACA;EACF,CAAC;EAGH,IAAI,WAAW,SAAS,aAAa,UAAU,SAAS,GACtD,SAAS;GACP,MAAM;GACN;GACA;EACF,CAAC;EAGH,IAAI,WAAW,SAAS,SACtB,SAAS;GACP,MAAM;GACN;GACA;GACA;EACF,CAAC;CAEL,CAAC;CAED,QAAQ,aAAa,WAAW;EAC9B,IAAI,EAAA,WAAA,QAAA,WAAA,KAAA,IAAA,KAAA,IAAC,OAAQ,OACX;EAGF,SAAS;GACP,MAAM,EAAE,MAAM,WAAW,UAAU,UAAU;GAE7C,MAAM,QAAQ,WAAW,IAAI,SAAS;GAEtC,IAAI,SAAS,WAAW;IACtB,IAAI,OAAO;KACT,MAAM,SAAS,KAAK;KACpB;IACF;IAEA,WAAW,MACT,aACA;KACE;KACA;IACF,GACA,KACF;GACF,OAAO,IAAI,SAAS,WACd;QAAA,OACF,WAAW,OAAO,KAAK;GAAA,OAEpB,IAAI,SAAS,SAAS;IAC3B,IAAI,OAAO;KACT,IAAI,MAAM,MAAM,SAAS,KAAA,GACvB,MAAM,SAAS,KAAK;KAEtB;IACF;IACA,WAAW,MACT,aACA;KACE;KACA;IACF,GACA,KACF;GACF;EACF,CAAC;CACH;CACA,aAAa;EACX,YAAY;EACZ,QAAQ,MAAM;CAChB;AACF"}
|
package/build/legacy/index.d.cts
CHANGED
|
@@ -7,8 +7,17 @@ import { QueryClient, QueryKey } from "@tanstack/query-core";
|
|
|
7
7
|
* can correlate failures with the originating query.
|
|
8
8
|
*/
|
|
9
9
|
export interface BroadcastErrorEvent {
|
|
10
|
+
/**
|
|
11
|
+
* The kind of cache event that was being broadcast.
|
|
12
|
+
*/
|
|
10
13
|
type: 'updated' | 'removed' | 'added';
|
|
14
|
+
/**
|
|
15
|
+
* The hash of the query the broadcast was about.
|
|
16
|
+
*/
|
|
11
17
|
queryHash: string;
|
|
18
|
+
/**
|
|
19
|
+
* The key of the query the broadcast was about.
|
|
20
|
+
*/
|
|
12
21
|
queryKey: QueryKey;
|
|
13
22
|
}
|
|
14
23
|
interface BroadcastQueryClientOptions {
|
|
@@ -35,6 +44,14 @@ interface BroadcastQueryClientOptions {
|
|
|
35
44
|
*/
|
|
36
45
|
onBroadcastError?: (error: unknown, event: BroadcastErrorEvent) => void | Promise<void>;
|
|
37
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
49
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
50
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
51
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
52
|
+
* and the `onBroadcastError` callback.
|
|
53
|
+
* @returns A function that stops syncing and closes the channel.
|
|
54
|
+
*/
|
|
38
55
|
export declare function broadcastQueryClient({ queryClient, broadcastChannel, options, onBroadcastError }: BroadcastQueryClientOptions): () => void;
|
|
39
56
|
//#endregion
|
|
40
57
|
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;iBASiB
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;iBASiB;;;;EAIf;;;;EAIA;;;;EAIA,UAAU;;UAQF;;EAER,aAAa;;;;;EAKb;;EAEA,UAAU;;;;;;;;;;;;;EAaV,oBACE,gBACA,OAAO,+BACG;;;;;;;;;;wBAWE,uBACd,aACA,kBACA,SACA,oBACC"}
|
package/build/legacy/index.d.ts
CHANGED
|
@@ -7,8 +7,17 @@ import { QueryClient, QueryKey } from "@tanstack/query-core";
|
|
|
7
7
|
* can correlate failures with the originating query.
|
|
8
8
|
*/
|
|
9
9
|
export interface BroadcastErrorEvent {
|
|
10
|
+
/**
|
|
11
|
+
* The kind of cache event that was being broadcast.
|
|
12
|
+
*/
|
|
10
13
|
type: 'updated' | 'removed' | 'added';
|
|
14
|
+
/**
|
|
15
|
+
* The hash of the query the broadcast was about.
|
|
16
|
+
*/
|
|
11
17
|
queryHash: string;
|
|
18
|
+
/**
|
|
19
|
+
* The key of the query the broadcast was about.
|
|
20
|
+
*/
|
|
12
21
|
queryKey: QueryKey;
|
|
13
22
|
}
|
|
14
23
|
interface BroadcastQueryClientOptions {
|
|
@@ -35,6 +44,14 @@ interface BroadcastQueryClientOptions {
|
|
|
35
44
|
*/
|
|
36
45
|
onBroadcastError?: (error: unknown, event: BroadcastErrorEvent) => void | Promise<void>;
|
|
37
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
49
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
50
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
51
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
52
|
+
* and the `onBroadcastError` callback.
|
|
53
|
+
* @returns A function that stops syncing and closes the channel.
|
|
54
|
+
*/
|
|
38
55
|
export declare function broadcastQueryClient({ queryClient, broadcastChannel, options, onBroadcastError }: BroadcastQueryClientOptions): () => void;
|
|
39
56
|
//#endregion
|
|
40
57
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;iBASiB
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;iBASiB;;;;EAIf;;;;EAIA;;;;EAIA,UAAU;;UAQF;;EAER,aAAa;;;;;EAKb;;EAEA,UAAU;;;;;;;;;;;;;EAaV,oBACE,gBACA,OAAO,+BACG;;;;;;;;;;wBAWE,uBACd,aACA,kBACA,SACA,oBACC"}
|
package/build/legacy/index.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { BroadcastChannel } from "broadcast-channel";
|
|
2
2
|
//#region src/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
5
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
6
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
7
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
8
|
+
* and the `onBroadcastError` callback.
|
|
9
|
+
* @returns A function that stops syncing and closes the channel.
|
|
10
|
+
*/
|
|
3
11
|
function broadcastQueryClient({ queryClient, broadcastChannel = "tanstack-query", options, onBroadcastError }) {
|
|
4
12
|
let transaction = false;
|
|
5
13
|
const tx = (cb) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient, QueryKey } from '@tanstack/query-core'\n\n/**\n * Metadata describing a broadcast that failed to be delivered to other tabs.\n * Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers\n * can correlate failures with the originating query.\n */\nexport interface BroadcastErrorEvent {\n type: 'updated' | 'removed' | 'added'\n queryHash: string\n queryKey: QueryKey\n}\n\ntype BroadcastMessage =\n | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown }\n | { type: 'removed'; queryHash: string; queryKey: QueryKey }\n | { type: 'added'; queryHash: string; queryKey: QueryKey; state: unknown }\n\ninterface BroadcastQueryClientOptions {\n /** The QueryClient to sync. */\n queryClient: QueryClient\n /**\n * Unique channel name used to communicate between tabs and windows.\n * @default 'tanstack-query'\n */\n broadcastChannel?: string\n /** Options forwarded to the underlying `BroadcastChannel`. */\n options?: BroadcastChannelOptions\n /**\n * Called when a query event fails to broadcast to other tabs — most\n * commonly because the query's `state.data`, `state.error`, or `queryKey`\n * contains a value the structured-clone algorithm cannot serialize\n * (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).\n *\n * Provide this to route failures to an error tracker. If omitted, a\n * `console.warn` is emitted in development so failures are never silent.\n *\n * May return a `Promise`; any rejection is caught internally so it cannot\n * cause a secondary unhandled rejection.\n */\n onBroadcastError?: (\n error: unknown,\n event: BroadcastErrorEvent,\n ) => void | Promise<void>\n}\n\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n onBroadcastError,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n try {\n cb()\n } finally {\n // Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`\n // triggering a listener that throws while applying an incoming\n // cross-tab message). Without this, `transaction` would stay `true`\n // forever, silently disabling this tab's own broadcasts to other tabs\n // for the rest of the session.\n transaction = false\n }\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const safePost = (message: BroadcastMessage): void => {\n channel.postMessage(message).catch((error: unknown) => {\n const event: BroadcastErrorEvent = {\n type: message.type,\n queryHash: message.queryHash,\n queryKey: message.queryKey,\n }\n\n if (onBroadcastError) {\n const warnCallbackError = (callbackError: unknown) => {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] onBroadcastError threw while handling \"${event.type}\" for query ${event.queryHash}.`,\n callbackError,\n )\n }\n }\n let result: void | Promise<void>\n try {\n result = onBroadcastError(error, event)\n } catch (callbackError) {\n warnCallbackError(callbackError)\n return\n }\n result?.catch(warnCallbackError)\n } else if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] Failed to broadcast \"${event.type}\" event for query ${event.queryHash}. ` +\n 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',\n error,\n )\n }\n })\n }\n\n const unsubscribe = queryCache.subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state, observers },\n } = queryEvent\n\n if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {\n safePost({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n safePost({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n safePost({\n type: 'added',\n queryHash,\n queryKey,\n state,\n })\n }\n })\n\n channel.onmessage = (action) => {\n if (!action?.type) {\n return\n }\n\n tx(() => {\n const { type, queryHash, queryKey, state } = action\n\n const query = queryCache.get(queryHash)\n\n if (type === 'updated') {\n if (query) {\n query.setState(state)\n return\n }\n\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n } else if (type === 'removed') {\n if (query) {\n queryCache.remove(query)\n }\n } else if (type === 'added') {\n if (query) {\n if (query.state.data === undefined) {\n query.setState(state)\n }\n return\n }\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n }\n })\n }\n return () => {\n unsubscribe()\n channel.close()\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient, QueryKey } from '@tanstack/query-core'\n\n/**\n * Metadata describing a broadcast that failed to be delivered to other tabs.\n * Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers\n * can correlate failures with the originating query.\n */\nexport interface BroadcastErrorEvent {\n /**\n * The kind of cache event that was being broadcast.\n */\n type: 'updated' | 'removed' | 'added'\n /**\n * The hash of the query the broadcast was about.\n */\n queryHash: string\n /**\n * The key of the query the broadcast was about.\n */\n queryKey: QueryKey\n}\n\ntype BroadcastMessage =\n | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown }\n | { type: 'removed'; queryHash: string; queryKey: QueryKey }\n | { type: 'added'; queryHash: string; queryKey: QueryKey; state: unknown }\n\ninterface BroadcastQueryClientOptions {\n /** The QueryClient to sync. */\n queryClient: QueryClient\n /**\n * Unique channel name used to communicate between tabs and windows.\n * @default 'tanstack-query'\n */\n broadcastChannel?: string\n /** Options forwarded to the underlying `BroadcastChannel`. */\n options?: BroadcastChannelOptions\n /**\n * Called when a query event fails to broadcast to other tabs — most\n * commonly because the query's `state.data`, `state.error`, or `queryKey`\n * contains a value the structured-clone algorithm cannot serialize\n * (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).\n *\n * Provide this to route failures to an error tracker. If omitted, a\n * `console.warn` is emitted in development so failures are never silent.\n *\n * May return a `Promise`; any rejection is caught internally so it cannot\n * cause a secondary unhandled rejection.\n */\n onBroadcastError?: (\n error: unknown,\n event: BroadcastErrorEvent,\n ) => void | Promise<void>\n}\n\n/**\n * Syncs a client's query cache with the caches of other tabs and windows through a\n * `BroadcastChannel`: added queries, successful updates, and removals of queries that have\n * observers are broadcast, and those received from other tabs are applied.\n * @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,\n * and the `onBroadcastError` callback.\n * @returns A function that stops syncing and closes the channel.\n */\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n onBroadcastError,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n try {\n cb()\n } finally {\n // Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`\n // triggering a listener that throws while applying an incoming\n // cross-tab message). Without this, `transaction` would stay `true`\n // forever, silently disabling this tab's own broadcasts to other tabs\n // for the rest of the session.\n transaction = false\n }\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const safePost = (message: BroadcastMessage): void => {\n channel.postMessage(message).catch((error: unknown) => {\n const event: BroadcastErrorEvent = {\n type: message.type,\n queryHash: message.queryHash,\n queryKey: message.queryKey,\n }\n\n if (onBroadcastError) {\n const warnCallbackError = (callbackError: unknown) => {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] onBroadcastError threw while handling \"${event.type}\" for query ${event.queryHash}.`,\n callbackError,\n )\n }\n }\n let result: void | Promise<void>\n try {\n result = onBroadcastError(error, event)\n } catch (callbackError) {\n warnCallbackError(callbackError)\n return\n }\n result?.catch(warnCallbackError)\n } else if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] Failed to broadcast \"${event.type}\" event for query ${event.queryHash}. ` +\n 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',\n error,\n )\n }\n })\n }\n\n const unsubscribe = queryCache.subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state, observers },\n } = queryEvent\n\n if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {\n safePost({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n safePost({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n safePost({\n type: 'added',\n queryHash,\n queryKey,\n state,\n })\n }\n })\n\n channel.onmessage = (action) => {\n if (!action?.type) {\n return\n }\n\n tx(() => {\n const { type, queryHash, queryKey, state } = action\n\n const query = queryCache.get(queryHash)\n\n if (type === 'updated') {\n if (query) {\n query.setState(state)\n return\n }\n\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n } else if (type === 'removed') {\n if (query) {\n queryCache.remove(query)\n }\n } else if (type === 'added') {\n if (query) {\n if (query.state.data === undefined) {\n query.setState(state)\n }\n return\n }\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n }\n })\n }\n return () => {\n unsubscribe()\n channel.close()\n }\n}\n"],"mappings":";;;;;;;;;;AAiEA,SAAgB,qBAAqB,EACnC,aACA,mBAAmB,kBACnB,SACA,oBAC0C;CAC1C,IAAI,cAAc;CAClB,MAAM,MAAM,OAAmB;EAC7B,cAAc;EACd,IAAI;GACF,GAAG;EACL,UAAU;GAMR,cAAc;EAChB;CACF;CAEA,MAAM,UAAU,IAAI,iBAAiB,kBAAkB;EACrD,kBAAkB;EAClB,GAAG;CACL,CAAC;CAED,MAAM,aAAa,YAAY,cAAc;CAE7C,MAAM,YAAY,YAAoC;EACpD,QAAQ,YAAY,OAAO,CAAC,CAAC,OAAO,UAAmB;GACrD,MAAM,QAA6B;IACjC,MAAM,QAAQ;IACd,WAAW,QAAQ;IACnB,UAAU,QAAQ;GACpB;GAEA,IAAI,kBAAkB;IACpB,MAAM,qBAAqB,kBAA2B;KACpD,IAAI,QAAQ,IAAI,aAAa,cAC3B,QAAQ,KACN,iEAAiE,MAAM,KAAK,cAAc,MAAM,UAAU,IAC1G,aACF;IAEJ;IACA,IAAI;IACJ,IAAI;KACF,SAAS,iBAAiB,OAAO,KAAK;IACxC,SAAS,eAAe;KACtB,kBAAkB,aAAa;KAC/B;IACF;IACA,WAAA,QAAA,WAAA,KAAA,KAAA,OAAQ,MAAM,iBAAiB;GACjC,OAAO,IAAI,QAAQ,IAAI,aAAa,cAClC,QAAQ,KACN,+CAA+C,MAAM,KAAK,oBAAoB,MAAM,UAAU,+FAE9F,KACF;EAEJ,CAAC;CACH;CAEA,MAAM,cAAc,WAAW,WAAW,eAAe;EACvD,IAAI,aACF;EAGF,MAAM,EACJ,OAAO,EAAE,WAAW,UAAU,OAAO,gBACnC;EAEJ,IAAI,WAAW,SAAS,aAAa,WAAW,OAAO,SAAS,WAC9D,SAAS;GACP,MAAM;GACN;GACA;GACA;EACF,CAAC;EAGH,IAAI,WAAW,SAAS,aAAa,UAAU,SAAS,GACtD,SAAS;GACP,MAAM;GACN;GACA;EACF,CAAC;EAGH,IAAI,WAAW,SAAS,SACtB,SAAS;GACP,MAAM;GACN;GACA;GACA;EACF,CAAC;CAEL,CAAC;CAED,QAAQ,aAAa,WAAW;EAC9B,IAAI,EAAA,WAAA,QAAA,WAAA,KAAA,IAAA,KAAA,IAAC,OAAQ,OACX;EAGF,SAAS;GACP,MAAM,EAAE,MAAM,WAAW,UAAU,UAAU;GAE7C,MAAM,QAAQ,WAAW,IAAI,SAAS;GAEtC,IAAI,SAAS,WAAW;IACtB,IAAI,OAAO;KACT,MAAM,SAAS,KAAK;KACpB;IACF;IAEA,WAAW,MACT,aACA;KACE;KACA;IACF,GACA,KACF;GACF,OAAO,IAAI,SAAS,WACd;QAAA,OACF,WAAW,OAAO,KAAK;GAAA,OAEpB,IAAI,SAAS,SAAS;IAC3B,IAAI,OAAO;KACT,IAAI,MAAM,MAAM,SAAS,KAAA,GACvB,MAAM,SAAS,KAAK;KAEtB;IACF;IACA,WAAW,MACT,aACA;KACE;KACA;IACF,GACA,KACF;GACF;EACF,CAAC;CACH;CACA,aAAa;EACX,YAAY;EACZ,QAAQ,MAAM;CAChB;AACF"}
|
package/build/modern/index.cjs
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let broadcast_channel = require("broadcast-channel");
|
|
3
3
|
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
6
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
7
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
8
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
9
|
+
* and the `onBroadcastError` callback.
|
|
10
|
+
* @returns A function that stops syncing and closes the channel.
|
|
11
|
+
*/
|
|
4
12
|
function broadcastQueryClient({ queryClient, broadcastChannel = "tanstack-query", options, onBroadcastError }) {
|
|
5
13
|
let transaction = false;
|
|
6
14
|
const tx = (cb) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["BroadcastChannel"],"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient, QueryKey } from '@tanstack/query-core'\n\n/**\n * Metadata describing a broadcast that failed to be delivered to other tabs.\n * Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers\n * can correlate failures with the originating query.\n */\nexport interface BroadcastErrorEvent {\n type: 'updated' | 'removed' | 'added'\n queryHash: string\n queryKey: QueryKey\n}\n\ntype BroadcastMessage =\n | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown }\n | { type: 'removed'; queryHash: string; queryKey: QueryKey }\n | { type: 'added'; queryHash: string; queryKey: QueryKey; state: unknown }\n\ninterface BroadcastQueryClientOptions {\n /** The QueryClient to sync. */\n queryClient: QueryClient\n /**\n * Unique channel name used to communicate between tabs and windows.\n * @default 'tanstack-query'\n */\n broadcastChannel?: string\n /** Options forwarded to the underlying `BroadcastChannel`. */\n options?: BroadcastChannelOptions\n /**\n * Called when a query event fails to broadcast to other tabs — most\n * commonly because the query's `state.data`, `state.error`, or `queryKey`\n * contains a value the structured-clone algorithm cannot serialize\n * (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).\n *\n * Provide this to route failures to an error tracker. If omitted, a\n * `console.warn` is emitted in development so failures are never silent.\n *\n * May return a `Promise`; any rejection is caught internally so it cannot\n * cause a secondary unhandled rejection.\n */\n onBroadcastError?: (\n error: unknown,\n event: BroadcastErrorEvent,\n ) => void | Promise<void>\n}\n\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n onBroadcastError,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n try {\n cb()\n } finally {\n // Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`\n // triggering a listener that throws while applying an incoming\n // cross-tab message). Without this, `transaction` would stay `true`\n // forever, silently disabling this tab's own broadcasts to other tabs\n // for the rest of the session.\n transaction = false\n }\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const safePost = (message: BroadcastMessage): void => {\n channel.postMessage(message).catch((error: unknown) => {\n const event: BroadcastErrorEvent = {\n type: message.type,\n queryHash: message.queryHash,\n queryKey: message.queryKey,\n }\n\n if (onBroadcastError) {\n const warnCallbackError = (callbackError: unknown) => {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] onBroadcastError threw while handling \"${event.type}\" for query ${event.queryHash}.`,\n callbackError,\n )\n }\n }\n let result: void | Promise<void>\n try {\n result = onBroadcastError(error, event)\n } catch (callbackError) {\n warnCallbackError(callbackError)\n return\n }\n result?.catch(warnCallbackError)\n } else if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] Failed to broadcast \"${event.type}\" event for query ${event.queryHash}. ` +\n 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',\n error,\n )\n }\n })\n }\n\n const unsubscribe = queryCache.subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state, observers },\n } = queryEvent\n\n if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {\n safePost({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n safePost({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n safePost({\n type: 'added',\n queryHash,\n queryKey,\n state,\n })\n }\n })\n\n channel.onmessage = (action) => {\n if (!action?.type) {\n return\n }\n\n tx(() => {\n const { type, queryHash, queryKey, state } = action\n\n const query = queryCache.get(queryHash)\n\n if (type === 'updated') {\n if (query) {\n query.setState(state)\n return\n }\n\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n } else if (type === 'removed') {\n if (query) {\n queryCache.remove(query)\n }\n } else if (type === 'added') {\n if (query) {\n if (query.state.data === undefined) {\n query.setState(state)\n }\n return\n }\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n }\n })\n }\n return () => {\n unsubscribe()\n channel.close()\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["BroadcastChannel"],"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient, QueryKey } from '@tanstack/query-core'\n\n/**\n * Metadata describing a broadcast that failed to be delivered to other tabs.\n * Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers\n * can correlate failures with the originating query.\n */\nexport interface BroadcastErrorEvent {\n /**\n * The kind of cache event that was being broadcast.\n */\n type: 'updated' | 'removed' | 'added'\n /**\n * The hash of the query the broadcast was about.\n */\n queryHash: string\n /**\n * The key of the query the broadcast was about.\n */\n queryKey: QueryKey\n}\n\ntype BroadcastMessage =\n | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown }\n | { type: 'removed'; queryHash: string; queryKey: QueryKey }\n | { type: 'added'; queryHash: string; queryKey: QueryKey; state: unknown }\n\ninterface BroadcastQueryClientOptions {\n /** The QueryClient to sync. */\n queryClient: QueryClient\n /**\n * Unique channel name used to communicate between tabs and windows.\n * @default 'tanstack-query'\n */\n broadcastChannel?: string\n /** Options forwarded to the underlying `BroadcastChannel`. */\n options?: BroadcastChannelOptions\n /**\n * Called when a query event fails to broadcast to other tabs — most\n * commonly because the query's `state.data`, `state.error`, or `queryKey`\n * contains a value the structured-clone algorithm cannot serialize\n * (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).\n *\n * Provide this to route failures to an error tracker. If omitted, a\n * `console.warn` is emitted in development so failures are never silent.\n *\n * May return a `Promise`; any rejection is caught internally so it cannot\n * cause a secondary unhandled rejection.\n */\n onBroadcastError?: (\n error: unknown,\n event: BroadcastErrorEvent,\n ) => void | Promise<void>\n}\n\n/**\n * Syncs a client's query cache with the caches of other tabs and windows through a\n * `BroadcastChannel`: added queries, successful updates, and removals of queries that have\n * observers are broadcast, and those received from other tabs are applied.\n * @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,\n * and the `onBroadcastError` callback.\n * @returns A function that stops syncing and closes the channel.\n */\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n onBroadcastError,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n try {\n cb()\n } finally {\n // Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`\n // triggering a listener that throws while applying an incoming\n // cross-tab message). Without this, `transaction` would stay `true`\n // forever, silently disabling this tab's own broadcasts to other tabs\n // for the rest of the session.\n transaction = false\n }\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const safePost = (message: BroadcastMessage): void => {\n channel.postMessage(message).catch((error: unknown) => {\n const event: BroadcastErrorEvent = {\n type: message.type,\n queryHash: message.queryHash,\n queryKey: message.queryKey,\n }\n\n if (onBroadcastError) {\n const warnCallbackError = (callbackError: unknown) => {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] onBroadcastError threw while handling \"${event.type}\" for query ${event.queryHash}.`,\n callbackError,\n )\n }\n }\n let result: void | Promise<void>\n try {\n result = onBroadcastError(error, event)\n } catch (callbackError) {\n warnCallbackError(callbackError)\n return\n }\n result?.catch(warnCallbackError)\n } else if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] Failed to broadcast \"${event.type}\" event for query ${event.queryHash}. ` +\n 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',\n error,\n )\n }\n })\n }\n\n const unsubscribe = queryCache.subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state, observers },\n } = queryEvent\n\n if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {\n safePost({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n safePost({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n safePost({\n type: 'added',\n queryHash,\n queryKey,\n state,\n })\n }\n })\n\n channel.onmessage = (action) => {\n if (!action?.type) {\n return\n }\n\n tx(() => {\n const { type, queryHash, queryKey, state } = action\n\n const query = queryCache.get(queryHash)\n\n if (type === 'updated') {\n if (query) {\n query.setState(state)\n return\n }\n\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n } else if (type === 'removed') {\n if (query) {\n queryCache.remove(query)\n }\n } else if (type === 'added') {\n if (query) {\n if (query.state.data === undefined) {\n query.setState(state)\n }\n return\n }\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n }\n })\n }\n return () => {\n unsubscribe()\n channel.close()\n }\n}\n"],"mappings":";;;;;;;;;;;AAiEA,SAAgB,qBAAqB,EACnC,aACA,mBAAmB,kBACnB,SACA,oBAC0C;CAC1C,IAAI,cAAc;CAClB,MAAM,MAAM,OAAmB;EAC7B,cAAc;EACd,IAAI;GACF,GAAG;EACL,UAAU;GAMR,cAAc;EAChB;CACF;CAEA,MAAM,UAAU,IAAIA,kBAAAA,iBAAiB,kBAAkB;EACrD,kBAAkB;EAClB,GAAG;CACL,CAAC;CAED,MAAM,aAAa,YAAY,cAAc;CAE7C,MAAM,YAAY,YAAoC;EACpD,QAAQ,YAAY,OAAO,CAAC,CAAC,OAAO,UAAmB;GACrD,MAAM,QAA6B;IACjC,MAAM,QAAQ;IACd,WAAW,QAAQ;IACnB,UAAU,QAAQ;GACpB;GAEA,IAAI,kBAAkB;IACpB,MAAM,qBAAqB,kBAA2B;KACpD,IAAI,QAAQ,IAAI,aAAa,cAC3B,QAAQ,KACN,iEAAiE,MAAM,KAAK,cAAc,MAAM,UAAU,IAC1G,aACF;IAEJ;IACA,IAAI;IACJ,IAAI;KACF,SAAS,iBAAiB,OAAO,KAAK;IACxC,SAAS,eAAe;KACtB,kBAAkB,aAAa;KAC/B;IACF;IACA,QAAQ,MAAM,iBAAiB;GACjC,OAAO,IAAI,QAAQ,IAAI,aAAa,cAClC,QAAQ,KACN,+CAA+C,MAAM,KAAK,oBAAoB,MAAM,UAAU,+FAE9F,KACF;EAEJ,CAAC;CACH;CAEA,MAAM,cAAc,WAAW,WAAW,eAAe;EACvD,IAAI,aACF;EAGF,MAAM,EACJ,OAAO,EAAE,WAAW,UAAU,OAAO,gBACnC;EAEJ,IAAI,WAAW,SAAS,aAAa,WAAW,OAAO,SAAS,WAC9D,SAAS;GACP,MAAM;GACN;GACA;GACA;EACF,CAAC;EAGH,IAAI,WAAW,SAAS,aAAa,UAAU,SAAS,GACtD,SAAS;GACP,MAAM;GACN;GACA;EACF,CAAC;EAGH,IAAI,WAAW,SAAS,SACtB,SAAS;GACP,MAAM;GACN;GACA;GACA;EACF,CAAC;CAEL,CAAC;CAED,QAAQ,aAAa,WAAW;EAC9B,IAAI,CAAC,QAAQ,MACX;EAGF,SAAS;GACP,MAAM,EAAE,MAAM,WAAW,UAAU,UAAU;GAE7C,MAAM,QAAQ,WAAW,IAAI,SAAS;GAEtC,IAAI,SAAS,WAAW;IACtB,IAAI,OAAO;KACT,MAAM,SAAS,KAAK;KACpB;IACF;IAEA,WAAW,MACT,aACA;KACE;KACA;IACF,GACA,KACF;GACF,OAAO,IAAI,SAAS,WACd;QAAA,OACF,WAAW,OAAO,KAAK;GAAA,OAEpB,IAAI,SAAS,SAAS;IAC3B,IAAI,OAAO;KACT,IAAI,MAAM,MAAM,SAAS,KAAA,GACvB,MAAM,SAAS,KAAK;KAEtB;IACF;IACA,WAAW,MACT,aACA;KACE;KACA;IACF,GACA,KACF;GACF;EACF,CAAC;CACH;CACA,aAAa;EACX,YAAY;EACZ,QAAQ,MAAM;CAChB;AACF"}
|
package/build/modern/index.d.cts
CHANGED
|
@@ -7,8 +7,17 @@ import { QueryClient, QueryKey } from "@tanstack/query-core";
|
|
|
7
7
|
* can correlate failures with the originating query.
|
|
8
8
|
*/
|
|
9
9
|
export interface BroadcastErrorEvent {
|
|
10
|
+
/**
|
|
11
|
+
* The kind of cache event that was being broadcast.
|
|
12
|
+
*/
|
|
10
13
|
type: 'updated' | 'removed' | 'added';
|
|
14
|
+
/**
|
|
15
|
+
* The hash of the query the broadcast was about.
|
|
16
|
+
*/
|
|
11
17
|
queryHash: string;
|
|
18
|
+
/**
|
|
19
|
+
* The key of the query the broadcast was about.
|
|
20
|
+
*/
|
|
12
21
|
queryKey: QueryKey;
|
|
13
22
|
}
|
|
14
23
|
interface BroadcastQueryClientOptions {
|
|
@@ -35,6 +44,14 @@ interface BroadcastQueryClientOptions {
|
|
|
35
44
|
*/
|
|
36
45
|
onBroadcastError?: (error: unknown, event: BroadcastErrorEvent) => void | Promise<void>;
|
|
37
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
49
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
50
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
51
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
52
|
+
* and the `onBroadcastError` callback.
|
|
53
|
+
* @returns A function that stops syncing and closes the channel.
|
|
54
|
+
*/
|
|
38
55
|
export declare function broadcastQueryClient({ queryClient, broadcastChannel, options, onBroadcastError }: BroadcastQueryClientOptions): () => void;
|
|
39
56
|
//#endregion
|
|
40
57
|
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;iBASiB
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;iBASiB;;;;EAIf;;;;EAIA;;;;EAIA,UAAU;;UAQF;;EAER,aAAa;;;;;EAKb;;EAEA,UAAU;;;;;;;;;;;;;EAaV,oBACE,gBACA,OAAO,+BACG;;;;;;;;;;wBAWE,uBACd,aACA,kBACA,SACA,oBACC"}
|
package/build/modern/index.d.ts
CHANGED
|
@@ -7,8 +7,17 @@ import { QueryClient, QueryKey } from "@tanstack/query-core";
|
|
|
7
7
|
* can correlate failures with the originating query.
|
|
8
8
|
*/
|
|
9
9
|
export interface BroadcastErrorEvent {
|
|
10
|
+
/**
|
|
11
|
+
* The kind of cache event that was being broadcast.
|
|
12
|
+
*/
|
|
10
13
|
type: 'updated' | 'removed' | 'added';
|
|
14
|
+
/**
|
|
15
|
+
* The hash of the query the broadcast was about.
|
|
16
|
+
*/
|
|
11
17
|
queryHash: string;
|
|
18
|
+
/**
|
|
19
|
+
* The key of the query the broadcast was about.
|
|
20
|
+
*/
|
|
12
21
|
queryKey: QueryKey;
|
|
13
22
|
}
|
|
14
23
|
interface BroadcastQueryClientOptions {
|
|
@@ -35,6 +44,14 @@ interface BroadcastQueryClientOptions {
|
|
|
35
44
|
*/
|
|
36
45
|
onBroadcastError?: (error: unknown, event: BroadcastErrorEvent) => void | Promise<void>;
|
|
37
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
49
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
50
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
51
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
52
|
+
* and the `onBroadcastError` callback.
|
|
53
|
+
* @returns A function that stops syncing and closes the channel.
|
|
54
|
+
*/
|
|
38
55
|
export declare function broadcastQueryClient({ queryClient, broadcastChannel, options, onBroadcastError }: BroadcastQueryClientOptions): () => void;
|
|
39
56
|
//#endregion
|
|
40
57
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;iBASiB
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;iBASiB;;;;EAIf;;;;EAIA;;;;EAIA,UAAU;;UAQF;;EAER,aAAa;;;;;EAKb;;EAEA,UAAU;;;;;;;;;;;;;EAaV,oBACE,gBACA,OAAO,+BACG;;;;;;;;;;wBAWE,uBACd,aACA,kBACA,SACA,oBACC"}
|
package/build/modern/index.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { BroadcastChannel } from "broadcast-channel";
|
|
2
2
|
//#region src/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
5
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
6
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
7
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
8
|
+
* and the `onBroadcastError` callback.
|
|
9
|
+
* @returns A function that stops syncing and closes the channel.
|
|
10
|
+
*/
|
|
3
11
|
function broadcastQueryClient({ queryClient, broadcastChannel = "tanstack-query", options, onBroadcastError }) {
|
|
4
12
|
let transaction = false;
|
|
5
13
|
const tx = (cb) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient, QueryKey } from '@tanstack/query-core'\n\n/**\n * Metadata describing a broadcast that failed to be delivered to other tabs.\n * Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers\n * can correlate failures with the originating query.\n */\nexport interface BroadcastErrorEvent {\n type: 'updated' | 'removed' | 'added'\n queryHash: string\n queryKey: QueryKey\n}\n\ntype BroadcastMessage =\n | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown }\n | { type: 'removed'; queryHash: string; queryKey: QueryKey }\n | { type: 'added'; queryHash: string; queryKey: QueryKey; state: unknown }\n\ninterface BroadcastQueryClientOptions {\n /** The QueryClient to sync. */\n queryClient: QueryClient\n /**\n * Unique channel name used to communicate between tabs and windows.\n * @default 'tanstack-query'\n */\n broadcastChannel?: string\n /** Options forwarded to the underlying `BroadcastChannel`. */\n options?: BroadcastChannelOptions\n /**\n * Called when a query event fails to broadcast to other tabs — most\n * commonly because the query's `state.data`, `state.error`, or `queryKey`\n * contains a value the structured-clone algorithm cannot serialize\n * (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).\n *\n * Provide this to route failures to an error tracker. If omitted, a\n * `console.warn` is emitted in development so failures are never silent.\n *\n * May return a `Promise`; any rejection is caught internally so it cannot\n * cause a secondary unhandled rejection.\n */\n onBroadcastError?: (\n error: unknown,\n event: BroadcastErrorEvent,\n ) => void | Promise<void>\n}\n\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n onBroadcastError,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n try {\n cb()\n } finally {\n // Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`\n // triggering a listener that throws while applying an incoming\n // cross-tab message). Without this, `transaction` would stay `true`\n // forever, silently disabling this tab's own broadcasts to other tabs\n // for the rest of the session.\n transaction = false\n }\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const safePost = (message: BroadcastMessage): void => {\n channel.postMessage(message).catch((error: unknown) => {\n const event: BroadcastErrorEvent = {\n type: message.type,\n queryHash: message.queryHash,\n queryKey: message.queryKey,\n }\n\n if (onBroadcastError) {\n const warnCallbackError = (callbackError: unknown) => {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] onBroadcastError threw while handling \"${event.type}\" for query ${event.queryHash}.`,\n callbackError,\n )\n }\n }\n let result: void | Promise<void>\n try {\n result = onBroadcastError(error, event)\n } catch (callbackError) {\n warnCallbackError(callbackError)\n return\n }\n result?.catch(warnCallbackError)\n } else if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] Failed to broadcast \"${event.type}\" event for query ${event.queryHash}. ` +\n 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',\n error,\n )\n }\n })\n }\n\n const unsubscribe = queryCache.subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state, observers },\n } = queryEvent\n\n if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {\n safePost({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n safePost({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n safePost({\n type: 'added',\n queryHash,\n queryKey,\n state,\n })\n }\n })\n\n channel.onmessage = (action) => {\n if (!action?.type) {\n return\n }\n\n tx(() => {\n const { type, queryHash, queryKey, state } = action\n\n const query = queryCache.get(queryHash)\n\n if (type === 'updated') {\n if (query) {\n query.setState(state)\n return\n }\n\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n } else if (type === 'removed') {\n if (query) {\n queryCache.remove(query)\n }\n } else if (type === 'added') {\n if (query) {\n if (query.state.data === undefined) {\n query.setState(state)\n }\n return\n }\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n }\n })\n }\n return () => {\n unsubscribe()\n channel.close()\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient, QueryKey } from '@tanstack/query-core'\n\n/**\n * Metadata describing a broadcast that failed to be delivered to other tabs.\n * Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers\n * can correlate failures with the originating query.\n */\nexport interface BroadcastErrorEvent {\n /**\n * The kind of cache event that was being broadcast.\n */\n type: 'updated' | 'removed' | 'added'\n /**\n * The hash of the query the broadcast was about.\n */\n queryHash: string\n /**\n * The key of the query the broadcast was about.\n */\n queryKey: QueryKey\n}\n\ntype BroadcastMessage =\n | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown }\n | { type: 'removed'; queryHash: string; queryKey: QueryKey }\n | { type: 'added'; queryHash: string; queryKey: QueryKey; state: unknown }\n\ninterface BroadcastQueryClientOptions {\n /** The QueryClient to sync. */\n queryClient: QueryClient\n /**\n * Unique channel name used to communicate between tabs and windows.\n * @default 'tanstack-query'\n */\n broadcastChannel?: string\n /** Options forwarded to the underlying `BroadcastChannel`. */\n options?: BroadcastChannelOptions\n /**\n * Called when a query event fails to broadcast to other tabs — most\n * commonly because the query's `state.data`, `state.error`, or `queryKey`\n * contains a value the structured-clone algorithm cannot serialize\n * (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).\n *\n * Provide this to route failures to an error tracker. If omitted, a\n * `console.warn` is emitted in development so failures are never silent.\n *\n * May return a `Promise`; any rejection is caught internally so it cannot\n * cause a secondary unhandled rejection.\n */\n onBroadcastError?: (\n error: unknown,\n event: BroadcastErrorEvent,\n ) => void | Promise<void>\n}\n\n/**\n * Syncs a client's query cache with the caches of other tabs and windows through a\n * `BroadcastChannel`: added queries, successful updates, and removals of queries that have\n * observers are broadcast, and those received from other tabs are applied.\n * @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,\n * and the `onBroadcastError` callback.\n * @returns A function that stops syncing and closes the channel.\n */\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n onBroadcastError,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n try {\n cb()\n } finally {\n // Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`\n // triggering a listener that throws while applying an incoming\n // cross-tab message). Without this, `transaction` would stay `true`\n // forever, silently disabling this tab's own broadcasts to other tabs\n // for the rest of the session.\n transaction = false\n }\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const safePost = (message: BroadcastMessage): void => {\n channel.postMessage(message).catch((error: unknown) => {\n const event: BroadcastErrorEvent = {\n type: message.type,\n queryHash: message.queryHash,\n queryKey: message.queryKey,\n }\n\n if (onBroadcastError) {\n const warnCallbackError = (callbackError: unknown) => {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] onBroadcastError threw while handling \"${event.type}\" for query ${event.queryHash}.`,\n callbackError,\n )\n }\n }\n let result: void | Promise<void>\n try {\n result = onBroadcastError(error, event)\n } catch (callbackError) {\n warnCallbackError(callbackError)\n return\n }\n result?.catch(warnCallbackError)\n } else if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `[broadcastQueryClient] Failed to broadcast \"${event.type}\" event for query ${event.queryHash}. ` +\n 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',\n error,\n )\n }\n })\n }\n\n const unsubscribe = queryCache.subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state, observers },\n } = queryEvent\n\n if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {\n safePost({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n safePost({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n safePost({\n type: 'added',\n queryHash,\n queryKey,\n state,\n })\n }\n })\n\n channel.onmessage = (action) => {\n if (!action?.type) {\n return\n }\n\n tx(() => {\n const { type, queryHash, queryKey, state } = action\n\n const query = queryCache.get(queryHash)\n\n if (type === 'updated') {\n if (query) {\n query.setState(state)\n return\n }\n\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n } else if (type === 'removed') {\n if (query) {\n queryCache.remove(query)\n }\n } else if (type === 'added') {\n if (query) {\n if (query.state.data === undefined) {\n query.setState(state)\n }\n return\n }\n queryCache.build(\n queryClient,\n {\n queryKey,\n queryHash,\n },\n state,\n )\n }\n })\n }\n return () => {\n unsubscribe()\n channel.close()\n }\n}\n"],"mappings":";;;;;;;;;;AAiEA,SAAgB,qBAAqB,EACnC,aACA,mBAAmB,kBACnB,SACA,oBAC0C;CAC1C,IAAI,cAAc;CAClB,MAAM,MAAM,OAAmB;EAC7B,cAAc;EACd,IAAI;GACF,GAAG;EACL,UAAU;GAMR,cAAc;EAChB;CACF;CAEA,MAAM,UAAU,IAAI,iBAAiB,kBAAkB;EACrD,kBAAkB;EAClB,GAAG;CACL,CAAC;CAED,MAAM,aAAa,YAAY,cAAc;CAE7C,MAAM,YAAY,YAAoC;EACpD,QAAQ,YAAY,OAAO,CAAC,CAAC,OAAO,UAAmB;GACrD,MAAM,QAA6B;IACjC,MAAM,QAAQ;IACd,WAAW,QAAQ;IACnB,UAAU,QAAQ;GACpB;GAEA,IAAI,kBAAkB;IACpB,MAAM,qBAAqB,kBAA2B;KACpD,IAAI,QAAQ,IAAI,aAAa,cAC3B,QAAQ,KACN,iEAAiE,MAAM,KAAK,cAAc,MAAM,UAAU,IAC1G,aACF;IAEJ;IACA,IAAI;IACJ,IAAI;KACF,SAAS,iBAAiB,OAAO,KAAK;IACxC,SAAS,eAAe;KACtB,kBAAkB,aAAa;KAC/B;IACF;IACA,QAAQ,MAAM,iBAAiB;GACjC,OAAO,IAAI,QAAQ,IAAI,aAAa,cAClC,QAAQ,KACN,+CAA+C,MAAM,KAAK,oBAAoB,MAAM,UAAU,+FAE9F,KACF;EAEJ,CAAC;CACH;CAEA,MAAM,cAAc,WAAW,WAAW,eAAe;EACvD,IAAI,aACF;EAGF,MAAM,EACJ,OAAO,EAAE,WAAW,UAAU,OAAO,gBACnC;EAEJ,IAAI,WAAW,SAAS,aAAa,WAAW,OAAO,SAAS,WAC9D,SAAS;GACP,MAAM;GACN;GACA;GACA;EACF,CAAC;EAGH,IAAI,WAAW,SAAS,aAAa,UAAU,SAAS,GACtD,SAAS;GACP,MAAM;GACN;GACA;EACF,CAAC;EAGH,IAAI,WAAW,SAAS,SACtB,SAAS;GACP,MAAM;GACN;GACA;GACA;EACF,CAAC;CAEL,CAAC;CAED,QAAQ,aAAa,WAAW;EAC9B,IAAI,CAAC,QAAQ,MACX;EAGF,SAAS;GACP,MAAM,EAAE,MAAM,WAAW,UAAU,UAAU;GAE7C,MAAM,QAAQ,WAAW,IAAI,SAAS;GAEtC,IAAI,SAAS,WAAW;IACtB,IAAI,OAAO;KACT,MAAM,SAAS,KAAK;KACpB;IACF;IAEA,WAAW,MACT,aACA;KACE;KACA;IACF,GACA,KACF;GACF,OAAO,IAAI,SAAS,WACd;QAAA,OACF,WAAW,OAAO,KAAK;GAAA,OAEpB,IAAI,SAAS,SAAS;IAC3B,IAAI,OAAO;KACT,IAAI,MAAM,MAAM,SAAS,KAAA,GACvB,MAAM,SAAS,KAAK;KAEtB;IACF;IACA,WAAW,MACT,aACA;KACE;KACA;IACF,GACA,KACF;GACF;EACF,CAAC;CACH;CACA,aAAa;EACX,YAAY;EACZ,QAAQ,MAAM;CAChB;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-broadcast-client-experimental",
|
|
3
|
-
"version": "5.104.
|
|
3
|
+
"version": "5.104.2",
|
|
4
4
|
"description": "An experimental plugin to for broadcasting the state of your queryClient between browser tabs/windows",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,26 +39,26 @@
|
|
|
39
39
|
"!src/__tests__"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@tanstack/query-core": "5.104.
|
|
42
|
+
"@tanstack/query-core": "5.104.2",
|
|
43
43
|
"broadcast-channel": "^7.4.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@tanstack/query-test-utils": "0.0.0",
|
|
47
47
|
"@testing-library/react": "^16.1.0",
|
|
48
48
|
"@vitejs/plugin-react": "^6.1.1",
|
|
49
|
-
"
|
|
49
|
+
"typescript": "6.0.3"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"clean": "premove ./build ./coverage ./dist-ts ./.cache/test-types",
|
|
53
53
|
"compile": "tsc --project tsconfig.json",
|
|
54
|
-
"test:
|
|
54
|
+
"test:oxlint": "oxlint ./src",
|
|
55
55
|
"test:types": "npm-run-all --serial test:types:*",
|
|
56
|
-
"test:types:
|
|
57
|
-
"test:types:
|
|
58
|
-
"test:types:
|
|
59
|
-
"test:types:ts59": "node ../../node_modules/typescript59/lib/tsc.js --project tsconfig.json --outDir .cache/test-types/ts59 --tsBuildInfoFile .cache/test-types/ts59.tsbuildinfo",
|
|
56
|
+
"test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts57 --tsBuildInfoFile .cache/test-types/ts57.tsbuildinfo",
|
|
57
|
+
"test:types:ts58": "node ../../node_modules/typescript58/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts58 --tsBuildInfoFile .cache/test-types/ts58.tsbuildinfo",
|
|
58
|
+
"test:types:ts59": "node ../../node_modules/typescript59/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts59 --tsBuildInfoFile .cache/test-types/ts59.tsbuildinfo",
|
|
60
59
|
"test:types:tscurrent": "tsc --project tsconfig.json --outDir .cache/test-types/tscurrent --tsBuildInfoFile .cache/test-types/tscurrent.tsbuildinfo",
|
|
61
|
-
"test:types:ts70": "node ../../node_modules/typescript70/lib/tsc.js --project tsconfig.json --outDir .cache/test-types/ts70 --tsBuildInfoFile .cache/test-types/ts70.tsbuildinfo",
|
|
60
|
+
"test:types:ts70": "node ../../node_modules/typescript70/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts70 --tsBuildInfoFile .cache/test-types/ts70.tsbuildinfo",
|
|
61
|
+
"test:types:ts71": "node ../../node_modules/typescript71/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts71 --tsBuildInfoFile .cache/test-types/ts71.tsbuildinfo",
|
|
62
62
|
"test:lib": "vitest",
|
|
63
63
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
64
64
|
"test:build": "publint --strict && attw --pack",
|
package/src/index.ts
CHANGED
|
@@ -8,8 +8,17 @@ import type { QueryClient, QueryKey } from '@tanstack/query-core'
|
|
|
8
8
|
* can correlate failures with the originating query.
|
|
9
9
|
*/
|
|
10
10
|
export interface BroadcastErrorEvent {
|
|
11
|
+
/**
|
|
12
|
+
* The kind of cache event that was being broadcast.
|
|
13
|
+
*/
|
|
11
14
|
type: 'updated' | 'removed' | 'added'
|
|
15
|
+
/**
|
|
16
|
+
* The hash of the query the broadcast was about.
|
|
17
|
+
*/
|
|
12
18
|
queryHash: string
|
|
19
|
+
/**
|
|
20
|
+
* The key of the query the broadcast was about.
|
|
21
|
+
*/
|
|
13
22
|
queryKey: QueryKey
|
|
14
23
|
}
|
|
15
24
|
|
|
@@ -46,6 +55,14 @@ interface BroadcastQueryClientOptions {
|
|
|
46
55
|
) => void | Promise<void>
|
|
47
56
|
}
|
|
48
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Syncs a client's query cache with the caches of other tabs and windows through a
|
|
60
|
+
* `BroadcastChannel`: added queries, successful updates, and removals of queries that have
|
|
61
|
+
* observers are broadcast, and those received from other tabs are applied.
|
|
62
|
+
* @param options - The `queryClient` to sync, the `broadcastChannel` name, the channel `options`,
|
|
63
|
+
* and the `onBroadcastError` callback.
|
|
64
|
+
* @returns A function that stops syncing and closes the channel.
|
|
65
|
+
*/
|
|
49
66
|
export function broadcastQueryClient({
|
|
50
67
|
queryClient,
|
|
51
68
|
broadcastChannel = 'tanstack-query',
|