-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Did some refactoring to move some functions to other locations that m…
…ake more sense. Updated antd package. Changed persist function into a singleton class that we can set the datastore for. It should hopefully make testing it easier.
- Loading branch information
Showing
7 changed files
with
295 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { DataPersister } from './DataPersister'; | ||
|
||
describe('Test DataPersister Class', () => { | ||
it('returns the DataPersistor singleton instance', () => { | ||
const persistor = DataPersister.Instance; | ||
|
||
expect(persistor).toBeInstanceOf(DataPersister); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import { SetterOrUpdater } from 'recoil'; | ||
import { loadSessionValue, saveSessionValue } from '../api'; | ||
|
||
export type PersistData<T> = { | ||
loader: () => Promise<T>; | ||
saver: () => Promise<void>; | ||
getter: () => T; | ||
setter: (value: T) => void; | ||
value: T; | ||
}; | ||
|
||
export class DataPersister { | ||
private static _instance: DataPersister; | ||
|
||
private _PERSISTENT_STORE: { | ||
[key: string]: PersistData<unknown>; | ||
} = {}; | ||
|
||
private constructor() { | ||
this._PERSISTENT_STORE = {}; | ||
} | ||
|
||
public static get Instance(): DataPersister { | ||
if (!this._instance) { | ||
this._instance = new this(); | ||
} | ||
return this._instance; | ||
} | ||
|
||
initializeDataStore(dataStore: { | ||
[key: string]: PersistData<unknown>; | ||
}): void { | ||
this._PERSISTENT_STORE = dataStore; | ||
} | ||
|
||
addNewVar<T>( | ||
varKey: string, | ||
defaultVal: T, | ||
setterFunc: SetterOrUpdater<T>, | ||
loaderFunc?: () => Promise<T> | ||
): void { | ||
if (Object.hasOwn(this._PERSISTENT_STORE, varKey)) { | ||
return; | ||
} | ||
|
||
const loader = async (): Promise<T> => { | ||
let val: T | null = null; | ||
if (loaderFunc) { | ||
val = await loaderFunc(); | ||
} else { | ||
val = await loadSessionValue<T>(varKey); | ||
} | ||
|
||
if (this._PERSISTENT_STORE[varKey]) { | ||
this._PERSISTENT_STORE[varKey].value = val || defaultVal; | ||
setterFunc(val || defaultVal); | ||
} | ||
return val || defaultVal; | ||
}; | ||
|
||
const saver = async (): Promise<void> => { | ||
if (this._PERSISTENT_STORE[varKey]) { | ||
await saveSessionValue<T>( | ||
varKey, | ||
this._PERSISTENT_STORE[varKey].value as T | ||
); | ||
} else { | ||
await saveSessionValue<T>(varKey, defaultVal); | ||
} | ||
}; | ||
|
||
const setter = (val: T): void => { | ||
if (this._PERSISTENT_STORE[varKey]) { | ||
this._PERSISTENT_STORE[varKey].value = val; | ||
setterFunc(val); | ||
} | ||
}; | ||
|
||
const getter = (): T => { | ||
if (this._PERSISTENT_STORE[varKey]) { | ||
return this._PERSISTENT_STORE[varKey].value as T; | ||
} | ||
return defaultVal; | ||
}; | ||
|
||
const newVar = { loader, saver, getter, setter, value: defaultVal }; | ||
this._PERSISTENT_STORE[varKey] = newVar as PersistData<unknown>; | ||
} | ||
|
||
getValue<T>(varKey: string): T | null { | ||
if (this._PERSISTENT_STORE[varKey]) { | ||
return this._PERSISTENT_STORE[varKey].value as T; | ||
} | ||
return null; | ||
} | ||
|
||
async loadValue<T>(varKey: string): Promise<T | null> { | ||
if (this._PERSISTENT_STORE[varKey]) { | ||
return this._PERSISTENT_STORE[varKey].loader() as Promise<T>; | ||
} | ||
return null; | ||
} | ||
|
||
async setValue<T>(varKey: string, value: T, save: boolean): Promise<void> { | ||
if (this._PERSISTENT_STORE[varKey]) { | ||
this._PERSISTENT_STORE[varKey].setter(value); | ||
|
||
if (save) { | ||
await this._PERSISTENT_STORE[varKey].saver(); | ||
} | ||
} | ||
} | ||
|
||
async loadAllValues(): Promise<void> { | ||
const loadFuncs: Promise<unknown>[] = []; | ||
Object.values(this._PERSISTENT_STORE).forEach((persistVar) => { | ||
if (persistVar && persistVar.loader) { | ||
loadFuncs.push(persistVar.loader()); | ||
} | ||
}); | ||
|
||
await Promise.all(loadFuncs); | ||
} | ||
|
||
async saveAllValues(): Promise<void> { | ||
const saveFuncs: Promise<void>[] = []; | ||
Object.values(this._PERSISTENT_STORE).forEach((persistVar) => { | ||
if (persistVar && persistVar.saver) { | ||
saveFuncs.push(persistVar.saver()); | ||
} | ||
}); | ||
|
||
await Promise.all(saveFuncs); | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.