diff --git a/extensions/telemetry/src/telemetry-preferences-store.ts b/extensions/telemetry/src/telemetry-preferences-store.ts index 732ad91524..e29b0ff461 100644 --- a/extensions/telemetry/src/telemetry-preferences-store.ts +++ b/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 { + enabled = true; + private constructor() { super({ configName: "preferences-store", @@ -15,17 +17,13 @@ export class TelemetryPreferencesStore extends Store.ExtensionStore extends ConfOptions { syncOptions?: IReactionOptions; } -export class BaseStore extends Singleton { +/** + * Note: T should only contain base JSON serializable types. + */ +export abstract class BaseStore extends Singleton { protected storeConfig: Config; protected syncDisposers: Function[] = []; @@ -146,16 +149,19 @@ export class BaseStore 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; } diff --git a/src/extensions/extension-store.ts b/src/extensions/extension-store.ts index d5372eceff..ec2f61948e 100644 --- a/src/extensions/extension-store.ts +++ b/src/extensions/extension-store.ts @@ -2,17 +2,17 @@ import { BaseStore } from "../common/base-store" import * as path from "path" import { LensExtension } from "./lens-extension" -export class ExtensionStore extends BaseStore { +export abstract class ExtensionStore extends BaseStore { 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() {