@tanstack/query-broadcast-client-experimental 5.101.3 → 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.
@@ -1,122 +1,98 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- broadcastQueryClient: () => broadcastQueryClient
24
- });
25
- module.exports = __toCommonJS(index_exports);
26
- var import_broadcast_channel = require("broadcast-channel");
27
- function broadcastQueryClient({
28
- queryClient,
29
- broadcastChannel = "tanstack-query",
30
- options
31
- }) {
32
- let transaction = false;
33
- const tx = (cb) => {
34
- transaction = true;
35
- cb();
36
- transaction = false;
37
- };
38
- const channel = new import_broadcast_channel.BroadcastChannel(broadcastChannel, {
39
- webWorkerSupport: false,
40
- ...options
41
- });
42
- const queryCache = queryClient.getQueryCache();
43
- const unsubscribe = queryClient.getQueryCache().subscribe((queryEvent) => {
44
- if (transaction) {
45
- return;
46
- }
47
- const {
48
- query: { queryHash, queryKey, state, observers }
49
- } = queryEvent;
50
- if (queryEvent.type === "updated" && queryEvent.action.type === "success") {
51
- channel.postMessage({
52
- type: "updated",
53
- queryHash,
54
- queryKey,
55
- state
56
- });
57
- }
58
- if (queryEvent.type === "removed" && observers.length > 0) {
59
- channel.postMessage({
60
- type: "removed",
61
- queryHash,
62
- queryKey
63
- });
64
- }
65
- if (queryEvent.type === "added") {
66
- channel.postMessage({
67
- type: "added",
68
- queryHash,
69
- queryKey
70
- });
71
- }
72
- });
73
- channel.onmessage = (action) => {
74
- if (!(action == null ? void 0 : action.type)) {
75
- return;
76
- }
77
- tx(() => {
78
- const { type, queryHash, queryKey, state } = action;
79
- const query = queryCache.get(queryHash);
80
- if (type === "updated") {
81
- if (query) {
82
- query.setState(state);
83
- return;
84
- }
85
- queryCache.build(
86
- queryClient,
87
- {
88
- queryKey,
89
- queryHash
90
- },
91
- state
92
- );
93
- } else if (type === "removed") {
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 === null || result === void 0 || 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 === null || action === void 0 ? void 0 : 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
- // Annotate the CommonJS export names for ESM import in node:
119
- 0 && (module.exports = {
120
- broadcastQueryClient
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 unsubscribe = queryClient.getQueryCache().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 channel.postMessage({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n channel.postMessage({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n channel.postMessage({\n type: 'added',\n queryHash,\n queryKey,\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"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ACF;AAEA,mBAAW;AAAA,UACT;AAAA,UACA;AAAA,YACE;AAAA,YACA;AAAA,UACF;AAAA,UACA;AAAA,QACF;AAAA,MACF,WAAW,SAAS,WAAW;AAC7B,YAAI,OAAO;AACT,qBAAW,OAAO,KAAK;AAAA,QACzB;AAAA,MACF,WAAW,SAAS,SAAS;AAC3B,YAAI,OAAO;AACT,gBAAM,SAAS,KAAK;AACpB;AAAA,QACF;AACA,mBAAW;AAAA,UACT;AAAA,UACA;AAAA,YACE;AAAA,YACA;AAAA,UACF;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO,MAAM;AACX,gBAAY;AACZ,YAAQ,MAAM;AAAA,EAChB;AACF;","names":[]}
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,a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M;CAChB;AACF"}
@@ -1 +1,41 @@
1
- export { broadcastQueryClient } from './_tsup-dts-rollup.cjs';
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"}
@@ -1 +1,41 @@
1
- export { broadcastQueryClient } from './_tsup-dts-rollup.js';
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"}
@@ -1,97 +1,97 @@
1
- // src/index.ts
2
1
  import { BroadcastChannel } from "broadcast-channel";
3
- function broadcastQueryClient({
4
- queryClient,
5
- broadcastChannel = "tanstack-query",
6
- options
7
- }) {
8
- let transaction = false;
9
- const tx = (cb) => {
10
- transaction = true;
11
- cb();
12
- transaction = false;
13
- };
14
- const channel = new BroadcastChannel(broadcastChannel, {
15
- webWorkerSupport: false,
16
- ...options
17
- });
18
- const queryCache = queryClient.getQueryCache();
19
- const unsubscribe = queryClient.getQueryCache().subscribe((queryEvent) => {
20
- if (transaction) {
21
- return;
22
- }
23
- const {
24
- query: { queryHash, queryKey, state, observers }
25
- } = queryEvent;
26
- if (queryEvent.type === "updated" && queryEvent.action.type === "success") {
27
- channel.postMessage({
28
- type: "updated",
29
- queryHash,
30
- queryKey,
31
- state
32
- });
33
- }
34
- if (queryEvent.type === "removed" && observers.length > 0) {
35
- channel.postMessage({
36
- type: "removed",
37
- queryHash,
38
- queryKey
39
- });
40
- }
41
- if (queryEvent.type === "added") {
42
- channel.postMessage({
43
- type: "added",
44
- queryHash,
45
- queryKey
46
- });
47
- }
48
- });
49
- channel.onmessage = (action) => {
50
- if (!(action == null ? void 0 : action.type)) {
51
- return;
52
- }
53
- tx(() => {
54
- const { type, queryHash, queryKey, state } = action;
55
- const query = queryCache.get(queryHash);
56
- if (type === "updated") {
57
- if (query) {
58
- query.setState(state);
59
- return;
60
- }
61
- queryCache.build(
62
- queryClient,
63
- {
64
- queryKey,
65
- queryHash
66
- },
67
- state
68
- );
69
- } else if (type === "removed") {
70
- if (query) {
71
- queryCache.remove(query);
72
- }
73
- } else if (type === "added") {
74
- if (query) {
75
- query.setState(state);
76
- return;
77
- }
78
- queryCache.build(
79
- queryClient,
80
- {
81
- queryKey,
82
- queryHash
83
- },
84
- state
85
- );
86
- }
87
- });
88
- };
89
- return () => {
90
- unsubscribe();
91
- channel.close();
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 === null || result === void 0 || 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 === null || action === void 0 ? void 0 : 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
- export {
95
- broadcastQueryClient
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 unsubscribe = queryClient.getQueryCache().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 channel.postMessage({\n type: 'updated',\n queryHash,\n queryKey,\n state,\n })\n }\n\n if (queryEvent.type === 'removed' && observers.length > 0) {\n channel.postMessage({\n type: 'removed',\n queryHash,\n queryKey,\n })\n }\n\n if (queryEvent.type === 'added') {\n channel.postMessage({\n type: 'added',\n queryHash,\n queryKey,\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":";AAAA,SAAS,wBAAwB;AAU1B,SAAS,qBAAqB;AAAA,EACnC;AAAA,EACA,mBAAmB;AAAA,EACnB;AACF,GAA4C;AAC1C,MAAI,cAAc;AAClB,QAAM,KAAK,CAAC,OAAmB;AAC7B,kBAAc;AACd,OAAG;AACH,kBAAc;AAAA,EAChB;AAEA,QAAM,UAAU,IAAI,iBAAiB,kBAAkB;AAAA,IACrD,kBAAkB;AAAA,IAClB,GAAG;AAAA,EACL,CAAC;AAED,QAAM,aAAa,YAAY,cAAc;AAE7C,QAAM,cAAc,YAAY,cAAc,EAAE,UAAU,CAAC,eAAe;AACxE,QAAI,aAAa;AACf;AAAA,IACF;AAEA,UAAM;AAAA,MACJ,OAAO,EAAE,WAAW,UAAU,OAAO,UAAU;AAAA,IACjD,IAAI;AAEJ,QAAI,WAAW,SAAS,aAAa,WAAW,OAAO,SAAS,WAAW;AACzE,cAAQ,YAAY;AAAA,QAClB,MAAM;AAAA,QACN;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH;AAEA,QAAI,WAAW,SAAS,aAAa,UAAU,SAAS,GAAG;AACzD,cAAQ,YAAY;AAAA,QAClB,MAAM;AAAA,QACN;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH;AAEA,QAAI,WAAW,SAAS,SAAS;AAC/B,cAAQ,YAAY;AAAA,QAClB,MAAM;AAAA,QACN;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AAED,UAAQ,YAAY,CAAC,WAAW;AAC9B,QAAI,EAAC,iCAAQ,OAAM;AACjB;AAAA,IACF;AAEA,OAAG,MAAM;AACP,YAAM,EAAE,MAAM,WAAW,UAAU,MAAM,IAAI;AAE7C,YAAM,QAAQ,WAAW,IAAI,SAAS;AAEtC,UAAI,SAAS,WAAW;AACtB,YAAI,OAAO;AACT,gBAAM,SAAS,KAAK;AACpB;AAAA,QACF;AAEA,mBAAW;AAAA,UACT;AAAA,UACA;AAAA,YACE;AAAA,YACA;AAAA,UACF;AAAA,UACA;AAAA,QACF;AAAA,MACF,WAAW,SAAS,WAAW;AAC7B,YAAI,OAAO;AACT,qBAAW,OAAO,KAAK;AAAA,QACzB;AAAA,MACF,WAAW,SAAS,SAAS;AAC3B,YAAI,OAAO;AACT,gBAAM,SAAS,KAAK;AACpB;AAAA,QACF;AACA,mBAAW;AAAA,UACT;AAAA,UACA;AAAA,YACE;AAAA,YACA;AAAA,UACF;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO,MAAM;AACX,gBAAY;AACZ,YAAQ,MAAM;AAAA,EAChB;AACF;","names":[]}
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,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