1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Await onActivate/onDeactive of extensions (#2963)

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-06-07 08:08:39 -04:00 committed by GitHub
parent c272ccb48a
commit d21f99e609
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 48 deletions

View File

@ -26,7 +26,7 @@ import { action, computed, makeObservable, observable, reaction, when } from "mo
import path from "path"; import path from "path";
import { getHostedCluster } from "../common/cluster-store"; import { getHostedCluster } from "../common/cluster-store";
import { broadcastMessage, handleRequest, requestMain, subscribeToBroadcast } from "../common/ipc"; import { broadcastMessage, handleRequest, requestMain, subscribeToBroadcast } from "../common/ipc";
import { Singleton, toJS } from "../common/utils"; import { Disposer, Singleton, toJS } from "../common/utils";
import logger from "../main/logger"; import logger from "../main/logger";
import type { InstalledExtension } from "./extension-discovery"; import type { InstalledExtension } from "./extension-discovery";
import { ExtensionsStore } from "./extensions-store"; import { ExtensionsStore } from "./extensions-store";
@ -296,7 +296,7 @@ export class ExtensionLoader extends Singleton {
}); });
} }
protected autoInitExtensions(register: (ext: LensExtension) => Promise<Function[]>) { protected autoInitExtensions(register: (ext: LensExtension) => Promise<Disposer[]>) {
return reaction(() => this.toJSON(), installedExtensions => { return reaction(() => this.toJSON(), installedExtensions => {
for (const [extId, extension] of installedExtensions) { for (const [extId, extension] of installedExtensions) {
const alreadyInit = this.instances.has(extId); const alreadyInit = this.instances.has(extId);
@ -311,8 +311,7 @@ export class ExtensionLoader extends Singleton {
const instance = new LensExtensionClass(extension); const instance = new LensExtensionClass(extension);
instance.whenEnabled(() => register(instance)); instance.enable(register);
instance.enable();
this.instances.set(extId, instance); this.instances.set(extId, instance);
} catch (err) { } catch (err) {
logger.error(`${logModule}: activation extension error`, { ext: extension, err }); logger.error(`${logModule}: activation extension error`, { ext: extension, err });

View File

@ -20,11 +20,11 @@
*/ */
import type { InstalledExtension } from "./extension-discovery"; import type { InstalledExtension } from "./extension-discovery";
import { action, observable, reaction, makeObservable } from "mobx"; import { action, observable, makeObservable } from "mobx";
import { FilesystemProvisionerStore } from "../main/extension-filesystem"; import { FilesystemProvisionerStore } from "../main/extension-filesystem";
import logger from "../main/logger"; import logger from "../main/logger";
import type { ProtocolHandlerRegistration } from "./registries"; import type { ProtocolHandlerRegistration } from "./registries";
import { disposer } from "../common/utils"; import { Disposer, disposer } from "../common/utils";
export type LensExtensionId = string; // path to manifest (package.json) export type LensExtensionId = string; // path to manifest (package.json)
export type LensExtensionConstructor = new (...args: ConstructorParameters<typeof LensExtension>) => LensExtension; export type LensExtensionConstructor = new (...args: ConstructorParameters<typeof LensExtension>) => LensExtension;
@ -83,59 +83,44 @@ export class LensExtension {
} }
@action @action
async enable() { async enable(register: (ext: LensExtension) => Promise<Disposer[]>) {
if (this.isEnabled) return; if (this.isEnabled) {
this.isEnabled = true; return;
this.onActivate?.(); }
logger.info(`[EXTENSION]: enabled ${this.name}@${this.version}`);
try {
await this.onActivate();
this.isEnabled = true;
this[Disposers].push(...await register(this));
logger.info(`[EXTENSION]: enabled ${this.name}@${this.version}`);
} catch (error) {
logger.error(`[EXTENSION]: failed to activate ${this.name}@${this.version}: ${error}`);
}
} }
@action @action
async disable() { async disable() {
if (!this.isEnabled) return; if (!this.isEnabled) {
this.isEnabled = false; return;
this.onDeactivate?.(); }
this[Disposers]();
logger.info(`[EXTENSION]: disabled ${this.name}@${this.version}`);
}
toggle(enable?: boolean) { this.isEnabled = false;
if (typeof enable === "boolean") {
enable ? this.enable() : this.disable(); try {
} else { await this.onDeactivate();
this.isEnabled ? this.disable() : this.enable(); this[Disposers]();
logger.info(`[EXTENSION]: disabled ${this.name}@${this.version}`);
} catch (error) {
logger.error(`[EXTENSION]: disabling ${this.name}@${this.version} threw an error: ${error}`);
} }
} }
async whenEnabled(handlers: () => Promise<Function[]>) { protected onActivate(): Promise<void> | void {
const disposers: Function[] = [];
const unregisterHandlers = () => {
disposers.forEach(unregister => unregister());
disposers.length = 0;
};
const cancelReaction = reaction(() => this.isEnabled, async (isEnabled) => {
if (isEnabled) {
const handlerDisposers = await handlers();
disposers.push(...handlerDisposers);
} else {
unregisterHandlers();
}
}, {
fireImmediately: true
});
return () => {
unregisterHandlers();
cancelReaction();
};
}
protected onActivate(): void {
return; return;
} }
protected onDeactivate(): void { protected onDeactivate(): Promise<void> | void {
return; return;
} }
} }