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.
package/lib/Onyx.js CHANGED
@@ -844,6 +844,20 @@ function remove(key) {
844
844
  return Storage.removeItem(key);
845
845
  }
846
846
 
847
+ /**
848
+ * @private
849
+ * @returns {Promise<void>}
850
+ */
851
+ function reportStorageQuota() {
852
+ return Storage.getDatabaseSize()
853
+ .then(({bytesUsed, bytesRemaining}) => {
854
+ Logger.logInfo(`Storage Quota Check -- bytesUsed: ${bytesUsed} bytesRemaining: ${bytesRemaining}`);
855
+ })
856
+ .catch((dbSizeError) => {
857
+ Logger.logAlert(`Unable to get database size. Error: ${dbSizeError}`);
858
+ });
859
+ }
860
+
847
861
  /**
848
862
  * If we fail to set or merge we must handle this by
849
863
  * evicting some data from Onyx and then retrying to do
@@ -856,7 +870,7 @@ function remove(key) {
856
870
  * @return {Promise}
857
871
  */
858
872
  function evictStorageAndRetry(error, onyxMethod, ...args) {
859
- Logger.logInfo(`Handled error: ${error}`);
873
+ Logger.logInfo(`Failed to save to storage. Error: ${error}. onyxMethod: ${onyxMethod.name}`);
860
874
 
861
875
  if (error && Str.startsWith(error.message, 'Failed to execute \'put\' on \'IDBObjectStore\'')) {
862
876
  Logger.logAlert('Attempted to set invalid data set in Onyx. Please ensure all data is serializable.');
@@ -865,14 +879,17 @@ function evictStorageAndRetry(error, onyxMethod, ...args) {
865
879
 
866
880
  // Find the first key that we can remove that has no subscribers in our blocklist
867
881
  const keyForRemoval = _.find(recentlyAccessedKeys, key => !evictionBlocklist[key]);
868
-
869
882
  if (!keyForRemoval) {
883
+ // If we have no acceptable keys to remove then we are possibly trying to save mission critical data. If this is the case,
884
+ // 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
885
+ // will allow this write to be skipped.
870
886
  Logger.logAlert('Out of storage. But found no acceptable keys to remove.');
871
- throw error;
887
+ return reportStorageQuota();
872
888
  }
873
889
 
874
890
  // Remove the least recently viewed key that is not currently being accessed and retry.
875
891
  Logger.logInfo(`Out of storage. Evicting least recently accessed key (${keyForRemoval}) and retrying.`);
892
+ reportStorageQuota();
876
893
  return remove(keyForRemoval)
877
894
  .then(() => onyxMethod(...args));
878
895
  }
@@ -55,6 +55,9 @@ const idbKeyvalMock = {
55
55
  return Promise.resolve(_.keys(storageMapInternal));
56
56
  },
57
57
  config() {},
58
+ getDatabaseSize() {
59
+ return Promise.resolve({bytesRemaining: 0, bytesUsed: 99999});
60
+ },
58
61
  };
59
62
 
60
63
  const idbKeyvalMockSpy = {
@@ -74,6 +77,7 @@ const idbKeyvalMockSpy = {
74
77
  setInitialMockData: jest.fn((data) => {
75
78
  storageMapInternal = data;
76
79
  }),
80
+ getDatabaseSize: jest.fn(idbKeyvalMock.getDatabaseSize),
77
81
  };
78
82
 
79
83
  export default idbKeyvalMockSpy;
@@ -113,6 +113,25 @@ const provider = {
113
113
  * @returns {Promise}
114
114
  */
115
115
  removeItems: keysParam => delMany(keysParam, getCustomStore()),
116
+
117
+ /**
118
+ * Gets the total bytes of the database file
119
+ * @returns {Promise<number>}
120
+ */
121
+ getDatabaseSize() {
122
+ if (!window.navigator || !window.navigator.storage) {
123
+ throw new Error('StorageManager browser API unavailable');
124
+ }
125
+
126
+ return window.navigator.storage.estimate()
127
+ .then(value => ({
128
+ bytesUsed: value.usage,
129
+ bytesRemaining: value.quota - value.usage,
130
+ }))
131
+ .catch((error) => {
132
+ throw new Error(`Unable to estimate web storage quota. Original error: ${error}`);
133
+ });
134
+ },
116
135
  };
117
136
 
118
137
  export default provider;
@@ -3,6 +3,7 @@
3
3
  * converting the value to a JSON string
4
4
  */
5
5
  import {open} from 'react-native-quick-sqlite';
6
+ import {getFreeDiskStorage} from 'react-native-device-info';
6
7
  import _ from 'underscore';
7
8
 
8
9
  const DB_NAME = 'OnyxDB';
@@ -143,6 +144,22 @@ const provider = {
143
144
  * Noop on mobile for now.
144
145
  */
145
146
  setMemoryOnlyKeys: () => {},
147
+
148
+ /**
149
+ * Gets the total bytes of the database file
150
+ * @returns {Promise}
151
+ */
152
+ getDatabaseSize() {
153
+ return Promise.all([db.executeAsync('PRAGMA page_size;'), db.executeAsync('PRAGMA page_count;'), getFreeDiskStorage()])
154
+ .then(([pageSizeResult, pageCountResult, bytesRemaining]) => {
155
+ const pageSize = pageSizeResult.rows.item(0).page_size;
156
+ const pageCount = pageCountResult.rows.item(0).page_count;
157
+ return {
158
+ bytesUsed: pageSize * pageCount,
159
+ bytesRemaining,
160
+ };
161
+ });
162
+ },
146
163
  };
147
164
 
148
165
  export default provider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.63",
3
+ "version": "1.0.64",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",
@@ -67,6 +67,7 @@
67
67
  "prop-types": "^15.7.2",
68
68
  "react": "18.2.0",
69
69
  "react-native": "0.71.2",
70
+ "react-native-device-info": "^10.3.0",
70
71
  "react-native-performance": "^2.0.0",
71
72
  "react-native-quick-sqlite": "^8.0.0-beta.2",
72
73
  "react-test-renderer": "18.1.0",
@@ -79,7 +80,8 @@
79
80
  "idb-keyval": "^6.2.1",
80
81
  "react": ">=18.1.0",
81
82
  "react-native-performance": "^4.0.0",
82
- "react-native-quick-sqlite": "^8.0.0-beta.2"
83
+ "react-native-quick-sqlite": "^8.0.0-beta.2",
84
+ "react-native-device-info": "^10.3.0"
83
85
  },
84
86
  "peerDependenciesMeta": {
85
87
  "idb-keyval": {
@@ -90,6 +92,9 @@
90
92
  },
91
93
  "react-native-quick-sqlite": {
92
94
  "optional": true
95
+ },
96
+ "react-native-device-info": {
97
+ "optional": true
93
98
  }
94
99
  },
95
100
  "engines": {