react-native-onyx 2.0.67 → 2.0.69
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/dist/Onyx.js +9 -2
- package/dist/OnyxUtils.d.ts +8 -6
- package/dist/OnyxUtils.js +33 -21
- package/package.json +1 -1
package/dist/Onyx.js
CHANGED
|
@@ -416,8 +416,15 @@ function clear(keysToPreserve = []) {
|
|
|
416
416
|
const newValue = (_a = defaultKeyStates[key]) !== null && _a !== void 0 ? _a : null;
|
|
417
417
|
if (newValue !== oldValue) {
|
|
418
418
|
OnyxCache_1.default.set(key, newValue);
|
|
419
|
-
|
|
420
|
-
|
|
419
|
+
let collectionKey;
|
|
420
|
+
try {
|
|
421
|
+
collectionKey = OnyxUtils_1.default.getCollectionKey(key);
|
|
422
|
+
}
|
|
423
|
+
catch (e) {
|
|
424
|
+
// If getCollectionKey() throws an error it means the key is not a collection key.
|
|
425
|
+
collectionKey = undefined;
|
|
426
|
+
}
|
|
427
|
+
if (collectionKey) {
|
|
421
428
|
if (!keyValuesToResetAsCollection[collectionKey]) {
|
|
422
429
|
keyValuesToResetAsCollection[collectionKey] = {};
|
|
423
430
|
}
|
package/dist/OnyxUtils.d.ts
CHANGED
|
@@ -75,7 +75,8 @@ declare function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>
|
|
|
75
75
|
/**
|
|
76
76
|
* Splits a collection member key into the collection key part and the ID part.
|
|
77
77
|
* @param key - The collection member key to split.
|
|
78
|
-
* @returns A tuple where the first element is the collection part and the second element is the ID part
|
|
78
|
+
* @returns A tuple where the first element is the collection part and the second element is the ID part,
|
|
79
|
+
* or throws an Error if the key is not a collection one.
|
|
79
80
|
*/
|
|
80
81
|
declare function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType = TKey extends `${infer Prefix}_${string}` ? `${Prefix}_` : never>(key: TKey): [CollectionKeyType, string];
|
|
81
82
|
/**
|
|
@@ -86,17 +87,18 @@ declare function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean;
|
|
|
86
87
|
/** Checks to see if this key has been flagged as safe for removal. */
|
|
87
88
|
declare function isSafeEvictionKey(testKey: OnyxKey): boolean;
|
|
88
89
|
/**
|
|
89
|
-
*
|
|
90
|
+
* Extracts the collection identifier of a given collection member key.
|
|
90
91
|
*
|
|
91
92
|
* For example:
|
|
92
93
|
* - `getCollectionKey("report_123")` would return "report_"
|
|
93
|
-
* - `getCollectionKey("report")` would return "report"
|
|
94
94
|
* - `getCollectionKey("report_")` would return "report_"
|
|
95
|
+
* - `getCollectionKey("report_-1_something")` would return "report_"
|
|
96
|
+
* - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
|
|
95
97
|
*
|
|
96
|
-
* @param
|
|
97
|
-
* @
|
|
98
|
+
* @param key - The collection key to process.
|
|
99
|
+
* @returns The plain collection key or throws an Error if the key is not a collection one.
|
|
98
100
|
*/
|
|
99
|
-
declare function getCollectionKey(key:
|
|
101
|
+
declare function getCollectionKey(key: CollectionKey): string;
|
|
100
102
|
/**
|
|
101
103
|
* Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
|
|
102
104
|
* If the requested key is a collection, it will return an object with all the collection members.
|
package/dist/OnyxUtils.js
CHANGED
|
@@ -332,16 +332,12 @@ function isCollectionMemberKey(collectionKey, key, collectionKeyLength) {
|
|
|
332
332
|
/**
|
|
333
333
|
* Splits a collection member key into the collection key part and the ID part.
|
|
334
334
|
* @param key - The collection member key to split.
|
|
335
|
-
* @returns A tuple where the first element is the collection part and the second element is the ID part
|
|
335
|
+
* @returns A tuple where the first element is the collection part and the second element is the ID part,
|
|
336
|
+
* or throws an Error if the key is not a collection one.
|
|
336
337
|
*/
|
|
337
338
|
function splitCollectionMemberKey(key) {
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
throw new Error(`Invalid ${key} key provided, only collection keys are allowed.`);
|
|
341
|
-
}
|
|
342
|
-
const collectionKey = key.substring(0, underscoreIndex + 1);
|
|
343
|
-
const memberKey = key.substring(underscoreIndex + 1);
|
|
344
|
-
return [collectionKey, memberKey];
|
|
339
|
+
const collectionKey = getCollectionKey(key);
|
|
340
|
+
return [collectionKey, key.slice(collectionKey.length)];
|
|
345
341
|
}
|
|
346
342
|
/**
|
|
347
343
|
* Checks to see if a provided key is the exact configured key of our connected subscriber
|
|
@@ -355,22 +351,32 @@ function isSafeEvictionKey(testKey) {
|
|
|
355
351
|
return evictionAllowList.some((key) => isKeyMatch(key, testKey));
|
|
356
352
|
}
|
|
357
353
|
/**
|
|
358
|
-
*
|
|
354
|
+
* Extracts the collection identifier of a given collection member key.
|
|
359
355
|
*
|
|
360
356
|
* For example:
|
|
361
357
|
* - `getCollectionKey("report_123")` would return "report_"
|
|
362
|
-
* - `getCollectionKey("report")` would return "report"
|
|
363
358
|
* - `getCollectionKey("report_")` would return "report_"
|
|
359
|
+
* - `getCollectionKey("report_-1_something")` would return "report_"
|
|
360
|
+
* - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
|
|
364
361
|
*
|
|
365
|
-
* @param
|
|
366
|
-
* @
|
|
362
|
+
* @param key - The collection key to process.
|
|
363
|
+
* @returns The plain collection key or throws an Error if the key is not a collection one.
|
|
367
364
|
*/
|
|
368
365
|
function getCollectionKey(key) {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
366
|
+
// Start by finding the position of the last underscore in the string
|
|
367
|
+
let lastUnderscoreIndex = key.lastIndexOf('_');
|
|
368
|
+
// Iterate backwards to find the longest key that ends with '_'
|
|
369
|
+
while (lastUnderscoreIndex > 0) {
|
|
370
|
+
const possibleKey = key.slice(0, lastUnderscoreIndex + 1);
|
|
371
|
+
// Check if the substring is a key in the Set
|
|
372
|
+
if (isCollectionKey(possibleKey)) {
|
|
373
|
+
// Return the matching key and the rest of the string
|
|
374
|
+
return possibleKey;
|
|
375
|
+
}
|
|
376
|
+
// Move to the next underscore to check smaller possible keys
|
|
377
|
+
lastUnderscoreIndex = key.lastIndexOf('_', lastUnderscoreIndex - 1);
|
|
372
378
|
}
|
|
373
|
-
|
|
379
|
+
throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`);
|
|
374
380
|
}
|
|
375
381
|
/**
|
|
376
382
|
* Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
|
|
@@ -639,11 +645,17 @@ function keyChanged(key, value, previousValue, canUpdateSubscriber = () => true,
|
|
|
639
645
|
// do the same in keysChanged, because we only call that function when a collection key changes, and it doesn't happen that often.
|
|
640
646
|
// For performance reason, we look for the given key and later if don't find it we look for the collection key, instead of checking if it is a collection key first.
|
|
641
647
|
let stateMappingKeys = (_a = onyxKeyToSubscriptionIDs.get(key)) !== null && _a !== void 0 ? _a : [];
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
648
|
+
let collectionKey;
|
|
649
|
+
try {
|
|
650
|
+
collectionKey = getCollectionKey(key);
|
|
651
|
+
}
|
|
652
|
+
catch (e) {
|
|
653
|
+
// If getCollectionKey() throws an error it means the key is not a collection key.
|
|
654
|
+
collectionKey = undefined;
|
|
655
|
+
}
|
|
656
|
+
if (collectionKey) {
|
|
645
657
|
// Getting the collection key from the specific key because only collection keys were stored in the mapping.
|
|
646
|
-
stateMappingKeys = [...stateMappingKeys, ...((_b = onyxKeyToSubscriptionIDs.get(
|
|
658
|
+
stateMappingKeys = [...stateMappingKeys, ...((_b = onyxKeyToSubscriptionIDs.get(collectionKey)) !== null && _b !== void 0 ? _b : [])];
|
|
647
659
|
if (stateMappingKeys.length === 0) {
|
|
648
660
|
return;
|
|
649
661
|
}
|
|
@@ -1023,7 +1035,7 @@ function subscribeToKey(connectOptions) {
|
|
|
1023
1035
|
// Performance improvement
|
|
1024
1036
|
// If the mapping is connected to an onyx key that is not a collection
|
|
1025
1037
|
// we can skip the call to getAllKeys() and return an array with a single item
|
|
1026
|
-
if (Boolean(mapping.key) && typeof mapping.key === 'string' && !mapping.key
|
|
1038
|
+
if (Boolean(mapping.key) && typeof mapping.key === 'string' && !isCollectionKey(mapping.key) && OnyxCache_1.default.getAllKeys().has(mapping.key)) {
|
|
1027
1039
|
return new Set([mapping.key]);
|
|
1028
1040
|
}
|
|
1029
1041
|
return getAllKeys();
|