Skip to content

Commit

Permalink
Revert "Make BaseStore abstract (#1431)"
Browse files Browse the repository at this point in the history
This reverts commit 4b56ab7.
  • Loading branch information
aleksfront committed Nov 20, 2020
1 parent a975e3d commit 3826877
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
14 changes: 8 additions & 6 deletions extensions/telemetry/src/telemetry-preferences-store.ts
@@ -1,13 +1,11 @@
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 @@ -17,13 +15,17 @@ export class TelemetryPreferencesStore extends Store.ExtensionStore<TelemetryPre
})
}

protected fromStore({ enabled }: TelemetryPreferencesModel): void {
this.enabled = enabled
get enabled() {
return this.data.enabled
}

set enabled(v: boolean) {
this.data.enabled = v
}

toJSON(): TelemetryPreferencesModel {
return toJS({
enabled: this.enabled
enabled: this.data.enabled
}, {
recurseEverything: true
})
Expand Down
32 changes: 13 additions & 19 deletions src/common/base-store.ts
Expand Up @@ -15,10 +15,7 @@ export interface BaseStoreParams<T = any> extends ConfOptions<T> {
syncOptions?: IReactionOptions;
}

/**
* Note: T should only contain base JSON serializable types.
*/
export abstract class BaseStore<T = any> extends Singleton {
export class BaseStore<T = any> extends Singleton {
protected storeConfig: Config<T>;
protected syncDisposers: Function[] = [];

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

/**
* 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;
@action
protected fromStore(data: T) {
if (!data) return;
this.data = data;
}

// todo: use "serializr" ?
toJSON(): T {
return toJS(this.data, {
recurseEverything: true,
})
}
}
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 abstract class ExtensionStore<T> extends BaseStore<T> {
export class ExtensionStore<T = any> extends BaseStore<T> {
protected extension: LensExtension

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

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

protected cwd() {
Expand Down

0 comments on commit 3826877

Please sign in to comment.