Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix idb keyval provider usage in electron environment #307

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions lib/storage/providers/IDBKeyVal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ import {
import _ from 'underscore';
import fastMerge from '../../fastMerge';

const customStore = createStore('OnyxDB', 'keyvaluepairs');
// We don't want to initialize the store while the JS bundle loads as idb-keyval will try to use global.indexedDB
// which might not be available in certain environments that load the bundle (e.g. electron main process).
let customStoreInstance;
const getCustomStore = () => {
if (!customStoreInstance) {
customStoreInstance = createStore('OnyxDB', 'keyvaluepairs');
}
return customStoreInstance;
};

const provider = {
/**
Expand All @@ -22,23 +30,23 @@ const provider = {
* @param {*} value
* @return {Promise<void>}
*/
setItem: (key, value) => set(key, value, customStore),
setItem: (key, value) => set(key, value, getCustomStore()),

/**
* Get multiple key-value pairs for the give array of keys in a batch.
* This is optimized to use only one database transaction.
* @param {String[]} keysParam
* @return {Promise<Array<[key, value]>>}
*/
multiGet: keysParam => getMany(keysParam, customStore)
multiGet: keysParam => getMany(keysParam, getCustomStore())
.then(values => _.map(values, (value, index) => [keysParam[index], value])),

/**
* Multiple merging of existing and new values in a batch
* @param {Array<[key, value]>} pairs
* @return {Promise<void>}
*/
multiMerge: pairs => customStore('readwrite', (store) => {
multiMerge: pairs => getCustomStore()('readwrite', (store) => {
// Note: we are using the manual store transaction here, to fit the read and update
// of the items in one transaction to achieve best performance.

Expand Down Expand Up @@ -70,41 +78,41 @@ const provider = {
* @param {Array<[key, value]>} pairs
* @return {Promise<void>}
*/
multiSet: pairs => setMany(pairs, customStore),
multiSet: pairs => setMany(pairs, getCustomStore()),

/**
* Clear everything from storage and also stops the SyncQueue from adding anything more to storage
* @returns {Promise<void>}
*/
clear: () => clear(customStore),
clear: () => clear(getCustomStore()),

/**
* Returns all keys available in storage
* @returns {Promise<String[]>}
*/
getAllKeys: () => keys(customStore),
getAllKeys: () => keys(getCustomStore()),

/**
* Get the value of a given key or return `null` if it's not available in storage
* @param {String} key
* @return {Promise<*>}
*/
getItem: key => get(key, customStore),
getItem: key => get(key, getCustomStore()),

/**
* Remove given key and it's value from storage
* @param {String} key
* @returns {Promise<void>}
*/
removeItem: key => del(key, customStore),
removeItem: key => del(key, getCustomStore()),

/**
* Remove given keys and their values from storage
*
* @param {Array} keysParam
* @returns {Promise}
*/
removeItems: keysParam => delMany(keysParam, customStore),
removeItems: keysParam => delMany(keysParam, getCustomStore()),
};

export default provider;