@tanstack/query-broadcast-client-experimental 5.101.4 → 5.102.0
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 +96 -120
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +41 -1
- package/build/legacy/index.d.cts.map +1 -0
- package/build/legacy/index.d.ts +41 -1
- package/build/legacy/index.d.ts.map +1 -0
- package/build/legacy/index.js +94 -94
- package/build/legacy/index.js.map +1 -1
- package/build/modern/index.cjs +96 -120
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +41 -1
- package/build/modern/index.d.cts.map +1 -0
- package/build/modern/index.d.ts +41 -1
- package/build/modern/index.d.ts.map +1 -0
- package/build/modern/index.js +94 -94
- package/build/modern/index.js.map +1 -1
- package/package.json +6 -7
- package/src/index.ts +90 -7
- package/build/legacy/_tsup-dts-rollup.d.cts +0 -12
- package/build/legacy/_tsup-dts-rollup.d.ts +0 -12
- package/build/modern/_tsup-dts-rollup.d.cts +0 -12
- package/build/modern/_tsup-dts-rollup.d.ts +0 -12
package/build/modern/index.cjs
CHANGED
|
@@ -1,122 +1,98 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (query) {
|
|
95
|
-
queryCache.remove(query);
|
|
96
|
-
}
|
|
97
|
-
} else if (type === "added") {
|
|
98
|
-
if (query) {
|
|
99
|
-
query.setState(state);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
queryCache.build(
|
|
103
|
-
queryClient,
|
|
104
|
-
{
|
|
105
|
-
queryKey,
|
|
106
|
-
queryHash
|
|
107
|
-
},
|
|
108
|
-
state
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
};
|
|
113
|
-
return () => {
|
|
114
|
-
unsubscribe();
|
|
115
|
-
channel.close();
|
|
116
|
-
};
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let broadcast_channel = require("broadcast-channel");
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
function broadcastQueryClient({ queryClient, broadcastChannel = "tanstack-query", options, onBroadcastError }) {
|
|
5
|
+
let transaction = false;
|
|
6
|
+
const tx = (cb) => {
|
|
7
|
+
transaction = true;
|
|
8
|
+
try {
|
|
9
|
+
cb();
|
|
10
|
+
} finally {
|
|
11
|
+
transaction = false;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const channel = new broadcast_channel.BroadcastChannel(broadcastChannel, {
|
|
15
|
+
webWorkerSupport: false,
|
|
16
|
+
...options
|
|
17
|
+
});
|
|
18
|
+
const queryCache = queryClient.getQueryCache();
|
|
19
|
+
const safePost = (message) => {
|
|
20
|
+
channel.postMessage(message).catch((error) => {
|
|
21
|
+
const event = {
|
|
22
|
+
type: message.type,
|
|
23
|
+
queryHash: message.queryHash,
|
|
24
|
+
queryKey: message.queryKey
|
|
25
|
+
};
|
|
26
|
+
if (onBroadcastError) {
|
|
27
|
+
const warnCallbackError = (callbackError) => {
|
|
28
|
+
if (process.env.NODE_ENV !== "production") console.warn(`[broadcastQueryClient] onBroadcastError threw while handling "${event.type}" for query ${event.queryHash}.`, callbackError);
|
|
29
|
+
};
|
|
30
|
+
let result;
|
|
31
|
+
try {
|
|
32
|
+
result = onBroadcastError(error, event);
|
|
33
|
+
} catch (callbackError) {
|
|
34
|
+
warnCallbackError(callbackError);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
result?.catch(warnCallbackError);
|
|
38
|
+
} else if (process.env.NODE_ENV !== "production") console.warn(`[broadcastQueryClient] Failed to broadcast "${event.type}" event for query ${event.queryHash}. The query value could not be structured-cloned; cross-tab sync for this query was skipped.`, error);
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
const unsubscribe = queryCache.subscribe((queryEvent) => {
|
|
42
|
+
if (transaction) return;
|
|
43
|
+
const { query: { queryHash, queryKey, state, observers } } = queryEvent;
|
|
44
|
+
if (queryEvent.type === "updated" && queryEvent.action.type === "success") safePost({
|
|
45
|
+
type: "updated",
|
|
46
|
+
queryHash,
|
|
47
|
+
queryKey,
|
|
48
|
+
state
|
|
49
|
+
});
|
|
50
|
+
if (queryEvent.type === "removed" && observers.length > 0) safePost({
|
|
51
|
+
type: "removed",
|
|
52
|
+
queryHash,
|
|
53
|
+
queryKey
|
|
54
|
+
});
|
|
55
|
+
if (queryEvent.type === "added") safePost({
|
|
56
|
+
type: "added",
|
|
57
|
+
queryHash,
|
|
58
|
+
queryKey,
|
|
59
|
+
state
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
channel.onmessage = (action) => {
|
|
63
|
+
if (!action?.type) return;
|
|
64
|
+
tx(() => {
|
|
65
|
+
const { type, queryHash, queryKey, state } = action;
|
|
66
|
+
const query = queryCache.get(queryHash);
|
|
67
|
+
if (type === "updated") {
|
|
68
|
+
if (query) {
|
|
69
|
+
query.setState(state);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
queryCache.build(queryClient, {
|
|
73
|
+
queryKey,
|
|
74
|
+
queryHash
|
|
75
|
+
}, state);
|
|
76
|
+
} else if (type === "removed") {
|
|
77
|
+
if (query) queryCache.remove(query);
|
|
78
|
+
} else if (type === "added") {
|
|
79
|
+
if (query) {
|
|
80
|
+
query.setState(state);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
queryCache.build(queryClient, {
|
|
84
|
+
queryKey,
|
|
85
|
+
queryHash
|
|
86
|
+
}, state);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
return () => {
|
|
91
|
+
unsubscribe();
|
|
92
|
+
channel.close();
|
|
93
|
+
};
|
|
117
94
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
});
|
|
95
|
+
//#endregion
|
|
96
|
+
exports.broadcastQueryClient = broadcastQueryClient;
|
|
97
|
+
|
|
122
98
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient } from '@tanstack/query-core'\n\ninterface BroadcastQueryClientOptions {\n queryClient: QueryClient\n broadcastChannel?: string\n options?: BroadcastChannelOptions\n}\n\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n cb()\n transaction = false\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const
|
|
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 query.setState(state)\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":";;;AAgDA,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,MAAM,SAAS,KAAK;KACpB;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
|
@@ -1 +1,41 @@
|
|
|
1
|
-
|
|
1
|
+
import { BroadcastChannelOptions } from "broadcast-channel";
|
|
2
|
+
import { QueryClient, QueryKey } from "@tanstack/query-core";
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Metadata describing a broadcast that failed to be delivered to other tabs.
|
|
6
|
+
* Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers
|
|
7
|
+
* can correlate failures with the originating query.
|
|
8
|
+
*/
|
|
9
|
+
interface BroadcastErrorEvent {
|
|
10
|
+
type: 'updated' | 'removed' | 'added';
|
|
11
|
+
queryHash: string;
|
|
12
|
+
queryKey: QueryKey;
|
|
13
|
+
}
|
|
14
|
+
interface BroadcastQueryClientOptions {
|
|
15
|
+
/** The QueryClient to sync. */
|
|
16
|
+
queryClient: QueryClient;
|
|
17
|
+
/**
|
|
18
|
+
* Unique channel name used to communicate between tabs and windows.
|
|
19
|
+
* @default 'tanstack-query'
|
|
20
|
+
*/
|
|
21
|
+
broadcastChannel?: string;
|
|
22
|
+
/** Options forwarded to the underlying `BroadcastChannel`. */
|
|
23
|
+
options?: BroadcastChannelOptions;
|
|
24
|
+
/**
|
|
25
|
+
* Called when a query event fails to broadcast to other tabs — most
|
|
26
|
+
* commonly because the query's `state.data`, `state.error`, or `queryKey`
|
|
27
|
+
* contains a value the structured-clone algorithm cannot serialize
|
|
28
|
+
* (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).
|
|
29
|
+
*
|
|
30
|
+
* Provide this to route failures to an error tracker. If omitted, a
|
|
31
|
+
* `console.warn` is emitted in development so failures are never silent.
|
|
32
|
+
*
|
|
33
|
+
* May return a `Promise`; any rejection is caught internally so it cannot
|
|
34
|
+
* cause a secondary unhandled rejection.
|
|
35
|
+
*/
|
|
36
|
+
onBroadcastError?: (error: unknown, event: BroadcastErrorEvent) => void | Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
declare function broadcastQueryClient({ queryClient, broadcastChannel, options, onBroadcastError }: BroadcastQueryClientOptions): () => void;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { BroadcastErrorEvent, broadcastQueryClient };
|
|
41
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;UASiB;EACf;EACA;EACA,UAAU;;UAQF;;EAER,aAAa;;;;;EAKb;;EAEA,UAAU;;;;;;;;;;;;;EAaV,oBACE,gBACA,OAAO,+BACG;;iBAGE,uBACd,aACA,kBACA,SACA,oBACC"}
|
package/build/modern/index.d.ts
CHANGED
|
@@ -1 +1,41 @@
|
|
|
1
|
-
|
|
1
|
+
import { BroadcastChannelOptions } from "broadcast-channel";
|
|
2
|
+
import { QueryClient, QueryKey } from "@tanstack/query-core";
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Metadata describing a broadcast that failed to be delivered to other tabs.
|
|
6
|
+
* Passed to {@link BroadcastQueryClientOptions.onBroadcastError} so callers
|
|
7
|
+
* can correlate failures with the originating query.
|
|
8
|
+
*/
|
|
9
|
+
interface BroadcastErrorEvent {
|
|
10
|
+
type: 'updated' | 'removed' | 'added';
|
|
11
|
+
queryHash: string;
|
|
12
|
+
queryKey: QueryKey;
|
|
13
|
+
}
|
|
14
|
+
interface BroadcastQueryClientOptions {
|
|
15
|
+
/** The QueryClient to sync. */
|
|
16
|
+
queryClient: QueryClient;
|
|
17
|
+
/**
|
|
18
|
+
* Unique channel name used to communicate between tabs and windows.
|
|
19
|
+
* @default 'tanstack-query'
|
|
20
|
+
*/
|
|
21
|
+
broadcastChannel?: string;
|
|
22
|
+
/** Options forwarded to the underlying `BroadcastChannel`. */
|
|
23
|
+
options?: BroadcastChannelOptions;
|
|
24
|
+
/**
|
|
25
|
+
* Called when a query event fails to broadcast to other tabs — most
|
|
26
|
+
* commonly because the query's `state.data`, `state.error`, or `queryKey`
|
|
27
|
+
* contains a value the structured-clone algorithm cannot serialize
|
|
28
|
+
* (e.g. `ReadableStream`, `File`, functions, Vue `reactive` proxies).
|
|
29
|
+
*
|
|
30
|
+
* Provide this to route failures to an error tracker. If omitted, a
|
|
31
|
+
* `console.warn` is emitted in development so failures are never silent.
|
|
32
|
+
*
|
|
33
|
+
* May return a `Promise`; any rejection is caught internally so it cannot
|
|
34
|
+
* cause a secondary unhandled rejection.
|
|
35
|
+
*/
|
|
36
|
+
onBroadcastError?: (error: unknown, event: BroadcastErrorEvent) => void | Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
declare function broadcastQueryClient({ queryClient, broadcastChannel, options, onBroadcastError }: BroadcastQueryClientOptions): () => void;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { BroadcastErrorEvent, broadcastQueryClient };
|
|
41
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;UASiB;EACf;EACA;EACA,UAAU;;UAQF;;EAER,aAAa;;;;;EAKb;;EAEA,UAAU;;;;;;;;;;;;;EAaV,oBACE,gBACA,OAAO,+BACG;;iBAGE,uBACd,aACA,kBACA,SACA,oBACC"}
|
package/build/modern/index.js
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
1
|
import { BroadcastChannel } from "broadcast-channel";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
function broadcastQueryClient({ queryClient, broadcastChannel = "tanstack-query", options, onBroadcastError }) {
|
|
4
|
+
let transaction = false;
|
|
5
|
+
const tx = (cb) => {
|
|
6
|
+
transaction = true;
|
|
7
|
+
try {
|
|
8
|
+
cb();
|
|
9
|
+
} finally {
|
|
10
|
+
transaction = false;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const channel = new BroadcastChannel(broadcastChannel, {
|
|
14
|
+
webWorkerSupport: false,
|
|
15
|
+
...options
|
|
16
|
+
});
|
|
17
|
+
const queryCache = queryClient.getQueryCache();
|
|
18
|
+
const safePost = (message) => {
|
|
19
|
+
channel.postMessage(message).catch((error) => {
|
|
20
|
+
const event = {
|
|
21
|
+
type: message.type,
|
|
22
|
+
queryHash: message.queryHash,
|
|
23
|
+
queryKey: message.queryKey
|
|
24
|
+
};
|
|
25
|
+
if (onBroadcastError) {
|
|
26
|
+
const warnCallbackError = (callbackError) => {
|
|
27
|
+
if (process.env.NODE_ENV !== "production") console.warn(`[broadcastQueryClient] onBroadcastError threw while handling "${event.type}" for query ${event.queryHash}.`, callbackError);
|
|
28
|
+
};
|
|
29
|
+
let result;
|
|
30
|
+
try {
|
|
31
|
+
result = onBroadcastError(error, event);
|
|
32
|
+
} catch (callbackError) {
|
|
33
|
+
warnCallbackError(callbackError);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
result?.catch(warnCallbackError);
|
|
37
|
+
} else if (process.env.NODE_ENV !== "production") console.warn(`[broadcastQueryClient] Failed to broadcast "${event.type}" event for query ${event.queryHash}. The query value could not be structured-cloned; cross-tab sync for this query was skipped.`, error);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const unsubscribe = queryCache.subscribe((queryEvent) => {
|
|
41
|
+
if (transaction) return;
|
|
42
|
+
const { query: { queryHash, queryKey, state, observers } } = queryEvent;
|
|
43
|
+
if (queryEvent.type === "updated" && queryEvent.action.type === "success") safePost({
|
|
44
|
+
type: "updated",
|
|
45
|
+
queryHash,
|
|
46
|
+
queryKey,
|
|
47
|
+
state
|
|
48
|
+
});
|
|
49
|
+
if (queryEvent.type === "removed" && observers.length > 0) safePost({
|
|
50
|
+
type: "removed",
|
|
51
|
+
queryHash,
|
|
52
|
+
queryKey
|
|
53
|
+
});
|
|
54
|
+
if (queryEvent.type === "added") safePost({
|
|
55
|
+
type: "added",
|
|
56
|
+
queryHash,
|
|
57
|
+
queryKey,
|
|
58
|
+
state
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
channel.onmessage = (action) => {
|
|
62
|
+
if (!action?.type) return;
|
|
63
|
+
tx(() => {
|
|
64
|
+
const { type, queryHash, queryKey, state } = action;
|
|
65
|
+
const query = queryCache.get(queryHash);
|
|
66
|
+
if (type === "updated") {
|
|
67
|
+
if (query) {
|
|
68
|
+
query.setState(state);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
queryCache.build(queryClient, {
|
|
72
|
+
queryKey,
|
|
73
|
+
queryHash
|
|
74
|
+
}, state);
|
|
75
|
+
} else if (type === "removed") {
|
|
76
|
+
if (query) queryCache.remove(query);
|
|
77
|
+
} else if (type === "added") {
|
|
78
|
+
if (query) {
|
|
79
|
+
query.setState(state);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
queryCache.build(queryClient, {
|
|
83
|
+
queryKey,
|
|
84
|
+
queryHash
|
|
85
|
+
}, state);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
return () => {
|
|
90
|
+
unsubscribe();
|
|
91
|
+
channel.close();
|
|
92
|
+
};
|
|
93
93
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
//#endregion
|
|
95
|
+
export { broadcastQueryClient };
|
|
96
|
+
|
|
97
97
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { BroadcastChannelOptions } from 'broadcast-channel'\nimport type { QueryClient } from '@tanstack/query-core'\n\ninterface BroadcastQueryClientOptions {\n queryClient: QueryClient\n broadcastChannel?: string\n options?: BroadcastChannelOptions\n}\n\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n options,\n}: BroadcastQueryClientOptions): () => void {\n let transaction = false\n const tx = (cb: () => void) => {\n transaction = true\n cb()\n transaction = false\n }\n\n const channel = new BroadcastChannel(broadcastChannel, {\n webWorkerSupport: false,\n ...options,\n })\n\n const queryCache = queryClient.getQueryCache()\n\n const
|
|
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 query.setState(state)\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":";;AAgDA,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,MAAM,SAAS,KAAK;KACpB;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.
|
|
3
|
+
"version": "5.102.0",
|
|
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",
|
|
@@ -40,29 +40,28 @@
|
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"broadcast-channel": "^7.0.0",
|
|
43
|
-
"@tanstack/query-core": "5.
|
|
43
|
+
"@tanstack/query-core": "5.102.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@testing-library/react": "^16.1.0",
|
|
47
47
|
"@vitejs/plugin-react": "^4.3.4",
|
|
48
|
-
"npm-run-all2": "^5.0.0"
|
|
48
|
+
"npm-run-all2": "^5.0.0",
|
|
49
|
+
"@tanstack/query-test-utils": "0.0.0"
|
|
49
50
|
},
|
|
50
51
|
"scripts": {
|
|
51
52
|
"clean": "premove ./build ./coverage ./dist-ts",
|
|
52
53
|
"compile": "tsc --build",
|
|
53
54
|
"test:eslint": "eslint --concurrency=auto ./src",
|
|
54
55
|
"test:types": "npm-run-all --serial test:types:*",
|
|
55
|
-
"test:types:ts54": "node ../../node_modules/typescript54/lib/tsc.js --build",
|
|
56
|
-
"test:types:ts55": "node ../../node_modules/typescript55/lib/tsc.js --build",
|
|
57
56
|
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js --build",
|
|
58
57
|
"test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js --build",
|
|
59
58
|
"test:types:ts58": "node ../../node_modules/typescript58/lib/tsc.js --build",
|
|
60
59
|
"test:types:ts59": "node ../../node_modules/typescript59/lib/tsc.js --build",
|
|
61
60
|
"test:types:tscurrent": "tsc --build",
|
|
62
|
-
"test:types:
|
|
61
|
+
"test:types:ts70": "node ../../node_modules/typescript70/lib/tsc.js --build",
|
|
63
62
|
"test:lib": "vitest",
|
|
64
63
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
65
64
|
"test:build": "publint --strict && attw --pack",
|
|
66
|
-
"build": "
|
|
65
|
+
"build": "tsdown --tsconfig tsconfig.prod.json"
|
|
67
66
|
}
|
|
68
67
|
}
|