react-native-onyx 1.0.21 → 1.0.22

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 CHANGED
@@ -13,7 +13,7 @@
13
13
  <dt><a href="#disconnect">disconnect(connectionID, [keyToRemoveFromEvictionBlocklist])</a></dt>
14
14
  <dd><p>Remove the listener for a react component</p>
15
15
  </dd>
16
- <dt><a href="#notifySubscribersOnNextTick">notifySubscribersOnNextTick(key, value)</a></dt>
16
+ <dt><a href="#notifySubscribersOnNextTick">notifySubscribersOnNextTick(key, value, [canUpdateSubscriber])</a></dt>
17
17
  <dd><p>This method mostly exists for historical reasons as this library was initially designed without a memory cache and one was added later.
18
18
  For this reason, Onyx works more similar to what you might expect from a native AsyncStorage with reads, writes, etc all becoming
19
19
  available async. Since we have code in our main applications that might expect things to work this way it&#39;s not safe to change this
@@ -115,7 +115,7 @@ Onyx.disconnect(connectionID);
115
115
  ```
116
116
  <a name="notifySubscribersOnNextTick"></a>
117
117
 
118
- ## notifySubscribersOnNextTick(key, value)
118
+ ## notifySubscribersOnNextTick(key, value, [canUpdateSubscriber])
119
119
  This method mostly exists for historical reasons as this library was initially designed without a memory cache and one was added later.
120
120
  For this reason, Onyx works more similar to what you might expect from a native AsyncStorage with reads, writes, etc all becoming
121
121
  available async. Since we have code in our main applications that might expect things to work this way it's not safe to change this
@@ -123,11 +123,16 @@ behavior just yet.
123
123
 
124
124
  **Kind**: global function
125
125
 
126
- | Param | Type |
127
- | --- | --- |
128
- | key | <code>String</code> |
129
- | value | <code>\*</code> |
126
+ | Param | Type | Description |
127
+ | --- | --- | --- |
128
+ | key | <code>String</code> | |
129
+ | value | <code>\*</code> | |
130
+ | [canUpdateSubscriber] | <code>function</code> | only subscribers that pass this truth test will be updated |
130
131
 
132
+ **Example**
133
+ ```js
134
+ notifySubscribersOnNextTick(key, value, subscriber => subscriber.initWithStoredValues === false)
135
+ ```
131
136
  <a name="set"></a>
132
137
 
133
138
  ## set(key, value) ⇒ <code>Promise</code>
@@ -252,6 +257,7 @@ Initialize the store with actions and listening for storage events
252
257
  | [options.captureMetrics] | <code>Boolean</code> | | Enables Onyx benchmarking and exposes the get/print/reset functions |
253
258
  | [options.shouldSyncMultipleInstances] | <code>Boolean</code> | | Auto synchronize storage events between multiple instances of Onyx running in different tabs/windows. Defaults to true for platforms that support local storage (web/desktop) |
254
259
  | [option.keysToDisableSyncEvents] | <code>Array.&lt;String&gt;</code> | <code>[]</code> | Contains keys for which we want to disable sync event across tabs. |
260
+ | [options.debugSetState] | <code>Boolean</code> | | Enables debugging setState() calls to connected components. |
255
261
 
256
262
  **Example**
257
263
  ```js
@@ -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/get"), require("localforage"), require("lodash/transform"), require("react"));
3
+ module.exports = factory(require("underscore"), require("expensify-common/lib/str"), require("lodash/get"), require("localforage"), require("fast-equals"), require("lodash/transform"), require("react"));
4
4
  else if(typeof define === 'function' && define.amd)
5
- define(["underscore", "expensify-common/lib/str", "lodash/get", "localforage", "lodash/transform", "react"], factory);
5
+ define(["underscore", "expensify-common/lib/str", "lodash/get", "localforage", "fast-equals", "lodash/transform", "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/get"), require("localforage"), require("lodash/transform"), require("react"));
7
+ exports["react-native-onyx/web"] = factory(require("underscore"), require("expensify-common/lib/str"), require("lodash/get"), require("localforage"), require("fast-equals"), require("lodash/transform"), require("react"));
8
8
  else
9
- root["react-native-onyx/web"] = factory(root["underscore"], root["expensify-common/lib/str"], root["lodash/get"], root["localforage"], root["lodash/transform"], 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_transform__, __WEBPACK_EXTERNAL_MODULE_react__) => {
9
+ root["react-native-onyx/web"] = factory(root["underscore"], root["expensify-common/lib/str"], root["lodash/get"], root["localforage"], root["fast-equals"], root["lodash/transform"], 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_fast_equals__, __WEBPACK_EXTERNAL_MODULE_lodash_transform__, __WEBPACK_EXTERNAL_MODULE_react__) => {
11
11
  return /******/ (() => { // webpackBootstrap
12
12
  /******/ var __webpack_modules__ = ({
13
13
 
@@ -858,11 +858,15 @@ function keysChanged(collectionKey, partialCollection) {
858
858
  /**
859
859
  * When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks
860
860
  *
861
+ * @example
862
+ * keyChanged(key, value, subscriber => subscriber.initWithStoredValues === false)
863
+ *
861
864
  * @private
862
865
  * @param {String} key
863
866
  * @param {*} data
867
+ * @param {Function} [canUpdateSubscriber] only subscribers that pass this truth test will be updated
864
868
  */
865
- function keyChanged(key, data) {
869
+ function keyChanged(key, data, canUpdateSubscriber) {
866
870
  // Add or remove this key from the recentlyAccessedKeys lists
867
871
  if (!_underscore.default.isNull(data)) {
868
872
  addLastAccessedKey(key);
@@ -876,7 +880,7 @@ function keyChanged(key, data) {
876
880
  var stateMappingKeys = _underscore.default.keys(callbackToStateMapping);var _loop2 = function _loop2(
877
881
  i) {
878
882
  var subscriber = callbackToStateMapping[stateMappingKeys[i]];
879
- if (!subscriber || !isKeyMatch(subscriber.key, key)) {
883
+ if (!subscriber || !isKeyMatch(subscriber.key, key) || _underscore.default.isFunction(canUpdateSubscriber) && !canUpdateSubscriber(subscriber)) {
880
884
  return "continue";
881
885
  }
882
886
 
@@ -1122,12 +1126,16 @@ function disconnect(connectionID, keyToRemoveFromEvictionBlocklist) {
1122
1126
  * available async. Since we have code in our main applications that might expect things to work this way it's not safe to change this
1123
1127
  * behavior just yet.
1124
1128
  *
1129
+ * @example
1130
+ * notifySubscribersOnNextTick(key, value, subscriber => subscriber.initWithStoredValues === false)
1131
+ *
1125
1132
  * @param {String} key
1126
1133
  * @param {*} value
1134
+ * @param {Function} [canUpdateSubscriber] only subscribers that pass this truth test will be updated
1127
1135
  */
1128
1136
  // eslint-disable-next-line rulesdir/no-negated-variables
1129
- function notifySubscribersOnNextTick(key, value) {
1130
- Promise.resolve().then(function () {return keyChanged(key, value);});
1137
+ function notifySubscribersOnNextTick(key, value, canUpdateSubscriber) {
1138
+ Promise.resolve().then(function () {return keyChanged(key, value, canUpdateSubscriber);});
1131
1139
  }
1132
1140
 
1133
1141
  /**
@@ -1194,6 +1202,14 @@ function set(key, value) {
1194
1202
  Logger.logAlert("Onyx.set() called after Onyx.merge() for key: " + key + ". It is recommended to use set() or merge() not both.");
1195
1203
  }
1196
1204
 
1205
+ // If the value in the cache is the same as what we have then do not update subscribers unless they
1206
+ // have initWithStoredValues: false then they MUST get all updates even if nothing has changed.
1207
+ if (!_OnyxCache.default.hasValueChanged(key, value)) {
1208
+ _OnyxCache.default.addToAccessedKeys(key);
1209
+ notifySubscribersOnNextTick(key, value, function (subscriber) {return subscriber.initWithStoredValues === false;});
1210
+ return Promise.resolve();
1211
+ }
1212
+
1197
1213
  // Adds the key to cache when it's not available
1198
1214
  _OnyxCache.default.set(key, value);
1199
1215
  notifySubscribersOnNextTick(key, value);
@@ -1623,6 +1639,7 @@ Onyx;exports["default"] = _default;
1623
1639
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
1624
1640
 
1625
1641
  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 _extends2 = _interopRequireDefault(__webpack_require__(/*! @babel/runtime/helpers/extends */ "./node_modules/@babel/runtime/helpers/extends.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"));
1642
+ var _fastEquals = __webpack_require__(/*! fast-equals */ "fast-equals");
1626
1643
  var _fastMerge = _interopRequireDefault(__webpack_require__(/*! ./fastMerge */ "./lib/fastMerge.js"));
1627
1644
 
1628
1645
  var isDefined = _underscore.default.negate(_underscore.default.isUndefined);
@@ -1814,6 +1831,15 @@ OnyxCache = /*#__PURE__*/function () {
1814
1831
  */ }, { key: "setRecentKeysLimit", value: function setRecentKeysLimit(
1815
1832
  limit) {
1816
1833
  this.maxRecentKeysSize = limit;
1834
+ }
1835
+
1836
+ /**
1837
+ * @param {String} key
1838
+ * @param {*} value
1839
+ * @returns {Boolean}
1840
+ */ }, { key: "hasValueChanged", value: function hasValueChanged(
1841
+ key, value) {
1842
+ return !(0, _fastEquals.deepEqual)(this.storageMap[key], value);
1817
1843
  } }]);return OnyxCache;}();
1818
1844
 
1819
1845
 
@@ -3562,6 +3588,17 @@ module.exports = __WEBPACK_EXTERNAL_MODULE_expensify_common_lib_str__;
3562
3588
 
3563
3589
  /***/ }),
3564
3590
 
3591
+ /***/ "fast-equals":
3592
+ /*!******************************!*\
3593
+ !*** external "fast-equals" ***!
3594
+ \******************************/
3595
+ /***/ ((module) => {
3596
+
3597
+ "use strict";
3598
+ module.exports = __WEBPACK_EXTERNAL_MODULE_fast_equals__;
3599
+
3600
+ /***/ }),
3601
+
3565
3602
  /***/ "localforage":
3566
3603
  /*!******************************!*\
3567
3604
  !*** external "localforage" ***!