react-native-onyx 2.0.11 → 2.0.12

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.
Files changed (2) hide show
  1. package/dist/Onyx.js +64 -7
  2. package/package.json +1 -1
package/dist/Onyx.js CHANGED
@@ -694,13 +694,70 @@ function addKeyToRecentlyAccessedIfNeeded(mapping) {
694
694
  * @param {Object} mapping
695
695
  */
696
696
  function getCollectionDataAndSendAsObject(matchingKeys, mapping) {
697
- Promise.all(underscore_1.default.map(matchingKeys, (key) => get(key)))
698
- .then((values) => underscore_1.default.reduce(values, (finalObject, value, i) => {
699
- // eslint-disable-next-line no-param-reassign
700
- finalObject[matchingKeys[i]] = value;
701
- return finalObject;
702
- }, {}))
703
- .then((val) => sendDataToConnection(mapping, val, undefined, true));
697
+ // Keys that are not in the cache
698
+ const missingKeys = [];
699
+ // Tasks that are pending
700
+ const pendingTasks = [];
701
+ // Keys for the tasks that are pending
702
+ const pendingKeys = [];
703
+ // We are going to combine all the data from the matching keys into a single object
704
+ const data = {};
705
+ /**
706
+ * We are going to iterate over all the matching keys and check if we have the data in the cache.
707
+ * If we do then we add it to the data object. If we do not then we check if there is a pending task
708
+ * for the key. If there is then we add the promise to the pendingTasks array and the key to the pendingKeys
709
+ * array. If there is no pending task then we add the key to the missingKeys array.
710
+ *
711
+ * These missingKeys will be later to use to multiGet the data from the storage.
712
+ */
713
+ matchingKeys.forEach((key) => {
714
+ const cacheValue = OnyxCache_1.default.getValue(key);
715
+ if (cacheValue) {
716
+ data[key] = cacheValue;
717
+ return;
718
+ }
719
+ const pendingKey = `get:${key}`;
720
+ if (OnyxCache_1.default.hasPendingTask(pendingKey)) {
721
+ pendingTasks.push(OnyxCache_1.default.getTaskPromise(pendingKey));
722
+ pendingKeys.push(key);
723
+ }
724
+ else {
725
+ missingKeys.push(key);
726
+ }
727
+ });
728
+ Promise.all(pendingTasks)
729
+ // We are going to wait for all the pending tasks to resolve and then add the data to the data object.
730
+ .then((values) => {
731
+ values.forEach((value, index) => {
732
+ data[pendingKeys[index]] = value;
733
+ });
734
+ return Promise.resolve();
735
+ })
736
+ // We are going to get the missing keys using multiGet from the storage.
737
+ .then(() => {
738
+ if (missingKeys.length === 0) {
739
+ return Promise.resolve();
740
+ }
741
+ return storage_1.default.multiGet(missingKeys);
742
+ })
743
+ // We are going to add the data from the missing keys to the data object and also merge it to the cache.
744
+ .then((values) => {
745
+ if (!values || values.length === 0) {
746
+ return Promise.resolve();
747
+ }
748
+ // temp object is used to merge the missing data into the cache
749
+ const temp = {};
750
+ values.forEach((value) => {
751
+ data[value[0]] = value[1];
752
+ temp[value[0]] = value[1];
753
+ });
754
+ OnyxCache_1.default.merge(temp);
755
+ return Promise.resolve();
756
+ })
757
+ // We are going to send the data to the subscriber.
758
+ .finally(() => {
759
+ sendDataToConnection(mapping, data, undefined, true);
760
+ });
704
761
  }
705
762
  /**
706
763
  * Subscribes a react component's state directly to a store key
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "2.0.11",
3
+ "version": "2.0.12",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",