react-native-onyx 1.0.63 → 1.0.64

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.
@@ -926,6 +926,20 @@ function remove(key) {
926
926
  return _storage__WEBPACK_IMPORTED_MODULE_4__["default"].removeItem(key);
927
927
  }
928
928
 
929
+ /**
930
+ * @private
931
+ * @returns {Promise<void>}
932
+ */
933
+ function reportStorageQuota() {
934
+ return _storage__WEBPACK_IMPORTED_MODULE_4__["default"].getDatabaseSize().
935
+ then((_ref) => {let { bytesUsed, bytesRemaining } = _ref;
936
+ _Logger__WEBPACK_IMPORTED_MODULE_5__.logInfo(`Storage Quota Check -- bytesUsed: ${bytesUsed} bytesRemaining: ${bytesRemaining}`);
937
+ }).
938
+ catch((dbSizeError) => {
939
+ _Logger__WEBPACK_IMPORTED_MODULE_5__.logAlert(`Unable to get database size. Error: ${dbSizeError}`);
940
+ });
941
+ }
942
+
929
943
  /**
930
944
  * If we fail to set or merge we must handle this by
931
945
  * evicting some data from Onyx and then retrying to do
@@ -938,7 +952,7 @@ function remove(key) {
938
952
  * @return {Promise}
939
953
  */
940
954
  function evictStorageAndRetry(error, onyxMethod) {for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {args[_key - 2] = arguments[_key];}
941
- _Logger__WEBPACK_IMPORTED_MODULE_5__.logInfo(`Handled error: ${error}`);
955
+ _Logger__WEBPACK_IMPORTED_MODULE_5__.logInfo(`Failed to save to storage. Error: ${error}. onyxMethod: ${onyxMethod.name}`);
942
956
 
943
957
  if (error && _Str__WEBPACK_IMPORTED_MODULE_6__.startsWith(error.message, 'Failed to execute \'put\' on \'IDBObjectStore\'')) {
944
958
  _Logger__WEBPACK_IMPORTED_MODULE_5__.logAlert('Attempted to set invalid data set in Onyx. Please ensure all data is serializable.');
@@ -947,14 +961,17 @@ function evictStorageAndRetry(error, onyxMethod) {for (var _len = arguments.leng
947
961
 
948
962
  // Find the first key that we can remove that has no subscribers in our blocklist
949
963
  const keyForRemoval = underscore__WEBPACK_IMPORTED_MODULE_1___default().find(recentlyAccessedKeys, (key) => !evictionBlocklist[key]);
950
-
951
964
  if (!keyForRemoval) {
965
+ // If we have no acceptable keys to remove then we are possibly trying to save mission critical data. If this is the case,
966
+ // then we should stop retrying as there is not much the user can do to fix this. Instead of getting them stuck in an infinite loop we
967
+ // will allow this write to be skipped.
952
968
  _Logger__WEBPACK_IMPORTED_MODULE_5__.logAlert('Out of storage. But found no acceptable keys to remove.');
953
- throw error;
969
+ return reportStorageQuota();
954
970
  }
955
971
 
956
972
  // Remove the least recently viewed key that is not currently being accessed and retry.
957
973
  _Logger__WEBPACK_IMPORTED_MODULE_5__.logInfo(`Out of storage. Evicting least recently accessed key (${keyForRemoval}) and retrying.`);
974
+ reportStorageQuota();
958
975
  return remove(keyForRemoval).
959
976
  then(() => onyxMethod(...args));
960
977
  }
@@ -1355,7 +1372,7 @@ function mergeCollection(collectionKey, collection) {
1355
1372
  */
1356
1373
  function update(data) {
1357
1374
  // First, validate the Onyx object is in the format we expect
1358
- underscore__WEBPACK_IMPORTED_MODULE_1___default().each(data, (_ref) => {let { onyxMethod, key } = _ref;
1375
+ underscore__WEBPACK_IMPORTED_MODULE_1___default().each(data, (_ref2) => {let { onyxMethod, key } = _ref2;
1359
1376
  if (!underscore__WEBPACK_IMPORTED_MODULE_1___default().contains([METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION], onyxMethod)) {
1360
1377
  throw new Error(`Invalid onyxMethod ${onyxMethod} in Onyx update.`);
1361
1378
  }
@@ -1367,7 +1384,7 @@ function update(data) {
1367
1384
  const promises = [];
1368
1385
  let clearPromise = Promise.resolve();
1369
1386
 
1370
- underscore__WEBPACK_IMPORTED_MODULE_1___default().each(data, (_ref2) => {let { onyxMethod, key, value } = _ref2;
1387
+ underscore__WEBPACK_IMPORTED_MODULE_1___default().each(data, (_ref3) => {let { onyxMethod, key, value } = _ref3;
1371
1388
  switch (onyxMethod) {
1372
1389
  case METHOD.SET:
1373
1390
  promises.push(() => set(key, value));
@@ -2263,7 +2280,26 @@ const provider = {
2263
2280
  * @param {Array} keysParam
2264
2281
  * @returns {Promise}
2265
2282
  */
2266
- removeItems: (keysParam) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.delMany)(keysParam, getCustomStore())
2283
+ removeItems: (keysParam) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.delMany)(keysParam, getCustomStore()),
2284
+
2285
+ /**
2286
+ * Gets the total bytes of the database file
2287
+ * @returns {Promise<number>}
2288
+ */
2289
+ getDatabaseSize() {
2290
+ if (!window.navigator || !window.navigator.storage) {
2291
+ throw new Error('StorageManager browser API unavailable');
2292
+ }
2293
+
2294
+ return window.navigator.storage.estimate().
2295
+ then((value) => ({
2296
+ bytesUsed: value.usage,
2297
+ bytesRemaining: value.quota - value.usage
2298
+ })).
2299
+ catch((error) => {
2300
+ throw new Error(`Unable to estimate web storage quota. Original error: ${error}`);
2301
+ });
2302
+ }
2267
2303
  };
2268
2304
 
2269
2305
  /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (provider);