@tanstack/query-broadcast-client-experimental 4.3.4 → 4.3.7
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/lib/index.esm.js +87 -0
- package/build/lib/index.esm.js.map +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { BroadcastChannel } from 'broadcast-channel';
|
|
2
|
+
|
|
3
|
+
function broadcastQueryClient({
|
|
4
|
+
queryClient,
|
|
5
|
+
broadcastChannel = 'tanstack-query'
|
|
6
|
+
}) {
|
|
7
|
+
let transaction = false;
|
|
8
|
+
|
|
9
|
+
const tx = cb => {
|
|
10
|
+
transaction = true;
|
|
11
|
+
cb();
|
|
12
|
+
transaction = false;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const channel = new BroadcastChannel(broadcastChannel, {
|
|
16
|
+
webWorkerSupport: false
|
|
17
|
+
});
|
|
18
|
+
const queryCache = queryClient.getQueryCache();
|
|
19
|
+
queryClient.getQueryCache().subscribe(queryEvent => {
|
|
20
|
+
if (transaction) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const {
|
|
25
|
+
query: {
|
|
26
|
+
queryHash,
|
|
27
|
+
queryKey,
|
|
28
|
+
state
|
|
29
|
+
}
|
|
30
|
+
} = queryEvent;
|
|
31
|
+
|
|
32
|
+
if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {
|
|
33
|
+
channel.postMessage({
|
|
34
|
+
type: 'updated',
|
|
35
|
+
queryHash,
|
|
36
|
+
queryKey,
|
|
37
|
+
state
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (queryEvent.type === 'removed') {
|
|
42
|
+
channel.postMessage({
|
|
43
|
+
type: 'removed',
|
|
44
|
+
queryHash,
|
|
45
|
+
queryKey
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
channel.onmessage = action => {
|
|
51
|
+
if (!(action != null && action.type)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
tx(() => {
|
|
56
|
+
const {
|
|
57
|
+
type,
|
|
58
|
+
queryHash,
|
|
59
|
+
queryKey,
|
|
60
|
+
state
|
|
61
|
+
} = action;
|
|
62
|
+
|
|
63
|
+
if (type === 'updated') {
|
|
64
|
+
const query = queryCache.get(queryHash);
|
|
65
|
+
|
|
66
|
+
if (query) {
|
|
67
|
+
query.setState(state);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
queryCache.build(queryClient, {
|
|
72
|
+
queryKey,
|
|
73
|
+
queryHash
|
|
74
|
+
}, state);
|
|
75
|
+
} else if (type === 'removed') {
|
|
76
|
+
const query = queryCache.get(queryHash);
|
|
77
|
+
|
|
78
|
+
if (query) {
|
|
79
|
+
queryCache.remove(query);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { broadcastQueryClient };
|
|
87
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.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,EAAAA,gBAAgB,GAAG,gBAAA;AAFgB,CAA9B,EAGyB;EAC9B,IAAIC,WAAW,GAAG,KAAlB,CAAA;;EACA,MAAMC,EAAE,GAAIC,EAAD,IAAoB;AAC7BF,IAAAA,WAAW,GAAG,IAAd,CAAA;IACAE,EAAE,EAAA,CAAA;AACFF,IAAAA,WAAW,GAAG,KAAd,CAAA;GAHF,CAAA;;AAMA,EAAA,MAAMG,OAAO,GAAG,IAAIC,gBAAJ,CAAqBL,gBAArB,EAAuC;AACrDM,IAAAA,gBAAgB,EAAE,KAAA;AADmC,GAAvC,CAAhB,CAAA;AAIA,EAAA,MAAMC,UAAU,GAAGR,WAAW,CAACS,aAAZ,EAAnB,CAAA;AAEAT,EAAAA,WAAW,CAACS,aAAZ,EAAA,CAA4BC,SAA5B,CAAuCC,UAAD,IAAgB;AACpD,IAAA,IAAIT,WAAJ,EAAiB;AACf,MAAA,OAAA;AACD,KAAA;;IAED,MAAM;AACJU,MA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A,CAAA;AAQD,OAhBD,MAgBO,IAAIC,IAAI,KAAK,SAAb,EAAwB;AAC7B,QAAA,MAAMJ,KAAK,GAAGJ,UAAU,CAACY,GAAX,CAAeP,SAAf,CAAd,CAAA;;AAEA,QAAA,IAAID,KAAJ,EAAW;UACTJ,UAAU,CAACe,MAAX,CAAkBX,KAAlB,CAAA,CAAA;AACD,SAAA;AACF,OAAA;AACF,KA1BC,CAAF,CAAA;GALF,CAAA;AAiCD;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-broadcast-client-experimental",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.7",
|
|
4
4
|
"description": "TODO",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"types": "build/lib/index.d.ts",
|
|
14
14
|
"main": "build/lib/index.js",
|
|
15
|
+
"module": "build/lib/index.esm.js",
|
|
15
16
|
"exports": {
|
|
16
17
|
".": {
|
|
17
18
|
"types": "./build/lib/index.d.ts",
|
|
@@ -32,7 +33,7 @@
|
|
|
32
33
|
"compile": "../../node_modules/.bin/tsc -p tsconfig.json --noEmit --emitDeclarationOnly false"
|
|
33
34
|
},
|
|
34
35
|
"dependencies": {
|
|
35
|
-
"@tanstack/query-core": "4.3.
|
|
36
|
+
"@tanstack/query-core": "4.3.7",
|
|
36
37
|
"broadcast-channel": "^4.14.0"
|
|
37
38
|
}
|
|
38
39
|
}
|