@tanstack/query-broadcast-client-experimental 4.0.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/cjs/packages/query-broadcast-client-experimental/src/index.js +101 -0
- package/build/cjs/packages/query-broadcast-client-experimental/src/index.js.map +1 -0
- package/build/esm/index.js +1106 -0
- package/build/esm/index.js.map +1 -0
- package/build/stats-html.html +2689 -0
- package/build/umd/index.development.js +1116 -0
- package/build/umd/index.development.js.map +1 -0
- package/build/umd/index.production.js +12 -0
- package/build/umd/index.production.js.map +1 -0
- package/package.json +28 -0
- package/src/index.ts +86 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* query-broadcast-client-experimental
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var broadcastChannel = require('../../../node_modules/broadcast-channel/dist/es/broadcast-channel.js');
|
|
16
|
+
|
|
17
|
+
function broadcastQueryClient({
|
|
18
|
+
queryClient,
|
|
19
|
+
broadcastChannel: broadcastChannel$1 = 'tanstack-query'
|
|
20
|
+
}) {
|
|
21
|
+
let transaction = false;
|
|
22
|
+
|
|
23
|
+
const tx = cb => {
|
|
24
|
+
transaction = true;
|
|
25
|
+
cb();
|
|
26
|
+
transaction = false;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const channel = new broadcastChannel.BroadcastChannel(broadcastChannel$1, {
|
|
30
|
+
webWorkerSupport: false
|
|
31
|
+
});
|
|
32
|
+
const queryCache = queryClient.getQueryCache();
|
|
33
|
+
queryClient.getQueryCache().subscribe(queryEvent => {
|
|
34
|
+
if (transaction) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const {
|
|
39
|
+
query: {
|
|
40
|
+
queryHash,
|
|
41
|
+
queryKey,
|
|
42
|
+
state
|
|
43
|
+
}
|
|
44
|
+
} = queryEvent;
|
|
45
|
+
|
|
46
|
+
if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {
|
|
47
|
+
channel.postMessage({
|
|
48
|
+
type: 'updated',
|
|
49
|
+
queryHash,
|
|
50
|
+
queryKey,
|
|
51
|
+
state
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (queryEvent.type === 'removed') {
|
|
56
|
+
channel.postMessage({
|
|
57
|
+
type: 'removed',
|
|
58
|
+
queryHash,
|
|
59
|
+
queryKey
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
channel.onmessage = action => {
|
|
65
|
+
if (!(action != null && action.type)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
tx(() => {
|
|
70
|
+
const {
|
|
71
|
+
type,
|
|
72
|
+
queryHash,
|
|
73
|
+
queryKey,
|
|
74
|
+
state
|
|
75
|
+
} = action;
|
|
76
|
+
|
|
77
|
+
if (type === 'updated') {
|
|
78
|
+
const query = queryCache.get(queryHash);
|
|
79
|
+
|
|
80
|
+
if (query) {
|
|
81
|
+
query.setState(state);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
queryCache.build(queryClient, {
|
|
86
|
+
queryKey,
|
|
87
|
+
queryHash
|
|
88
|
+
}, state);
|
|
89
|
+
} else if (type === 'removed') {
|
|
90
|
+
const query = queryCache.get(queryHash);
|
|
91
|
+
|
|
92
|
+
if (query) {
|
|
93
|
+
queryCache.remove(query);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
exports.broadcastQueryClient = broadcastQueryClient;
|
|
101
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../../src/index.ts"],"sourcesContent":["import { BroadcastChannel } from 'broadcast-channel'\nimport type { QueryClient } from '@tanstack/query-core'\n\ninterface BroadcastQueryClientOptions {\n queryClient: QueryClient\n broadcastChannel?: string\n}\n\nexport function broadcastQueryClient({\n queryClient,\n broadcastChannel = 'tanstack-query',\n}: BroadcastQueryClientOptions) {\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 })\n\n const queryCache = queryClient.getQueryCache()\n\n queryClient.getQueryCache().subscribe((queryEvent) => {\n if (transaction) {\n return\n }\n\n const {\n query: { queryHash, queryKey, state },\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') {\n channel.postMessage({\n type: 'removed',\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 if (type === 'updated') {\n const query = queryCache.get(queryHash)\n\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 const query = queryCache.get(queryHash)\n\n if (query) {\n queryCache.remove(query)\n }\n }\n })\n }\n}\n"],"names":["broadcastQueryClient","queryClient","broadcastChannel","transaction","tx","cb","channel","BroadcastChannel","webWorkerSupport","queryCache","getQueryCache","subscribe","queryEvent","query","queryHash","queryKey","state","type","action","postMessage","onmessage","get","setState","build","remove"],"mappings":";;;;;;;;;;;;;;;;AAQO,SAASA,oBAAT,CAA8B;EACnCC,WADmC;AAEnCC,oBAAA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