react-native-onyx 1.0.119 → 1.0.121

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,22 @@
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};
@@ -0,0 +1,23 @@
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 {
19
+ isClientTheLeader,
20
+ init,
21
+ isReady,
22
+ subscribeToClientChange,
23
+ };
@@ -0,0 +1,99 @@
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 {
95
+ isClientTheLeader,
96
+ init,
97
+ isReady,
98
+ subscribeToClientChange,
99
+ };
package/lib/Logger.js CHANGED
@@ -28,8 +28,4 @@ function logInfo(message) {
28
28
  logger({message: `[Onyx] ${message}`, level: 'info'});
29
29
  }
30
30
 
31
- export {
32
- registerLogger,
33
- logInfo,
34
- logAlert,
35
- };
31
+ export {registerLogger, logInfo, logAlert};
package/lib/MDTable.js CHANGED
@@ -24,17 +24,16 @@ class MDTable extends AsciTable {
24
24
  toString() {
25
25
  // Ignore modifying the first |---| for titled tables
26
26
  let idx = this.getTitle() ? -2 : -1;
27
- const ascii = super.toString()
28
- .replace(/-\|/g, () => {
29
- /* we replace "----|" with "---:|" to align the data to the right in MD */
30
- idx++;
27
+ const ascii = super.toString().replace(/-\|/g, () => {
28
+ /* we replace "----|" with "---:|" to align the data to the right in MD */
29
+ idx++;
31
30
 
32
- if (idx < 0 || this.leftAlignedCols.includes(idx)) {
33
- return '-|';
34
- }
31
+ if (idx < 0 || this.leftAlignedCols.includes(idx)) {
32
+ return '-|';
33
+ }
35
34
 
36
- return ':|';
37
- });
35
+ return ':|';
36
+ });
38
37
 
39
38
  // strip the top and the bottom row (----) to make an MD table
40
39
  const md = ascii.split('\n').slice(1, -1).join('\n');
@@ -52,16 +51,14 @@ class MDTable extends AsciTable {
52
51
  * @param {Array} [options.rows] The table can be initialized with row. Rows can also be added by `addRow`
53
52
  * @returns {MDTable}
54
53
  */
55
- MDTable.factory = ({
56
- title, heading, leftAlignedCols = [], rows = [],
57
- }) => {
54
+ MDTable.factory = ({title, heading, leftAlignedCols = [], rows = []}) => {
58
55
  const table = new MDTable({title, heading, rows});
59
56
  table.leftAlignedCols = leftAlignedCols;
60
57
 
61
58
  /* By default we want everything aligned to the right as most values are numbers
62
- * we just override the columns that are not right aligned */
59
+ * we just override the columns that are not right aligned */
63
60
  heading.forEach((name, idx) => table.setAlign(idx, AsciTable.RIGHT));
64
- leftAlignedCols.forEach(idx => table.setAlign(idx, AsciTable.LEFT));
61
+ leftAlignedCols.forEach((idx) => table.setAlign(idx, AsciTable.LEFT));
65
62
 
66
63
  return table;
67
64
  };
package/lib/Onyx.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {Component} from 'react';
2
2
  import * as Logger from './Logger';
3
+ import * as ActiveClientManager from './ActiveClientManager';
3
4
  import {CollectionKey, CollectionKeyBase, DeepRecord, KeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey} from './types';
4
5
 
5
6
  /**
@@ -243,10 +244,7 @@ declare function clear(keysToPreserve?: OnyxKey[]): Promise<void>;
243
244
  * @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT`
244
245
  * @param collection Object collection keyed by individual collection member keys and values
245
246
  */
246
- declare function mergeCollection<TKey extends CollectionKeyBase, TMap>(
247
- collectionKey: TKey,
248
- collection: Collection<TKey, TMap, NullishDeep<KeyValueMapping[TKey]>>,
249
- ): Promise<void>;
247
+ declare function mergeCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: Collection<TKey, TMap, NullishDeep<KeyValueMapping[TKey]>>): Promise<void>;
250
248
 
251
249
  /**
252
250
  * Insert API responses and lifecycle data into Onyx
@@ -293,6 +291,11 @@ declare function hasPendingMergeForKey(key: OnyxKey): boolean;
293
291
  */
294
292
  declare function setMemoryOnlyKeys(keyList: OnyxKey[]): void;
295
293
 
294
+ /**
295
+ * Sets the callback to be called when the clear finishes executing.
296
+ */
297
+ declare function onClear(callback: () => void): void;
298
+
296
299
  declare const Onyx: {
297
300
  connect: typeof connect;
298
301
  disconnect: typeof disconnect;
@@ -311,6 +314,10 @@ declare const Onyx: {
311
314
  isSafeEvictionKey: typeof isSafeEvictionKey;
312
315
  METHOD: typeof METHOD;
313
316
  setMemoryOnlyKeys: typeof setMemoryOnlyKeys;
317
+ onClear: typeof onClear;
318
+ isClientManagerReady: typeof ActiveClientManager.isReady,
319
+ isClientTheLeader: typeof ActiveClientManager.isClientTheLeader,
320
+ subscribeToClientChange: typeof ActiveClientManager.subscribeToClientChange,
314
321
  };
315
322
 
316
323
  export default Onyx;