react-native-onyx 3.0.22 → 3.0.24

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 CHANGED
@@ -60,7 +60,11 @@ function init({ keys = {}, initialKeyStates = {}, evictableKeys = [], maxCachedK
60
60
  if (shouldSyncMultipleInstances) {
61
61
  (_a = storage_1.default.keepInstancesSync) === null || _a === void 0 ? void 0 : _a.call(storage_1.default, (key, value) => {
62
62
  OnyxCache_1.default.set(key, value);
63
- OnyxUtils_1.default.keyChanged(key, value);
63
+ // Check if this is a collection member key to prevent duplicate callbacks
64
+ // When a collection is updated, individual members sync separately to other tabs
65
+ // Setting isProcessingCollectionUpdate=true prevents triggering collection callbacks for each individual update
66
+ const isKeyCollectionMember = OnyxUtils_1.default.isCollectionMember(key);
67
+ OnyxUtils_1.default.keyChanged(key, value, undefined, true, isKeyCollectionMember);
64
68
  });
65
69
  }
66
70
  if (maxCachedKeysCount > 0) {
@@ -107,6 +107,12 @@ declare function getCollectionKeys(): Set<OnyxKey>;
107
107
  */
108
108
  declare function isCollectionKey(key: OnyxKey): key is CollectionKeyBase;
109
109
  declare function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}`;
110
+ /**
111
+ * Checks if a given key is a collection member key (not just a collection key).
112
+ * @param key - The key to check
113
+ * @returns true if the key is a collection member, false otherwise
114
+ */
115
+ declare function isCollectionMember(key: OnyxKey): boolean;
110
116
  /**
111
117
  * Splits a collection member key into the collection key part and the ID part.
112
118
  * @param key - The collection member key to split.
@@ -328,6 +334,7 @@ declare const OnyxUtils: {
328
334
  getCollectionKeys: typeof getCollectionKeys;
329
335
  isCollectionKey: typeof isCollectionKey;
330
336
  isCollectionMemberKey: typeof isCollectionMemberKey;
337
+ isCollectionMember: typeof isCollectionMember;
331
338
  splitCollectionMemberKey: typeof splitCollectionMemberKey;
332
339
  isKeyMatch: typeof isKeyMatch;
333
340
  tryGetCachedValue: typeof tryGetCachedValue;
package/dist/OnyxUtils.js CHANGED
@@ -391,6 +391,22 @@ function isCollectionKey(key) {
391
391
  function isCollectionMemberKey(collectionKey, key) {
392
392
  return key.startsWith(collectionKey) && key.length > collectionKey.length;
393
393
  }
394
+ /**
395
+ * Checks if a given key is a collection member key (not just a collection key).
396
+ * @param key - The key to check
397
+ * @returns true if the key is a collection member, false otherwise
398
+ */
399
+ function isCollectionMember(key) {
400
+ try {
401
+ const collectionKey = getCollectionKey(key);
402
+ // If the key is longer than the collection key, it's a collection member
403
+ return key.length > collectionKey.length;
404
+ }
405
+ catch (e) {
406
+ // If getCollectionKey throws, the key is not a collection member
407
+ return false;
408
+ }
409
+ }
394
410
  /**
395
411
  * Splits a collection member key into the collection key part and the ID part.
396
412
  * @param key - The collection member key to split.
@@ -1393,6 +1409,7 @@ const OnyxUtils = {
1393
1409
  getCollectionKeys,
1394
1410
  isCollectionKey,
1395
1411
  isCollectionMemberKey,
1412
+ isCollectionMember,
1396
1413
  splitCollectionMemberKey,
1397
1414
  isKeyMatch,
1398
1415
  tryGetCachedValue,
package/dist/useOnyx.js CHANGED
@@ -86,6 +86,7 @@ function useOnyx(key, options, dependencies = []) {
86
86
  const previousValueRef = (0, react_1.useRef)(null);
87
87
  // Stores the newest cached value in order to compare with the previous one and optimize `getSnapshot()` execution.
88
88
  const newValueRef = (0, react_1.useRef)(null);
89
+ const lastConnectedKeyRef = (0, react_1.useRef)(key);
89
90
  // Stores the previously result returned by the hook, containing the data from cache and the fetch status.
90
91
  // We initialize it to `undefined` and `loading` fetch status to simulate the initial result when the hook is loading from the cache.
91
92
  // However, if `initWithStoredValues` is `false` we set the fetch status to `loaded` since we want to signal that data is ready.
@@ -114,6 +115,15 @@ function useOnyx(key, options, dependencies = []) {
114
115
  canBeMissing: options === null || options === void 0 ? void 0 : options.canBeMissing,
115
116
  }), [options === null || options === void 0 ? void 0 : options.selector, options === null || options === void 0 ? void 0 : options.initWithStoredValues, options === null || options === void 0 ? void 0 : options.allowStaleData, options === null || options === void 0 ? void 0 : options.canBeMissing]);
116
117
  (0, react_1.useEffect)(() => () => OnyxSnapshotCache_1.default.deregisterConsumer(key, cacheKey), [key, cacheKey]);
118
+ (0, react_1.useEffect)(() => {
119
+ if (lastConnectedKeyRef.current === key) {
120
+ return;
121
+ }
122
+ lastConnectedKeyRef.current = key;
123
+ shouldGetCachedValueRef.current = true;
124
+ previousValueRef.current = null;
125
+ resultRef.current = [undefined, { status: (options === null || options === void 0 ? void 0 : options.initWithStoredValues) === false ? 'loaded' : 'loading' }];
126
+ }, [key, options === null || options === void 0 ? void 0 : options.initWithStoredValues]);
117
127
  (0, react_1.useEffect)(() => {
118
128
  // These conditions will ensure we can only handle dynamic collection member keys from the same collection.
119
129
  if ((options === null || options === void 0 ? void 0 : options.allowDynamicKey) || previousKey === key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "3.0.22",
3
+ "version": "3.0.24",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",