Make BaseStore abstract (#1431)

* make BaseStore abstract so that implementers are forced to decide how to store data

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2020-11-19 09:24:41 -05:00
1 parent b64694acda
commit 0f3f5611ba
3 files changed
+27 -23

No files matched your search

+18 -12
View File
@@ -15,7 +15,10 @@ export interface BaseStoreParams<T = any> extends ConfOptions<T> {
syncOptions?: IReactionOptions;
}
export class BaseStore<T = any> extends Singleton {
/**
* Note: T should only contain base JSON serializable types.
*/
export abstract class BaseStore<T = any> extends Singleton {
protected storeConfig: Config<T>;
protected syncDisposers: Function[] = [];
@@ -146,16 +149,19 @@ export class BaseStore<T = any> extends Singleton {
}
}
@action
protected fromStore(data: T) {
if (!data) return;
this.data = data;
}
/**
* fromStore is called internally when a child class syncs with the file
* system.
* @param data the parsed information read from the stored JSON file
*/
protected abstract fromStore(data: T): void;
// todo: use "serializr" ?
toJSON(): T {
return toJS(this.data, {
recurseEverything: true,
})
}
/**
* toJSON is called when syncing the store to the filesystem. It should
* produce a JSON serializable object representaion of the current state.
*
* It is recommended that a round trip is valid. Namely, calling
* `this.fromStore(this.toJSON())` shouldn't change the state.
*/
abstract toJSON(): T;
}