react-native-onyx 1.0.62 → 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));
@@ -2164,7 +2181,15 @@ __webpack_require__.r(__webpack_exports__);
2164
2181
 
2165
2182
 
2166
2183
 
2167
- const customStore = (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.createStore)('OnyxDB', 'keyvaluepairs');
2184
+ // We don't want to initialize the store while the JS bundle loads as idb-keyval will try to use global.indexedDB
2185
+ // which might not be available in certain environments that load the bundle (e.g. electron main process).
2186
+ let customStoreInstance;
2187
+ const getCustomStore = () => {
2188
+ if (!customStoreInstance) {
2189
+ customStoreInstance = (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.createStore)('OnyxDB', 'keyvaluepairs');
2190
+ }
2191
+ return customStoreInstance;
2192
+ };
2168
2193
 
2169
2194
  const provider = {
2170
2195
  /**
@@ -2173,7 +2198,7 @@ const provider = {
2173
2198
  * @param {*} value
2174
2199
  * @return {Promise<void>}
2175
2200
  */
2176
- setItem: (key, value) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.set)(key, value, customStore),
2201
+ setItem: (key, value) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.set)(key, value, getCustomStore()),
2177
2202
 
2178
2203
  /**
2179
2204
  * Get multiple key-value pairs for the give array of keys in a batch.
@@ -2181,7 +2206,7 @@ const provider = {
2181
2206
  * @param {String[]} keysParam
2182
2207
  * @return {Promise<Array<[key, value]>>}
2183
2208
  */
2184
- multiGet: (keysParam) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.getMany)(keysParam, customStore).
2209
+ multiGet: (keysParam) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.getMany)(keysParam, getCustomStore()).
2185
2210
  then((values) => underscore__WEBPACK_IMPORTED_MODULE_1___default().map(values, (value, index) => [keysParam[index], value])),
2186
2211
 
2187
2212
  /**
@@ -2189,7 +2214,7 @@ const provider = {
2189
2214
  * @param {Array<[key, value]>} pairs
2190
2215
  * @return {Promise<void>}
2191
2216
  */
2192
- multiMerge: (pairs) => customStore('readwrite', (store) => {
2217
+ multiMerge: (pairs) => getCustomStore()('readwrite', (store) => {
2193
2218
  // Note: we are using the manual store transaction here, to fit the read and update
2194
2219
  // of the items in one transaction to achieve best performance.
2195
2220
 
@@ -2221,33 +2246,33 @@ const provider = {
2221
2246
  * @param {Array<[key, value]>} pairs
2222
2247
  * @return {Promise<void>}
2223
2248
  */
2224
- multiSet: (pairs) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.setMany)(pairs, customStore),
2249
+ multiSet: (pairs) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.setMany)(pairs, getCustomStore()),
2225
2250
 
2226
2251
  /**
2227
2252
  * Clear everything from storage and also stops the SyncQueue from adding anything more to storage
2228
2253
  * @returns {Promise<void>}
2229
2254
  */
2230
- clear: () => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.clear)(customStore),
2255
+ clear: () => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.clear)(getCustomStore()),
2231
2256
 
2232
2257
  /**
2233
2258
  * Returns all keys available in storage
2234
2259
  * @returns {Promise<String[]>}
2235
2260
  */
2236
- getAllKeys: () => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.keys)(customStore),
2261
+ getAllKeys: () => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.keys)(getCustomStore()),
2237
2262
 
2238
2263
  /**
2239
2264
  * Get the value of a given key or return `null` if it's not available in storage
2240
2265
  * @param {String} key
2241
2266
  * @return {Promise<*>}
2242
2267
  */
2243
- getItem: (key) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.get)(key, customStore),
2268
+ getItem: (key) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.get)(key, getCustomStore()),
2244
2269
 
2245
2270
  /**
2246
2271
  * Remove given key and it's value from storage
2247
2272
  * @param {String} key
2248
2273
  * @returns {Promise<void>}
2249
2274
  */
2250
- removeItem: (key) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.del)(key, customStore),
2275
+ removeItem: (key) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.del)(key, getCustomStore()),
2251
2276
 
2252
2277
  /**
2253
2278
  * Remove given keys and their values from storage
@@ -2255,7 +2280,26 @@ const provider = {
2255
2280
  * @param {Array} keysParam
2256
2281
  * @returns {Promise}
2257
2282
  */
2258
- removeItems: (keysParam) => (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.delMany)(keysParam, customStore)
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
+ }
2259
2303
  };
2260
2304
 
2261
2305
  /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (provider);