mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
make BaseStore abstract so that implementors are forced to decide how to store data
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
5589a99e7e
commit
6fa0692bea
@ -1,11 +1,13 @@
|
||||
import { Store } from "@k8slens/extensions";
|
||||
import { toJS } from "mobx"
|
||||
|
||||
export type TelemetryPreferencesModel = {
|
||||
export type TelemetryPreferencesModel = {
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export class TelemetryPreferencesStore extends Store.ExtensionStore<TelemetryPreferencesModel> {
|
||||
enabled = true;
|
||||
|
||||
private constructor() {
|
||||
super({
|
||||
configName: "preferences-store",
|
||||
@ -15,17 +17,13 @@ export class TelemetryPreferencesStore extends Store.ExtensionStore<TelemetryPre
|
||||
})
|
||||
}
|
||||
|
||||
get enabled() {
|
||||
return this.data.enabled
|
||||
}
|
||||
|
||||
set enabled(v: boolean) {
|
||||
this.data.enabled = v
|
||||
protected fromStore({ enabled }: TelemetryPreferencesModel): void {
|
||||
this.enabled = enabled
|
||||
}
|
||||
|
||||
toJSON(): TelemetryPreferencesModel {
|
||||
return toJS({
|
||||
enabled: this.data.enabled
|
||||
enabled: this.enabled
|
||||
}, {
|
||||
recurseEverything: true
|
||||
})
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user