@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.
@@ -0,0 +1,1106 @@
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
+ /**
12
+ * returns true if the given object is a promise
13
+ */
14
+ function isPromise(obj) {
15
+ if (obj && typeof obj.then === 'function') {
16
+ return true;
17
+ } else {
18
+ return false;
19
+ }
20
+ }
21
+ function sleep(time) {
22
+ if (!time) time = 0;
23
+ return new Promise(function (res) {
24
+ return setTimeout(res, time);
25
+ });
26
+ }
27
+ function randomInt(min, max) {
28
+ return Math.floor(Math.random() * (max - min + 1) + min);
29
+ }
30
+ /**
31
+ * https://stackoverflow.com/a/8084248
32
+ */
33
+
34
+ function randomToken() {
35
+ return Math.random().toString(36).substring(2);
36
+ }
37
+ var lastMs = 0;
38
+ var additional = 0;
39
+ /**
40
+ * returns the current time in micro-seconds,
41
+ * WARNING: This is a pseudo-function
42
+ * Performance.now is not reliable in webworkers, so we just make sure to never return the same time.
43
+ * This is enough in browsers, and this function will not be used in nodejs.
44
+ * The main reason for this hack is to ensure that BroadcastChannel behaves equal to production when it is used in fast-running unit tests.
45
+ */
46
+
47
+ function microSeconds$4() {
48
+ var ms = new Date().getTime();
49
+
50
+ if (ms === lastMs) {
51
+ additional++;
52
+ return ms * 1000 + additional;
53
+ } else {
54
+ lastMs = ms;
55
+ additional = 0;
56
+ return ms * 1000;
57
+ }
58
+ }
59
+ /**
60
+ * copied from the 'detect-node' npm module
61
+ * We cannot use the module directly because it causes problems with rollup
62
+ * @link https://github.com/iliakan/detect-node/blob/master/index.js
63
+ */
64
+
65
+ var isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
66
+
67
+ var microSeconds$3 = microSeconds$4;
68
+ var type$3 = 'native';
69
+ function create$3(channelName) {
70
+ var state = {
71
+ messagesCallback: null,
72
+ bc: new BroadcastChannel(channelName),
73
+ subFns: [] // subscriberFunctions
74
+
75
+ };
76
+
77
+ state.bc.onmessage = function (msg) {
78
+ if (state.messagesCallback) {
79
+ state.messagesCallback(msg.data);
80
+ }
81
+ };
82
+
83
+ return state;
84
+ }
85
+ function close$3(channelState) {
86
+ channelState.bc.close();
87
+ channelState.subFns = [];
88
+ }
89
+ function postMessage$3(channelState, messageJson) {
90
+ try {
91
+ channelState.bc.postMessage(messageJson, false);
92
+ return Promise.resolve();
93
+ } catch (err) {
94
+ return Promise.reject(err);
95
+ }
96
+ }
97
+ function onMessage$3(channelState, fn) {
98
+ channelState.messagesCallback = fn;
99
+ }
100
+ function canBeUsed$3() {
101
+ /**
102
+ * in the electron-renderer, isNode will be true even if we are in browser-context
103
+ * so we also check if window is undefined
104
+ */
105
+ if (isNode && typeof window === 'undefined') return false;
106
+
107
+ if (typeof BroadcastChannel === 'function') {
108
+ if (BroadcastChannel._pubkey) {
109
+ throw new Error('BroadcastChannel: Do not overwrite window.BroadcastChannel with this module, this is not a polyfill');
110
+ }
111
+
112
+ return true;
113
+ } else return false;
114
+ }
115
+ function averageResponseTime$3() {
116
+ return 150;
117
+ }
118
+ var NativeMethod = {
119
+ create: create$3,
120
+ close: close$3,
121
+ onMessage: onMessage$3,
122
+ postMessage: postMessage$3,
123
+ canBeUsed: canBeUsed$3,
124
+ type: type$3,
125
+ averageResponseTime: averageResponseTime$3,
126
+ microSeconds: microSeconds$3
127
+ };
128
+
129
+ /**
130
+ * this is a set which automatically forgets
131
+ * a given entry when a new entry is set and the ttl
132
+ * of the old one is over
133
+ */
134
+ var ObliviousSet = /** @class */ (function () {
135
+ function ObliviousSet(ttl) {
136
+ this.ttl = ttl;
137
+ this.set = new Set();
138
+ this.timeMap = new Map();
139
+ }
140
+ ObliviousSet.prototype.has = function (value) {
141
+ return this.set.has(value);
142
+ };
143
+ ObliviousSet.prototype.add = function (value) {
144
+ var _this = this;
145
+ this.timeMap.set(value, now());
146
+ this.set.add(value);
147
+ /**
148
+ * When a new value is added,
149
+ * start the cleanup at the next tick
150
+ * to not block the cpu for more important stuff
151
+ * that might happen.
152
+ */
153
+ setTimeout(function () {
154
+ removeTooOldValues(_this);
155
+ }, 0);
156
+ };
157
+ ObliviousSet.prototype.clear = function () {
158
+ this.set.clear();
159
+ this.timeMap.clear();
160
+ };
161
+ return ObliviousSet;
162
+ }());
163
+ /**
164
+ * Removes all entries from the set
165
+ * where the TTL has expired
166
+ */
167
+ function removeTooOldValues(obliviousSet) {
168
+ var olderThen = now() - obliviousSet.ttl;
169
+ var iterator = obliviousSet.set[Symbol.iterator]();
170
+ /**
171
+ * Because we can assume the new values are added at the bottom,
172
+ * we start from the top and stop as soon as we reach a non-too-old value.
173
+ */
174
+ while (true) {
175
+ var value = iterator.next().value;
176
+ if (!value) {
177
+ return; // no more elements
178
+ }
179
+ var time = obliviousSet.timeMap.get(value);
180
+ if (time < olderThen) {
181
+ obliviousSet.timeMap.delete(value);
182
+ obliviousSet.set.delete(value);
183
+ }
184
+ else {
185
+ // We reached a value that is not old enough
186
+ return;
187
+ }
188
+ }
189
+ }
190
+ function now() {
191
+ return new Date().getTime();
192
+ }
193
+
194
+ function fillOptionsWithDefaults() {
195
+ var originalOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
196
+ var options = JSON.parse(JSON.stringify(originalOptions)); // main
197
+
198
+ if (typeof options.webWorkerSupport === 'undefined') options.webWorkerSupport = true; // indexed-db
199
+
200
+ if (!options.idb) options.idb = {}; // after this time the messages get deleted
201
+
202
+ if (!options.idb.ttl) options.idb.ttl = 1000 * 45;
203
+ if (!options.idb.fallbackInterval) options.idb.fallbackInterval = 150; // handles abrupt db onclose events.
204
+
205
+ if (originalOptions.idb && typeof originalOptions.idb.onclose === 'function') options.idb.onclose = originalOptions.idb.onclose; // localstorage
206
+
207
+ if (!options.localstorage) options.localstorage = {};
208
+ if (!options.localstorage.removeTimeout) options.localstorage.removeTimeout = 1000 * 60; // custom methods
209
+
210
+ if (originalOptions.methods) options.methods = originalOptions.methods; // node
211
+
212
+ if (!options.node) options.node = {};
213
+ if (!options.node.ttl) options.node.ttl = 1000 * 60 * 2; // 2 minutes;
214
+
215
+ if (typeof options.node.useFastPath === 'undefined') options.node.useFastPath = true;
216
+ return options;
217
+ }
218
+
219
+ /**
220
+ * this method uses indexeddb to store the messages
221
+ * There is currently no observerAPI for idb
222
+ * @link https://github.com/w3c/IndexedDB/issues/51
223
+ */
224
+ var microSeconds$2 = microSeconds$4;
225
+ var DB_PREFIX = 'pubkey.broadcast-channel-0-';
226
+ var OBJECT_STORE_ID = 'messages';
227
+ var type$2 = 'idb';
228
+ function getIdb() {
229
+ if (typeof indexedDB !== 'undefined') return indexedDB;
230
+
231
+ if (typeof window !== 'undefined') {
232
+ if (typeof window.mozIndexedDB !== 'undefined') return window.mozIndexedDB;
233
+ if (typeof window.webkitIndexedDB !== 'undefined') return window.webkitIndexedDB;
234
+ if (typeof window.msIndexedDB !== 'undefined') return window.msIndexedDB;
235
+ }
236
+
237
+ return false;
238
+ }
239
+ function createDatabase(channelName) {
240
+ var IndexedDB = getIdb(); // create table
241
+
242
+ var dbName = DB_PREFIX + channelName;
243
+ var openRequest = IndexedDB.open(dbName, 1);
244
+
245
+ openRequest.onupgradeneeded = function (ev) {
246
+ var db = ev.target.result;
247
+ db.createObjectStore(OBJECT_STORE_ID, {
248
+ keyPath: 'id',
249
+ autoIncrement: true
250
+ });
251
+ };
252
+
253
+ var dbPromise = new Promise(function (res, rej) {
254
+ openRequest.onerror = function (ev) {
255
+ return rej(ev);
256
+ };
257
+
258
+ openRequest.onsuccess = function () {
259
+ res(openRequest.result);
260
+ };
261
+ });
262
+ return dbPromise;
263
+ }
264
+ /**
265
+ * writes the new message to the database
266
+ * so other readers can find it
267
+ */
268
+
269
+ function writeMessage(db, readerUuid, messageJson) {
270
+ var time = new Date().getTime();
271
+ var writeObject = {
272
+ uuid: readerUuid,
273
+ time: time,
274
+ data: messageJson
275
+ };
276
+ var transaction = db.transaction([OBJECT_STORE_ID], 'readwrite');
277
+ return new Promise(function (res, rej) {
278
+ transaction.oncomplete = function () {
279
+ return res();
280
+ };
281
+
282
+ transaction.onerror = function (ev) {
283
+ return rej(ev);
284
+ };
285
+
286
+ var objectStore = transaction.objectStore(OBJECT_STORE_ID);
287
+ objectStore.add(writeObject);
288
+ });
289
+ }
290
+ function getMessagesHigherThan(db, lastCursorId) {
291
+ var objectStore = db.transaction(OBJECT_STORE_ID).objectStore(OBJECT_STORE_ID);
292
+ var ret = [];
293
+
294
+ function openCursor() {
295
+ // Occasionally Safari will fail on IDBKeyRange.bound, this
296
+ // catches that error, having it open the cursor to the first
297
+ // item. When it gets data it will advance to the desired key.
298
+ try {
299
+ var keyRangeValue = IDBKeyRange.bound(lastCursorId + 1, Infinity);
300
+ return objectStore.openCursor(keyRangeValue);
301
+ } catch (e) {
302
+ return objectStore.openCursor();
303
+ }
304
+ }
305
+
306
+ return new Promise(function (res) {
307
+ openCursor().onsuccess = function (ev) {
308
+ var cursor = ev.target.result;
309
+
310
+ if (cursor) {
311
+ if (cursor.value.id < lastCursorId + 1) {
312
+ cursor["continue"](lastCursorId + 1);
313
+ } else {
314
+ ret.push(cursor.value);
315
+ cursor["continue"]();
316
+ }
317
+ } else {
318
+ res(ret);
319
+ }
320
+ };
321
+ });
322
+ }
323
+ function removeMessageById(db, id) {
324
+ var request = db.transaction([OBJECT_STORE_ID], 'readwrite').objectStore(OBJECT_STORE_ID)["delete"](id);
325
+ return new Promise(function (res) {
326
+ request.onsuccess = function () {
327
+ return res();
328
+ };
329
+ });
330
+ }
331
+ function getOldMessages(db, ttl) {
332
+ var olderThen = new Date().getTime() - ttl;
333
+ var objectStore = db.transaction(OBJECT_STORE_ID).objectStore(OBJECT_STORE_ID);
334
+ var ret = [];
335
+ return new Promise(function (res) {
336
+ objectStore.openCursor().onsuccess = function (ev) {
337
+ var cursor = ev.target.result;
338
+
339
+ if (cursor) {
340
+ var msgObk = cursor.value;
341
+
342
+ if (msgObk.time < olderThen) {
343
+ ret.push(msgObk); //alert("Name for SSN " + cursor.key + " is " + cursor.value.name);
344
+
345
+ cursor["continue"]();
346
+ } else {
347
+ // no more old messages,
348
+ res(ret);
349
+ return;
350
+ }
351
+ } else {
352
+ res(ret);
353
+ }
354
+ };
355
+ });
356
+ }
357
+ function cleanOldMessages(db, ttl) {
358
+ return getOldMessages(db, ttl).then(function (tooOld) {
359
+ return Promise.all(tooOld.map(function (msgObj) {
360
+ return removeMessageById(db, msgObj.id);
361
+ }));
362
+ });
363
+ }
364
+ function create$2(channelName, options) {
365
+ options = fillOptionsWithDefaults(options);
366
+ return createDatabase(channelName).then(function (db) {
367
+ var state = {
368
+ closed: false,
369
+ lastCursorId: 0,
370
+ channelName: channelName,
371
+ options: options,
372
+ uuid: randomToken(),
373
+
374
+ /**
375
+ * emittedMessagesIds
376
+ * contains all messages that have been emitted before
377
+ * @type {ObliviousSet}
378
+ */
379
+ eMIs: new ObliviousSet(options.idb.ttl * 2),
380
+ // ensures we do not read messages in parrallel
381
+ writeBlockPromise: Promise.resolve(),
382
+ messagesCallback: null,
383
+ readQueuePromises: [],
384
+ db: db
385
+ };
386
+ /**
387
+ * Handle abrupt closes that do not originate from db.close().
388
+ * This could happen, for example, if the underlying storage is
389
+ * removed or if the user clears the database in the browser's
390
+ * history preferences.
391
+ */
392
+
393
+ db.onclose = function () {
394
+ state.closed = true;
395
+ if (options.idb.onclose) options.idb.onclose();
396
+ };
397
+ /**
398
+ * if service-workers are used,
399
+ * we have no 'storage'-event if they post a message,
400
+ * therefore we also have to set an interval
401
+ */
402
+
403
+
404
+ _readLoop(state);
405
+
406
+ return state;
407
+ });
408
+ }
409
+
410
+ function _readLoop(state) {
411
+ if (state.closed) return;
412
+ readNewMessages(state).then(function () {
413
+ return sleep(state.options.idb.fallbackInterval);
414
+ }).then(function () {
415
+ return _readLoop(state);
416
+ });
417
+ }
418
+
419
+ function _filterMessage(msgObj, state) {
420
+ if (msgObj.uuid === state.uuid) return false; // send by own
421
+
422
+ if (state.eMIs.has(msgObj.id)) return false; // already emitted
423
+
424
+ if (msgObj.data.time < state.messagesCallbackTime) return false; // older then onMessageCallback
425
+
426
+ return true;
427
+ }
428
+ /**
429
+ * reads all new messages from the database and emits them
430
+ */
431
+
432
+
433
+ function readNewMessages(state) {
434
+ // channel already closed
435
+ if (state.closed) return Promise.resolve(); // if no one is listening, we do not need to scan for new messages
436
+
437
+ if (!state.messagesCallback) return Promise.resolve();
438
+ return getMessagesHigherThan(state.db, state.lastCursorId).then(function (newerMessages) {
439
+ var useMessages = newerMessages
440
+ /**
441
+ * there is a bug in iOS where the msgObj can be undefined some times
442
+ * so we filter them out
443
+ * @link https://github.com/pubkey/broadcast-channel/issues/19
444
+ */
445
+ .filter(function (msgObj) {
446
+ return !!msgObj;
447
+ }).map(function (msgObj) {
448
+ if (msgObj.id > state.lastCursorId) {
449
+ state.lastCursorId = msgObj.id;
450
+ }
451
+
452
+ return msgObj;
453
+ }).filter(function (msgObj) {
454
+ return _filterMessage(msgObj, state);
455
+ }).sort(function (msgObjA, msgObjB) {
456
+ return msgObjA.time - msgObjB.time;
457
+ }); // sort by time
458
+
459
+ useMessages.forEach(function (msgObj) {
460
+ if (state.messagesCallback) {
461
+ state.eMIs.add(msgObj.id);
462
+ state.messagesCallback(msgObj.data);
463
+ }
464
+ });
465
+ return Promise.resolve();
466
+ });
467
+ }
468
+
469
+ function close$2(channelState) {
470
+ channelState.closed = true;
471
+ channelState.db.close();
472
+ }
473
+ function postMessage$2(channelState, messageJson) {
474
+ channelState.writeBlockPromise = channelState.writeBlockPromise.then(function () {
475
+ return writeMessage(channelState.db, channelState.uuid, messageJson);
476
+ }).then(function () {
477
+ if (randomInt(0, 10) === 0) {
478
+ /* await (do not await) */
479
+ cleanOldMessages(channelState.db, channelState.options.idb.ttl);
480
+ }
481
+ });
482
+ return channelState.writeBlockPromise;
483
+ }
484
+ function onMessage$2(channelState, fn, time) {
485
+ channelState.messagesCallbackTime = time;
486
+ channelState.messagesCallback = fn;
487
+ readNewMessages(channelState);
488
+ }
489
+ function canBeUsed$2() {
490
+ if (isNode) return false;
491
+ var idb = getIdb();
492
+ if (!idb) return false;
493
+ return true;
494
+ }
495
+ function averageResponseTime$2(options) {
496
+ return options.idb.fallbackInterval * 2;
497
+ }
498
+ var IndexeDbMethod = {
499
+ create: create$2,
500
+ close: close$2,
501
+ onMessage: onMessage$2,
502
+ postMessage: postMessage$2,
503
+ canBeUsed: canBeUsed$2,
504
+ type: type$2,
505
+ averageResponseTime: averageResponseTime$2,
506
+ microSeconds: microSeconds$2
507
+ };
508
+
509
+ /**
510
+ * A localStorage-only method which uses localstorage and its 'storage'-event
511
+ * This does not work inside of webworkers because they have no access to locastorage
512
+ * This is basically implemented to support IE9 or your grandmothers toaster.
513
+ * @link https://caniuse.com/#feat=namevalue-storage
514
+ * @link https://caniuse.com/#feat=indexeddb
515
+ */
516
+ var microSeconds$1 = microSeconds$4;
517
+ var KEY_PREFIX = 'pubkey.broadcastChannel-';
518
+ var type$1 = 'localstorage';
519
+ /**
520
+ * copied from crosstab
521
+ * @link https://github.com/tejacques/crosstab/blob/master/src/crosstab.js#L32
522
+ */
523
+
524
+ function getLocalStorage() {
525
+ var localStorage;
526
+ if (typeof window === 'undefined') return null;
527
+
528
+ try {
529
+ localStorage = window.localStorage;
530
+ localStorage = window['ie8-eventlistener/storage'] || window.localStorage;
531
+ } catch (e) {// New versions of Firefox throw a Security exception
532
+ // if cookies are disabled. See
533
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1028153
534
+ }
535
+
536
+ return localStorage;
537
+ }
538
+ function storageKey(channelName) {
539
+ return KEY_PREFIX + channelName;
540
+ }
541
+ /**
542
+ * writes the new message to the storage
543
+ * and fires the storage-event so other readers can find it
544
+ */
545
+
546
+ function postMessage$1(channelState, messageJson) {
547
+ return new Promise(function (res) {
548
+ sleep().then(function () {
549
+ var key = storageKey(channelState.channelName);
550
+ var writeObj = {
551
+ token: randomToken(),
552
+ time: new Date().getTime(),
553
+ data: messageJson,
554
+ uuid: channelState.uuid
555
+ };
556
+ var value = JSON.stringify(writeObj);
557
+ getLocalStorage().setItem(key, value);
558
+ /**
559
+ * StorageEvent does not fire the 'storage' event
560
+ * in the window that changes the state of the local storage.
561
+ * So we fire it manually
562
+ */
563
+
564
+ var ev = document.createEvent('Event');
565
+ ev.initEvent('storage', true, true);
566
+ ev.key = key;
567
+ ev.newValue = value;
568
+ window.dispatchEvent(ev);
569
+ res();
570
+ });
571
+ });
572
+ }
573
+ function addStorageEventListener(channelName, fn) {
574
+ var key = storageKey(channelName);
575
+
576
+ var listener = function listener(ev) {
577
+ if (ev.key === key) {
578
+ fn(JSON.parse(ev.newValue));
579
+ }
580
+ };
581
+
582
+ window.addEventListener('storage', listener);
583
+ return listener;
584
+ }
585
+ function removeStorageEventListener(listener) {
586
+ window.removeEventListener('storage', listener);
587
+ }
588
+ function create$1(channelName, options) {
589
+ options = fillOptionsWithDefaults(options);
590
+
591
+ if (!canBeUsed$1()) {
592
+ throw new Error('BroadcastChannel: localstorage cannot be used');
593
+ }
594
+
595
+ var uuid = randomToken();
596
+ /**
597
+ * eMIs
598
+ * contains all messages that have been emitted before
599
+ * @type {ObliviousSet}
600
+ */
601
+
602
+ var eMIs = new ObliviousSet(options.localstorage.removeTimeout);
603
+ var state = {
604
+ channelName: channelName,
605
+ uuid: uuid,
606
+ eMIs: eMIs // emittedMessagesIds
607
+
608
+ };
609
+ state.listener = addStorageEventListener(channelName, function (msgObj) {
610
+ if (!state.messagesCallback) return; // no listener
611
+
612
+ if (msgObj.uuid === uuid) return; // own message
613
+
614
+ if (!msgObj.token || eMIs.has(msgObj.token)) return; // already emitted
615
+
616
+ if (msgObj.data.time && msgObj.data.time < state.messagesCallbackTime) return; // too old
617
+
618
+ eMIs.add(msgObj.token);
619
+ state.messagesCallback(msgObj.data);
620
+ });
621
+ return state;
622
+ }
623
+ function close$1(channelState) {
624
+ removeStorageEventListener(channelState.listener);
625
+ }
626
+ function onMessage$1(channelState, fn, time) {
627
+ channelState.messagesCallbackTime = time;
628
+ channelState.messagesCallback = fn;
629
+ }
630
+ function canBeUsed$1() {
631
+ if (isNode) return false;
632
+ var ls = getLocalStorage();
633
+ if (!ls) return false;
634
+
635
+ try {
636
+ var key = '__broadcastchannel_check';
637
+ ls.setItem(key, 'works');
638
+ ls.removeItem(key);
639
+ } catch (e) {
640
+ // Safari 10 in private mode will not allow write access to local
641
+ // storage and fail with a QuotaExceededError. See
642
+ // https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API#Private_Browsing_Incognito_modes
643
+ return false;
644
+ }
645
+
646
+ return true;
647
+ }
648
+ function averageResponseTime$1() {
649
+ var defaultTime = 120;
650
+ var userAgent = navigator.userAgent.toLowerCase();
651
+
652
+ if (userAgent.includes('safari') && !userAgent.includes('chrome')) {
653
+ // safari is much slower so this time is higher
654
+ return defaultTime * 2;
655
+ }
656
+
657
+ return defaultTime;
658
+ }
659
+ var LocalstorageMethod = {
660
+ create: create$1,
661
+ close: close$1,
662
+ onMessage: onMessage$1,
663
+ postMessage: postMessage$1,
664
+ canBeUsed: canBeUsed$1,
665
+ type: type$1,
666
+ averageResponseTime: averageResponseTime$1,
667
+ microSeconds: microSeconds$1
668
+ };
669
+
670
+ var microSeconds = microSeconds$4;
671
+ var type = 'simulate';
672
+ var SIMULATE_CHANNELS = new Set();
673
+ function create(channelName) {
674
+ var state = {
675
+ name: channelName,
676
+ messagesCallback: null
677
+ };
678
+ SIMULATE_CHANNELS.add(state);
679
+ return state;
680
+ }
681
+ function close(channelState) {
682
+ SIMULATE_CHANNELS["delete"](channelState);
683
+ }
684
+ function postMessage(channelState, messageJson) {
685
+ return new Promise(function (res) {
686
+ return setTimeout(function () {
687
+ var channelArray = Array.from(SIMULATE_CHANNELS);
688
+ channelArray.filter(function (channel) {
689
+ return channel.name === channelState.name;
690
+ }).filter(function (channel) {
691
+ return channel !== channelState;
692
+ }).filter(function (channel) {
693
+ return !!channel.messagesCallback;
694
+ }).forEach(function (channel) {
695
+ return channel.messagesCallback(messageJson);
696
+ });
697
+ res();
698
+ }, 5);
699
+ });
700
+ }
701
+ function onMessage(channelState, fn) {
702
+ channelState.messagesCallback = fn;
703
+ }
704
+ function canBeUsed() {
705
+ return true;
706
+ }
707
+ function averageResponseTime() {
708
+ return 5;
709
+ }
710
+ var SimulateMethod = {
711
+ create: create,
712
+ close: close,
713
+ onMessage: onMessage,
714
+ postMessage: postMessage,
715
+ canBeUsed: canBeUsed,
716
+ type: type,
717
+ averageResponseTime: averageResponseTime,
718
+ microSeconds: microSeconds
719
+ };
720
+
721
+ var METHODS = [NativeMethod, // fastest
722
+ IndexeDbMethod, LocalstorageMethod];
723
+ /**
724
+ * The NodeMethod is loaded lazy
725
+ * so it will not get bundled in browser-builds
726
+ */
727
+
728
+ if (isNode) {
729
+ /**
730
+ * we use the non-transpiled code for nodejs
731
+ * because it runs faster
732
+ */
733
+ var NodeMethod = require('../../src/methods/' + // use this hack so that browserify and others
734
+ // do not import the node-method by default
735
+ // when bundling.
736
+ 'node.js');
737
+ /**
738
+ * this will be false for webpackbuilds
739
+ * which will shim the node-method with an empty object {}
740
+ */
741
+
742
+
743
+ if (typeof NodeMethod.canBeUsed === 'function') {
744
+ METHODS.push(NodeMethod);
745
+ }
746
+ }
747
+
748
+ function chooseMethod(options) {
749
+ var chooseMethods = [].concat(options.methods, METHODS).filter(Boolean); // directly chosen
750
+
751
+ if (options.type) {
752
+ if (options.type === 'simulate') {
753
+ // only use simulate-method if directly chosen
754
+ return SimulateMethod;
755
+ }
756
+
757
+ var ret = chooseMethods.find(function (m) {
758
+ return m.type === options.type;
759
+ });
760
+ if (!ret) throw new Error('method-type ' + options.type + ' not found');else return ret;
761
+ }
762
+ /**
763
+ * if no webworker support is needed,
764
+ * remove idb from the list so that localstorage is been chosen
765
+ */
766
+
767
+
768
+ if (!options.webWorkerSupport && !isNode) {
769
+ chooseMethods = chooseMethods.filter(function (m) {
770
+ return m.type !== 'idb';
771
+ });
772
+ }
773
+
774
+ var useMethod = chooseMethods.find(function (method) {
775
+ return method.canBeUsed();
776
+ });
777
+ if (!useMethod) throw new Error('No useable methode found:' + JSON.stringify(METHODS.map(function (m) {
778
+ return m.type;
779
+ })));else return useMethod;
780
+ }
781
+
782
+ var BroadcastChannel$1 = function BroadcastChannel(name, options) {
783
+ this.name = name;
784
+
785
+ if (ENFORCED_OPTIONS) {
786
+ options = ENFORCED_OPTIONS;
787
+ }
788
+
789
+ this.options = fillOptionsWithDefaults(options);
790
+ this.method = chooseMethod(this.options); // isListening
791
+
792
+ this._iL = false;
793
+ /**
794
+ * _onMessageListener
795
+ * setting onmessage twice,
796
+ * will overwrite the first listener
797
+ */
798
+
799
+ this._onML = null;
800
+ /**
801
+ * _addEventListeners
802
+ */
803
+
804
+ this._addEL = {
805
+ message: [],
806
+ internal: []
807
+ };
808
+ /**
809
+ * Unsend message promises
810
+ * where the sending is still in progress
811
+ * @type {Set<Promise>}
812
+ */
813
+
814
+ this._uMP = new Set();
815
+ /**
816
+ * _beforeClose
817
+ * array of promises that will be awaited
818
+ * before the channel is closed
819
+ */
820
+
821
+ this._befC = [];
822
+ /**
823
+ * _preparePromise
824
+ */
825
+
826
+ this._prepP = null;
827
+
828
+ _prepareChannel(this);
829
+ }; // STATICS
830
+
831
+ /**
832
+ * used to identify if someone overwrites
833
+ * window.BroadcastChannel with this
834
+ * See methods/native.js
835
+ */
836
+
837
+ BroadcastChannel$1._pubkey = true;
838
+ /**
839
+ * if set, this method is enforced,
840
+ * no mather what the options are
841
+ */
842
+
843
+ var ENFORCED_OPTIONS;
844
+
845
+ BroadcastChannel$1.prototype = {
846
+ postMessage: function postMessage(msg) {
847
+ if (this.closed) {
848
+ throw new Error('BroadcastChannel.postMessage(): ' + 'Cannot post message after channel has closed');
849
+ }
850
+
851
+ return _post(this, 'message', msg);
852
+ },
853
+ postInternal: function postInternal(msg) {
854
+ return _post(this, 'internal', msg);
855
+ },
856
+
857
+ set onmessage(fn) {
858
+ var time = this.method.microSeconds();
859
+ var listenObj = {
860
+ time: time,
861
+ fn: fn
862
+ };
863
+
864
+ _removeListenerObject(this, 'message', this._onML);
865
+
866
+ if (fn && typeof fn === 'function') {
867
+ this._onML = listenObj;
868
+
869
+ _addListenerObject(this, 'message', listenObj);
870
+ } else {
871
+ this._onML = null;
872
+ }
873
+ },
874
+
875
+ addEventListener: function addEventListener(type, fn) {
876
+ var time = this.method.microSeconds();
877
+ var listenObj = {
878
+ time: time,
879
+ fn: fn
880
+ };
881
+
882
+ _addListenerObject(this, type, listenObj);
883
+ },
884
+ removeEventListener: function removeEventListener(type, fn) {
885
+ var obj = this._addEL[type].find(function (obj) {
886
+ return obj.fn === fn;
887
+ });
888
+
889
+ _removeListenerObject(this, type, obj);
890
+ },
891
+ close: function close() {
892
+ var _this = this;
893
+
894
+ if (this.closed) {
895
+ return;
896
+ }
897
+
898
+ this.closed = true;
899
+ var awaitPrepare = this._prepP ? this._prepP : Promise.resolve();
900
+ this._onML = null;
901
+ this._addEL.message = [];
902
+ return awaitPrepare // wait until all current sending are processed
903
+ .then(function () {
904
+ return Promise.all(Array.from(_this._uMP));
905
+ }) // run before-close hooks
906
+ .then(function () {
907
+ return Promise.all(_this._befC.map(function (fn) {
908
+ return fn();
909
+ }));
910
+ }) // close the channel
911
+ .then(function () {
912
+ return _this.method.close(_this._state);
913
+ });
914
+ },
915
+
916
+ get type() {
917
+ return this.method.type;
918
+ },
919
+
920
+ get isClosed() {
921
+ return this.closed;
922
+ }
923
+
924
+ };
925
+ /**
926
+ * Post a message over the channel
927
+ * @returns {Promise} that resolved when the message sending is done
928
+ */
929
+
930
+ function _post(broadcastChannel, type, msg) {
931
+ var time = broadcastChannel.method.microSeconds();
932
+ var msgObj = {
933
+ time: time,
934
+ type: type,
935
+ data: msg
936
+ };
937
+ var awaitPrepare = broadcastChannel._prepP ? broadcastChannel._prepP : Promise.resolve();
938
+ return awaitPrepare.then(function () {
939
+ var sendPromise = broadcastChannel.method.postMessage(broadcastChannel._state, msgObj); // add/remove to unsend messages list
940
+
941
+ broadcastChannel._uMP.add(sendPromise);
942
+
943
+ sendPromise["catch"]().then(function () {
944
+ return broadcastChannel._uMP["delete"](sendPromise);
945
+ });
946
+ return sendPromise;
947
+ });
948
+ }
949
+
950
+ function _prepareChannel(channel) {
951
+ var maybePromise = channel.method.create(channel.name, channel.options);
952
+
953
+ if (isPromise(maybePromise)) {
954
+ channel._prepP = maybePromise;
955
+ maybePromise.then(function (s) {
956
+ // used in tests to simulate slow runtime
957
+
958
+ /*if (channel.options.prepareDelay) {
959
+ await new Promise(res => setTimeout(res, this.options.prepareDelay));
960
+ }*/
961
+ channel._state = s;
962
+ });
963
+ } else {
964
+ channel._state = maybePromise;
965
+ }
966
+ }
967
+
968
+ function _hasMessageListeners(channel) {
969
+ if (channel._addEL.message.length > 0) return true;
970
+ if (channel._addEL.internal.length > 0) return true;
971
+ return false;
972
+ }
973
+
974
+ function _addListenerObject(channel, type, obj) {
975
+ channel._addEL[type].push(obj);
976
+
977
+ _startListening(channel);
978
+ }
979
+
980
+ function _removeListenerObject(channel, type, obj) {
981
+ channel._addEL[type] = channel._addEL[type].filter(function (o) {
982
+ return o !== obj;
983
+ });
984
+
985
+ _stopListening(channel);
986
+ }
987
+
988
+ function _startListening(channel) {
989
+ if (!channel._iL && _hasMessageListeners(channel)) {
990
+ // someone is listening, start subscribing
991
+ var listenerFn = function listenerFn(msgObj) {
992
+ channel._addEL[msgObj.type].forEach(function (obj) {
993
+ if (msgObj.time >= obj.time) {
994
+ obj.fn(msgObj.data);
995
+ }
996
+ });
997
+ };
998
+
999
+ var time = channel.method.microSeconds();
1000
+
1001
+ if (channel._prepP) {
1002
+ channel._prepP.then(function () {
1003
+ channel._iL = true;
1004
+ channel.method.onMessage(channel._state, listenerFn, time);
1005
+ });
1006
+ } else {
1007
+ channel._iL = true;
1008
+ channel.method.onMessage(channel._state, listenerFn, time);
1009
+ }
1010
+ }
1011
+ }
1012
+
1013
+ function _stopListening(channel) {
1014
+ if (channel._iL && !_hasMessageListeners(channel)) {
1015
+ // noone is listening, stop subscribing
1016
+ channel._iL = false;
1017
+ var time = channel.method.microSeconds();
1018
+ channel.method.onMessage(channel._state, null, time);
1019
+ }
1020
+ }
1021
+
1022
+ function broadcastQueryClient({
1023
+ queryClient,
1024
+ broadcastChannel = 'tanstack-query'
1025
+ }) {
1026
+ let transaction = false;
1027
+
1028
+ const tx = cb => {
1029
+ transaction = true;
1030
+ cb();
1031
+ transaction = false;
1032
+ };
1033
+
1034
+ const channel = new BroadcastChannel$1(broadcastChannel, {
1035
+ webWorkerSupport: false
1036
+ });
1037
+ const queryCache = queryClient.getQueryCache();
1038
+ queryClient.getQueryCache().subscribe(queryEvent => {
1039
+ if (transaction) {
1040
+ return;
1041
+ }
1042
+
1043
+ const {
1044
+ query: {
1045
+ queryHash,
1046
+ queryKey,
1047
+ state
1048
+ }
1049
+ } = queryEvent;
1050
+
1051
+ if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {
1052
+ channel.postMessage({
1053
+ type: 'updated',
1054
+ queryHash,
1055
+ queryKey,
1056
+ state
1057
+ });
1058
+ }
1059
+
1060
+ if (queryEvent.type === 'removed') {
1061
+ channel.postMessage({
1062
+ type: 'removed',
1063
+ queryHash,
1064
+ queryKey
1065
+ });
1066
+ }
1067
+ });
1068
+
1069
+ channel.onmessage = action => {
1070
+ if (!(action != null && action.type)) {
1071
+ return;
1072
+ }
1073
+
1074
+ tx(() => {
1075
+ const {
1076
+ type,
1077
+ queryHash,
1078
+ queryKey,
1079
+ state
1080
+ } = action;
1081
+
1082
+ if (type === 'updated') {
1083
+ const query = queryCache.get(queryHash);
1084
+
1085
+ if (query) {
1086
+ query.setState(state);
1087
+ return;
1088
+ }
1089
+
1090
+ queryCache.build(queryClient, {
1091
+ queryKey,
1092
+ queryHash
1093
+ }, state);
1094
+ } else if (type === 'removed') {
1095
+ const query = queryCache.get(queryHash);
1096
+
1097
+ if (query) {
1098
+ queryCache.remove(query);
1099
+ }
1100
+ }
1101
+ });
1102
+ };
1103
+ }
1104
+
1105
+ export { broadcastQueryClient };
1106
+ //# sourceMappingURL=index.js.map