Changeset View
Changeset View
Standalone View
Standalone View
web/redux/comm-redux-storage-engine.js
// @flow | // @flow | ||||
import storage from 'redux-persist/es/storage/index.js'; | |||||
import { databaseModule } from '../database/database-module-provider.js'; | import { databaseModule } from '../database/database-module-provider.js'; | ||||
import { workerRequestMessageTypes } from '../types/worker-types.js'; | import { workerRequestMessageTypes } from '../types/worker-types.js'; | ||||
const commReduxStorageEngine = { | const commReduxStorageEngine = { | ||||
getItem: async (key: string): Promise<string> => { | getItem: async (key: string): Promise<string> => { | ||||
const isSupported = await databaseModule.isDatabaseSupported(); | |||||
if (!isSupported) { | |||||
return await storage.getItem(key); | |||||
} | |||||
const result = await databaseModule.schedule({ | const result = await databaseModule.schedule({ | ||||
type: workerRequestMessageTypes.GET_PERSIST_STORAGE_ITEM, | type: workerRequestMessageTypes.GET_PERSIST_STORAGE_ITEM, | ||||
key, | key, | ||||
}); | }); | ||||
if (typeof result?.item !== 'string') { | if (typeof result?.item !== 'string') { | ||||
throw new Error('Wrong type returned for storage item'); | throw new Error('Wrong type returned for storage item'); | ||||
} | } | ||||
return result.item; | return result.item; | ||||
}, | }, | ||||
setItem: async (key: string, item: string): Promise<void> => { | setItem: async (key: string, item: string): Promise<void> => { | ||||
const isSupported = await databaseModule.isDatabaseSupported(); | |||||
if (!isSupported) { | |||||
await storage.setItem(key, item); | |||||
return; | |||||
} | |||||
await databaseModule.schedule({ | await databaseModule.schedule({ | ||||
type: workerRequestMessageTypes.SET_PERSIST_STORAGE_ITEM, | type: workerRequestMessageTypes.SET_PERSIST_STORAGE_ITEM, | ||||
key, | key, | ||||
item, | item, | ||||
}); | }); | ||||
}, | }, | ||||
removeItem: async (key: string): Promise<void> => { | removeItem: async (key: string): Promise<void> => { | ||||
const isSupported = await databaseModule.isDatabaseSupported(); | |||||
if (!isSupported) { | |||||
await storage.removeItem(key); | |||||
return; | |||||
} | |||||
await databaseModule.schedule({ | await databaseModule.schedule({ | ||||
type: workerRequestMessageTypes.REMOVE_PERSIST_STORAGE_ITEM, | type: workerRequestMessageTypes.REMOVE_PERSIST_STORAGE_ITEM, | ||||
key, | key, | ||||
}); | }); | ||||
}, | }, | ||||
}; | }; | ||||
export default commReduxStorageEngine; | export default commReduxStorageEngine; |