Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make BaseStore abstract #1431

Merged
merged 2 commits into from Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions extensions/telemetry/src/telemetry-preferences-store.ts
@@ -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",
Expand All @@ -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
})
Expand Down
32 changes: 19 additions & 13 deletions src/common/base-store.ts
Expand Up @@ -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[] = [];

Expand Down Expand Up @@ -146,16 +149,19 @@ export class BaseStore<T = any> extends Singleton {
}
}

@action
protected fromStore(data: T) {
if (!data) return;
this.data = data;
}

// todo: use "serializr" ?
toJSON(): T {
return toJS(this.data, {
recurseEverything: true,
})
}
/**
* 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;

/**
* 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;
}
6 changes: 3 additions & 3 deletions src/extensions/extension-store.ts
Expand Up @@ -2,17 +2,17 @@ import { BaseStore } from "../common/base-store"
import * as path from "path"
import { LensExtension } from "./lens-extension"

export class ExtensionStore<T = any> extends BaseStore<T> {
export abstract class ExtensionStore<T> extends BaseStore<T> {
protected extension: LensExtension

async loadExtension(extension: LensExtension) {
this.extension = extension
await super.load()
return super.load()
}

async load() {
if (!this.extension) { return }
await super.load()
return super.load()
}

protected cwd() {
Expand Down