react-native-onyx 3.0.88 → 3.0.90
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/API.md +0 -2
- package/README.md +2 -12
- package/dist/Onyx.d.ts +0 -2
- package/dist/Onyx.js +0 -2
- package/dist/OnyxConnectionManager.js +19 -17
- package/dist/OnyxUtils.js +19 -55
- package/dist/types.d.ts +12 -28
- package/dist/useOnyx.d.ts +2 -3
- package/dist/useOnyx.js +1 -9
- package/package.json +1 -1
package/API.md
CHANGED
|
@@ -80,7 +80,6 @@ This method will be deprecated soon. Please use `Onyx.connectWithoutView()` inst
|
|
|
80
80
|
| connectOptions | The options object that will define the behavior of the connection. |
|
|
81
81
|
| connectOptions.key | The Onyx key to subscribe to. |
|
|
82
82
|
| connectOptions.callback | A function that will be called when the Onyx data we are subscribed changes. |
|
|
83
|
-
| connectOptions.waitForCollectionCallback | If set to `true`, it will return the entire collection to the callback as a single object. |
|
|
84
83
|
| connectOptions.selector | This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.** Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
|
|
85
84
|
|
|
86
85
|
**Example**
|
|
@@ -103,7 +102,6 @@ Connects to an Onyx key given the options passed and listens to its changes.
|
|
|
103
102
|
| connectOptions | The options object that will define the behavior of the connection. |
|
|
104
103
|
| connectOptions.key | The Onyx key to subscribe to. |
|
|
105
104
|
| connectOptions.callback | A function that will be called when the Onyx data we are subscribed changes. |
|
|
106
|
-
| connectOptions.waitForCollectionCallback | If set to `true`, it will return the entire collection to the callback as a single object. |
|
|
107
105
|
| connectOptions.selector | This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.** Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
|
|
108
106
|
|
|
109
107
|
**Example**
|
package/README.md
CHANGED
|
@@ -257,20 +257,10 @@ export default MyComponent;
|
|
|
257
257
|
This will add a prop to the component called `allReports` which is an object of collection member key/values. Changes to the individual member keys will modify the entire object and new props will be passed with each individual key update. The prop doesn't update on the initial rendering of the component until the entire collection has been read out of Onyx.
|
|
258
258
|
|
|
259
259
|
```js
|
|
260
|
-
Onyx.connect({key: ONYXKEYS.COLLECTION.REPORT}, callback: (
|
|
260
|
+
Onyx.connect({key: ONYXKEYS.COLLECTION.REPORT}, callback: (allReports, collectionKey) => {...});
|
|
261
261
|
```
|
|
262
262
|
|
|
263
|
-
This will fire the callback once
|
|
264
|
-
|
|
265
|
-
```js
|
|
266
|
-
Onyx.connect({
|
|
267
|
-
key: ONYXKEYS.COLLECTION.REPORT,
|
|
268
|
-
waitForCollectionCallback: true,
|
|
269
|
-
callback: (allReports, collectionKey, sourceValue) => {...},
|
|
270
|
-
});
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
This final option forces `Onyx.connect()` to behave more like `useOnyx()` and only update the callback once with the entire collection initially and later with an updated version of the collection when individual keys update. The `sourceValue` parameter contains only the specific keys and values that triggered the current update, which can be useful for optimizing your code to only process what changed. This parameter is not available when `waitForCollectionCallback` is false or the key is not a collection key.
|
|
263
|
+
This will fire the callback once with the entire collection initially and later with an updated version of the collection when individual keys update.
|
|
274
264
|
|
|
275
265
|
### Performance Considerations When Using Collections
|
|
276
266
|
|
package/dist/Onyx.d.ts
CHANGED
|
@@ -18,7 +18,6 @@ declare function init({ keys, initialKeyStates, evictableKeys, shouldSyncMultipl
|
|
|
18
18
|
* @param connectOptions The options object that will define the behavior of the connection.
|
|
19
19
|
* @param connectOptions.key The Onyx key to subscribe to.
|
|
20
20
|
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
|
|
21
|
-
* @param connectOptions.waitForCollectionCallback If set to `true`, it will return the entire collection to the callback as a single object.
|
|
22
21
|
* @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.**
|
|
23
22
|
* Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render
|
|
24
23
|
* when the subset of data changes. Otherwise, any change of data on any property would normally
|
|
@@ -40,7 +39,6 @@ declare function connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TK
|
|
|
40
39
|
* @param connectOptions The options object that will define the behavior of the connection.
|
|
41
40
|
* @param connectOptions.key The Onyx key to subscribe to.
|
|
42
41
|
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
|
|
43
|
-
* @param connectOptions.waitForCollectionCallback If set to `true`, it will return the entire collection to the callback as a single object.
|
|
44
42
|
* @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.**
|
|
45
43
|
* Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render
|
|
46
44
|
* when the subset of data changes. Otherwise, any change of data on any property would normally
|
package/dist/Onyx.js
CHANGED
|
@@ -93,7 +93,6 @@ function init({ keys = {}, initialKeyStates = {}, evictableKeys = [], shouldSync
|
|
|
93
93
|
* @param connectOptions The options object that will define the behavior of the connection.
|
|
94
94
|
* @param connectOptions.key The Onyx key to subscribe to.
|
|
95
95
|
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
|
|
96
|
-
* @param connectOptions.waitForCollectionCallback If set to `true`, it will return the entire collection to the callback as a single object.
|
|
97
96
|
* @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.**
|
|
98
97
|
* Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render
|
|
99
98
|
* when the subset of data changes. Otherwise, any change of data on any property would normally
|
|
@@ -117,7 +116,6 @@ function connect(connectOptions) {
|
|
|
117
116
|
* @param connectOptions The options object that will define the behavior of the connection.
|
|
118
117
|
* @param connectOptions.key The Onyx key to subscribe to.
|
|
119
118
|
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
|
|
120
|
-
* @param connectOptions.waitForCollectionCallback If set to `true`, it will return the entire collection to the callback as a single object.
|
|
121
119
|
* @param connectOptions.selector This will be used to subscribe to a subset of an Onyx key's data. **Only used inside `useOnyx()` hook.**
|
|
122
120
|
* Using this setting on `useOnyx()` can have very positive performance benefits because the component will only re-render
|
|
123
121
|
* when the subset of data changes. Otherwise, any change of data on any property would normally
|
|
@@ -60,18 +60,17 @@ class OnyxConnectionManager {
|
|
|
60
60
|
* according to their purpose and effect they produce in the Onyx connection.
|
|
61
61
|
*/
|
|
62
62
|
generateConnectionID(connectOptions) {
|
|
63
|
-
const { key, reuseConnection
|
|
63
|
+
const { key, reuseConnection } = connectOptions;
|
|
64
64
|
// The current session ID is appended to the connection ID so we can have different connections
|
|
65
65
|
// after an `Onyx.clear()` operation.
|
|
66
66
|
let suffix = `,sessionID=${this.sessionID}`;
|
|
67
|
-
// We will generate a unique ID
|
|
68
|
-
//
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
if (reuseConnection === false || (OnyxKeys_1.default.isCollectionKey(key) && (waitForCollectionCallback === undefined || waitForCollectionCallback === false))) {
|
|
67
|
+
// We will generate a unique ID when `reuseConnection` is `false`, which means the subscriber
|
|
68
|
+
// explicitly wants the connection to not be reused. Collection-root subscriptions are now always
|
|
69
|
+
// snapshot mode, so they can be reused like any other connection.
|
|
70
|
+
if (reuseConnection === false) {
|
|
72
71
|
suffix += `,uniqueID=${Str.guid()}`;
|
|
73
72
|
}
|
|
74
|
-
return `onyxKey=${key}
|
|
73
|
+
return `onyxKey=${key}${suffix}`;
|
|
75
74
|
}
|
|
76
75
|
/**
|
|
77
76
|
* Fires all the subscribers callbacks associated with that connection ID.
|
|
@@ -82,11 +81,16 @@ class OnyxConnectionManager {
|
|
|
82
81
|
return;
|
|
83
82
|
}
|
|
84
83
|
for (const callback of connection.callbacks.values()) {
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
try {
|
|
85
|
+
if (OnyxKeys_1.default.isCollectionKey(connection.onyxKey)) {
|
|
86
|
+
callback(connection.cachedCallbackValue, connection.cachedCallbackKey);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
callback(connection.cachedCallbackValue, connection.cachedCallbackKey);
|
|
90
|
+
}
|
|
87
91
|
}
|
|
88
|
-
|
|
89
|
-
callback
|
|
92
|
+
catch (error) {
|
|
93
|
+
Logger.logAlert(`[ConnectionManager] Subscriber callback threw an error for key '${connection.onyxKey}': ${error}`);
|
|
90
94
|
}
|
|
91
95
|
}
|
|
92
96
|
}
|
|
@@ -103,7 +107,7 @@ class OnyxConnectionManager {
|
|
|
103
107
|
const callbackID = String(this.lastCallbackID++);
|
|
104
108
|
// If there is no connection yet for that connection ID, we create a new one.
|
|
105
109
|
if (!connectionMetadata) {
|
|
106
|
-
const callback = (value, key
|
|
110
|
+
const callback = (value, key) => {
|
|
107
111
|
const createdConnection = this.connectionsMap.get(connectionID);
|
|
108
112
|
if (createdConnection) {
|
|
109
113
|
// We signal that the first connection was made and now any new subscribers
|
|
@@ -111,17 +115,15 @@ class OnyxConnectionManager {
|
|
|
111
115
|
createdConnection.isConnectionMade = true;
|
|
112
116
|
createdConnection.cachedCallbackValue = value;
|
|
113
117
|
createdConnection.cachedCallbackKey = key;
|
|
114
|
-
createdConnection.sourceValue = sourceValue;
|
|
115
118
|
this.fireCallbacks(connectionID);
|
|
116
119
|
}
|
|
117
120
|
};
|
|
118
|
-
subscriptionID = OnyxUtils_1.default.subscribeToKey(Object.assign(Object.assign({}, connectOptions), { callback
|
|
121
|
+
subscriptionID = OnyxUtils_1.default.subscribeToKey(Object.assign(Object.assign({}, connectOptions), { callback }));
|
|
119
122
|
connectionMetadata = {
|
|
120
123
|
subscriptionID,
|
|
121
124
|
onyxKey: connectOptions.key,
|
|
122
125
|
isConnectionMade: false,
|
|
123
126
|
callbacks: new Map(),
|
|
124
|
-
waitForCollectionCallback: connectOptions.waitForCollectionCallback,
|
|
125
127
|
};
|
|
126
128
|
this.connectionsMap.set(connectionID, connectionMetadata);
|
|
127
129
|
}
|
|
@@ -134,8 +136,8 @@ class OnyxConnectionManager {
|
|
|
134
136
|
// Defer the callback execution to the next tick of the event loop.
|
|
135
137
|
// This ensures that the current execution flow completes and the result connection object is available when the callback fires.
|
|
136
138
|
Promise.resolve().then(() => {
|
|
137
|
-
var _a
|
|
138
|
-
(
|
|
139
|
+
var _a;
|
|
140
|
+
(_a = connectOptions.callback) === null || _a === void 0 ? void 0 : _a.call(connectOptions, connectionMetadata.cachedCallbackValue, connectionMetadata.cachedCallbackKey);
|
|
139
141
|
});
|
|
140
142
|
}
|
|
141
143
|
return { id: connectionID, callbackID };
|
package/dist/OnyxUtils.js
CHANGED
|
@@ -482,29 +482,8 @@ function keysChanged(collectionKey, partialCollection, partialPreviousCollection
|
|
|
482
482
|
continue;
|
|
483
483
|
}
|
|
484
484
|
try {
|
|
485
|
-
lastConnectionCallbackData.set(subscriber.subscriptionID, {
|
|
486
|
-
|
|
487
|
-
matchedKey: subscriber.key,
|
|
488
|
-
});
|
|
489
|
-
if (subscriber.waitForCollectionCallback) {
|
|
490
|
-
subscriber.callback(cachedCollection, subscriber.key, partialCollection);
|
|
491
|
-
continue;
|
|
492
|
-
}
|
|
493
|
-
// Not using waitForCollectionCallback — notify per changed key.
|
|
494
|
-
// Re-check the subscription on each iteration because the callback may
|
|
495
|
-
// synchronously disconnect itself (removing it from callbackToStateMapping),
|
|
496
|
-
// in which case we must stop firing further callbacks for this subscriber.
|
|
497
|
-
for (const dataKey of changedMemberKeys) {
|
|
498
|
-
const currentSubscriber = callbackToStateMapping[subID];
|
|
499
|
-
if (!currentSubscriber || typeof currentSubscriber.callback !== 'function') {
|
|
500
|
-
break;
|
|
501
|
-
}
|
|
502
|
-
if (cachedCollection[dataKey] === previousCollection[dataKey]) {
|
|
503
|
-
continue;
|
|
504
|
-
}
|
|
505
|
-
const currentSubscriberCallback = currentSubscriber.callback;
|
|
506
|
-
currentSubscriberCallback(cachedCollection[dataKey], dataKey);
|
|
507
|
-
}
|
|
485
|
+
lastConnectionCallbackData.set(subscriber.subscriptionID, { value: cachedCollection, matchedKey: subscriber.key });
|
|
486
|
+
subscriber.callback(cachedCollection, subscriber.key);
|
|
508
487
|
}
|
|
509
488
|
catch (error) {
|
|
510
489
|
Logger.logAlert(`[OnyxUtils.keysChanged] Subscriber callback threw an error for key '${collectionKey}': ${error}`);
|
|
@@ -574,9 +553,9 @@ function keyChanged(key, value, canUpdateSubscriber = () => true, isProcessingCo
|
|
|
574
553
|
if (lastData && lastData.matchedKey === key && lastData.value === value) {
|
|
575
554
|
continue;
|
|
576
555
|
}
|
|
577
|
-
if (OnyxKeys_1.default.isCollectionKey(subscriber.key)
|
|
578
|
-
// Skip individual key changes
|
|
579
|
-
//
|
|
556
|
+
if (OnyxKeys_1.default.isCollectionKey(subscriber.key)) {
|
|
557
|
+
// Skip individual key changes during collection updates to prevent duplicate
|
|
558
|
+
// callbacks - the collection update will handle this properly.
|
|
580
559
|
if (isProcessingCollectionUpdate) {
|
|
581
560
|
continue;
|
|
582
561
|
}
|
|
@@ -587,13 +566,8 @@ function keyChanged(key, value, canUpdateSubscriber = () => true, isProcessingCo
|
|
|
587
566
|
cachedCollection = getCachedCollection(subscriber.key);
|
|
588
567
|
cachedCollections[subscriber.key] = cachedCollection;
|
|
589
568
|
}
|
|
590
|
-
lastConnectionCallbackData.set(subscriber.subscriptionID, {
|
|
591
|
-
|
|
592
|
-
matchedKey: subscriber.key,
|
|
593
|
-
});
|
|
594
|
-
subscriber.callback(cachedCollection, subscriber.key, {
|
|
595
|
-
[key]: value,
|
|
596
|
-
});
|
|
569
|
+
lastConnectionCallbackData.set(subscriber.subscriptionID, { value: cachedCollection, matchedKey: subscriber.key });
|
|
570
|
+
subscriber.callback(cachedCollection, subscriber.key);
|
|
597
571
|
continue;
|
|
598
572
|
}
|
|
599
573
|
const subscriberCallback = subscriber.callback;
|
|
@@ -616,17 +590,17 @@ function keyChanged(key, value, canUpdateSubscriber = () => true, isProcessingCo
|
|
|
616
590
|
* Sends the data obtained from the keys to the connection.
|
|
617
591
|
*/
|
|
618
592
|
function sendDataToConnection(mapping, matchedKey) {
|
|
619
|
-
var _a
|
|
593
|
+
var _a;
|
|
620
594
|
// If the mapping no longer exists then we should not send any data.
|
|
621
595
|
// This means our subscriber was disconnected.
|
|
622
596
|
if (!callbackToStateMapping[mapping.subscriptionID]) {
|
|
623
597
|
return;
|
|
624
598
|
}
|
|
625
599
|
// Always read the latest value from cache to avoid stale or duplicate data.
|
|
626
|
-
// For collection subscribers
|
|
600
|
+
// For collection-root subscribers, read the full collection.
|
|
627
601
|
// For individual key subscribers, read just that key's value.
|
|
628
602
|
let value;
|
|
629
|
-
if (OnyxKeys_1.default.isCollectionKey(mapping.key)
|
|
603
|
+
if (OnyxKeys_1.default.isCollectionKey(mapping.key)) {
|
|
630
604
|
const collection = getCachedCollection(mapping.key);
|
|
631
605
|
value = Object.keys(collection).length > 0 ? collection : undefined;
|
|
632
606
|
}
|
|
@@ -642,7 +616,7 @@ function sendDataToConnection(mapping, matchedKey) {
|
|
|
642
616
|
if (lastData && lastData.matchedKey === matchedKey && (0, fast_equals_1.shallowEqual)(lastData.value, value)) {
|
|
643
617
|
return;
|
|
644
618
|
}
|
|
645
|
-
(
|
|
619
|
+
(_a = mapping.callback) === null || _a === void 0 ? void 0 : _a.call(mapping, value, matchedKey);
|
|
646
620
|
}
|
|
647
621
|
/**
|
|
648
622
|
* Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.
|
|
@@ -984,27 +958,17 @@ function subscribeToKey(connectOptions) {
|
|
|
984
958
|
if (mapping.key) {
|
|
985
959
|
OnyxCache_1.default.addNullishStorageKey(mapping.key);
|
|
986
960
|
}
|
|
987
|
-
const matchedKey = OnyxKeys_1.default.isCollectionKey(mapping.key)
|
|
961
|
+
const matchedKey = OnyxKeys_1.default.isCollectionKey(mapping.key) ? mapping.key : undefined;
|
|
988
962
|
// Here we cannot use batching because the nullish value is expected to be set immediately for default props
|
|
989
963
|
// or they will be undefined.
|
|
990
964
|
sendDataToConnection(mapping, matchedKey);
|
|
991
965
|
return;
|
|
992
966
|
}
|
|
993
|
-
// When using a callback subscriber
|
|
994
|
-
//
|
|
995
|
-
// combined with a subscription to a collection key.
|
|
967
|
+
// When using a callback subscriber, a subscription to a collection key combines all matching
|
|
968
|
+
// member values into a single object and makes one call with the whole collection object.
|
|
996
969
|
if (typeof mapping.callback === 'function') {
|
|
997
970
|
if (OnyxKeys_1.default.isCollectionKey(mapping.key)) {
|
|
998
|
-
|
|
999
|
-
getCollectionDataAndSendAsObject(matchingKeys, mapping);
|
|
1000
|
-
return;
|
|
1001
|
-
}
|
|
1002
|
-
// We did not opt into using waitForCollectionCallback mode so the callback is called for every matching key.
|
|
1003
|
-
multiGet(matchingKeys).then(() => {
|
|
1004
|
-
for (const key of matchingKeys) {
|
|
1005
|
-
sendDataToConnection(mapping, key);
|
|
1006
|
-
}
|
|
1007
|
-
});
|
|
971
|
+
getCollectionDataAndSendAsObject(matchingKeys, mapping);
|
|
1008
972
|
return;
|
|
1009
973
|
}
|
|
1010
974
|
// If we are not subscribed to a collection key then there's only a single key to send an update for.
|
|
@@ -1217,7 +1181,7 @@ function multiSetWithRetry(data, retryAttempt) {
|
|
|
1217
1181
|
// and subscriber state, matching the original per-key notification semantics.
|
|
1218
1182
|
OnyxCache_1.default.set(key, value);
|
|
1219
1183
|
// Skip subscriber notification on retry — already notified on attempt 0.
|
|
1220
|
-
//
|
|
1184
|
+
// Collection-root subscribers re-fire on every keyChanged by contract.
|
|
1221
1185
|
if (!retryAttempt) {
|
|
1222
1186
|
keyChanged(key, value);
|
|
1223
1187
|
}
|
|
@@ -1296,7 +1260,7 @@ function setCollectionWithRetry({ collectionKey, collection }, retryAttempt) {
|
|
|
1296
1260
|
for (const [key, value] of keyValuePairs)
|
|
1297
1261
|
OnyxCache_1.default.set(key, value);
|
|
1298
1262
|
// Skip subscriber notification on retry — already notified on attempt 0.
|
|
1299
|
-
//
|
|
1263
|
+
// Collection-root subscribers re-fire on every keysChanged by contract.
|
|
1300
1264
|
if (!retryAttempt) {
|
|
1301
1265
|
keysChanged(collectionKey, mutableCollection, previousCollection);
|
|
1302
1266
|
}
|
|
@@ -1419,7 +1383,7 @@ function mergeCollectionWithPatches({ collectionKey, collection, mergeReplaceNul
|
|
|
1419
1383
|
const previousCollection = getCachedCollection(collectionKey, existingKeys);
|
|
1420
1384
|
OnyxCache_1.default.merge(finalMergedCollection);
|
|
1421
1385
|
// Skip subscriber notification on retry — already notified on attempt 0.
|
|
1422
|
-
//
|
|
1386
|
+
// Collection-root subscribers re-fire on every keysChanged by contract.
|
|
1423
1387
|
if (!retryAttempt) {
|
|
1424
1388
|
keysChanged(collectionKey, finalMergedCollection, previousCollection);
|
|
1425
1389
|
}
|
|
@@ -1494,7 +1458,7 @@ function partialSetCollection({ collectionKey, collection }, retryAttempt) {
|
|
|
1494
1458
|
for (const [key, value] of keyValuePairs)
|
|
1495
1459
|
OnyxCache_1.default.set(key, value);
|
|
1496
1460
|
// Skip subscriber notification on retry — already notified on attempt 0.
|
|
1497
|
-
//
|
|
1461
|
+
// Collection-root subscribers re-fire on every keysChanged by contract.
|
|
1498
1462
|
if (!retryAttempt) {
|
|
1499
1463
|
keysChanged(collectionKey, mutableCollection, previousCollection);
|
|
1500
1464
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -187,37 +187,21 @@ type BaseConnectOptions = {
|
|
|
187
187
|
/** Represents the callback function used in `Onyx.connect()` method with a regular key. */
|
|
188
188
|
type DefaultConnectCallback<TKey extends OnyxKey> = (value: OnyxEntry<KeyValueMapping[TKey]>, key: TKey) => void;
|
|
189
189
|
/** Represents the callback function used in `Onyx.connect()` method with a collection key. */
|
|
190
|
-
type CollectionConnectCallback<TKey extends OnyxKey> = (value: NonUndefined<OnyxCollection<KeyValueMapping[TKey]>>, key: TKey
|
|
191
|
-
/** Represents the options used in `Onyx.connect()` method with a regular key. */
|
|
192
|
-
type DefaultConnectOptions<TKey extends OnyxKey> = BaseConnectOptions & {
|
|
193
|
-
/** The Onyx key to subscribe to. */
|
|
194
|
-
key: TKey;
|
|
195
|
-
/** A function that will be called when the Onyx data we are subscribed changes. */
|
|
196
|
-
callback?: DefaultConnectCallback<TKey>;
|
|
197
|
-
/** If set to `true`, it will return the entire collection to the callback as a single object. */
|
|
198
|
-
waitForCollectionCallback?: false;
|
|
199
|
-
};
|
|
200
|
-
/** Represents the options used in `Onyx.connect()` method with a collection key. */
|
|
201
|
-
type CollectionConnectOptions<TKey extends OnyxKey> = BaseConnectOptions & {
|
|
202
|
-
/** The Onyx key to subscribe to. */
|
|
203
|
-
key: TKey extends CollectionKeyBase ? TKey : never;
|
|
204
|
-
/** A function that will be called when the Onyx data we are subscribed changes. */
|
|
205
|
-
callback?: CollectionConnectCallback<TKey>;
|
|
206
|
-
/** If set to `true`, it will return the entire collection to the callback as a single object. */
|
|
207
|
-
waitForCollectionCallback: true;
|
|
208
|
-
};
|
|
190
|
+
type CollectionConnectCallback<TKey extends OnyxKey> = (value: NonUndefined<OnyxCollection<KeyValueMapping[TKey]>>, key: TKey) => void;
|
|
209
191
|
/**
|
|
210
192
|
* Represents the options used in `Onyx.connect()` method.
|
|
211
|
-
* The type is built from `DefaultConnectOptions`/`CollectionConnectOptions` depending on the `waitForCollectionCallback` property.
|
|
212
|
-
* It includes two different forms, depending on whether we are waiting for a collection callback or not.
|
|
213
|
-
*
|
|
214
|
-
* If `waitForCollectionCallback` is `true`, it expects `key` to be a Onyx collection key and `callback` will be triggered with the whole collection
|
|
215
|
-
* and will pass `value` as an `OnyxCollection`.
|
|
216
193
|
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
194
|
+
* For a collection root key (e.g. `ONYXKEYS.COLLECTION.REPORT`), the callback fires
|
|
195
|
+
* with the entire collection object whenever any member changes (signature
|
|
196
|
+
* `(collection, key)`). For any other key, the callback fires with the value at
|
|
197
|
+
* that key (signature `(value, key)`).
|
|
219
198
|
*/
|
|
220
|
-
type ConnectOptions<TKey extends OnyxKey> =
|
|
199
|
+
type ConnectOptions<TKey extends OnyxKey> = BaseConnectOptions & {
|
|
200
|
+
/** The Onyx key to subscribe to. */
|
|
201
|
+
key: TKey;
|
|
202
|
+
/** A function that will be called when the Onyx data we are subscribed changes. */
|
|
203
|
+
callback?: (value: TKey extends CollectionKeyBase ? NonUndefined<OnyxCollection<KeyValueMapping[TKey]>> : OnyxEntry<KeyValueMapping[TKey]>, key: TKey) => void;
|
|
204
|
+
};
|
|
221
205
|
type CallbackToStateMapping<TKey extends OnyxKey> = ConnectOptions<TKey> & {
|
|
222
206
|
subscriptionID: number;
|
|
223
207
|
};
|
|
@@ -379,4 +363,4 @@ type MixedOperationsQueue = {
|
|
|
379
363
|
mergeReplaceNullPatches: MultiMergeReplaceNullPatches;
|
|
380
364
|
set: OnyxInputKeyValueMapping;
|
|
381
365
|
};
|
|
382
|
-
export type { BaseConnectOptions, Collection, CollectionConnectCallback,
|
|
366
|
+
export type { BaseConnectOptions, Collection, CollectionConnectCallback, CollectionKey, CollectionKeyBase, ConnectOptions, CustomTypeOptions, DeepRecord, DefaultConnectCallback, ExtractOnyxCollectionValue, GenericFunction, InitOptions, Key, KeyValueMapping, CallbackToStateMapping, NonNull, NonUndefined, OnyxInputKeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey, OnyxInputValue, OnyxCollectionInputValue, OnyxInput, OnyxSetInput, OnyxMultiSetInput, OnyxMergeInput, OnyxMergeCollectionInput, OnyxSetCollectionInput, OnyxMethod, OnyxMethodMap, OnyxUpdate, OnyxValue, Selector, SetOptions, SetParams, SetCollectionParams, MergeCollectionWithPatchesParams, MultiMergeReplaceNullPatches, MixedOperationsQueue, RetriableOnyxOperation, };
|
package/dist/useOnyx.d.ts
CHANGED
|
@@ -17,11 +17,10 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = {
|
|
|
17
17
|
selector?: UseOnyxSelector<TKey, TReturnValue>;
|
|
18
18
|
};
|
|
19
19
|
type FetchStatus = 'loading' | 'loaded';
|
|
20
|
-
type ResultMetadata
|
|
20
|
+
type ResultMetadata = {
|
|
21
21
|
status: FetchStatus;
|
|
22
|
-
sourceValue?: NonNullable<TValue> | undefined;
|
|
23
22
|
};
|
|
24
|
-
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata
|
|
23
|
+
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata];
|
|
25
24
|
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: UseOnyxOptions<TKey, TReturnValue>, dependencies?: DependencyList): UseOnyxResult<TReturnValue>;
|
|
26
25
|
export default useOnyx;
|
|
27
26
|
export type { FetchStatus, ResultMetadata, UseOnyxResult, UseOnyxOptions, UseOnyxSelector };
|
package/dist/useOnyx.js
CHANGED
|
@@ -41,7 +41,6 @@ const react_1 = require("react");
|
|
|
41
41
|
const OnyxCache_1 = __importStar(require("./OnyxCache"));
|
|
42
42
|
const OnyxConnectionManager_1 = __importDefault(require("./OnyxConnectionManager"));
|
|
43
43
|
const OnyxUtils_1 = __importDefault(require("./OnyxUtils"));
|
|
44
|
-
const OnyxKeys_1 = __importDefault(require("./OnyxKeys"));
|
|
45
44
|
const OnyxSnapshotCache_1 = __importDefault(require("./OnyxSnapshotCache"));
|
|
46
45
|
const useLiveRef_1 = __importDefault(require("./useLiveRef"));
|
|
47
46
|
function useOnyx(key, options, dependencies = []) {
|
|
@@ -104,8 +103,6 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
104
103
|
const onStoreChangeFnRef = (0, react_1.useRef)(null);
|
|
105
104
|
// Indicates if we should get the newest cached value from Onyx during `getSnapshot()` execution.
|
|
106
105
|
const shouldGetCachedValueRef = (0, react_1.useRef)(true);
|
|
107
|
-
// Inside useOnyx.ts, we need to track the sourceValue separately
|
|
108
|
-
const sourceValueRef = (0, react_1.useRef)(undefined);
|
|
109
106
|
// Cache the options key to avoid regenerating it every getSnapshot call
|
|
110
107
|
const cacheKey = (0, react_1.useMemo)(() => OnyxSnapshotCache_1.default.registerConsumer(key, {
|
|
111
108
|
selector: options === null || options === void 0 ? void 0 : options.selector,
|
|
@@ -192,7 +189,6 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
192
189
|
(_c = previousValueRef.current) !== null && _c !== void 0 ? _c : undefined,
|
|
193
190
|
{
|
|
194
191
|
status: newFetchStatus,
|
|
195
|
-
sourceValue: sourceValueRef.current,
|
|
196
192
|
},
|
|
197
193
|
];
|
|
198
194
|
}
|
|
@@ -208,7 +204,6 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
208
204
|
if (hasMountedRef.current) {
|
|
209
205
|
previousValueRef.current = null;
|
|
210
206
|
newValueRef.current = null;
|
|
211
|
-
sourceValueRef.current = undefined;
|
|
212
207
|
resultRef.current = [undefined, { status: 'loading' }];
|
|
213
208
|
shouldGetCachedValueRef.current = true;
|
|
214
209
|
}
|
|
@@ -217,7 +212,7 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
217
212
|
onStoreChangeFnRef.current = onStoreChange;
|
|
218
213
|
connectionRef.current = OnyxConnectionManager_1.default.connect({
|
|
219
214
|
key,
|
|
220
|
-
callback: (
|
|
215
|
+
callback: () => {
|
|
221
216
|
isConnectingRef.current = false;
|
|
222
217
|
onStoreChangeFnRef.current = onStoreChange;
|
|
223
218
|
// Signals that the first connection was made for this key, so some logics
|
|
@@ -225,14 +220,11 @@ function useOnyx(key, options, dependencies = []) {
|
|
|
225
220
|
connectedKeyRef.current = key;
|
|
226
221
|
// Signals that we want to get the newest cached value again in `getSnapshot()`.
|
|
227
222
|
shouldGetCachedValueRef.current = true;
|
|
228
|
-
// sourceValue is unknown type, so we need to cast it to the correct type.
|
|
229
|
-
sourceValueRef.current = sourceValue;
|
|
230
223
|
// Invalidate snapshot cache for this key when data changes
|
|
231
224
|
OnyxSnapshotCache_1.default.invalidateForKey(key);
|
|
232
225
|
// Finally, we signal that the store changed, making `getSnapshot()` be called again.
|
|
233
226
|
onStoreChange();
|
|
234
227
|
},
|
|
235
|
-
waitForCollectionCallback: OnyxKeys_1.default.isCollectionKey(key),
|
|
236
228
|
reuseConnection: options === null || options === void 0 ? void 0 : options.reuseConnection,
|
|
237
229
|
});
|
|
238
230
|
return () => {
|