@@ -13,7 +13,15 @@ import {
1313import _ from 'underscore' ;
1414import fastMerge from '../../fastMerge' ;
1515
16- const customStore = createStore ( 'OnyxDB' , 'keyvaluepairs' ) ;
16+ // We don't want to initialize the store while the JS bundle loads as idb-keyval will try to use global.indexedDB
17+ // which might not be available in certain environments that load the bundle (e.g. electron main process).
18+ let customStoreInstance ;
19+ const getCustomStore = ( ) => {
20+ if ( ! customStoreInstance ) {
21+ customStoreInstance = createStore ( 'OnyxDB' , 'keyvaluepairs' ) ;
22+ }
23+ return customStoreInstance ;
24+ } ;
1725
1826const provider = {
1927 /**
@@ -22,23 +30,23 @@ const provider = {
2230 * @param {* } value
2331 * @return {Promise<void> }
2432 */
25- setItem : ( key , value ) => set ( key , value , customStore ) ,
33+ setItem : ( key , value ) => set ( key , value , getCustomStore ( ) ) ,
2634
2735 /**
2836 * Get multiple key-value pairs for the give array of keys in a batch.
2937 * This is optimized to use only one database transaction.
3038 * @param {String[] } keysParam
3139 * @return {Promise<Array<[key, value]>> }
3240 */
33- multiGet : keysParam => getMany ( keysParam , customStore )
41+ multiGet : keysParam => getMany ( keysParam , getCustomStore ( ) )
3442 . then ( values => _ . map ( values , ( value , index ) => [ keysParam [ index ] , value ] ) ) ,
3543
3644 /**
3745 * Multiple merging of existing and new values in a batch
3846 * @param {Array<[key, value]> } pairs
3947 * @return {Promise<void> }
4048 */
41- multiMerge : pairs => customStore ( 'readwrite' , ( store ) => {
49+ multiMerge : pairs => getCustomStore ( ) ( 'readwrite' , ( store ) => {
4250 // Note: we are using the manual store transaction here, to fit the read and update
4351 // of the items in one transaction to achieve best performance.
4452
@@ -70,41 +78,41 @@ const provider = {
7078 * @param {Array<[key, value]> } pairs
7179 * @return {Promise<void> }
7280 */
73- multiSet : pairs => setMany ( pairs , customStore ) ,
81+ multiSet : pairs => setMany ( pairs , getCustomStore ( ) ) ,
7482
7583 /**
7684 * Clear everything from storage and also stops the SyncQueue from adding anything more to storage
7785 * @returns {Promise<void> }
7886 */
79- clear : ( ) => clear ( customStore ) ,
87+ clear : ( ) => clear ( getCustomStore ( ) ) ,
8088
8189 /**
8290 * Returns all keys available in storage
8391 * @returns {Promise<String[]> }
8492 */
85- getAllKeys : ( ) => keys ( customStore ) ,
93+ getAllKeys : ( ) => keys ( getCustomStore ( ) ) ,
8694
8795 /**
8896 * Get the value of a given key or return `null` if it's not available in storage
8997 * @param {String } key
9098 * @return {Promise<*> }
9199 */
92- getItem : key => get ( key , customStore ) ,
100+ getItem : key => get ( key , getCustomStore ( ) ) ,
93101
94102 /**
95103 * Remove given key and it's value from storage
96104 * @param {String } key
97105 * @returns {Promise<void> }
98106 */
99- removeItem : key => del ( key , customStore ) ,
107+ removeItem : key => del ( key , getCustomStore ( ) ) ,
100108
101109 /**
102110 * Remove given keys and their values from storage
103111 *
104112 * @param {Array } keysParam
105113 * @returns {Promise }
106114 */
107- removeItems : keysParam => delMany ( keysParam , customStore ) ,
115+ removeItems : keysParam => delMany ( keysParam , getCustomStore ( ) ) ,
108116} ;
109117
110118export default provider ;
0 commit comments