react-native-onyx 1.0.16 → 1.0.18
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 +12 -0
- package/README.md +2 -2
- package/dist/web.development.js +231 -141
- package/dist/web.development.js.map +1 -1
- package/dist/web.min.js +1 -1
- package/dist/web.min.js.map +1 -1
- package/lib/Onyx.js +207 -109
- package/lib/OnyxCache.js +2 -3
- package/lib/{customizerForMergeWith.js → mergeWithCustomized.js} +8 -3
- package/lib/storage/providers/LocalForage.js +2 -3
- package/package.json +1 -1
package/dist/web.development.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
(function webpackUniversalModuleDefinition(root, factory) {
|
|
2
2
|
if(typeof exports === 'object' && typeof module === 'object')
|
|
3
|
-
module.exports = factory(require("underscore"), require("expensify-common/lib/str"), require("lodash/
|
|
3
|
+
module.exports = factory(require("underscore"), require("expensify-common/lib/str"), require("lodash/get"), require("localforage"), require("lodash/mergeWith"), require("react"));
|
|
4
4
|
else if(typeof define === 'function' && define.amd)
|
|
5
|
-
define(["underscore", "expensify-common/lib/str", "lodash/
|
|
5
|
+
define(["underscore", "expensify-common/lib/str", "lodash/get", "localforage", "lodash/mergeWith", "react"], factory);
|
|
6
6
|
else if(typeof exports === 'object')
|
|
7
|
-
exports["react-native-onyx/web"] = factory(require("underscore"), require("expensify-common/lib/str"), require("lodash/
|
|
7
|
+
exports["react-native-onyx/web"] = factory(require("underscore"), require("expensify-common/lib/str"), require("lodash/get"), require("localforage"), require("lodash/mergeWith"), require("react"));
|
|
8
8
|
else
|
|
9
|
-
root["react-native-onyx/web"] = factory(root["underscore"], root["expensify-common/lib/str"], root["lodash/
|
|
10
|
-
})(self, (__WEBPACK_EXTERNAL_MODULE_underscore__, __WEBPACK_EXTERNAL_MODULE_expensify_common_lib_str__,
|
|
9
|
+
root["react-native-onyx/web"] = factory(root["underscore"], root["expensify-common/lib/str"], root["lodash/get"], root["localforage"], root["lodash/mergeWith"], root["react"]);
|
|
10
|
+
})(self, (__WEBPACK_EXTERNAL_MODULE_underscore__, __WEBPACK_EXTERNAL_MODULE_expensify_common_lib_str__, __WEBPACK_EXTERNAL_MODULE_lodash_get__, __WEBPACK_EXTERNAL_MODULE_localforage__, __WEBPACK_EXTERNAL_MODULE_lodash_mergeWith__, __WEBPACK_EXTERNAL_MODULE_react__) => {
|
|
11
11
|
return /******/ (() => { // webpackBootstrap
|
|
12
12
|
/******/ var __webpack_modules__ = ({
|
|
13
13
|
|
|
@@ -489,16 +489,15 @@ function logInfo(message) {
|
|
|
489
489
|
\*********************/
|
|
490
490
|
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
491
491
|
|
|
492
|
-
var _interopRequireDefault = __webpack_require__(/*! @babel/runtime/helpers/interopRequireDefault */ "./node_modules/@babel/runtime/helpers/interopRequireDefault.js");Object.defineProperty(exports, "__esModule", ({ value: true }));exports["default"] = void 0;var _slicedToArray2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/slicedToArray */ "./node_modules/@babel/runtime/helpers/slicedToArray.js"));var _toConsumableArray2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/toConsumableArray */ "./node_modules/@babel/runtime/helpers/toConsumableArray.js"));var
|
|
492
|
+
var _interopRequireDefault = __webpack_require__(/*! @babel/runtime/helpers/interopRequireDefault */ "./node_modules/@babel/runtime/helpers/interopRequireDefault.js");Object.defineProperty(exports, "__esModule", ({ value: true }));exports["default"] = void 0;var _slicedToArray2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/slicedToArray */ "./node_modules/@babel/runtime/helpers/slicedToArray.js"));var _toConsumableArray2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/toConsumableArray */ "./node_modules/@babel/runtime/helpers/toConsumableArray.js"));var _extends3 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/extends */ "./node_modules/@babel/runtime/helpers/extends.js"));var _defineProperty2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/defineProperty */ "./node_modules/@babel/runtime/helpers/defineProperty.js"));
|
|
493
|
+
var _underscore = _interopRequireDefault(__webpack_require__(/*! underscore */ "underscore"));
|
|
493
494
|
var _str = _interopRequireDefault(__webpack_require__(/*! expensify-common/lib/str */ "expensify-common/lib/str"));
|
|
494
|
-
var _merge = _interopRequireDefault(__webpack_require__(/*! lodash/merge */ "lodash/merge"));
|
|
495
|
-
var _mergeWith = _interopRequireDefault(__webpack_require__(/*! lodash/mergeWith */ "lodash/mergeWith"));
|
|
496
495
|
var _get = _interopRequireDefault(__webpack_require__(/*! lodash/get */ "lodash/get"));
|
|
497
496
|
var _storage = _interopRequireDefault(__webpack_require__(/*! ./storage */ "./lib/storage/index.web.js"));
|
|
498
497
|
var Logger = _interopRequireWildcard(__webpack_require__(/*! ./Logger */ "./lib/Logger.js"));
|
|
499
498
|
var _OnyxCache = _interopRequireDefault(__webpack_require__(/*! ./OnyxCache */ "./lib/OnyxCache.js"));
|
|
500
499
|
var _createDeferredTask = _interopRequireDefault(__webpack_require__(/*! ./createDeferredTask */ "./lib/createDeferredTask.js"));
|
|
501
|
-
var
|
|
500
|
+
var _mergeWithCustomized = _interopRequireDefault(__webpack_require__(/*! ./mergeWithCustomized */ "./lib/mergeWithCustomized.js"));function _getRequireWildcardCache(nodeInterop) {if (typeof WeakMap !== "function") return null;var cacheBabelInterop = new WeakMap();var cacheNodeInterop = new WeakMap();return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) {return nodeInterop ? cacheNodeInterop : cacheBabelInterop;})(nodeInterop);}function _interopRequireWildcard(obj, nodeInterop) {if (!nodeInterop && obj && obj.__esModule) {return obj;}if (obj === null || typeof obj !== "object" && typeof obj !== "function") {return { default: obj };}var cache = _getRequireWildcardCache(nodeInterop);if (cache && cache.has(obj)) {return cache.get(obj);}var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {Object.defineProperty(newObj, key, desc);} else {newObj[key] = obj[key];}}}newObj.default = obj;if (cache) {cache.set(obj, newObj);}return newObj;} /* eslint-disable no-continue */
|
|
502
501
|
|
|
503
502
|
// Keeps track of the last connectionID that was used so we can keep incrementing it
|
|
504
503
|
var lastConnectionID = 0;
|
|
@@ -599,8 +598,17 @@ function isCollectionKey(key) {
|
|
|
599
598
|
}
|
|
600
599
|
|
|
601
600
|
/**
|
|
602
|
-
*
|
|
603
|
-
*
|
|
601
|
+
* @param {String} collectionKey
|
|
602
|
+
* @param {String} key
|
|
603
|
+
* @returns {Boolean}
|
|
604
|
+
*/
|
|
605
|
+
function isCollectionMemberKey(collectionKey, key) {
|
|
606
|
+
return _str.default.startsWith(key, collectionKey) && key.length > collectionKey.length;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Checks to see if a provided key is the exact configured key of our connected subscriber
|
|
611
|
+
* or if the provided key is a collection member key (in case our configured key is a "collection key")
|
|
604
612
|
*
|
|
605
613
|
* @private
|
|
606
614
|
* @param {String} configKey
|
|
@@ -717,7 +725,7 @@ function addAllSafeEvictionKeysToRecentlyAccessedList() {
|
|
|
717
725
|
*/
|
|
718
726
|
function getCachedCollection(collectionKey) {
|
|
719
727
|
var collectionMemberKeys = _underscore.default.filter(_OnyxCache.default.getAllKeys(),
|
|
720
|
-
function (storedKey) {return
|
|
728
|
+
function (storedKey) {return isCollectionMemberKey(collectionKey, storedKey);});
|
|
721
729
|
|
|
722
730
|
|
|
723
731
|
return _underscore.default.reduce(collectionMemberKeys, function (prev, curr) {
|
|
@@ -737,73 +745,103 @@ function getCachedCollection(collectionKey) {
|
|
|
737
745
|
*
|
|
738
746
|
* @private
|
|
739
747
|
* @param {String} collectionKey
|
|
740
|
-
* @param {Object} collection
|
|
748
|
+
* @param {Object} partialCollection - a partial collection of grouped member keys
|
|
741
749
|
*/
|
|
742
|
-
function keysChanged(collectionKey,
|
|
743
|
-
//
|
|
744
|
-
|
|
750
|
+
function keysChanged(collectionKey, partialCollection) {
|
|
751
|
+
// We are iterating over all subscribers similar to keyChanged(). However, we are looking for subscribers who are subscribing to either a collection key or
|
|
752
|
+
// 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
|
|
753
|
+
// 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().
|
|
754
|
+
var stateMappingKeys = _underscore.default.keys(callbackToStateMapping);var _loop = function _loop(
|
|
755
|
+
i) {
|
|
756
|
+
var subscriber = callbackToStateMapping[stateMappingKeys[i]];
|
|
745
757
|
if (!subscriber) {
|
|
746
|
-
return;
|
|
758
|
+
return "continue";
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// Skip iteration if we do not have a collection key or a collection member key on this subscriber
|
|
762
|
+
if (!_str.default.startsWith(subscriber.key, collectionKey)) {
|
|
763
|
+
return "continue";
|
|
747
764
|
}
|
|
748
765
|
|
|
749
766
|
/**
|
|
750
767
|
* e.g. Onyx.connect({key: ONYXKEYS.COLLECTION.REPORT, callback: ...});
|
|
751
768
|
*/
|
|
752
|
-
var isSubscribedToCollectionKey =
|
|
753
|
-
isCollectionKey(subscriber.key);
|
|
769
|
+
var isSubscribedToCollectionKey = subscriber.key === collectionKey;
|
|
754
770
|
|
|
755
771
|
/**
|
|
756
772
|
* e.g. Onyx.connect({key: `${ONYXKEYS.COLLECTION.REPORT}{reportID}`, callback: ...});
|
|
757
773
|
*/
|
|
758
|
-
var isSubscribedToCollectionMemberKey = subscriber.key
|
|
774
|
+
var isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key);
|
|
759
775
|
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
776
|
+
// We prepare the "cached collection" which is the entire collection + the new partial data that
|
|
777
|
+
// was merged in via mergeCollection().
|
|
778
|
+
var cachedCollection = getCachedCollection(collectionKey);
|
|
763
779
|
|
|
780
|
+
// Regular Onyx.connect() subscriber found.
|
|
781
|
+
if (_underscore.default.isFunction(subscriber.callback)) {
|
|
782
|
+
// If they are subscribed to the collection key and using waitForCollectionCallback then we'll
|
|
783
|
+
// send the whole cached collection.
|
|
784
|
+
if (isSubscribedToCollectionKey) {
|
|
764
785
|
if (subscriber.waitForCollectionCallback) {
|
|
765
786
|
subscriber.callback(cachedCollection);
|
|
766
|
-
return;
|
|
787
|
+
return "continue";
|
|
767
788
|
}
|
|
768
789
|
|
|
769
|
-
|
|
790
|
+
// If they are not using waitForCollectionCallback then we notify the subscriber with
|
|
791
|
+
// the new merged data but only for any keys in the partial collection.
|
|
792
|
+
var dataKeys = _underscore.default.keys(partialCollection);
|
|
793
|
+
for (var j = 0; j < dataKeys.length; j++) {
|
|
794
|
+
var dataKey = dataKeys[j];
|
|
770
795
|
subscriber.callback(cachedCollection[dataKey], dataKey);
|
|
771
|
-
}
|
|
772
|
-
|
|
796
|
+
}
|
|
797
|
+
return "continue";
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// And if the subscriber is specifically only tracking a particular collection member key then we will
|
|
801
|
+
// notify them with the cached data for that key only.
|
|
802
|
+
if (isSubscribedToCollectionMemberKey) {
|
|
803
|
+
subscriber.callback(cachedCollection[subscriber.key], subscriber.key);
|
|
804
|
+
return "continue";
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
return "continue";
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// React component subscriber found.
|
|
811
|
+
if (subscriber.withOnyxInstance) {
|
|
812
|
+
// We are subscribed to a collection key so we must update the data in state with the new
|
|
813
|
+
// collection member key values from the partial update.
|
|
814
|
+
if (isSubscribedToCollectionKey) {
|
|
773
815
|
subscriber.withOnyxInstance.setState(function (prevState) {
|
|
774
816
|
var finalCollection = _underscore.default.clone(prevState[subscriber.statePropertyName] || {});
|
|
775
|
-
_underscore.default.each(collection, function (data, dataKey) {
|
|
776
|
-
if (finalCollection[dataKey]) {
|
|
777
|
-
(0, _merge.default)(finalCollection[dataKey], data);
|
|
778
|
-
} else {
|
|
779
|
-
finalCollection[dataKey] = data;
|
|
780
|
-
}
|
|
781
|
-
});
|
|
782
817
|
|
|
818
|
+
var dataKeys = _underscore.default.keys(partialCollection);
|
|
819
|
+
for (var _j = 0; _j < dataKeys.length; _j++) {
|
|
820
|
+
var _dataKey = dataKeys[_j];
|
|
821
|
+
finalCollection[_dataKey] = cachedCollection[_dataKey];
|
|
822
|
+
}
|
|
783
823
|
return (0, _defineProperty2.default)({},
|
|
784
824
|
subscriber.statePropertyName, finalCollection);
|
|
785
825
|
|
|
786
826
|
});
|
|
827
|
+
return "continue";
|
|
787
828
|
}
|
|
788
|
-
} else if (isSubscribedToCollectionMemberKey) {
|
|
789
|
-
var dataFromCollection = collection[subscriber.key];
|
|
790
829
|
|
|
791
|
-
// If
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
prevState[subscriber.statePropertyName],
|
|
801
|
-
dataFromCollection) :
|
|
830
|
+
// If a React component is only interested in a single key then we can set the cached value directly to the state name.
|
|
831
|
+
if (isSubscribedToCollectionMemberKey) {
|
|
832
|
+
// However, we only want to update this subscriber if the partial data contains a change.
|
|
833
|
+
// Otherwise, we would update them with a value they already have and trigger an unnecessary re-render.
|
|
834
|
+
var dataFromCollection = partialCollection[subscriber.key];
|
|
835
|
+
if (_underscore.default.isUndefined(dataFromCollection)) {
|
|
836
|
+
return "continue";
|
|
837
|
+
}
|
|
802
838
|
|
|
803
|
-
|
|
839
|
+
subscriber.withOnyxInstance.setState((0, _defineProperty2.default)({},
|
|
840
|
+
subscriber.statePropertyName, cachedCollection[subscriber.key]));
|
|
804
841
|
|
|
805
|
-
|
|
806
|
-
|
|
842
|
+
}
|
|
843
|
+
}};for (var i = stateMappingKeys.length; i--;) {var _ret = _loop(i);if (_ret === "continue") continue;
|
|
844
|
+
}
|
|
807
845
|
}
|
|
808
846
|
|
|
809
847
|
/**
|
|
@@ -821,43 +859,54 @@ function keyChanged(key, data) {
|
|
|
821
859
|
removeLastAccessedKey(key);
|
|
822
860
|
}
|
|
823
861
|
|
|
824
|
-
//
|
|
825
|
-
|
|
862
|
+
// We are iterating over all subscribers to see if they are interested in the key that has just changed. If the subscriber's key is a collection key then we will
|
|
863
|
+
// notify them if the key that changed is a collection member. Or if it is a regular key notify them when there is an exact match. Depending on whether the subscriber
|
|
864
|
+
// was connected via withOnyx we will call setState() directly on the withOnyx instance. If it is a regular connection we will pass the data to the provided callback.
|
|
865
|
+
var stateMappingKeys = _underscore.default.keys(callbackToStateMapping);var _loop2 = function _loop2(
|
|
866
|
+
i) {
|
|
867
|
+
var subscriber = callbackToStateMapping[stateMappingKeys[i]];
|
|
826
868
|
if (!subscriber || !isKeyMatch(subscriber.key, key)) {
|
|
827
|
-
return;
|
|
869
|
+
return "continue";
|
|
828
870
|
}
|
|
829
871
|
|
|
872
|
+
// Subscriber is a regular call to connect() and provided a callback
|
|
830
873
|
if (_underscore.default.isFunction(subscriber.callback)) {
|
|
831
|
-
if (subscriber.waitForCollectionCallback) {
|
|
874
|
+
if (isCollectionKey(subscriber.key) && subscriber.waitForCollectionCallback) {
|
|
832
875
|
var cachedCollection = getCachedCollection(subscriber.key);
|
|
833
876
|
cachedCollection[key] = data;
|
|
834
877
|
subscriber.callback(cachedCollection);
|
|
835
|
-
return;
|
|
878
|
+
return "continue";
|
|
836
879
|
}
|
|
837
880
|
|
|
838
881
|
subscriber.callback(data, key);
|
|
839
|
-
return;
|
|
882
|
+
return "continue";
|
|
840
883
|
}
|
|
841
884
|
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
885
|
+
// Subscriber connected via withOnyx() HOC
|
|
886
|
+
if (subscriber.withOnyxInstance) {
|
|
887
|
+
// Check if we are subscribing to a collection key and overwrite the collection member key value in state
|
|
888
|
+
if (isCollectionKey(subscriber.key)) {
|
|
889
|
+
subscriber.withOnyxInstance.setState(function (prevState) {
|
|
890
|
+
var collection = prevState[subscriber.statePropertyName] || {};
|
|
891
|
+
return (0, _defineProperty2.default)({},
|
|
892
|
+
subscriber.statePropertyName, (0, _extends3.default)({},
|
|
893
|
+
collection, (0, _defineProperty2.default)({},
|
|
894
|
+
key, data)));
|
|
845
895
|
|
|
846
|
-
// Check if we are subscribing to a collection key and add this item as a collection
|
|
847
|
-
if (isCollectionKey(subscriber.key)) {
|
|
848
|
-
subscriber.withOnyxInstance.setState(function (prevState) {
|
|
849
|
-
var collection = _underscore.default.clone(prevState[subscriber.statePropertyName] || {});
|
|
850
|
-
collection[key] = data;
|
|
851
|
-
return (0, _defineProperty2.default)({},
|
|
852
|
-
subscriber.statePropertyName, collection);
|
|
853
896
|
|
|
854
|
-
|
|
855
|
-
|
|
897
|
+
});
|
|
898
|
+
return "continue";
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// If we did not match on a collection key then we just set the new data to the state property
|
|
856
902
|
subscriber.withOnyxInstance.setState((0, _defineProperty2.default)({},
|
|
857
903
|
subscriber.statePropertyName, data));
|
|
858
904
|
|
|
905
|
+
return "continue";
|
|
859
906
|
}
|
|
860
|
-
|
|
907
|
+
|
|
908
|
+
console.error('Warning: Found a matching subscriber to a key that changed, but no callback or withOnyxInstance could be found.');};for (var i = stateMappingKeys.length; i--;) {var _ret2 = _loop2(i);if (_ret2 === "continue") continue;
|
|
909
|
+
}
|
|
861
910
|
}
|
|
862
911
|
|
|
863
912
|
/**
|
|
@@ -871,9 +920,9 @@ function keyChanged(key, data) {
|
|
|
871
920
|
* @param {string} [config.statePropertyName]
|
|
872
921
|
* @param {function} [config.callback]
|
|
873
922
|
* @param {*|null} val
|
|
874
|
-
* @param {String}
|
|
923
|
+
* @param {String} matchedKey
|
|
875
924
|
*/
|
|
876
|
-
function sendDataToConnection(config, val,
|
|
925
|
+
function sendDataToConnection(config, val, matchedKey) {
|
|
877
926
|
// If the mapping no longer exists then we should not send any data.
|
|
878
927
|
// This means our subscriber disconnected or withOnyx wrapped component unmounted.
|
|
879
928
|
if (!callbackToStateMapping[config.connectionID]) {
|
|
@@ -883,10 +932,54 @@ function sendDataToConnection(config, val, key) {
|
|
|
883
932
|
if (config.withOnyxInstance) {
|
|
884
933
|
config.withOnyxInstance.setWithOnyxState(config.statePropertyName, val);
|
|
885
934
|
} else if (_underscore.default.isFunction(config.callback)) {
|
|
886
|
-
config.callback(val,
|
|
935
|
+
config.callback(val, matchedKey);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* We check to see if this key is flagged as safe for eviction and add it to the recentlyAccessedKeys list so that when we
|
|
941
|
+
* run out of storage the least recently accessed key can be removed.
|
|
942
|
+
*
|
|
943
|
+
* @private
|
|
944
|
+
* @param {Object} mapping
|
|
945
|
+
*/
|
|
946
|
+
function addKeyToRecentlyAccessedIfNeeded(mapping) {
|
|
947
|
+
if (!isSafeEvictionKey(mapping.key)) {
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
// Try to free some cache whenever we connect to a safe eviction key
|
|
952
|
+
_OnyxCache.default.removeLeastRecentlyUsedKeys();
|
|
953
|
+
|
|
954
|
+
if (mapping.withOnyxInstance && !isCollectionKey(mapping.key)) {
|
|
955
|
+
// All React components subscribing to a key flagged as a safe eviction key must implement the canEvict property.
|
|
956
|
+
if (_underscore.default.isUndefined(mapping.canEvict)) {
|
|
957
|
+
throw new Error("Cannot subscribe to safe eviction key '" +
|
|
958
|
+
mapping.key + "' without providing a canEvict value.");
|
|
959
|
+
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
addLastAccessedKey(mapping.key);
|
|
887
963
|
}
|
|
888
964
|
}
|
|
889
965
|
|
|
966
|
+
/**
|
|
967
|
+
* Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.
|
|
968
|
+
*
|
|
969
|
+
* @private
|
|
970
|
+
* @param {Array} matchingKeys
|
|
971
|
+
* @param {Object} mapping
|
|
972
|
+
*/
|
|
973
|
+
function getCollectionDataAndSendAsObject(matchingKeys, mapping) {
|
|
974
|
+
Promise.all(_underscore.default.map(matchingKeys, function (key) {return get(key);})).
|
|
975
|
+
then(function (values) {return _underscore.default.reduce(values, function (finalObject, value, i) {
|
|
976
|
+
// eslint-disable-next-line no-param-reassign
|
|
977
|
+
finalObject[matchingKeys[i]] = value;
|
|
978
|
+
return finalObject;
|
|
979
|
+
}, {});}).
|
|
980
|
+
then(function (val) {return sendDataToConnection(mapping, val);});
|
|
981
|
+
}
|
|
982
|
+
|
|
890
983
|
/**
|
|
891
984
|
* Subscribes a react component's state directly to a store key
|
|
892
985
|
*
|
|
@@ -919,58 +1012,63 @@ function connect(mapping) {
|
|
|
919
1012
|
|
|
920
1013
|
// Commit connection only after init passes
|
|
921
1014
|
deferredInitTask.promise.
|
|
922
|
-
then(function () {
|
|
923
|
-
|
|
924
|
-
|
|
1015
|
+
then(function () {return addKeyToRecentlyAccessedIfNeeded(mapping);}).
|
|
1016
|
+
then(getAllKeys).
|
|
1017
|
+
then(function (keys) {
|
|
1018
|
+
// We search all the keys in storage to see if any are a "match" for the subscriber we are connecting so that we
|
|
1019
|
+
// can send data back to the subscriber. Note that multiple keys can match as a subscriber could either be
|
|
1020
|
+
// subscribed to a "collection key" or a single key.
|
|
1021
|
+
var matchingKeys = _underscore.default.filter(keys, function (key) {return isKeyMatch(mapping.key, key);});
|
|
1022
|
+
|
|
1023
|
+
// If the key being connected to does not exist we initialize the value with null. For subscribers that connected
|
|
1024
|
+
// directly via connect() they will simply get a null value sent to them without any information about which key matched
|
|
1025
|
+
// since there are none matched. In withOnyx() we wait for all connected keys to return a value before rendering the child
|
|
1026
|
+
// component. This null value will be filtered out so that the connected component can utilize defaultProps.
|
|
1027
|
+
if (matchingKeys.length === 0) {
|
|
1028
|
+
sendDataToConnection(mapping, null);
|
|
925
1029
|
return;
|
|
926
1030
|
}
|
|
927
1031
|
|
|
928
|
-
//
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
if (
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
1032
|
+
// When using a callback subscriber we will either trigger the provided callback for each key we find or combine all values
|
|
1033
|
+
// into an object and just make a single call. The latter behavior is enabled by providing a waitForCollectionCallback key
|
|
1034
|
+
// combined with a subscription to a collection key.
|
|
1035
|
+
if (_underscore.default.isFunction(mapping.callback)) {
|
|
1036
|
+
if (isCollectionKey(mapping.key)) {
|
|
1037
|
+
if (mapping.waitForCollectionCallback) {
|
|
1038
|
+
getCollectionDataAndSendAsObject(matchingKeys, mapping);
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
937
1041
|
|
|
1042
|
+
// We did not opt into using waitForCollectionCallback mode so the callback is called for every matching key.
|
|
1043
|
+
var _loop3 = function _loop3(i) {
|
|
1044
|
+
get(matchingKeys[i]).then(function (val) {return sendDataToConnection(mapping, val, matchingKeys[i]);});};for (var i = 0; i < matchingKeys.length; i++) {_loop3(i);
|
|
1045
|
+
}
|
|
1046
|
+
return;
|
|
938
1047
|
}
|
|
939
1048
|
|
|
940
|
-
|
|
1049
|
+
// If we are not subscribed to a collection key then there's only a single key to send an update for.
|
|
1050
|
+
get(mapping.key).then(function (val) {return sendDataToConnection(mapping, val, mapping.key);});
|
|
1051
|
+
return;
|
|
941
1052
|
}
|
|
942
|
-
}).
|
|
943
|
-
then(getAllKeys).
|
|
944
|
-
then(function (keys) {
|
|
945
|
-
// Find all the keys matched by the config key
|
|
946
|
-
var matchingKeys = _underscore.default.filter(keys, function (key) {return isKeyMatch(mapping.key, key);});
|
|
947
1053
|
|
|
948
|
-
// If
|
|
949
|
-
|
|
950
|
-
|
|
1054
|
+
// If we have a withOnyxInstance that means a React component has subscribed via the withOnyx() HOC and we need to
|
|
1055
|
+
// group collection key member data into an object.
|
|
1056
|
+
if (mapping.withOnyxInstance) {
|
|
1057
|
+
if (isCollectionKey(mapping.key)) {
|
|
1058
|
+
getCollectionDataAndSendAsObject(matchingKeys, mapping);
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
// If the subscriber is not using a collection key then we just send a single value back to the subscriber
|
|
1063
|
+
get(mapping.key).then(function (val) {return sendDataToConnection(mapping, val, mapping.key);});
|
|
951
1064
|
return;
|
|
952
1065
|
}
|
|
953
1066
|
|
|
954
|
-
|
|
955
|
-
// for each key we find. It's up to the subscriber to know whether
|
|
956
|
-
// to expect a single key or multiple keys in the case of a collection.
|
|
957
|
-
// React components are an exception since we'll want to send their
|
|
958
|
-
// initial data as a single object when using collection keys.
|
|
959
|
-
if (mapping.withOnyxInstance && isCollectionKey(mapping.key) || mapping.waitForCollectionCallback) {
|
|
960
|
-
Promise.all(_underscore.default.map(matchingKeys, function (key) {return get(key);})).
|
|
961
|
-
then(function (values) {return _underscore.default.reduce(values, function (finalObject, value, i) {
|
|
962
|
-
// eslint-disable-next-line no-param-reassign
|
|
963
|
-
finalObject[matchingKeys[i]] = value;
|
|
964
|
-
return finalObject;
|
|
965
|
-
}, {});}).
|
|
966
|
-
then(function (val) {return sendDataToConnection(mapping, val);});
|
|
967
|
-
} else {
|
|
968
|
-
_underscore.default.each(matchingKeys, function (key) {
|
|
969
|
-
get(key).then(function (val) {return sendDataToConnection(mapping, val, key);});
|
|
970
|
-
});
|
|
971
|
-
}
|
|
1067
|
+
console.error('Warning: Onyx.connect() was found without a callback or withOnyxInstance');
|
|
972
1068
|
});
|
|
973
1069
|
|
|
1070
|
+
// The connectionID is returned back to the caller so that it can be used to clean up the connection when it's no longer needed
|
|
1071
|
+
// by calling Onyx.disconnect(connectionID).
|
|
974
1072
|
return connectionID;
|
|
975
1073
|
}
|
|
976
1074
|
|
|
@@ -1154,7 +1252,7 @@ function applyMerge(key, data) {
|
|
|
1154
1252
|
if (_underscore.default.isObject(data) || _underscore.default.every(mergeValues, _underscore.default.isObject)) {
|
|
1155
1253
|
// Object values are merged one after the other
|
|
1156
1254
|
return _underscore.default.reduce(mergeValues, function (modifiedData, mergeValue) {
|
|
1157
|
-
var newData = (0,
|
|
1255
|
+
var newData = (0, _mergeWithCustomized.default)({}, modifiedData, mergeValue);
|
|
1158
1256
|
|
|
1159
1257
|
// We will also delete any object keys that are undefined or null.
|
|
1160
1258
|
// Deleting keys is not supported by AsyncStorage so we do it this way.
|
|
@@ -1225,7 +1323,7 @@ function initializeWithDefaultKeyStates() {
|
|
|
1225
1323
|
then(function (pairs) {
|
|
1226
1324
|
var asObject = _underscore.default.object(pairs);
|
|
1227
1325
|
|
|
1228
|
-
var merged = (0,
|
|
1326
|
+
var merged = (0, _mergeWithCustomized.default)(asObject, defaultKeyStates);
|
|
1229
1327
|
_OnyxCache.default.merge(merged);
|
|
1230
1328
|
_underscore.default.each(merged, function (val, key) {return keyChanged(key, val);});
|
|
1231
1329
|
});
|
|
@@ -1280,7 +1378,7 @@ function clear() {
|
|
|
1280
1378
|
*/
|
|
1281
1379
|
function mergeCollection(collectionKey, collection) {
|
|
1282
1380
|
// Confirm all the collection keys belong to the same parent
|
|
1283
|
-
_underscore.default.each(collection, function (
|
|
1381
|
+
_underscore.default.each(collection, function (_data, dataKey) {
|
|
1284
1382
|
if (isKeyMatch(collectionKey, dataKey)) {
|
|
1285
1383
|
return;
|
|
1286
1384
|
}
|
|
@@ -1332,7 +1430,7 @@ function mergeCollection(collectionKey, collection) {
|
|
|
1332
1430
|
*/
|
|
1333
1431
|
function update(data) {
|
|
1334
1432
|
// First, validate the Onyx object is in the format we expect
|
|
1335
|
-
_underscore.default.each(data, function (
|
|
1433
|
+
_underscore.default.each(data, function (_ref3) {var onyxMethod = _ref3.onyxMethod,key = _ref3.key;
|
|
1336
1434
|
if (!_underscore.default.contains(['clear', 'set', 'merge', 'mergecollection'], onyxMethod)) {
|
|
1337
1435
|
throw new Error("Invalid onyxMethod " + onyxMethod + " in Onyx update.");
|
|
1338
1436
|
}
|
|
@@ -1341,7 +1439,7 @@ function update(data) {
|
|
|
1341
1439
|
}
|
|
1342
1440
|
});
|
|
1343
1441
|
|
|
1344
|
-
_underscore.default.each(data, function (
|
|
1442
|
+
_underscore.default.each(data, function (_ref4) {var onyxMethod = _ref4.onyxMethod,key = _ref4.key,value = _ref4.value;
|
|
1345
1443
|
switch (onyxMethod) {
|
|
1346
1444
|
case 'set':
|
|
1347
1445
|
set(key, value);
|
|
@@ -1395,7 +1493,7 @@ function init()
|
|
|
1395
1493
|
|
|
1396
1494
|
|
|
1397
1495
|
|
|
1398
|
-
{var
|
|
1496
|
+
{var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},_ref5$keys = _ref5.keys,keys = _ref5$keys === void 0 ? {} : _ref5$keys,_ref5$initialKeyState = _ref5.initialKeyStates,initialKeyStates = _ref5$initialKeyState === void 0 ? {} : _ref5$initialKeyState,_ref5$safeEvictionKey = _ref5.safeEvictionKeys,safeEvictionKeys = _ref5$safeEvictionKey === void 0 ? [] : _ref5$safeEvictionKey,_ref5$maxCachedKeysCo = _ref5.maxCachedKeysCount,maxCachedKeysCount = _ref5$maxCachedKeysCo === void 0 ? 1000 : _ref5$maxCachedKeysCo,_ref5$captureMetrics = _ref5.captureMetrics,captureMetrics = _ref5$captureMetrics === void 0 ? false : _ref5$captureMetrics,_ref5$shouldSyncMulti = _ref5.shouldSyncMultipleInstances,shouldSyncMultipleInstances = _ref5$shouldSyncMulti === void 0 ? Boolean(__webpack_require__.g.localStorage) : _ref5$shouldSyncMulti,_ref5$keysToDisableSy = _ref5.keysToDisableSyncEvents,keysToDisableSyncEvents = _ref5$keysToDisableSy === void 0 ? [] : _ref5$keysToDisableSy;
|
|
1399
1497
|
if (captureMetrics) {
|
|
1400
1498
|
// The code here is only bundled and applied when the captureMetrics is set
|
|
1401
1499
|
// eslint-disable-next-line no-use-before-define
|
|
@@ -1495,8 +1593,7 @@ Onyx;exports["default"] = _default;
|
|
|
1495
1593
|
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
1496
1594
|
|
|
1497
1595
|
var _interopRequireDefault = __webpack_require__(/*! @babel/runtime/helpers/interopRequireDefault */ "./node_modules/@babel/runtime/helpers/interopRequireDefault.js");Object.defineProperty(exports, "__esModule", ({ value: true }));exports["default"] = void 0;var _toConsumableArray2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/toConsumableArray */ "./node_modules/@babel/runtime/helpers/toConsumableArray.js"));var _classCallCheck2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/classCallCheck */ "./node_modules/@babel/runtime/helpers/classCallCheck.js"));var _createClass2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/createClass */ "./node_modules/@babel/runtime/helpers/createClass.js"));var _underscore = _interopRequireDefault(__webpack_require__(/*! underscore */ "underscore"));
|
|
1498
|
-
var
|
|
1499
|
-
var _customizerForMergeWith = _interopRequireDefault(__webpack_require__(/*! ./customizerForMergeWith */ "./lib/customizerForMergeWith.js"));
|
|
1596
|
+
var _mergeWithCustomized = _interopRequireDefault(__webpack_require__(/*! ./mergeWithCustomized */ "./lib/mergeWithCustomized.js"));
|
|
1500
1597
|
|
|
1501
1598
|
var isDefined = _underscore.default.negate(_underscore.default.isUndefined);
|
|
1502
1599
|
|
|
@@ -1607,7 +1704,7 @@ OnyxCache = /*#__PURE__*/function () {
|
|
|
1607
1704
|
* @param {Record<string, *>} data - a map of (cache) key - values
|
|
1608
1705
|
*/ }, { key: "merge", value: function merge(
|
|
1609
1706
|
data) {var _this = this;
|
|
1610
|
-
this.storageMap = (0,
|
|
1707
|
+
this.storageMap = (0, _mergeWithCustomized.default)({}, this.storageMap, data);
|
|
1611
1708
|
|
|
1612
1709
|
var storageKeys = this.getAllKeys();
|
|
1613
1710
|
var mergedKeys = _underscore.default.keys(data);
|
|
@@ -1778,13 +1875,13 @@ function createDeferredTask() {
|
|
|
1778
1875
|
|
|
1779
1876
|
/***/ }),
|
|
1780
1877
|
|
|
1781
|
-
/***/ "./lib/
|
|
1782
|
-
|
|
1783
|
-
!*** ./lib/
|
|
1784
|
-
|
|
1878
|
+
/***/ "./lib/mergeWithCustomized.js":
|
|
1879
|
+
/*!************************************!*\
|
|
1880
|
+
!*** ./lib/mergeWithCustomized.js ***!
|
|
1881
|
+
\************************************/
|
|
1785
1882
|
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
1786
1883
|
|
|
1787
|
-
var _interopRequireDefault = __webpack_require__(/*! @babel/runtime/helpers/interopRequireDefault */ "./node_modules/@babel/runtime/helpers/interopRequireDefault.js");Object.defineProperty(exports, "__esModule", ({ value: true }));exports["default"] = void 0;var
|
|
1884
|
+
var _interopRequireDefault = __webpack_require__(/*! @babel/runtime/helpers/interopRequireDefault */ "./node_modules/@babel/runtime/helpers/interopRequireDefault.js");Object.defineProperty(exports, "__esModule", ({ value: true }));exports["default"] = void 0;var _mergeWith = _interopRequireDefault(__webpack_require__(/*! lodash/mergeWith */ "lodash/mergeWith"));
|
|
1788
1885
|
|
|
1789
1886
|
/**
|
|
1790
1887
|
* When merging 2 objects into onyx that contain an array, we want to completely replace the array instead of the default
|
|
@@ -1799,12 +1896,17 @@ var _interopRequireDefault = __webpack_require__(/*! @babel/runtime/helpers/inte
|
|
|
1799
1896
|
*/
|
|
1800
1897
|
// eslint-disable-next-line rulesdir/prefer-early-return
|
|
1801
1898
|
function customizerForMergeWith(objValue, srcValue) {
|
|
1802
|
-
|
|
1899
|
+
// eslint-disable-next-line rulesdir/prefer-underscore-method
|
|
1900
|
+
if (Array.isArray(objValue)) {
|
|
1803
1901
|
return srcValue;
|
|
1804
1902
|
}
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
function mergeWithCustomized() {for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}
|
|
1906
|
+
return _mergeWith.default.apply(void 0, args.concat([customizerForMergeWith]));
|
|
1805
1907
|
}var _default =
|
|
1806
1908
|
|
|
1807
|
-
|
|
1909
|
+
mergeWithCustomized;exports["default"] = _default;
|
|
1808
1910
|
|
|
1809
1911
|
/***/ }),
|
|
1810
1912
|
|
|
@@ -1914,9 +2016,8 @@ var _interopRequireDefault = __webpack_require__(/*! @babel/runtime/helpers/inte
|
|
|
1914
2016
|
|
|
1915
2017
|
var _localforage = _interopRequireDefault(__webpack_require__(/*! localforage */ "localforage"));
|
|
1916
2018
|
var _underscore = _interopRequireDefault(__webpack_require__(/*! underscore */ "underscore"));
|
|
1917
|
-
var _mergeWith = _interopRequireDefault(__webpack_require__(/*! lodash/mergeWith */ "lodash/mergeWith"));
|
|
1918
2019
|
var _SyncQueue = _interopRequireDefault(__webpack_require__(/*! ../../SyncQueue */ "./lib/SyncQueue.js"));
|
|
1919
|
-
var
|
|
2020
|
+
var _mergeWithCustomized = _interopRequireDefault(__webpack_require__(/*! ../../mergeWithCustomized */ "./lib/mergeWithCustomized.js")); /**
|
|
1920
2021
|
* @file
|
|
1921
2022
|
* The storage provider based on localforage allows us to store most anything in its
|
|
1922
2023
|
* natural form in the underlying DB without having to stringify or de-stringify it
|
|
@@ -1933,7 +2034,7 @@ var provider = {
|
|
|
1933
2034
|
return _localforage.default.getItem(key).
|
|
1934
2035
|
then(function (existingValue) {
|
|
1935
2036
|
var newValue = _underscore.default.isObject(existingValue) ?
|
|
1936
|
-
(0,
|
|
2037
|
+
(0, _mergeWithCustomized.default)({}, existingValue, value) :
|
|
1937
2038
|
value;
|
|
1938
2039
|
return _localforage.default.setItem(key, newValue);
|
|
1939
2040
|
});
|
|
@@ -3335,17 +3436,6 @@ module.exports = __WEBPACK_EXTERNAL_MODULE_lodash_get__;
|
|
|
3335
3436
|
|
|
3336
3437
|
/***/ }),
|
|
3337
3438
|
|
|
3338
|
-
/***/ "lodash/merge":
|
|
3339
|
-
/*!*******************************!*\
|
|
3340
|
-
!*** external "lodash/merge" ***!
|
|
3341
|
-
\*******************************/
|
|
3342
|
-
/***/ ((module) => {
|
|
3343
|
-
|
|
3344
|
-
"use strict";
|
|
3345
|
-
module.exports = __WEBPACK_EXTERNAL_MODULE_lodash_merge__;
|
|
3346
|
-
|
|
3347
|
-
/***/ }),
|
|
3348
|
-
|
|
3349
3439
|
/***/ "lodash/mergeWith":
|
|
3350
3440
|
/*!***********************************!*\
|
|
3351
3441
|
!*** external "lodash/mergeWith" ***!
|