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:
parent
c272ccb48a
commit
d21f99e609
@ -26,7 +26,7 @@ import { action, computed, makeObservable, observable, reaction, when } from "mo
|
||||
import path from "path";
|
||||
import { getHostedCluster } from "../common/cluster-store";
|
||||
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 type { InstalledExtension } from "./extension-discovery";
|
||||
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 => {
|
||||
for (const [extId, extension] of installedExtensions) {
|
||||
const alreadyInit = this.instances.has(extId);
|
||||
@ -311,8 +311,7 @@ export class ExtensionLoader extends Singleton {
|
||||
|
||||
const instance = new LensExtensionClass(extension);
|
||||
|
||||
instance.whenEnabled(() => register(instance));
|
||||
instance.enable();
|
||||
instance.enable(register);
|
||||
this.instances.set(extId, instance);
|
||||
} catch (err) {
|
||||
logger.error(`${logModule}: activation extension error`, { ext: extension, err });
|
||||
|
||||
@ -20,11 +20,11 @@
|
||||
*/
|
||||
|
||||
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 logger from "../main/logger";
|
||||
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 LensExtensionConstructor = new (...args: ConstructorParameters<typeof LensExtension>) => LensExtension;
|
||||
@ -83,59 +83,44 @@ export class LensExtension {
|
||||
}
|
||||
|
||||
@action
|
||||
async enable() {
|
||||
if (this.isEnabled) return;
|
||||
async enable(register: (ext: LensExtension) => Promise<Disposer[]>) {
|
||||
if (this.isEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await this.onActivate();
|
||||
this.isEnabled = true;
|
||||
this.onActivate?.();
|
||||
|
||||
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
|
||||
async disable() {
|
||||
if (!this.isEnabled) return;
|
||||
this.isEnabled = false;
|
||||
this.onDeactivate?.();
|
||||
this[Disposers]();
|
||||
logger.info(`[EXTENSION]: disabled ${this.name}@${this.version}`);
|
||||
}
|
||||
|
||||
toggle(enable?: boolean) {
|
||||
if (typeof enable === "boolean") {
|
||||
enable ? this.enable() : this.disable();
|
||||
} else {
|
||||
this.isEnabled ? this.disable() : this.enable();
|
||||
}
|
||||
}
|
||||
|
||||
async whenEnabled(handlers: () => Promise<Function[]>) {
|
||||
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 {
|
||||
if (!this.isEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
protected onDeactivate(): void {
|
||||
this.isEnabled = false;
|
||||
|
||||
try {
|
||||
await this.onDeactivate();
|
||||
this[Disposers]();
|
||||
logger.info(`[EXTENSION]: disabled ${this.name}@${this.version}`);
|
||||
} catch (error) {
|
||||
logger.error(`[EXTENSION]: disabling ${this.name}@${this.version} threw an error: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
protected onActivate(): Promise<void> | void {
|
||||
return;
|
||||
}
|
||||
|
||||
protected onDeactivate(): Promise<void> | void {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user