react-native-onyx 1.0.38 → 1.0.40

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.
@@ -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("fast-equals"), require("lodash/get"), require("localforage"), require("lodash/transform"), require("react"));
3
+ module.exports = factory(require("underscore"), require("expensify-common/lib/str"), require("fast-equals"), require("lodash/get"), require("localforage"), require("localforage-removeitems"), require("lodash/transform"), require("react"));
4
4
  else if(typeof define === 'function' && define.amd)
5
- define(["underscore", "expensify-common/lib/str", "fast-equals", "lodash/get", "localforage", "lodash/transform", "react"], factory);
5
+ define(["underscore", "expensify-common/lib/str", "fast-equals", "lodash/get", "localforage", "localforage-removeitems", "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("fast-equals"), 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("fast-equals"), require("lodash/get"), require("localforage"), require("localforage-removeitems"), require("lodash/transform"), require("react"));
8
8
  else
9
- root["react-native-onyx/web"] = factory(root["underscore"], root["expensify-common/lib/str"], root["fast-equals"], 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_fast_equals__, __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["fast-equals"], root["lodash/get"], root["localforage"], root["localforage-removeitems"], root["lodash/transform"], root["react"]);
10
+ })(self, (__WEBPACK_EXTERNAL_MODULE_underscore__, __WEBPACK_EXTERNAL_MODULE_expensify_common_lib_str__, __WEBPACK_EXTERNAL_MODULE_fast_equals__, __WEBPACK_EXTERNAL_MODULE_lodash_get__, __WEBPACK_EXTERNAL_MODULE_localforage__, __WEBPACK_EXTERNAL_MODULE_localforage_removeitems__, __WEBPACK_EXTERNAL_MODULE_lodash_transform__, __WEBPACK_EXTERNAL_MODULE_react__) => {
11
11
  return /******/ (() => { // webpackBootstrap
12
12
  /******/ var __webpack_modules__ = ({
13
13
 
@@ -1132,55 +1132,46 @@ function initializeWithDefaultKeyStates() {
1132
1132
  function clear() {let keysToPreserve = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1133
1133
  return getAllKeys().
1134
1134
  then((keys) => {
1135
- const keyValuesToReset = [];
1136
- const defaultKeys = underscore__WEBPACK_IMPORTED_MODULE_0___default().keys(defaultKeyStates);
1137
-
1138
- // Get all the values for the keys that need to be preserved. These key/value pairs will be set
1139
- // in Onyx after the database is cleared().
1140
- const keyValuesToPreserve = underscore__WEBPACK_IMPORTED_MODULE_0___default().map(keysToPreserve, (key) => [key, _OnyxCache__WEBPACK_IMPORTED_MODULE_5__["default"].getValue(key)]);
1135
+ const keysToBeClearedFromStorage = [];
1136
+ const keyValuesToResetAsCollection = {};
1137
+ const keyValuesToResetIndividually = {};
1141
1138
 
1142
1139
  // The only keys that should not be cleared are:
1143
1140
  // 1. Anything specifically passed in keysToPreserve (because some keys like language preferences, offline
1144
1141
  // status, or activeClients need to remain in Onyx even when signed out)
1145
1142
  // 2. Any keys with a default state (because they need to remain in Onyx as their default, and setting them
1146
1143
  // to null would cause unknown behavior)
1147
- const keysToClear = underscore__WEBPACK_IMPORTED_MODULE_0___default().difference(keys, keysToPreserve, defaultKeys);
1148
- keyValuesToReset.push(...underscore__WEBPACK_IMPORTED_MODULE_0___default().map(keysToClear, (key) => [key, null]));
1149
-
1150
- // Remove any keysToPreserve from the defaultKeyStates because if they are passed in it has been explicitly
1151
- // called out to preserve those values instead of resetting them back
1152
- // to the default.
1153
- const defaultKeyValuePairs = underscore__WEBPACK_IMPORTED_MODULE_0___default().pairs(underscore__WEBPACK_IMPORTED_MODULE_0___default().omit(defaultKeyStates, ...keysToPreserve));
1154
-
1155
- // Add the default key value pairs to the keyValuesToReset so that they get set back to their default values
1156
- // when we clear Onyx
1157
- keyValuesToReset.push(...defaultKeyValuePairs);
1158
-
1159
- // We now have all the key/values that need to be reset, but we're not done yet!
1160
- // There will be two groups of key/values and they each need to be updated a little bit differently.
1161
- // Collection keys need to be notified differently than non collection keys
1162
- const keyValuesToResetAsCollection = {};
1163
- const keyValuesToResetIndividually = {};
1164
-
1165
- // Make sure that we also reset the cache values before clearing the values from storage.
1166
- // We do this before clearing Storage so that any call to clear() followed by merge() on a key with a
1167
- // default state results in the merged value getting saved, since the update from the merge() call would
1168
- // happen on the tick after the update from this clear()
1169
- underscore__WEBPACK_IMPORTED_MODULE_0___default().each(keyValuesToReset, (keyValue) => {
1170
- const key = keyValue[0];
1171
- const value = keyValue[1];
1172
- _OnyxCache__WEBPACK_IMPORTED_MODULE_5__["default"].set(key, value);
1173
-
1174
- const collectionKey = key.substring(0, key.indexOf('_') + 1);
1175
- if (collectionKey) {
1176
- if (!keyValuesToResetAsCollection[collectionKey]) {
1177
- keyValuesToResetAsCollection[collectionKey] = {};
1144
+ underscore__WEBPACK_IMPORTED_MODULE_0___default().each(keys, (key) => {
1145
+ const isKeyToPreserve = underscore__WEBPACK_IMPORTED_MODULE_0___default().contains(keysToPreserve, key);
1146
+ const isDefaultKey = underscore__WEBPACK_IMPORTED_MODULE_0___default().has(defaultKeyStates, key);
1147
+
1148
+ // If the key is being removed or reset to default:
1149
+ // 1. Update it in the cache
1150
+ // 2. Figure out whether it is a collection key or not,
1151
+ // since collection key subscribers need to be updated differently
1152
+ if (!isKeyToPreserve) {
1153
+ const oldValue = _OnyxCache__WEBPACK_IMPORTED_MODULE_5__["default"].getValue(key);
1154
+ const newValue = underscore__WEBPACK_IMPORTED_MODULE_0___default().get(defaultKeyStates, key, null);
1155
+ if (newValue !== oldValue) {
1156
+ _OnyxCache__WEBPACK_IMPORTED_MODULE_5__["default"].set(key, newValue);
1157
+ const collectionKey = key.substring(0, key.indexOf('_') + 1);
1158
+ if (collectionKey) {
1159
+ if (!keyValuesToResetAsCollection[collectionKey]) {
1160
+ keyValuesToResetAsCollection[collectionKey] = {};
1161
+ }
1162
+ keyValuesToResetAsCollection[collectionKey][key] = newValue;
1163
+ } else {
1164
+ keyValuesToResetIndividually[key] = newValue;
1165
+ }
1178
1166
  }
1179
- keyValuesToResetAsCollection[collectionKey][key] = value;
1167
+ }
1168
+
1169
+ if (isKeyToPreserve || isDefaultKey) {
1180
1170
  return;
1181
1171
  }
1182
1172
 
1183
- keyValuesToResetIndividually[key] = value;
1173
+ // If it isn't preserved and doesn't have a default, we'll remove it
1174
+ keysToBeClearedFromStorage.push(key);
1184
1175
  });
1185
1176
 
1186
1177
  // Notify the subscribers for each key/value group so they can receive the new values
@@ -1191,11 +1182,10 @@ function clear() {let keysToPreserve = arguments.length > 0 && arguments[0] !==
1191
1182
  notifyCollectionSubscribersOnNextTick(key, value);
1192
1183
  });
1193
1184
 
1194
- // Call clear() and make sure that the default key/values and the key/values from the parameter
1195
- // are preserved in storage. This makes sure to always leave storage in a state that contains
1196
- // all the default values and any additional values that we want to remain after the database is cleared.
1197
- return _storage__WEBPACK_IMPORTED_MODULE_6__["default"].clear().
1198
- then(() => _storage__WEBPACK_IMPORTED_MODULE_6__["default"].multiSet([...defaultKeyValuePairs, ...keyValuesToPreserve]));
1185
+ const defaultKeyValuePairs = underscore__WEBPACK_IMPORTED_MODULE_0___default().pairs(underscore__WEBPACK_IMPORTED_MODULE_0___default().omit(defaultKeyStates, keysToPreserve));
1186
+
1187
+ // Remove only the items that we want cleared from storage, and reset others to default
1188
+ return _storage__WEBPACK_IMPORTED_MODULE_6__["default"].removeItems(keysToBeClearedFromStorage).then(() => _storage__WEBPACK_IMPORTED_MODULE_6__["default"].multiSet(defaultKeyValuePairs));
1199
1189
  });
1200
1190
  }
1201
1191
 
@@ -1996,6 +1986,12 @@ function raiseStorageSyncEvent(onyxKey) {
1996
1986
  __webpack_require__.g.localStorage.removeItem(SYNC_ONYX, onyxKey);
1997
1987
  }
1998
1988
 
1989
+ function raiseStorageSyncManyKeysEvent(onyxKeys) {
1990
+ underscore__WEBPACK_IMPORTED_MODULE_0___default().each(onyxKeys, (onyxKey) => {
1991
+ raiseStorageSyncEvent(onyxKey);
1992
+ });
1993
+ }
1994
+
1999
1995
  const webStorage = {
2000
1996
  ..._providers_LocalForage__WEBPACK_IMPORTED_MODULE_1__["default"],
2001
1997
 
@@ -2010,6 +2006,9 @@ const webStorage = {
2010
2006
  this.removeItem = (key) => _providers_LocalForage__WEBPACK_IMPORTED_MODULE_1__["default"].removeItem(key).
2011
2007
  then(() => raiseStorageSyncEvent(key));
2012
2008
 
2009
+ this.removeItems = (keys) => _providers_LocalForage__WEBPACK_IMPORTED_MODULE_1__["default"].removeItems(keys).
2010
+ then(() => raiseStorageSyncManyKeysEvent(keys));
2011
+
2013
2012
  // If we just call Storage.clear other tabs will have no idea which keys were available previously
2014
2013
  // so that they can call keysChanged for them. That's why we iterate over every key and raise a storage sync
2015
2014
  // event for each one
@@ -2080,8 +2079,10 @@ __webpack_require__.r(__webpack_exports__);
2080
2079
  /* harmony import */ var localforage__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(localforage__WEBPACK_IMPORTED_MODULE_0__);
2081
2080
  /* harmony import */ var underscore__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! underscore */ "underscore");
2082
2081
  /* harmony import */ var underscore__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(underscore__WEBPACK_IMPORTED_MODULE_1__);
2083
- /* harmony import */ var _SyncQueue__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../SyncQueue */ "./lib/SyncQueue.js");
2084
- /* harmony import */ var _fastMerge__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../fastMerge */ "./lib/fastMerge.js");
2082
+ /* harmony import */ var localforage_removeitems__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! localforage-removeitems */ "localforage-removeitems");
2083
+ /* harmony import */ var localforage_removeitems__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(localforage_removeitems__WEBPACK_IMPORTED_MODULE_2__);
2084
+ /* harmony import */ var _SyncQueue__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../SyncQueue */ "./lib/SyncQueue.js");
2085
+ /* harmony import */ var _fastMerge__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../../fastMerge */ "./lib/fastMerge.js");
2085
2086
  /**
2086
2087
  * @file
2087
2088
  * The storage provider based on localforage allows us to store most anything in its
@@ -2093,6 +2094,9 @@ __webpack_require__.r(__webpack_exports__);
2093
2094
 
2094
2095
 
2095
2096
 
2097
+
2098
+ (0,localforage_removeitems__WEBPACK_IMPORTED_MODULE_2__.extendPrototype)((localforage__WEBPACK_IMPORTED_MODULE_0___default()));
2099
+
2096
2100
  localforage__WEBPACK_IMPORTED_MODULE_0___default().config({
2097
2101
  name: 'OnyxDB'
2098
2102
  });
@@ -2103,7 +2107,7 @@ const provider = {
2103
2107
  * So, we are slowing this process down by waiting until one write is complete before moving on
2104
2108
  * to the next.
2105
2109
  */
2106
- setItemQueue: new _SyncQueue__WEBPACK_IMPORTED_MODULE_2__["default"]((_ref) => {let { key, value, shouldMerge } = _ref;
2110
+ setItemQueue: new _SyncQueue__WEBPACK_IMPORTED_MODULE_3__["default"]((_ref) => {let { key, value, shouldMerge } = _ref;
2107
2111
  if (shouldMerge) {
2108
2112
  return localforage__WEBPACK_IMPORTED_MODULE_0___default().getItem(key).
2109
2113
  then((existingValue) => {
@@ -2111,7 +2115,7 @@ const provider = {
2111
2115
 
2112
2116
  // lodash adds a small overhead so we don't use it here
2113
2117
  // eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
2114
- ? Object.assign({}, (0,_fastMerge__WEBPACK_IMPORTED_MODULE_3__["default"])(existingValue, value)) :
2118
+ ? Object.assign({}, (0,_fastMerge__WEBPACK_IMPORTED_MODULE_4__["default"])(existingValue, value)) :
2115
2119
  value;
2116
2120
  return localforage__WEBPACK_IMPORTED_MODULE_0___default().setItem(key, newValue);
2117
2121
  });
@@ -2187,6 +2191,16 @@ const provider = {
2187
2191
  */
2188
2192
  removeItem: (localforage__WEBPACK_IMPORTED_MODULE_0___default().removeItem),
2189
2193
 
2194
+ /**
2195
+ * Remove given keys and their values from storage
2196
+ *
2197
+ * @param {Array} keys
2198
+ * @returns {Promise}
2199
+ */
2200
+ removeItems(keys) {
2201
+ return localforage__WEBPACK_IMPORTED_MODULE_0___default().removeItems(keys);
2202
+ },
2203
+
2190
2204
  /**
2191
2205
  * Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
2192
2206
  * @param {String} key
@@ -3531,6 +3545,17 @@ module.exports = __WEBPACK_EXTERNAL_MODULE_localforage__;
3531
3545
 
3532
3546
  /***/ }),
3533
3547
 
3548
+ /***/ "localforage-removeitems":
3549
+ /*!******************************************!*\
3550
+ !*** external "localforage-removeitems" ***!
3551
+ \******************************************/
3552
+ /***/ ((module) => {
3553
+
3554
+ "use strict";
3555
+ module.exports = __WEBPACK_EXTERNAL_MODULE_localforage_removeitems__;
3556
+
3557
+ /***/ }),
3558
+
3534
3559
  /***/ "lodash/get":
3535
3560
  /*!*****************************!*\
3536
3561
  !*** external "lodash/get" ***!