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