react-native-onyx 1.0.130 → 1.0.131
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/README.md +0 -8
- package/dist/web.development.js +2 -287
- package/dist/web.development.js.map +1 -1
- package/dist/web.min.js +1 -1
- package/dist/web.min.js.map +1 -1
- package/lib/Onyx.d.ts +0 -10
- package/lib/Onyx.js +1 -106
- package/lib/Str.js +1 -16
- package/lib/utils.d.ts +2 -2
- package/package.json +8 -12
- package/lib/ActiveClientManager/index.d.ts +0 -22
- package/lib/ActiveClientManager/index.native.js +0 -18
- package/lib/ActiveClientManager/index.web.js +0 -94
- package/lib/broadcast/index.d.ts +0 -17
- package/lib/broadcast/index.native.js +0 -12
- package/lib/broadcast/index.web.js +0 -33
package/lib/Onyx.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {Component} from 'react';
|
|
2
2
|
import * as Logger from './Logger';
|
|
3
|
-
import * as ActiveClientManager from './ActiveClientManager';
|
|
4
3
|
import {CollectionKey, CollectionKeyBase, DeepRecord, KeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey} from './types';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -292,11 +291,6 @@ declare function hasPendingMergeForKey(key: OnyxKey): boolean;
|
|
|
292
291
|
*/
|
|
293
292
|
declare function setMemoryOnlyKeys(keyList: OnyxKey[]): void;
|
|
294
293
|
|
|
295
|
-
/**
|
|
296
|
-
* Sets the callback to be called when the clear finishes executing.
|
|
297
|
-
*/
|
|
298
|
-
declare function onClear(callback: () => void): void;
|
|
299
|
-
|
|
300
294
|
declare const Onyx: {
|
|
301
295
|
connect: typeof connect;
|
|
302
296
|
disconnect: typeof disconnect;
|
|
@@ -315,10 +309,6 @@ declare const Onyx: {
|
|
|
315
309
|
isSafeEvictionKey: typeof isSafeEvictionKey;
|
|
316
310
|
METHOD: typeof METHOD;
|
|
317
311
|
setMemoryOnlyKeys: typeof setMemoryOnlyKeys;
|
|
318
|
-
onClear: typeof onClear;
|
|
319
|
-
isClientManagerReady: typeof ActiveClientManager.isReady;
|
|
320
|
-
isClientTheLeader: typeof ActiveClientManager.isClientTheLeader;
|
|
321
|
-
subscribeToClientChange: typeof ActiveClientManager.subscribeToClientChange;
|
|
322
312
|
};
|
|
323
313
|
|
|
324
314
|
export default Onyx;
|
package/lib/Onyx.js
CHANGED
|
@@ -7,8 +7,6 @@ import * as Str from './Str';
|
|
|
7
7
|
import createDeferredTask from './createDeferredTask';
|
|
8
8
|
import * as PerformanceUtils from './metrics/PerformanceUtils';
|
|
9
9
|
import Storage from './storage';
|
|
10
|
-
import * as Broadcast from './broadcast';
|
|
11
|
-
import * as ActiveClientManager from './ActiveClientManager';
|
|
12
10
|
import utils from './utils';
|
|
13
11
|
import unstable_batchedUpdates from './batch';
|
|
14
12
|
import DevTools from './DevTools';
|
|
@@ -22,8 +20,6 @@ const METHOD = {
|
|
|
22
20
|
CLEAR: 'clear',
|
|
23
21
|
};
|
|
24
22
|
|
|
25
|
-
const ON_CLEAR = 'on_clear';
|
|
26
|
-
|
|
27
23
|
// Key/value store of Onyx key and arrays of values to merge
|
|
28
24
|
const mergeQueue = {};
|
|
29
25
|
const mergeQueuePromise = {};
|
|
@@ -54,12 +50,6 @@ let defaultKeyStates = {};
|
|
|
54
50
|
// Connections can be made before `Onyx.init`. They would wait for this task before resolving
|
|
55
51
|
const deferredInitTask = createDeferredTask();
|
|
56
52
|
|
|
57
|
-
// The promise of the clear function, saved so that no writes happen while it's executing
|
|
58
|
-
let isClearing = false;
|
|
59
|
-
|
|
60
|
-
// Callback to be executed after the clear execution ends
|
|
61
|
-
let onClearCallback = null;
|
|
62
|
-
|
|
63
53
|
let batchUpdatesPromise = null;
|
|
64
54
|
let batchUpdatesQueue = [];
|
|
65
55
|
|
|
@@ -1090,15 +1080,6 @@ function removeNullValues(key, value) {
|
|
|
1090
1080
|
* @returns {Promise}
|
|
1091
1081
|
*/
|
|
1092
1082
|
function set(key, value) {
|
|
1093
|
-
if (!ActiveClientManager.isClientTheLeader()) {
|
|
1094
|
-
Broadcast.sendMessage({type: METHOD.SET, key, value});
|
|
1095
|
-
return Promise.resolve();
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
if (isClearing) {
|
|
1099
|
-
return Promise.resolve();
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
1083
|
// If the value is null, we remove the key from storage
|
|
1103
1084
|
const {value: valueAfterRemoving, wasRemoved} = removeNullValues(key, value);
|
|
1104
1085
|
|
|
@@ -1155,15 +1136,6 @@ function prepareKeyValuePairsForStorage(data) {
|
|
|
1155
1136
|
* @returns {Promise}
|
|
1156
1137
|
*/
|
|
1157
1138
|
function multiSet(data) {
|
|
1158
|
-
if (!ActiveClientManager.isClientTheLeader()) {
|
|
1159
|
-
Broadcast.sendMessage({type: METHOD.MULTI_SET, data});
|
|
1160
|
-
return Promise.resolve();
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
if (isClearing) {
|
|
1164
|
-
return Promise.resolve();
|
|
1165
|
-
}
|
|
1166
|
-
|
|
1167
1139
|
const keyValuePairs = prepareKeyValuePairsForStorage(data);
|
|
1168
1140
|
|
|
1169
1141
|
const updatePromises = _.map(keyValuePairs, ([key, value]) => {
|
|
@@ -1227,15 +1199,6 @@ function applyMerge(existingValue, changes, shouldRemoveNullObjectValues) {
|
|
|
1227
1199
|
* @returns {Promise}
|
|
1228
1200
|
*/
|
|
1229
1201
|
function merge(key, changes) {
|
|
1230
|
-
if (!ActiveClientManager.isClientTheLeader()) {
|
|
1231
|
-
Broadcast.sendMessage({type: METHOD.MERGE, key, changes});
|
|
1232
|
-
return Promise.resolve();
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
if (isClearing) {
|
|
1236
|
-
return Promise.resolve();
|
|
1237
|
-
}
|
|
1238
|
-
|
|
1239
1202
|
// Top-level undefined values are ignored
|
|
1240
1203
|
// Therefore we need to prevent adding them to the merge queue
|
|
1241
1204
|
if (_.isUndefined(changes)) {
|
|
@@ -1289,7 +1252,7 @@ function merge(key, changes) {
|
|
|
1289
1252
|
const updatePromise = broadcastUpdate(key, modifiedData, 'merge', hasChanged, wasRemoved);
|
|
1290
1253
|
|
|
1291
1254
|
// If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead.
|
|
1292
|
-
if (!hasChanged ||
|
|
1255
|
+
if (!hasChanged || wasRemoved) {
|
|
1293
1256
|
return updatePromise;
|
|
1294
1257
|
}
|
|
1295
1258
|
|
|
@@ -1344,17 +1307,6 @@ function initializeWithDefaultKeyStates() {
|
|
|
1344
1307
|
* @returns {Promise<void>}
|
|
1345
1308
|
*/
|
|
1346
1309
|
function clear(keysToPreserve = []) {
|
|
1347
|
-
if (!ActiveClientManager.isClientTheLeader()) {
|
|
1348
|
-
Broadcast.sendMessage({type: METHOD.CLEAR, keysToPreserve});
|
|
1349
|
-
return Promise.resolve();
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
if (isClearing) {
|
|
1353
|
-
return Promise.resolve();
|
|
1354
|
-
}
|
|
1355
|
-
|
|
1356
|
-
isClearing = true;
|
|
1357
|
-
|
|
1358
1310
|
return getAllKeys().then((keys) => {
|
|
1359
1311
|
const keysToBeClearedFromStorage = [];
|
|
1360
1312
|
const keyValuesToResetAsCollection = {};
|
|
@@ -1415,8 +1367,6 @@ function clear(keysToPreserve = []) {
|
|
|
1415
1367
|
return Storage.removeItems(keysToBeClearedFromStorage)
|
|
1416
1368
|
.then(() => Storage.multiSet(defaultKeyValuePairs))
|
|
1417
1369
|
.then(() => {
|
|
1418
|
-
isClearing = false;
|
|
1419
|
-
Broadcast.sendMessage({type: METHOD.CLEAR, keysToPreserve});
|
|
1420
1370
|
DevTools.clearState(keysToPreserve);
|
|
1421
1371
|
return Promise.all(updatePromises);
|
|
1422
1372
|
});
|
|
@@ -1571,48 +1521,6 @@ function setMemoryOnlyKeys(keyList) {
|
|
|
1571
1521
|
cache.setRecentKeysLimit(Infinity);
|
|
1572
1522
|
}
|
|
1573
1523
|
|
|
1574
|
-
/**
|
|
1575
|
-
* Sets the callback to be called when the clear finishes executing.
|
|
1576
|
-
* @param {Function} callback
|
|
1577
|
-
*/
|
|
1578
|
-
function onClear(callback) {
|
|
1579
|
-
onClearCallback = callback;
|
|
1580
|
-
}
|
|
1581
|
-
|
|
1582
|
-
/**
|
|
1583
|
-
* Subscribes to the Broadcast channel and executes actions based on the
|
|
1584
|
-
* types of events.
|
|
1585
|
-
*/
|
|
1586
|
-
function subscribeToEvents() {
|
|
1587
|
-
Broadcast.subscribe(({data}) => {
|
|
1588
|
-
if (!ActiveClientManager.isClientTheLeader()) {
|
|
1589
|
-
return;
|
|
1590
|
-
}
|
|
1591
|
-
switch (data.type) {
|
|
1592
|
-
case METHOD.CLEAR:
|
|
1593
|
-
clear(data.keysToPreserve);
|
|
1594
|
-
break;
|
|
1595
|
-
case METHOD.SET:
|
|
1596
|
-
set(data.key, data.value);
|
|
1597
|
-
break;
|
|
1598
|
-
case METHOD.MULTI_SET:
|
|
1599
|
-
multiSet(data.key, data.value);
|
|
1600
|
-
break;
|
|
1601
|
-
case METHOD.MERGE:
|
|
1602
|
-
merge(data.key, data.changes);
|
|
1603
|
-
break;
|
|
1604
|
-
case ON_CLEAR:
|
|
1605
|
-
if (!onClearCallback) {
|
|
1606
|
-
break;
|
|
1607
|
-
}
|
|
1608
|
-
onClearCallback();
|
|
1609
|
-
break;
|
|
1610
|
-
default:
|
|
1611
|
-
break;
|
|
1612
|
-
}
|
|
1613
|
-
});
|
|
1614
|
-
}
|
|
1615
|
-
|
|
1616
1524
|
/**
|
|
1617
1525
|
* Initialize the store with actions and listening for storage events
|
|
1618
1526
|
*
|
|
@@ -1647,15 +1555,6 @@ function init({
|
|
|
1647
1555
|
shouldSyncMultipleInstances = Boolean(global.localStorage),
|
|
1648
1556
|
debugSetState = false,
|
|
1649
1557
|
} = {}) {
|
|
1650
|
-
ActiveClientManager.init();
|
|
1651
|
-
|
|
1652
|
-
ActiveClientManager.isReady().then(() => {
|
|
1653
|
-
if (!ActiveClientManager.isClientTheLeader()) {
|
|
1654
|
-
return;
|
|
1655
|
-
}
|
|
1656
|
-
subscribeToEvents();
|
|
1657
|
-
});
|
|
1658
|
-
|
|
1659
1558
|
if (captureMetrics) {
|
|
1660
1559
|
// The code here is only bundled and applied when the captureMetrics is set
|
|
1661
1560
|
// eslint-disable-next-line no-use-before-define
|
|
@@ -1720,10 +1619,6 @@ const Onyx = {
|
|
|
1720
1619
|
setMemoryOnlyKeys,
|
|
1721
1620
|
tryGetCachedValue,
|
|
1722
1621
|
hasPendingMergeForKey,
|
|
1723
|
-
onClear,
|
|
1724
|
-
isClientManagerReady: ActiveClientManager.isReady,
|
|
1725
|
-
isClientTheLeader: ActiveClientManager.isClientTheLeader,
|
|
1726
|
-
subscribeToClientChange: ActiveClientManager.subscribeToClientChange,
|
|
1727
1622
|
};
|
|
1728
1623
|
|
|
1729
1624
|
/**
|
package/lib/Str.js
CHANGED
|
@@ -24,19 +24,4 @@ function result(parameter, ...args) {
|
|
|
24
24
|
return _.isFunction(parameter) ? parameter(...args) : parameter;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
* A simple GUID generator taken from https://stackoverflow.com/a/32760401/9114791
|
|
29
|
-
*
|
|
30
|
-
* @param {String} [prefix] an optional prefix to put in front of the guid
|
|
31
|
-
* @returns {String}
|
|
32
|
-
*/
|
|
33
|
-
function guid(prefix = '') {
|
|
34
|
-
function s4() {
|
|
35
|
-
return Math.floor((1 + Math.random()) * 0x10000)
|
|
36
|
-
.toString(16)
|
|
37
|
-
.substring(1);
|
|
38
|
-
}
|
|
39
|
-
return `${prefix}${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export {guid, startsWith, result};
|
|
27
|
+
export {startsWith, result};
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {OnyxKey} from './types';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Merges two objects and removes null values if "shouldRemoveNullObjectValues" is set to true
|
|
@@ -14,4 +14,4 @@ declare function fastMerge<T>(target: T, source: T, shouldRemoveNullObjectValues
|
|
|
14
14
|
*/
|
|
15
15
|
declare function formatActionName(method: string, key?: OnyxKey): string;
|
|
16
16
|
|
|
17
|
-
export default {
|
|
17
|
+
export default {fastMerge, formatActionName};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-onyx",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.131",
|
|
4
4
|
"author": "Expensify, Inc.",
|
|
5
5
|
"homepage": "https://expensify.com",
|
|
6
6
|
"description": "State management for React Native",
|
|
@@ -33,14 +33,12 @@
|
|
|
33
33
|
"browser": "web.js",
|
|
34
34
|
"types": "lib/index.d.ts",
|
|
35
35
|
"scripts": {
|
|
36
|
-
"lint": "eslint .",
|
|
37
|
-
"lint-tests": "eslint tests/**",
|
|
38
|
-
"test": "jest",
|
|
39
36
|
"build": "webpack --config webpack.config.js",
|
|
40
37
|
"build:docs": "node buildDocs.js",
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"prettier": "prettier --write ."
|
|
38
|
+
"lint": "eslint .",
|
|
39
|
+
"lint-tests": "eslint tests/**",
|
|
40
|
+
"prettier": "prettier --write .",
|
|
41
|
+
"test": "jest"
|
|
44
42
|
},
|
|
45
43
|
"dependencies": {
|
|
46
44
|
"ascii-table": "0.0.9",
|
|
@@ -51,7 +49,6 @@
|
|
|
51
49
|
"@babel/core": "7.20.12",
|
|
52
50
|
"@babel/preset-env": "^7.20.2",
|
|
53
51
|
"@babel/preset-react": "^7.18.6",
|
|
54
|
-
"@playwright/test": "^1.38.1",
|
|
55
52
|
"@react-native-community/eslint-config": "^2.0.0",
|
|
56
53
|
"@testing-library/jest-native": "^3.4.2",
|
|
57
54
|
"@testing-library/react-native": "^7.0.2",
|
|
@@ -88,9 +85,9 @@
|
|
|
88
85
|
"idb-keyval": "^6.2.1",
|
|
89
86
|
"react": ">=18.1.0",
|
|
90
87
|
"react-dom": ">=18.1.0",
|
|
91
|
-
"react-native-device-info": "^10.3.0",
|
|
92
88
|
"react-native-performance": "^5.1.0",
|
|
93
|
-
"react-native-quick-sqlite": "^8.0.0-beta.2"
|
|
89
|
+
"react-native-quick-sqlite": "^8.0.0-beta.2",
|
|
90
|
+
"react-native-device-info": "^10.3.0"
|
|
94
91
|
},
|
|
95
92
|
"peerDependenciesMeta": {
|
|
96
93
|
"idb-keyval": {
|
|
@@ -120,8 +117,7 @@
|
|
|
120
117
|
],
|
|
121
118
|
"testPathIgnorePatterns": [
|
|
122
119
|
"<rootDir>/node_modules/",
|
|
123
|
-
"<rootDir>/tests/unit/mocks/"
|
|
124
|
-
"<rootDir>/tests/e2e/"
|
|
120
|
+
"<rootDir>/tests/unit/mocks/"
|
|
125
121
|
],
|
|
126
122
|
"testMatch": [
|
|
127
123
|
"**/tests/unit/**/*.[jt]s?(x)",
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Determines when the client is ready. We need to wait till the init method is called and the leader message is sent.
|
|
3
|
-
*/
|
|
4
|
-
declare function isReady(): Promise<void>;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Subscribes to the broadcast channel to listen for messages from other tabs, so that
|
|
8
|
-
* all tabs agree on who the leader is, which should always be the last tab to open.
|
|
9
|
-
*/
|
|
10
|
-
declare function init(): void;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Returns a boolean indicating if the current client is the leader.
|
|
14
|
-
*/
|
|
15
|
-
declare function isClientTheLeader(): boolean;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Subscribes to when the client changes.
|
|
19
|
-
*/
|
|
20
|
-
declare function subscribeToClientChange(callback: () => {}): void;
|
|
21
|
-
|
|
22
|
-
export {isReady, init, isClientTheLeader, subscribeToClientChange};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* For native devices, there will never be more than one
|
|
3
|
-
* client running at a time, so this lib is a big no-op
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
function isReady() {
|
|
7
|
-
return Promise.resolve();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function isClientTheLeader() {
|
|
11
|
-
return true;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function init() {}
|
|
15
|
-
|
|
16
|
-
function subscribeToClientChange() {}
|
|
17
|
-
|
|
18
|
-
export {isClientTheLeader, init, isReady, subscribeToClientChange};
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* When you have many tabs in one browser, the data of Onyx is shared between all of them. Since we persist write requests in Onyx, we need to ensure that
|
|
3
|
-
* only one tab is processing those saved requests or we would be duplicating data (or creating errors).
|
|
4
|
-
* This file ensures exactly that by tracking all the clientIDs connected, storing the most recent one last and it considers that last clientID the "leader".
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as Str from '../Str';
|
|
8
|
-
import * as Broadcast from '../broadcast';
|
|
9
|
-
|
|
10
|
-
const NEW_LEADER_MESSAGE = 'NEW_LEADER';
|
|
11
|
-
const REMOVED_LEADER_MESSAGE = 'REMOVE_LEADER';
|
|
12
|
-
|
|
13
|
-
const clientID = Str.guid();
|
|
14
|
-
const subscribers = [];
|
|
15
|
-
let timestamp = null;
|
|
16
|
-
|
|
17
|
-
let activeClientID = null;
|
|
18
|
-
let setIsReady = () => {};
|
|
19
|
-
const isReadyPromise = new Promise((resolve) => {
|
|
20
|
-
setIsReady = resolve;
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Determines when the client is ready. We need to wait both till we saved our ID in onyx AND the init method was called
|
|
25
|
-
* @returns {Promise}
|
|
26
|
-
*/
|
|
27
|
-
function isReady() {
|
|
28
|
-
return isReadyPromise;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Returns a boolean indicating if the current client is the leader.
|
|
33
|
-
*
|
|
34
|
-
* @returns {Boolean}
|
|
35
|
-
*/
|
|
36
|
-
function isClientTheLeader() {
|
|
37
|
-
return activeClientID === clientID;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Subscribes to when the client changes.
|
|
42
|
-
* @param {Function} callback
|
|
43
|
-
*/
|
|
44
|
-
function subscribeToClientChange(callback) {
|
|
45
|
-
subscribers.push(callback);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Subscribe to the broadcast channel to listen for messages from other tabs, so that
|
|
50
|
-
* all tabs agree on who the leader is, which should always be the last tab to open.
|
|
51
|
-
*/
|
|
52
|
-
function init() {
|
|
53
|
-
Broadcast.subscribe((message) => {
|
|
54
|
-
switch (message.data.type) {
|
|
55
|
-
case NEW_LEADER_MESSAGE: {
|
|
56
|
-
// Only update the active leader if the message received was from another
|
|
57
|
-
// tab that initialized after the current one; if the timestamps are the
|
|
58
|
-
// same, it uses the client ID to tie-break
|
|
59
|
-
const isTimestampEqual = timestamp === message.data.timestamp;
|
|
60
|
-
const isTimestampNewer = timestamp > message.data.timestamp;
|
|
61
|
-
if (isClientTheLeader() && (isTimestampNewer || (isTimestampEqual && clientID > message.data.clientID))) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
activeClientID = message.data.clientID;
|
|
65
|
-
|
|
66
|
-
subscribers.forEach((callback) => callback());
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
case REMOVED_LEADER_MESSAGE:
|
|
70
|
-
activeClientID = clientID;
|
|
71
|
-
timestamp = Date.now();
|
|
72
|
-
Broadcast.sendMessage({type: NEW_LEADER_MESSAGE, clientID, timestamp});
|
|
73
|
-
subscribers.forEach((callback) => callback());
|
|
74
|
-
break;
|
|
75
|
-
default:
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
activeClientID = clientID;
|
|
81
|
-
timestamp = Date.now();
|
|
82
|
-
|
|
83
|
-
Broadcast.sendMessage({type: NEW_LEADER_MESSAGE, clientID, timestamp});
|
|
84
|
-
setIsReady();
|
|
85
|
-
|
|
86
|
-
window.addEventListener('beforeunload', () => {
|
|
87
|
-
if (!isClientTheLeader()) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
Broadcast.sendMessage({type: REMOVED_LEADER_MESSAGE, clientID});
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export {isClientTheLeader, init, isReady, subscribeToClientChange};
|
package/lib/broadcast/index.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sends a message to the broadcast channel.
|
|
3
|
-
*/
|
|
4
|
-
declare function sendMessage(message: string): void;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Subscribes to the broadcast channel. Every time a new message
|
|
8
|
-
* is received, the callback is called.
|
|
9
|
-
*/
|
|
10
|
-
declare function subscribe(callback: () => {}): void;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Disconnects from the broadcast channel.
|
|
14
|
-
*/
|
|
15
|
-
declare function disconnect(): void;
|
|
16
|
-
|
|
17
|
-
export {sendMessage, subscribe, disconnect};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* For native devices, there will never be more than one
|
|
3
|
-
* client running at a time, so this lib is a big no-op
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
function sendMessage() {}
|
|
7
|
-
|
|
8
|
-
function subscribe() {}
|
|
9
|
-
|
|
10
|
-
function disconnect() {}
|
|
11
|
-
|
|
12
|
-
export {sendMessage, subscribe, disconnect};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
const BROADCAST_ONYX = 'BROADCAST_ONYX';
|
|
2
|
-
|
|
3
|
-
const subscriptions = [];
|
|
4
|
-
const channel = new BroadcastChannel(BROADCAST_ONYX);
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Sends a message to the broadcast channel.
|
|
8
|
-
* @param {String} message
|
|
9
|
-
*/
|
|
10
|
-
function sendMessage(message) {
|
|
11
|
-
channel.postMessage(message);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Subscribes to the broadcast channel. Every time a new message
|
|
16
|
-
* is received, the callback is called.
|
|
17
|
-
* @param {Function} callback
|
|
18
|
-
*/
|
|
19
|
-
function subscribe(callback) {
|
|
20
|
-
subscriptions.push(callback);
|
|
21
|
-
channel.onmessage = (message) => {
|
|
22
|
-
subscriptions.forEach((c) => c(message));
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Disconnects from the broadcast channel.
|
|
28
|
-
*/
|
|
29
|
-
function disconnect() {
|
|
30
|
-
channel.close();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export {sendMessage, subscribe, disconnect};
|