@bevelo/query 0.0.0-stage → 0.1.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.
@@ -0,0 +1,68 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_key = require("../key-DS_LgCI1.cjs");
3
+ // src/live/broadcast.ts
4
+ let nextFallbackSender = 1;
5
+ /** Broadcasts query data updates, invalidations, and removals across browser tabs. */
6
+ function syncQueryClientAcrossTabs(options) {
7
+ const name = options.channelName ?? "bevelo:query-sync";
8
+ const createChannel = options.createChannel ?? (typeof BroadcastChannel !== "undefined" ? (n) => new BroadcastChannel(n) : void 0);
9
+ if (!createChannel) return () => void 0;
10
+ const channel = createChannel(name);
11
+ const sender = options.senderId ?? `tab-${nextFallbackSender++}`;
12
+ const suppressedHashes = /* @__PURE__ */ new Set();
13
+ const unsub = options.queryClient.getQueryCache().subscribe((event) => {
14
+ const hash = event.query.queryHash;
15
+ if (suppressedHashes.has(hash)) return;
16
+ if (event.type === "updated") {
17
+ if (event.action.type === "success" || event.action.type === "setData") {
18
+ const msg = {
19
+ sender,
20
+ kind: "setData",
21
+ queryKey: event.query.queryKey,
22
+ data: event.query.state.data,
23
+ updatedAt: event.query.state.dataUpdatedAt
24
+ };
25
+ channel.postMessage(msg);
26
+ } else if (event.action.type === "invalidate") {
27
+ const msg = {
28
+ sender,
29
+ kind: "invalidate",
30
+ queryKey: event.query.queryKey
31
+ };
32
+ channel.postMessage(msg);
33
+ }
34
+ } else if (event.type === "removed") {
35
+ const msg = {
36
+ sender,
37
+ kind: "remove",
38
+ queryKey: event.query.queryKey
39
+ };
40
+ channel.postMessage(msg);
41
+ }
42
+ });
43
+ channel.onmessage = (rawEvent) => {
44
+ const msg = rawEvent.data;
45
+ if (!msg || typeof msg !== "object" || msg.sender === sender || !Array.isArray(msg.queryKey)) return;
46
+ const hash = require_key.serializeKey(msg.queryKey);
47
+ suppressedHashes.add(hash);
48
+ try {
49
+ if (msg.kind === "setData") options.queryClient.setQueryData(msg.queryKey, msg.data, { updatedAt: typeof msg.updatedAt === "number" ? msg.updatedAt : void 0 });
50
+ else if (msg.kind === "invalidate") options.queryClient.invalidateQueries({
51
+ queryKey: msg.queryKey,
52
+ exact: true
53
+ });
54
+ else if (msg.kind === "remove") options.queryClient.removeQueries({
55
+ queryKey: msg.queryKey,
56
+ exact: true
57
+ });
58
+ } finally {
59
+ suppressedHashes.delete(hash);
60
+ }
61
+ };
62
+ return () => {
63
+ unsub();
64
+ channel.onmessage = null;
65
+ channel.close();
66
+ };
67
+ }
68
+ exports.syncQueryClientAcrossTabs = syncQueryClientAcrossTabs;
@@ -0,0 +1,2 @@
1
+ import { n as BroadcastSyncOptions, r as syncQueryClientAcrossTabs, t as BroadcastChannelLike } from "../broadcast-G0JvCc7n.cjs";
2
+ export { BroadcastChannelLike, BroadcastSyncOptions, syncQueryClientAcrossTabs };
@@ -0,0 +1,2 @@
1
+ import { n as BroadcastSyncOptions, r as syncQueryClientAcrossTabs, t as BroadcastChannelLike } from "../broadcast-DfOq-zMG.js";
2
+ export { BroadcastChannelLike, BroadcastSyncOptions, syncQueryClientAcrossTabs };
@@ -0,0 +1,67 @@
1
+ import { o as serializeKey } from "../key-BtYrMAlu.js";
2
+ // src/live/broadcast.ts
3
+ let nextFallbackSender = 1;
4
+ /** Broadcasts query data updates, invalidations, and removals across browser tabs. */
5
+ function syncQueryClientAcrossTabs(options) {
6
+ const name = options.channelName ?? "bevelo:query-sync";
7
+ const createChannel = options.createChannel ?? (typeof BroadcastChannel !== "undefined" ? (n) => new BroadcastChannel(n) : void 0);
8
+ if (!createChannel) return () => void 0;
9
+ const channel = createChannel(name);
10
+ const sender = options.senderId ?? `tab-${nextFallbackSender++}`;
11
+ const suppressedHashes = /* @__PURE__ */ new Set();
12
+ const unsub = options.queryClient.getQueryCache().subscribe((event) => {
13
+ const hash = event.query.queryHash;
14
+ if (suppressedHashes.has(hash)) return;
15
+ if (event.type === "updated") {
16
+ if (event.action.type === "success" || event.action.type === "setData") {
17
+ const msg = {
18
+ sender,
19
+ kind: "setData",
20
+ queryKey: event.query.queryKey,
21
+ data: event.query.state.data,
22
+ updatedAt: event.query.state.dataUpdatedAt
23
+ };
24
+ channel.postMessage(msg);
25
+ } else if (event.action.type === "invalidate") {
26
+ const msg = {
27
+ sender,
28
+ kind: "invalidate",
29
+ queryKey: event.query.queryKey
30
+ };
31
+ channel.postMessage(msg);
32
+ }
33
+ } else if (event.type === "removed") {
34
+ const msg = {
35
+ sender,
36
+ kind: "remove",
37
+ queryKey: event.query.queryKey
38
+ };
39
+ channel.postMessage(msg);
40
+ }
41
+ });
42
+ channel.onmessage = (rawEvent) => {
43
+ const msg = rawEvent.data;
44
+ if (!msg || typeof msg !== "object" || msg.sender === sender || !Array.isArray(msg.queryKey)) return;
45
+ const hash = serializeKey(msg.queryKey);
46
+ suppressedHashes.add(hash);
47
+ try {
48
+ if (msg.kind === "setData") options.queryClient.setQueryData(msg.queryKey, msg.data, { updatedAt: typeof msg.updatedAt === "number" ? msg.updatedAt : void 0 });
49
+ else if (msg.kind === "invalidate") options.queryClient.invalidateQueries({
50
+ queryKey: msg.queryKey,
51
+ exact: true
52
+ });
53
+ else if (msg.kind === "remove") options.queryClient.removeQueries({
54
+ queryKey: msg.queryKey,
55
+ exact: true
56
+ });
57
+ } finally {
58
+ suppressedHashes.delete(hash);
59
+ }
60
+ };
61
+ return () => {
62
+ unsub();
63
+ channel.onmessage = null;
64
+ channel.close();
65
+ };
66
+ }
67
+ export { syncQueryClientAcrossTabs };