react-native-onyx 2.0.35 → 2.0.36

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
@@ -401,6 +401,9 @@ function mergeCollection(collectionKey, collection) {
401
401
  // because we will simply overwrite the existing values in storage.
402
402
  const keyValuePairsForNewCollection = OnyxUtils_1.default.prepareKeyValuePairsForStorage(newCollection, true);
403
403
  const promises = [];
404
+ // We need to get the previously existing values so we can compare the new ones
405
+ // against them, to avoid unnecessary subscriber updates.
406
+ const previousCollectionPromise = Promise.all(existingKeys.map((key) => OnyxUtils_1.default.get(key).then((value) => [key, value]))).then(Object.fromEntries);
404
407
  // New keys will be added via multiSet while existing keys will be updated using multiMerge
405
408
  // This is because setting a key that doesn't exist yet with multiMerge will throw errors
406
409
  if (keyValuePairsForExistingCollection.length > 0) {
@@ -413,9 +416,9 @@ function mergeCollection(collectionKey, collection) {
413
416
  const finalMergedCollection = Object.assign(Object.assign({}, existingKeyCollection), newCollection);
414
417
  // Prefill cache if necessary by calling get() on any existing keys and then merge original data to cache
415
418
  // and update all subscribers
416
- const promiseUpdate = Promise.all(existingKeys.map(OnyxUtils_1.default.get)).then(() => {
419
+ const promiseUpdate = previousCollectionPromise.then((previousCollection) => {
417
420
  OnyxCache_1.default.merge(finalMergedCollection);
418
- return OnyxUtils_1.default.scheduleNotifyCollectionSubscribers(collectionKey, finalMergedCollection);
421
+ return OnyxUtils_1.default.scheduleNotifyCollectionSubscribers(collectionKey, finalMergedCollection, previousCollection);
419
422
  });
420
423
  return Promise.all(promises)
421
424
  .catch((error) => OnyxUtils_1.default.evictStorageAndRetry(error, mergeCollection, collectionKey, mergedCollection))
@@ -107,14 +107,14 @@ declare function getCachedCollection<TKey extends CollectionKeyBase>(collectionK
107
107
  /**
108
108
  * When a collection of keys change, search for any callbacks matching the collection key and trigger those callbacks
109
109
  */
110
- declare function keysChanged<TKey extends CollectionKeyBase>(collectionKey: TKey, partialCollection: OnyxCollection<KeyValueMapping[TKey]>, notifyRegularSubscibers?: boolean, notifyWithOnyxSubscibers?: boolean): void;
110
+ declare function keysChanged<TKey extends CollectionKeyBase>(collectionKey: TKey, partialCollection: OnyxCollection<KeyValueMapping[TKey]>, previousPartialCollection: OnyxCollection<KeyValueMapping[TKey]> | undefined, notifyRegularSubscibers?: boolean, notifyWithOnyxSubscibers?: boolean): void;
111
111
  /**
112
112
  * When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks
113
113
  *
114
114
  * @example
115
115
  * keyChanged(key, value, subscriber => subscriber.initWithStoredValues === false)
116
116
  */
117
- declare function keyChanged<TKey extends OnyxKey>(key: TKey, data: OnyxValue<TKey>, prevData: OnyxValue<TKey>, canUpdateSubscriber?: (subscriber?: Mapping<OnyxKey>) => boolean, notifyRegularSubscibers?: boolean, notifyWithOnyxSubscibers?: boolean): void;
117
+ declare function keyChanged<TKey extends OnyxKey>(key: TKey, value: OnyxValue<TKey>, previousValue: OnyxValue<TKey>, canUpdateSubscriber?: (subscriber?: Mapping<OnyxKey>) => boolean, notifyRegularSubscibers?: boolean, notifyWithOnyxSubscibers?: boolean): void;
118
118
  /**
119
119
  * Sends the data obtained from the keys to the connection. It either:
120
120
  * - sets state on the withOnyxInstances
@@ -136,13 +136,13 @@ declare function getCollectionDataAndSendAsObject<TKey extends OnyxKey>(matching
136
136
  * @example
137
137
  * scheduleSubscriberUpdate(key, value, subscriber => subscriber.initWithStoredValues === false)
138
138
  */
139
- declare function scheduleSubscriberUpdate<TKey extends OnyxKey>(key: TKey, value: OnyxValue<TKey>, prevValue: OnyxValue<TKey>, canUpdateSubscriber?: (subscriber?: Mapping<OnyxKey>) => boolean): Promise<void>;
139
+ declare function scheduleSubscriberUpdate<TKey extends OnyxKey>(key: TKey, value: OnyxValue<TKey>, previousValue: OnyxValue<TKey>, canUpdateSubscriber?: (subscriber?: Mapping<OnyxKey>) => boolean): Promise<void>;
140
140
  /**
141
141
  * This method is similar to notifySubscribersOnNextTick but it is built for working specifically with collections
142
142
  * so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
143
143
  * subscriber callbacks receive the data in a different format than they normally expect and it breaks code.
144
144
  */
145
- declare function scheduleNotifyCollectionSubscribers<TKey extends OnyxKey>(key: TKey, value: OnyxCollection<KeyValueMapping[TKey]>): Promise<void>;
145
+ declare function scheduleNotifyCollectionSubscribers<TKey extends OnyxKey>(key: TKey, value: OnyxCollection<KeyValueMapping[TKey]>, previousValue?: OnyxCollection<KeyValueMapping[TKey]>): Promise<void>;
146
146
  /**
147
147
  * Remove a key from Onyx and update the subscribers
148
148
  */
package/dist/OnyxUtils.js CHANGED
@@ -338,7 +338,16 @@ function getCachedCollection(collectionKey, collectionMemberKeys) {
338
338
  /**
339
339
  * When a collection of keys change, search for any callbacks matching the collection key and trigger those callbacks
340
340
  */
341
- function keysChanged(collectionKey, partialCollection, notifyRegularSubscibers = true, notifyWithOnyxSubscibers = true) {
341
+ function keysChanged(collectionKey, partialCollection, previousPartialCollection, notifyRegularSubscibers = true, notifyWithOnyxSubscibers = true) {
342
+ const previousCollectionWithoutNestedNulls = previousPartialCollection === undefined ? {} : utils_1.default.removeNestedNullValues(previousPartialCollection);
343
+ // We prepare the "cached collection" which is the entire collection + the new partial data that
344
+ // was merged in via mergeCollection().
345
+ const cachedCollection = getCachedCollection(collectionKey);
346
+ const cachedCollectionWithoutNestedNulls = utils_1.default.removeNestedNullValues(cachedCollection);
347
+ // If the previous collection equals the new collection then we do not need to notify any subscribers.
348
+ if (previousPartialCollection !== undefined && (0, fast_equals_1.deepEqual)(cachedCollectionWithoutNestedNulls, previousCollectionWithoutNestedNulls)) {
349
+ return;
350
+ }
342
351
  // We are iterating over all subscribers similar to keyChanged(). However, we are looking for subscribers who are subscribing to either a collection key or
343
352
  // individual collection key member for the collection that is being updated. It is important to note that the collection parameter cane be a PARTIAL collection
344
353
  // and does not represent all of the combined keys and values for a collection key. It is just the "new" data that was merged in via mergeCollection().
@@ -360,10 +369,6 @@ function keysChanged(collectionKey, partialCollection, notifyRegularSubscibers =
360
369
  * e.g. Onyx.connect({key: `${ONYXKEYS.COLLECTION.REPORT}{reportID}`, callback: ...});
361
370
  */
362
371
  const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key);
363
- // We prepare the "cached collection" which is the entire collection + the new partial data that
364
- // was merged in via mergeCollection().
365
- const cachedCollection = getCachedCollection(collectionKey);
366
- const cachedCollectionWithoutNestedNulls = utils_1.default.removeNestedNullValues(cachedCollection);
367
372
  // Regular Onyx.connect() subscriber found.
368
373
  if (typeof subscriber.callback === 'function') {
369
374
  if (!notifyRegularSubscibers) {
@@ -381,6 +386,9 @@ function keysChanged(collectionKey, partialCollection, notifyRegularSubscibers =
381
386
  const dataKeys = Object.keys(partialCollection !== null && partialCollection !== void 0 ? partialCollection : {});
382
387
  for (let j = 0; j < dataKeys.length; j++) {
383
388
  const dataKey = dataKeys[j];
389
+ if ((0, fast_equals_1.deepEqual)(cachedCollectionWithoutNestedNulls[dataKey], previousCollectionWithoutNestedNulls[dataKey])) {
390
+ continue;
391
+ }
384
392
  subscriber.callback(cachedCollectionWithoutNestedNulls[dataKey], dataKey);
385
393
  }
386
394
  continue;
@@ -388,6 +396,9 @@ function keysChanged(collectionKey, partialCollection, notifyRegularSubscibers =
388
396
  // And if the subscriber is specifically only tracking a particular collection member key then we will
389
397
  // notify them with the cached data for that key only.
390
398
  if (isSubscribedToCollectionMemberKey) {
399
+ if ((0, fast_equals_1.deepEqual)(cachedCollectionWithoutNestedNulls[subscriber.key], previousCollectionWithoutNestedNulls[subscriber.key])) {
400
+ continue;
401
+ }
391
402
  const subscriberCallback = subscriber.callback;
392
403
  subscriberCallback(cachedCollectionWithoutNestedNulls[subscriber.key], subscriber.key);
393
404
  continue;
@@ -435,6 +446,9 @@ function keysChanged(collectionKey, partialCollection, notifyRegularSubscibers =
435
446
  }
436
447
  // If a React component is only interested in a single key then we can set the cached value directly to the state name.
437
448
  if (isSubscribedToCollectionMemberKey) {
449
+ if ((0, fast_equals_1.deepEqual)(cachedCollectionWithoutNestedNulls[subscriber.key], previousCollectionWithoutNestedNulls[subscriber.key])) {
450
+ continue;
451
+ }
438
452
  // However, we only want to update this subscriber if the partial data contains a change.
439
453
  // Otherwise, we would update them with a value they already have and trigger an unnecessary re-render.
440
454
  const dataFromCollection = partialCollection === null || partialCollection === void 0 ? void 0 : partialCollection[subscriber.key];
@@ -484,9 +498,9 @@ function keysChanged(collectionKey, partialCollection, notifyRegularSubscibers =
484
498
  * @example
485
499
  * keyChanged(key, value, subscriber => subscriber.initWithStoredValues === false)
486
500
  */
487
- function keyChanged(key, data, prevData, canUpdateSubscriber = () => true, notifyRegularSubscibers = true, notifyWithOnyxSubscibers = true) {
501
+ function keyChanged(key, value, previousValue, canUpdateSubscriber = () => true, notifyRegularSubscibers = true, notifyWithOnyxSubscibers = true) {
488
502
  // Add or remove this key from the recentlyAccessedKeys lists
489
- if (data !== null) {
503
+ if (value !== null) {
490
504
  addLastAccessedKey(key);
491
505
  }
492
506
  else {
@@ -509,13 +523,13 @@ function keyChanged(key, data, prevData, canUpdateSubscriber = () => true, notif
509
523
  if (isCollectionKey(subscriber.key) && subscriber.waitForCollectionCallback) {
510
524
  const cachedCollection = getCachedCollection(subscriber.key);
511
525
  const cachedCollectionWithoutNestedNulls = utils_1.default.removeNestedNullValues(cachedCollection);
512
- cachedCollectionWithoutNestedNulls[key] = data;
526
+ cachedCollectionWithoutNestedNulls[key] = value;
513
527
  subscriber.callback(cachedCollectionWithoutNestedNulls);
514
528
  continue;
515
529
  }
516
- const dataWithoutNestedNulls = utils_1.default.removeNestedNullValues(data);
530
+ const valueWithoutNestedNulls = utils_1.default.removeNestedNullValues(value);
517
531
  const subscriberCallback = subscriber.callback;
518
- subscriberCallback(dataWithoutNestedNulls, key);
532
+ subscriberCallback(valueWithoutNestedNulls, key);
519
533
  continue;
520
534
  }
521
535
  // Subscriber connected via withOnyx() HOC
@@ -532,7 +546,7 @@ function keyChanged(key, data, prevData, canUpdateSubscriber = () => true, notif
532
546
  subscriber.withOnyxInstance.setStateProxy((prevState) => {
533
547
  const prevWithOnyxData = prevState[subscriber.statePropertyName];
534
548
  const newWithOnyxData = {
535
- [key]: selector(data, subscriber.withOnyxInstance.state),
549
+ [key]: selector(value, subscriber.withOnyxInstance.state),
536
550
  };
537
551
  const prevDataWithNewData = Object.assign(Object.assign({}, prevWithOnyxData), newWithOnyxData);
538
552
  if (!(0, fast_equals_1.deepEqual)(prevWithOnyxData, prevDataWithNewData)) {
@@ -547,7 +561,7 @@ function keyChanged(key, data, prevData, canUpdateSubscriber = () => true, notif
547
561
  }
548
562
  subscriber.withOnyxInstance.setStateProxy((prevState) => {
549
563
  const collection = prevState[subscriber.statePropertyName] || {};
550
- const newCollection = Object.assign(Object.assign({}, collection), { [key]: data });
564
+ const newCollection = Object.assign(Object.assign({}, collection), { [key]: value });
551
565
  PerformanceUtils.logSetStateCall(subscriber, collection, newCollection, 'keyChanged', key);
552
566
  return {
553
567
  [subscriber.statePropertyName]: newCollection,
@@ -559,9 +573,9 @@ function keyChanged(key, data, prevData, canUpdateSubscriber = () => true, notif
559
573
  // returned by the selector and only if the selected data has changed.
560
574
  if (selector) {
561
575
  subscriber.withOnyxInstance.setStateProxy(() => {
562
- const previousValue = selector(prevData, subscriber.withOnyxInstance.state);
563
- const newValue = selector(data, subscriber.withOnyxInstance.state);
564
- if (!(0, fast_equals_1.deepEqual)(previousValue, newValue)) {
576
+ const prevValue = selector(previousValue, subscriber.withOnyxInstance.state);
577
+ const newValue = selector(value, subscriber.withOnyxInstance.state);
578
+ if (!(0, fast_equals_1.deepEqual)(prevValue, newValue)) {
565
579
  return {
566
580
  [subscriber.statePropertyName]: newValue,
567
581
  };
@@ -572,17 +586,17 @@ function keyChanged(key, data, prevData, canUpdateSubscriber = () => true, notif
572
586
  }
573
587
  // If we did not match on a collection key then we just set the new data to the state property
574
588
  subscriber.withOnyxInstance.setStateProxy((prevState) => {
575
- const prevWithOnyxData = prevState[subscriber.statePropertyName];
589
+ const prevWithOnyxValue = prevState[subscriber.statePropertyName];
576
590
  // Avoids triggering unnecessary re-renders when feeding empty objects
577
- if (utils_1.default.isEmptyObject(data) && utils_1.default.isEmptyObject(prevWithOnyxData)) {
591
+ if (utils_1.default.isEmptyObject(value) && utils_1.default.isEmptyObject(prevWithOnyxValue)) {
578
592
  return null;
579
593
  }
580
- if (prevWithOnyxData === data) {
594
+ if (prevWithOnyxValue === value) {
581
595
  return null;
582
596
  }
583
- PerformanceUtils.logSetStateCall(subscriber, prevData, data, 'keyChanged', key);
597
+ PerformanceUtils.logSetStateCall(subscriber, previousValue, value, 'keyChanged', key);
584
598
  return {
585
- [subscriber.statePropertyName]: data,
599
+ [subscriber.statePropertyName]: value,
586
600
  };
587
601
  });
588
602
  continue;
@@ -719,9 +733,9 @@ function getCollectionDataAndSendAsObject(matchingKeys, mapping) {
719
733
  * @example
720
734
  * scheduleSubscriberUpdate(key, value, subscriber => subscriber.initWithStoredValues === false)
721
735
  */
722
- function scheduleSubscriberUpdate(key, value, prevValue, canUpdateSubscriber = () => true) {
723
- const promise = Promise.resolve().then(() => keyChanged(key, value, prevValue, canUpdateSubscriber, true, false));
724
- batchUpdates(() => keyChanged(key, value, prevValue, canUpdateSubscriber, false, true));
736
+ function scheduleSubscriberUpdate(key, value, previousValue, canUpdateSubscriber = () => true) {
737
+ const promise = Promise.resolve().then(() => keyChanged(key, value, previousValue, canUpdateSubscriber, true, false));
738
+ batchUpdates(() => keyChanged(key, value, previousValue, canUpdateSubscriber, false, true));
725
739
  return Promise.all([maybeFlushBatchUpdates(), promise]).then(() => undefined);
726
740
  }
727
741
  /**
@@ -729,9 +743,9 @@ function scheduleSubscriberUpdate(key, value, prevValue, canUpdateSubscriber = (
729
743
  * so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
730
744
  * subscriber callbacks receive the data in a different format than they normally expect and it breaks code.
731
745
  */
732
- function scheduleNotifyCollectionSubscribers(key, value) {
733
- const promise = Promise.resolve().then(() => keysChanged(key, value, true, false));
734
- batchUpdates(() => keysChanged(key, value, false, true));
746
+ function scheduleNotifyCollectionSubscribers(key, value, previousValue) {
747
+ const promise = Promise.resolve().then(() => keysChanged(key, value, previousValue, true, false));
748
+ batchUpdates(() => keysChanged(key, value, previousValue, false, true));
735
749
  return Promise.all([maybeFlushBatchUpdates(), promise]).then(() => undefined);
736
750
  }
737
751
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "2.0.35",
3
+ "version": "2.0.36",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",