Extensions api fixes (#1233)

* fix: create extension instance only when enabled

Signed-off-by: Roman <ixrock@gmail.com>

* mark extension.isEnabled with private modifier

Signed-off-by: Roman <ixrock@gmail.com>

* try-catch errors for extension.disable()

Signed-off-by: Roman <ixrock@gmail.com>

* fixes & refactoring

Signed-off-by: Roman <ixrock@gmail.com>

* make ext.isBundled non optional

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman authored and GitHub committed 2020-11-07 18:56:26 +02:00
1 parent 1b71106ed5
commit 94ac081588
8 files changed
+170 -81

No files matched your search

+6 -35
View File
@@ -1,7 +1,6 @@
import type { InstalledExtension } from "./extension-manager";
import { action, reaction } from "mobx";
import { action, observable, reaction } from "mobx";
import logger from "../main/logger";
import { ExtensionStore } from "./extension-store";
export type LensExtensionId = string; // path to manifest (package.json)
export type LensExtensionConstructor = new (...args: ConstructorParameters<typeof LensExtension>) => LensExtension;
@@ -14,35 +13,17 @@ export interface LensExtensionManifest {
renderer?: string; // path to %ext/dist/renderer.js
}
export interface LensExtensionStoreModel {
isEnabled: boolean;
}
export class LensExtension<S extends ExtensionStore<LensExtensionStoreModel> = any> {
protected store: S;
export class LensExtension {
readonly manifest: LensExtensionManifest;
readonly manifestPath: string;
readonly isBundled: boolean;
@observable private isEnabled = false;
constructor({ manifest, manifestPath, isBundled }: InstalledExtension) {
this.manifest = manifest
this.manifestPath = manifestPath
this.isBundled = !!isBundled
this.init();
}
protected async init(store: S = createBaseStore().getInstance()) {
this.store = store;
await this.store.loadExtension(this);
reaction(() => this.store.data.isEnabled, (isEnabled = true) => {
this.toggle(isEnabled); // handle activation & deactivation
}, {
fireImmediately: true
});
}
get isEnabled() {
return !!this.store.data.isEnabled;
}
get id(): LensExtensionId {
@@ -64,7 +45,7 @@ export class LensExtension<S extends ExtensionStore<LensExtensionStoreModel> = a
@action
async enable() {
if (this.isEnabled) return;
this.store.data.isEnabled = true;
this.isEnabled = true;
this.onActivate();
logger.info(`[EXTENSION]: enabled ${this.name}@${this.version}`);
}
@@ -72,7 +53,7 @@ export class LensExtension<S extends ExtensionStore<LensExtensionStoreModel> = a
@action
async disable() {
if (!this.isEnabled) return;
this.store.data.isEnabled = false;
this.isEnabled = false;
this.onDeactivate();
logger.info(`[EXTENSION]: disabled ${this.name}@${this.version}`);
}
@@ -114,13 +95,3 @@ export class LensExtension<S extends ExtensionStore<LensExtensionStoreModel> = a
// mock
}
}
function createBaseStore() {
return class extends ExtensionStore<LensExtensionStoreModel> {
constructor() {
super({
configName: "state"
});
}
}
}