react-native-onyx 3.0.1 → 3.0.2

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.
@@ -0,0 +1,3 @@
1
+ import type { UseStore } from 'idb-keyval';
2
+ declare function createStore(dbName: string, storeName: string): UseStore;
3
+ export default createStore;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const idb_keyval_1 = require("idb-keyval");
4
+ const Logger_1 = require("../../../Logger");
5
+ // This is a copy of the createStore function from idb-keyval, we need a custom implementation
6
+ // because we need to create the database manually in order to ensure that the store exists before we use it.
7
+ // If the store does not exist, idb-keyval will throw an error
8
+ // source: https://github.com/jakearchibald/idb-keyval/blob/9d19315b4a83897df1e0193dccdc29f78466a0f3/src/index.ts#L12
9
+ function createStore(dbName, storeName) {
10
+ let dbp;
11
+ const getDB = () => {
12
+ if (dbp)
13
+ return dbp;
14
+ const request = indexedDB.open(dbName);
15
+ request.onupgradeneeded = () => request.result.createObjectStore(storeName);
16
+ dbp = (0, idb_keyval_1.promisifyRequest)(request);
17
+ dbp.then((db) => {
18
+ // It seems like Safari sometimes likes to just close the connection.
19
+ // It's supposed to fire this event when that happens. Let's hope it does!
20
+ // eslint-disable-next-line no-param-reassign
21
+ db.onclose = () => (dbp = undefined);
22
+ },
23
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
24
+ () => { });
25
+ return dbp;
26
+ };
27
+ // Ensures the store exists in the DB. If missing, bumps the version to trigger
28
+ // onupgradeneeded, recreates the store, and returns a promise to the new DB.
29
+ const verifyStoreExists = (db) => {
30
+ if (db.objectStoreNames.contains(storeName)) {
31
+ return db;
32
+ }
33
+ (0, Logger_1.logInfo)(`Store ${storeName} does not exist in database ${dbName}.`);
34
+ const nextVersion = db.version + 1;
35
+ db.close();
36
+ const request = indexedDB.open(dbName, nextVersion);
37
+ request.onupgradeneeded = () => {
38
+ const updatedDatabase = request.result;
39
+ if (updatedDatabase.objectStoreNames.contains(storeName)) {
40
+ return;
41
+ }
42
+ (0, Logger_1.logInfo)(`Creating store ${storeName} in database ${dbName}.`);
43
+ updatedDatabase.createObjectStore(storeName);
44
+ };
45
+ dbp = (0, idb_keyval_1.promisifyRequest)(request);
46
+ return dbp;
47
+ };
48
+ return (txMode, callback) => getDB()
49
+ .then(verifyStoreExists)
50
+ .then((db) => callback(db.transaction(storeName, txMode).objectStore(storeName)));
51
+ }
52
+ exports.default = createStore;
@@ -1,3 +1,3 @@
1
- import type StorageProvider from './types';
1
+ import type StorageProvider from '../types';
2
2
  declare const provider: StorageProvider;
3
3
  export default provider;
@@ -4,10 +4,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const idb_keyval_1 = require("idb-keyval");
7
- const utils_1 = __importDefault(require("../../utils"));
7
+ const utils_1 = __importDefault(require("../../../utils"));
8
+ const createStore_1 = __importDefault(require("./createStore"));
8
9
  // We don't want to initialize the store while the JS bundle loads as idb-keyval will try to use global.indexedDB
9
10
  // which might not be available in certain environments that load the bundle (e.g. electron main process).
10
11
  let idbKeyValStore;
12
+ const DB_NAME = 'OnyxDB';
13
+ const STORE_NAME = 'keyvaluepairs';
11
14
  const provider = {
12
15
  /**
13
16
  * The name of the provider that can be printed to the logs
@@ -17,9 +20,10 @@ const provider = {
17
20
  * Initializes the storage provider
18
21
  */
19
22
  init() {
20
- const newIdbKeyValStore = (0, idb_keyval_1.createStore)('OnyxDB', 'keyvaluepairs');
21
- if (newIdbKeyValStore == null)
23
+ const newIdbKeyValStore = (0, createStore_1.default)(DB_NAME, STORE_NAME);
24
+ if (newIdbKeyValStore == null) {
22
25
  throw Error('IDBKeyVal store could not be created');
26
+ }
23
27
  idbKeyValStore = newIdbKeyValStore;
24
28
  },
25
29
  setItem: (key, value) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",