react-native-onyx 2.0.15 → 2.0.16
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/dist/Onyx.js +7 -8
- package/dist/storage/NativeStorage.d.ts +2 -0
- package/dist/storage/{platforms/index.js → NativeStorage.js} +2 -2
- package/dist/storage/WebStorage.d.ts +3 -0
- package/dist/storage/WebStorage.js +62 -0
- package/dist/storage/__mocks__/index.d.ts +0 -2
- package/dist/storage/__mocks__/index.js +0 -4
- package/dist/storage/index.d.ts +2 -6
- package/dist/storage/index.js +2 -106
- package/dist/storage/index.native.d.ts +2 -0
- package/dist/storage/{platforms/index.native.js → index.native.js} +2 -2
- package/dist/storage/providers/{IDBKeyValProvider.js → IDBKeyVal.js} +16 -19
- package/dist/storage/providers/{SQLiteProvider.js → SQLiteStorage.js} +9 -13
- package/dist/storage/providers/types.d.ts +1 -5
- package/package.json +1 -1
- package/dist/storage/InstanceSync/index.d.ts +0 -13
- package/dist/storage/InstanceSync/index.js +0 -19
- package/dist/storage/InstanceSync/index.web.d.ts +0 -24
- package/dist/storage/InstanceSync/index.web.js +0 -53
- package/dist/storage/platforms/index.d.ts +0 -2
- package/dist/storage/platforms/index.native.d.ts +0 -2
- /package/dist/storage/providers/{IDBKeyValProvider.d.ts → IDBKeyVal.d.ts} +0 -0
- /package/dist/storage/providers/{SQLiteProvider.d.ts → SQLiteStorage.d.ts} +0 -0
package/dist/Onyx.js
CHANGED
|
@@ -1436,14 +1436,6 @@ function setMemoryOnlyKeys(keyList) {
|
|
|
1436
1436
|
* });
|
|
1437
1437
|
*/
|
|
1438
1438
|
function init({ keys = {}, initialKeyStates = {}, safeEvictionKeys = [], maxCachedKeysCount = 1000, shouldSyncMultipleInstances = Boolean(global.localStorage), debugSetState = false } = {}) {
|
|
1439
|
-
storage_1.default.init();
|
|
1440
|
-
if (shouldSyncMultipleInstances) {
|
|
1441
|
-
storage_1.default.keepInstancesSync((key, value) => {
|
|
1442
|
-
const prevValue = OnyxCache_1.default.getValue(key, false);
|
|
1443
|
-
OnyxCache_1.default.set(key, value);
|
|
1444
|
-
keyChanged(key, value, prevValue);
|
|
1445
|
-
});
|
|
1446
|
-
}
|
|
1447
1439
|
if (debugSetState) {
|
|
1448
1440
|
PerformanceUtils.setShouldDebugSetState(true);
|
|
1449
1441
|
}
|
|
@@ -1464,6 +1456,13 @@ function init({ keys = {}, initialKeyStates = {}, safeEvictionKeys = [], maxCach
|
|
|
1464
1456
|
evictionAllowList = safeEvictionKeys;
|
|
1465
1457
|
// Initialize all of our keys with data provided then give green light to any pending connections
|
|
1466
1458
|
Promise.all([addAllSafeEvictionKeysToRecentlyAccessedList(), initializeWithDefaultKeyStates()]).then(deferredInitTask.resolve);
|
|
1459
|
+
if (shouldSyncMultipleInstances && underscore_1.default.isFunction(storage_1.default.keepInstancesSync)) {
|
|
1460
|
+
storage_1.default.keepInstancesSync((key, value) => {
|
|
1461
|
+
const prevValue = OnyxCache_1.default.getValue(key, false);
|
|
1462
|
+
OnyxCache_1.default.set(key, value);
|
|
1463
|
+
keyChanged(key, value, prevValue);
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1467
1466
|
}
|
|
1468
1467
|
const Onyx = {
|
|
1469
1468
|
connect,
|
|
@@ -3,5 +3,5 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
7
|
-
exports.default =
|
|
6
|
+
const SQLiteStorage_1 = __importDefault(require("./providers/SQLiteStorage"));
|
|
7
|
+
exports.default = SQLiteStorage_1.default;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/**
|
|
7
|
+
* This file is here to wrap IDBKeyVal with a layer that provides data-changed events like the ones that exist
|
|
8
|
+
* when using LocalStorage APIs in the browser. These events are great because multiple tabs can listen for when
|
|
9
|
+
* data changes and then stay up-to-date with everything happening in Onyx.
|
|
10
|
+
*/
|
|
11
|
+
const IDBKeyVal_1 = __importDefault(require("./providers/IDBKeyVal"));
|
|
12
|
+
const SYNC_ONYX = 'SYNC_ONYX';
|
|
13
|
+
/**
|
|
14
|
+
* Raise an event thorough `localStorage` to let other tabs know a value changed
|
|
15
|
+
*/
|
|
16
|
+
function raiseStorageSyncEvent(onyxKey) {
|
|
17
|
+
global.localStorage.setItem(SYNC_ONYX, onyxKey);
|
|
18
|
+
global.localStorage.removeItem(SYNC_ONYX);
|
|
19
|
+
}
|
|
20
|
+
function raiseStorageSyncManyKeysEvent(onyxKeys) {
|
|
21
|
+
onyxKeys.forEach((onyxKey) => {
|
|
22
|
+
raiseStorageSyncEvent(onyxKey);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const webStorage = Object.assign(Object.assign({}, IDBKeyVal_1.default), {
|
|
26
|
+
/**
|
|
27
|
+
* @param onStorageKeyChanged Storage synchronization mechanism keeping all opened tabs in sync
|
|
28
|
+
*/
|
|
29
|
+
keepInstancesSync(onStorageKeyChanged) {
|
|
30
|
+
// Override set, remove and clear to raise storage events that we intercept in other tabs
|
|
31
|
+
this.setItem = (key, value) => IDBKeyVal_1.default.setItem(key, value).then(() => raiseStorageSyncEvent(key));
|
|
32
|
+
this.removeItem = (key) => IDBKeyVal_1.default.removeItem(key).then(() => raiseStorageSyncEvent(key));
|
|
33
|
+
this.removeItems = (keys) => IDBKeyVal_1.default.removeItems(keys).then(() => raiseStorageSyncManyKeysEvent(keys));
|
|
34
|
+
this.mergeItem = (key, batchedChanges, modifiedData) => IDBKeyVal_1.default.mergeItem(key, batchedChanges, modifiedData).then(() => raiseStorageSyncEvent(key));
|
|
35
|
+
// If we just call Storage.clear other tabs will have no idea which keys were available previously
|
|
36
|
+
// so that they can call keysChanged for them. That's why we iterate over every key and raise a storage sync
|
|
37
|
+
// event for each one
|
|
38
|
+
this.clear = () => {
|
|
39
|
+
let allKeys;
|
|
40
|
+
// The keys must be retrieved before storage is cleared or else the list of keys would be empty
|
|
41
|
+
return IDBKeyVal_1.default.getAllKeys()
|
|
42
|
+
.then((keys) => {
|
|
43
|
+
allKeys = keys;
|
|
44
|
+
})
|
|
45
|
+
.then(() => IDBKeyVal_1.default.clear())
|
|
46
|
+
.then(() => {
|
|
47
|
+
// Now that storage is cleared, the storage sync event can happen which is a more atomic action
|
|
48
|
+
// for other browser tabs
|
|
49
|
+
allKeys.forEach(raiseStorageSyncEvent);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
// This listener will only be triggered by events coming from other tabs
|
|
53
|
+
global.addEventListener('storage', (event) => {
|
|
54
|
+
// Ignore events that don't originate from the SYNC_ONYX logic
|
|
55
|
+
if (event.key !== SYNC_ONYX || !event.newValue) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const onyxKey = event.newValue;
|
|
59
|
+
IDBKeyVal_1.default.getItem(onyxKey).then((value) => onStorageKeyChanged(onyxKey, value));
|
|
60
|
+
});
|
|
61
|
+
} });
|
|
62
|
+
exports.default = webStorage;
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
import type { KeyValuePairList } from '../providers/types';
|
|
3
3
|
declare const idbKeyvalMockSpy: {
|
|
4
4
|
idbKeyvalSet: jest.Mock<Promise<any>, [key: any, value: any]>;
|
|
5
|
-
init: jest.Mock<void, []>;
|
|
6
5
|
setItem: jest.Mock<Promise<void | import("react-native-quick-sqlite").QueryResult>, [key: string, value: IDBValidKey]>;
|
|
7
6
|
getItem: jest.Mock<Promise<IDBValidKey | null>, [key: string]>;
|
|
8
7
|
removeItem: jest.Mock<Promise<void | import("react-native-quick-sqlite").QueryResult>, [key: string]>;
|
|
@@ -20,6 +19,5 @@ declare const idbKeyvalMockSpy: {
|
|
|
20
19
|
bytesRemaining: number;
|
|
21
20
|
}>, []>;
|
|
22
21
|
setMemoryOnlyKeys: jest.Mock<void, []>;
|
|
23
|
-
keepInstancesSync: jest.Mock<void, [onStorageKeyChanged: import("../providers/types").OnStorageKeyChanged]>;
|
|
24
22
|
};
|
|
25
23
|
export default idbKeyvalMockSpy;
|
|
@@ -10,7 +10,6 @@ const set = jest.fn((key, value) => {
|
|
|
10
10
|
return Promise.resolve(value);
|
|
11
11
|
});
|
|
12
12
|
const idbKeyvalMock = {
|
|
13
|
-
init: () => undefined,
|
|
14
13
|
setItem(key, value) {
|
|
15
14
|
return set(key, value);
|
|
16
15
|
},
|
|
@@ -59,11 +58,9 @@ const idbKeyvalMock = {
|
|
|
59
58
|
},
|
|
60
59
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
61
60
|
setMemoryOnlyKeys() { },
|
|
62
|
-
keepInstancesSync: () => undefined,
|
|
63
61
|
};
|
|
64
62
|
const idbKeyvalMockSpy = {
|
|
65
63
|
idbKeyvalSet: set,
|
|
66
|
-
init: jest.fn(idbKeyvalMock.init),
|
|
67
64
|
setItem: jest.fn(idbKeyvalMock.setItem),
|
|
68
65
|
getItem: jest.fn(idbKeyvalMock.getItem),
|
|
69
66
|
removeItem: jest.fn(idbKeyvalMock.removeItem),
|
|
@@ -80,6 +77,5 @@ const idbKeyvalMockSpy = {
|
|
|
80
77
|
}),
|
|
81
78
|
getDatabaseSize: jest.fn(idbKeyvalMock.getDatabaseSize),
|
|
82
79
|
setMemoryOnlyKeys: jest.fn(idbKeyvalMock.setMemoryOnlyKeys),
|
|
83
|
-
keepInstancesSync: jest.fn(idbKeyvalMock.keepInstancesSync),
|
|
84
80
|
};
|
|
85
81
|
exports.default = idbKeyvalMockSpy;
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
getStorageProvider: () => StorageProvider;
|
|
4
|
-
} & StorageProvider;
|
|
5
|
-
declare const Storage: Storage;
|
|
6
|
-
export default Storage;
|
|
1
|
+
import WebStorage from './WebStorage';
|
|
2
|
+
export default WebStorage;
|
package/dist/storage/index.js
CHANGED
|
@@ -3,109 +3,5 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const provider = platforms_1.default;
|
|
9
|
-
let shouldKeepInstancesSync = false;
|
|
10
|
-
const Storage = {
|
|
11
|
-
/**
|
|
12
|
-
* Returns the storage provider currently in use
|
|
13
|
-
*/
|
|
14
|
-
getStorageProvider() {
|
|
15
|
-
return provider;
|
|
16
|
-
},
|
|
17
|
-
/**
|
|
18
|
-
* Initializes all providers in the list of storage providers
|
|
19
|
-
* and enables fallback providers if necessary
|
|
20
|
-
*/
|
|
21
|
-
init() {
|
|
22
|
-
provider.init();
|
|
23
|
-
},
|
|
24
|
-
/**
|
|
25
|
-
* Get the value of a given key or return `null` if it's not available
|
|
26
|
-
*/
|
|
27
|
-
getItem: (key) => provider.getItem(key),
|
|
28
|
-
/**
|
|
29
|
-
* Get multiple key-value pairs for the give array of keys in a batch
|
|
30
|
-
*/
|
|
31
|
-
multiGet: (keys) => provider.multiGet(keys),
|
|
32
|
-
/**
|
|
33
|
-
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
|
|
34
|
-
*/
|
|
35
|
-
setItem: (key, value) => {
|
|
36
|
-
const promise = provider.setItem(key, value);
|
|
37
|
-
if (shouldKeepInstancesSync) {
|
|
38
|
-
return promise.then(() => InstanceSync_1.default.setItem(key));
|
|
39
|
-
}
|
|
40
|
-
return promise;
|
|
41
|
-
},
|
|
42
|
-
/**
|
|
43
|
-
* Stores multiple key-value pairs in a batch
|
|
44
|
-
*/
|
|
45
|
-
multiSet: (pairs) => provider.multiSet(pairs),
|
|
46
|
-
/**
|
|
47
|
-
* Merging an existing value with a new one
|
|
48
|
-
*/
|
|
49
|
-
mergeItem: (key, changes, modifiedData) => {
|
|
50
|
-
const promise = provider.mergeItem(key, changes, modifiedData);
|
|
51
|
-
if (shouldKeepInstancesSync) {
|
|
52
|
-
return promise.then(() => InstanceSync_1.default.mergeItem(key));
|
|
53
|
-
}
|
|
54
|
-
return promise;
|
|
55
|
-
},
|
|
56
|
-
/**
|
|
57
|
-
* Multiple merging of existing and new values in a batch
|
|
58
|
-
* This function also removes all nested null values from an object.
|
|
59
|
-
*/
|
|
60
|
-
multiMerge: (pairs) => provider.multiMerge(pairs),
|
|
61
|
-
/**
|
|
62
|
-
* Removes given key and its value
|
|
63
|
-
*/
|
|
64
|
-
removeItem: (key) => {
|
|
65
|
-
const promise = provider.removeItem(key);
|
|
66
|
-
if (shouldKeepInstancesSync) {
|
|
67
|
-
return promise.then(() => InstanceSync_1.default.removeItem(key));
|
|
68
|
-
}
|
|
69
|
-
return promise;
|
|
70
|
-
},
|
|
71
|
-
/**
|
|
72
|
-
* Remove given keys and their values
|
|
73
|
-
*/
|
|
74
|
-
removeItems: (keys) => {
|
|
75
|
-
const promise = provider.removeItems(keys);
|
|
76
|
-
if (shouldKeepInstancesSync) {
|
|
77
|
-
return promise.then(() => InstanceSync_1.default.removeItems(keys));
|
|
78
|
-
}
|
|
79
|
-
return promise;
|
|
80
|
-
},
|
|
81
|
-
/**
|
|
82
|
-
* Clears everything
|
|
83
|
-
*/
|
|
84
|
-
clear: () => {
|
|
85
|
-
if (shouldKeepInstancesSync) {
|
|
86
|
-
return InstanceSync_1.default.clear(() => provider.clear());
|
|
87
|
-
}
|
|
88
|
-
return provider.clear();
|
|
89
|
-
},
|
|
90
|
-
// This is a noop for now in order to keep clients from crashing see https://github.com/Expensify/Expensify/issues/312438
|
|
91
|
-
setMemoryOnlyKeys: () => provider.setMemoryOnlyKeys(),
|
|
92
|
-
/**
|
|
93
|
-
* Returns all available keys
|
|
94
|
-
*/
|
|
95
|
-
getAllKeys: () => provider.getAllKeys(),
|
|
96
|
-
/**
|
|
97
|
-
* Gets the total bytes of the store
|
|
98
|
-
*/
|
|
99
|
-
getDatabaseSize: () => provider.getDatabaseSize(),
|
|
100
|
-
/**
|
|
101
|
-
* @param onStorageKeyChanged - Storage synchronization mechanism keeping all opened tabs in sync (web only)
|
|
102
|
-
*/
|
|
103
|
-
keepInstancesSync(onStorageKeyChanged) {
|
|
104
|
-
// If InstanceSync is null, it means we're on a native platform and we don't need to keep instances in sync
|
|
105
|
-
if (InstanceSync_1.default == null)
|
|
106
|
-
return;
|
|
107
|
-
shouldKeepInstancesSync = true;
|
|
108
|
-
InstanceSync_1.default.init(onStorageKeyChanged);
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
exports.default = Storage;
|
|
6
|
+
const WebStorage_1 = __importDefault(require("./WebStorage"));
|
|
7
|
+
exports.default = WebStorage_1.default;
|
|
@@ -3,5 +3,5 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
7
|
-
exports.default =
|
|
6
|
+
const NativeStorage_1 = __importDefault(require("./NativeStorage"));
|
|
7
|
+
exports.default = NativeStorage_1.default;
|
|
@@ -7,20 +7,17 @@ const idb_keyval_1 = require("idb-keyval");
|
|
|
7
7
|
const utils_1 = __importDefault(require("../../utils"));
|
|
8
8
|
// We don't want to initialize the store while the JS bundle loads as idb-keyval will try to use global.indexedDB
|
|
9
9
|
// which might not be available in certain environments that load the bundle (e.g. electron main process).
|
|
10
|
-
let
|
|
10
|
+
let customStoreInstance;
|
|
11
|
+
function getCustomStore() {
|
|
12
|
+
if (!customStoreInstance) {
|
|
13
|
+
customStoreInstance = (0, idb_keyval_1.createStore)('OnyxDB', 'keyvaluepairs');
|
|
14
|
+
}
|
|
15
|
+
return customStoreInstance;
|
|
16
|
+
}
|
|
11
17
|
const provider = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
init() {
|
|
16
|
-
const newIdbKeyValStore = (0, idb_keyval_1.createStore)('OnyxDB', 'keyvaluepairs');
|
|
17
|
-
if (newIdbKeyValStore == null)
|
|
18
|
-
throw Error('IDBKeyVal store could not be created');
|
|
19
|
-
idbKeyValStore = newIdbKeyValStore;
|
|
20
|
-
},
|
|
21
|
-
setItem: (key, value) => (0, idb_keyval_1.set)(key, value, idbKeyValStore),
|
|
22
|
-
multiGet: (keysParam) => (0, idb_keyval_1.getMany)(keysParam, idbKeyValStore).then((values) => values.map((value, index) => [keysParam[index], value])),
|
|
23
|
-
multiMerge: (pairs) => idbKeyValStore('readwrite', (store) => {
|
|
18
|
+
setItem: (key, value) => (0, idb_keyval_1.set)(key, value, getCustomStore()),
|
|
19
|
+
multiGet: (keysParam) => (0, idb_keyval_1.getMany)(keysParam, getCustomStore()).then((values) => values.map((value, index) => [keysParam[index], value])),
|
|
20
|
+
multiMerge: (pairs) => getCustomStore()('readwrite', (store) => {
|
|
24
21
|
// Note: we are using the manual store transaction here, to fit the read and update
|
|
25
22
|
// of the items in one transaction to achieve best performance.
|
|
26
23
|
const getValues = Promise.all(pairs.map(([key]) => (0, idb_keyval_1.promisifyRequest)(store.get(key))));
|
|
@@ -38,16 +35,16 @@ const provider = {
|
|
|
38
35
|
// Since Onyx also merged the existing value with the changes, we can just set the value directly
|
|
39
36
|
return provider.setItem(key, modifiedData);
|
|
40
37
|
},
|
|
41
|
-
multiSet: (pairs) => (0, idb_keyval_1.setMany)(pairs,
|
|
42
|
-
clear: () => (0, idb_keyval_1.clear)(
|
|
38
|
+
multiSet: (pairs) => (0, idb_keyval_1.setMany)(pairs, getCustomStore()),
|
|
39
|
+
clear: () => (0, idb_keyval_1.clear)(getCustomStore()),
|
|
43
40
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
44
41
|
setMemoryOnlyKeys: () => { },
|
|
45
|
-
getAllKeys: () => (0, idb_keyval_1.keys)(
|
|
46
|
-
getItem: (key) => (0, idb_keyval_1.get)(key,
|
|
42
|
+
getAllKeys: () => (0, idb_keyval_1.keys)(getCustomStore()),
|
|
43
|
+
getItem: (key) => (0, idb_keyval_1.get)(key, getCustomStore())
|
|
47
44
|
// idb-keyval returns undefined for missing items, but this needs to return null so that idb-keyval does the same thing as SQLiteStorage.
|
|
48
45
|
.then((val) => (val === undefined ? null : val)),
|
|
49
|
-
removeItem: (key) => (0, idb_keyval_1.del)(key,
|
|
50
|
-
removeItems: (keysParam) => (0, idb_keyval_1.delMany)(keysParam,
|
|
46
|
+
removeItem: (key) => (0, idb_keyval_1.del)(key, getCustomStore()),
|
|
47
|
+
removeItems: (keysParam) => (0, idb_keyval_1.delMany)(keysParam, getCustomStore()),
|
|
51
48
|
getDatabaseSize() {
|
|
52
49
|
if (!window.navigator || !window.navigator.storage) {
|
|
53
50
|
throw new Error('StorageManager browser API unavailable');
|
|
@@ -7,20 +7,14 @@ const react_native_quick_sqlite_1 = require("react-native-quick-sqlite");
|
|
|
7
7
|
const react_native_device_info_1 = require("react-native-device-info");
|
|
8
8
|
const utils_1 = __importDefault(require("../../utils"));
|
|
9
9
|
const DB_NAME = 'OnyxDB';
|
|
10
|
-
|
|
10
|
+
const db = (0, react_native_quick_sqlite_1.open)({ name: DB_NAME });
|
|
11
|
+
db.execute('CREATE TABLE IF NOT EXISTS keyvaluepairs (record_key TEXT NOT NULL PRIMARY KEY , valueJSON JSON NOT NULL) WITHOUT ROWID;');
|
|
12
|
+
// All of the 3 pragmas below were suggested by SQLite team.
|
|
13
|
+
// You can find more info about them here: https://www.sqlite.org/pragma.html
|
|
14
|
+
db.execute('PRAGMA CACHE_SIZE=-20000;');
|
|
15
|
+
db.execute('PRAGMA synchronous=NORMAL;');
|
|
16
|
+
db.execute('PRAGMA journal_mode=WAL;');
|
|
11
17
|
const provider = {
|
|
12
|
-
/**
|
|
13
|
-
* Initializes the storage provider
|
|
14
|
-
*/
|
|
15
|
-
init() {
|
|
16
|
-
db = (0, react_native_quick_sqlite_1.open)({ name: DB_NAME });
|
|
17
|
-
db.execute('CREATE TABLE IF NOT EXISTS keyvaluepairs (record_key TEXT NOT NULL PRIMARY KEY , valueJSON JSON NOT NULL) WITHOUT ROWID;');
|
|
18
|
-
// All of the 3 pragmas below were suggested by SQLite team.
|
|
19
|
-
// You can find more info about them here: https://www.sqlite.org/pragma.html
|
|
20
|
-
db.execute('PRAGMA CACHE_SIZE=-20000;');
|
|
21
|
-
db.execute('PRAGMA synchronous=NORMAL;');
|
|
22
|
-
db.execute('PRAGMA journal_mode=WAL;');
|
|
23
|
-
},
|
|
24
18
|
getItem(key) {
|
|
25
19
|
return db.executeAsync('SELECT record_key, valueJSON FROM keyvaluepairs WHERE record_key = ?;', [key]).then(({ rows }) => {
|
|
26
20
|
if (!rows || (rows === null || rows === void 0 ? void 0 : rows.length) === 0) {
|
|
@@ -92,5 +86,7 @@ const provider = {
|
|
|
92
86
|
},
|
|
93
87
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
94
88
|
setMemoryOnlyKeys: () => { },
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
90
|
+
keepInstancesSync: () => { },
|
|
95
91
|
};
|
|
96
92
|
exports.default = provider;
|
|
@@ -6,10 +6,6 @@ type KeyList = Key[];
|
|
|
6
6
|
type KeyValuePairList = KeyValuePair[];
|
|
7
7
|
type OnStorageKeyChanged = (key: Key, value: Value | null) => void;
|
|
8
8
|
type StorageProvider = {
|
|
9
|
-
/**
|
|
10
|
-
* Initializes the storage provider
|
|
11
|
-
*/
|
|
12
|
-
init: () => void;
|
|
13
9
|
/**
|
|
14
10
|
* Gets the value of a given key or return `null` if it's not available in storage
|
|
15
11
|
*/
|
|
@@ -69,4 +65,4 @@ type StorageProvider = {
|
|
|
69
65
|
keepInstancesSync?: (onStorageKeyChanged: OnStorageKeyChanged) => void;
|
|
70
66
|
};
|
|
71
67
|
export default StorageProvider;
|
|
72
|
-
export type { Value, Key, KeyList, KeyValuePairList
|
|
68
|
+
export type { Value, Key, KeyList, KeyValuePairList };
|
package/package.json
CHANGED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This is used to keep multiple browser tabs in sync, therefore only needed on web
|
|
3
|
-
* On native platforms, we omit this syncing logic by setting this to mock implementation.
|
|
4
|
-
*/
|
|
5
|
-
declare const InstanceSync: {
|
|
6
|
-
init: (...args: any[]) => void;
|
|
7
|
-
setItem: (...args: any[]) => void;
|
|
8
|
-
removeItem: (...args: any[]) => void;
|
|
9
|
-
removeItems: (...args: any[]) => void;
|
|
10
|
-
mergeItem: (...args: any[]) => void;
|
|
11
|
-
clear: <T extends () => void>(callback: T) => Promise<void>;
|
|
12
|
-
};
|
|
13
|
-
export default InstanceSync;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const noop_1 = __importDefault(require("lodash/noop"));
|
|
7
|
-
/**
|
|
8
|
-
* This is used to keep multiple browser tabs in sync, therefore only needed on web
|
|
9
|
-
* On native platforms, we omit this syncing logic by setting this to mock implementation.
|
|
10
|
-
*/
|
|
11
|
-
const InstanceSync = {
|
|
12
|
-
init: noop_1.default,
|
|
13
|
-
setItem: noop_1.default,
|
|
14
|
-
removeItem: noop_1.default,
|
|
15
|
-
removeItems: noop_1.default,
|
|
16
|
-
mergeItem: noop_1.default,
|
|
17
|
-
clear: (callback) => Promise.resolve(callback()),
|
|
18
|
-
};
|
|
19
|
-
exports.default = InstanceSync;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The InstancesSync object provides data-changed events like the ones that exist
|
|
3
|
-
* when using LocalStorage APIs in the browser. These events are great because multiple tabs can listen for when
|
|
4
|
-
* data changes and then stay up-to-date with everything happening in Onyx.
|
|
5
|
-
*/
|
|
6
|
-
import type { KeyList, Key, OnStorageKeyChanged } from '../providers/types';
|
|
7
|
-
/**
|
|
8
|
-
* Raise an event through `localStorage` to let other tabs know a value changed
|
|
9
|
-
* @param {String} onyxKey
|
|
10
|
-
*/
|
|
11
|
-
declare function raiseStorageSyncEvent(onyxKey: Key): void;
|
|
12
|
-
declare function raiseStorageSyncManyKeysEvent(onyxKeys: KeyList): void;
|
|
13
|
-
declare const InstanceSync: {
|
|
14
|
-
/**
|
|
15
|
-
* @param {Function} onStorageKeyChanged Storage synchronization mechanism keeping all opened tabs in sync
|
|
16
|
-
*/
|
|
17
|
-
init: (onStorageKeyChanged: OnStorageKeyChanged) => void;
|
|
18
|
-
setItem: typeof raiseStorageSyncEvent;
|
|
19
|
-
removeItem: typeof raiseStorageSyncEvent;
|
|
20
|
-
removeItems: typeof raiseStorageSyncManyKeysEvent;
|
|
21
|
-
mergeItem: typeof raiseStorageSyncEvent;
|
|
22
|
-
clear: (clearImplementation: () => void) => any;
|
|
23
|
-
};
|
|
24
|
-
export default InstanceSync;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const SYNC_ONYX = 'SYNC_ONYX';
|
|
4
|
-
/**
|
|
5
|
-
* Raise an event through `localStorage` to let other tabs know a value changed
|
|
6
|
-
* @param {String} onyxKey
|
|
7
|
-
*/
|
|
8
|
-
function raiseStorageSyncEvent(onyxKey) {
|
|
9
|
-
global.localStorage.setItem(SYNC_ONYX, onyxKey);
|
|
10
|
-
global.localStorage.removeItem(SYNC_ONYX);
|
|
11
|
-
}
|
|
12
|
-
function raiseStorageSyncManyKeysEvent(onyxKeys) {
|
|
13
|
-
onyxKeys.forEach((onyxKey) => {
|
|
14
|
-
raiseStorageSyncEvent(onyxKey);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
const InstanceSync = {
|
|
18
|
-
/**
|
|
19
|
-
* @param {Function} onStorageKeyChanged Storage synchronization mechanism keeping all opened tabs in sync
|
|
20
|
-
*/
|
|
21
|
-
init: (onStorageKeyChanged) => {
|
|
22
|
-
// This listener will only be triggered by events coming from other tabs
|
|
23
|
-
global.addEventListener('storage', (event) => {
|
|
24
|
-
// Ignore events that don't originate from the SYNC_ONYX logic
|
|
25
|
-
if (event.key !== SYNC_ONYX || !event.newValue) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
const onyxKey = event.newValue;
|
|
29
|
-
// @ts-expect-error `this` will be substituted later in actual function call
|
|
30
|
-
this.getItem(onyxKey).then((value) => onStorageKeyChanged(onyxKey, value));
|
|
31
|
-
});
|
|
32
|
-
},
|
|
33
|
-
setItem: raiseStorageSyncEvent,
|
|
34
|
-
removeItem: raiseStorageSyncEvent,
|
|
35
|
-
removeItems: raiseStorageSyncManyKeysEvent,
|
|
36
|
-
mergeItem: raiseStorageSyncEvent,
|
|
37
|
-
clear: (clearImplementation) => {
|
|
38
|
-
let allKeys;
|
|
39
|
-
// The keys must be retrieved before storage is cleared or else the list of keys would be empty
|
|
40
|
-
// @ts-expect-error `this` will be substituted later in actual function call
|
|
41
|
-
return this.getAllKeys()
|
|
42
|
-
.then((keys) => {
|
|
43
|
-
allKeys = keys;
|
|
44
|
-
})
|
|
45
|
-
.then(() => clearImplementation())
|
|
46
|
-
.then(() => {
|
|
47
|
-
// Now that storage is cleared, the storage sync event can happen which is a more atomic action
|
|
48
|
-
// for other browser tabs
|
|
49
|
-
raiseStorageSyncManyKeysEvent(allKeys);
|
|
50
|
-
});
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
exports.default = InstanceSync;
|
|
File without changes
|
|
File without changes
|