Created
March 4, 2020 12:54
-
-
Save bitflower/e0c78ff03a111a7bbb46da73ce5e50b8 to your computer and use it in GitHub Desktop.
Debouncedd Capacitor Storage for redux-persist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| FileDeleteResult, | |
| FileReadResult, | |
| FilesystemDirectory, | |
| FileWriteOptions, | |
| FileWriteResult, | |
| Plugins, | |
| ReaddirResult | |
| } from '@capacitor/core'; | |
| import { getLogger, loggableObject, makeDebounce } from '@case-os/commons'; | |
| import { WebStorage } from 'redux-persist'; | |
| import { readDir, readFile, writeFile } from '../filesystem'; | |
| const { Filesystem } = Plugins; | |
| const { Data } = FilesystemDirectory; | |
| const d = getLogger('storage/co-file-storage.ts (persistence)'); | |
| /* | |
| Idea: The key is the filename. | |
| */ | |
| const STORAGE_PATH: string = 'co-storage'; | |
| const FILE_EXTENTION: string = 'txt'; | |
| export class CoFileStorageClass implements WebStorage { | |
| private _saveFns: any = {}; | |
| public async setup(): Promise<void> { | |
| d('setup'); | |
| try { | |
| // Make sure the folder exists | |
| const files: ReaddirResult = await readDir({ | |
| directory: Data, | |
| path: STORAGE_PATH | |
| }); | |
| d('setup, files', files.files); | |
| } catch (e) { | |
| d('setup, ERROR', e); | |
| return; | |
| } | |
| } | |
| private async _save(key: string, value: string): Promise<void> { | |
| d( | |
| '_save', | |
| loggableObject({ | |
| path: `${STORAGE_PATH}/${key}.${FILE_EXTENTION}`, | |
| key, | |
| value | |
| }) | |
| ); | |
| const fileWriteOptions: FileWriteOptions = { | |
| directory: Data, | |
| path: `${STORAGE_PATH}/${key}.${FILE_EXTENTION}`, | |
| data: value | |
| }; | |
| this._saveFns[key] = | |
| this._saveFns[key] || | |
| makeDebounce(writeFile, 500, null, { leading: true }); | |
| try { | |
| const writeResult: FileWriteResult = await this._saveFns[key]( | |
| fileWriteOptions | |
| ); | |
| d( | |
| '_save, AFTER', | |
| loggableObject({ | |
| path: `${STORAGE_PATH}/${key}.${FILE_EXTENTION}`, | |
| fileWriteOptions, | |
| writeResult | |
| }) | |
| ); | |
| } catch (e) { | |
| d('_save, ERROR', loggableObject(e)); | |
| } | |
| } | |
| private async _remove(key: string): Promise<void> { | |
| d('_remove', { path: `${STORAGE_PATH}/${key}.${FILE_EXTENTION}`, key }); | |
| try { | |
| // TODO: Abort eventual debounced save calls | |
| const deleteResult: FileDeleteResult = await Filesystem.deleteFile({ | |
| path: `${STORAGE_PATH}/${key}.${FILE_EXTENTION}`, | |
| directory: Data | |
| }); | |
| d('_remove, AFTER', loggableObject(deleteResult)); | |
| } catch (e) { | |
| d('_remove, ERROR', loggableObject(e)); | |
| } | |
| } | |
| public async getItem(key: string): Promise<string> { | |
| d('getItem', { key }); | |
| try { | |
| const fileReadResult: FileReadResult = await readFile({ | |
| directory: Data, | |
| path: `${STORAGE_PATH}/${key}.${FILE_EXTENTION}` | |
| }); | |
| d('getItem, AFTER', loggableObject({ fileReadResult })); | |
| return fileReadResult.data; | |
| } catch (e) { | |
| d('getItem, AFTER', loggableObject(e)); | |
| return null; | |
| } | |
| } | |
| public setItem(key: string, value: string): Promise<void> { | |
| d('setItem', loggableObject({ key, value })); | |
| return this._save(key, value); | |
| } | |
| public removeItem(key: string): Promise<void> { | |
| d('removeItem', { key }); | |
| return this._remove(key); | |
| } | |
| } | |
| const CoFileStorage: CoFileStorageClass = new CoFileStorageClass(); | |
| export { CoFileStorage }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
makeDebouncejust useslodash'sdebouncemethod