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

Make sure that extensions have been initialized before exposing them as enabled extensions

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2021-12-09 07:41:43 +02:00
parent 41837e86bb
commit f9cf08b4a8
No known key found for this signature in database
GPG Key ID: 5F465B5672372402
3 changed files with 18 additions and 11 deletions

View File

@ -46,7 +46,7 @@ const logModule = "[EXTENSIONS-LOADER]";
*/ */
export class ExtensionLoader extends Singleton { export class ExtensionLoader extends Singleton {
protected extensions = observable.map<LensExtensionId, InstalledExtension>(); protected extensions = observable.map<LensExtensionId, InstalledExtension>();
instances = observable.map<LensExtensionId, LensExtension>(); protected instances = observable.map<LensExtensionId, LensExtension>();
/** /**
* This is the set of extensions that don't come with either * This is the set of extensions that don't come with either
@ -96,6 +96,10 @@ export class ExtensionLoader extends Singleton {
}); });
} }
@computed get enabledExtensionInstances() : LensExtension[] {
return [...this.instances.values()].filter(extension => extension.isEnabled);
}
@computed get userExtensions(): Map<LensExtensionId, InstalledExtension> { @computed get userExtensions(): Map<LensExtensionId, InstalledExtension> {
const extensions = this.toJSON(); const extensions = this.toJSON();

View File

@ -28,15 +28,13 @@ const extensionsInjectable: Injectable<
{ extensionLoader: ExtensionLoader } { extensionLoader: ExtensionLoader }
> = { > = {
getDependencies: () => ({ getDependencies: () => ({
extensionLoader: ExtensionLoader.createInstance(), extensionLoader: ExtensionLoader.getInstance(),
}), }),
lifecycle: lifecycleEnum.singleton, lifecycle: lifecycleEnum.singleton,
instantiate: ({ extensionLoader }) => instantiate: ({ extensionLoader }) =>
computed(() => computed(() => extensionLoader.enabledExtensionInstances),
[...extensionLoader.instances.values()],
),
}; };
export default extensionsInjectable; export default extensionsInjectable;

View File

@ -20,7 +20,7 @@
*/ */
import type { InstalledExtension } from "./extension-discovery"; import type { InstalledExtension } from "./extension-discovery";
import { action, observable, makeObservable } from "mobx"; import { action, observable, makeObservable, computed } 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";
@ -47,7 +47,12 @@ export class LensExtension {
protocolHandlers: ProtocolHandlerRegistration[] = []; protocolHandlers: ProtocolHandlerRegistration[] = [];
@observable private isEnabled = false; @observable private _isEnabled = false;
@computed get isEnabled() {
return this._isEnabled;
}
[Disposers] = disposer(); [Disposers] = disposer();
constructor({ id, manifest, manifestPath, isBundled }: InstalledExtension) { constructor({ id, manifest, manifestPath, isBundled }: InstalledExtension) {
@ -83,13 +88,13 @@ export class LensExtension {
@action @action
async enable(register: (ext: LensExtension) => Promise<Disposer[]>) { async enable(register: (ext: LensExtension) => Promise<Disposer[]>) {
if (this.isEnabled) { if (this._isEnabled) {
return; return;
} }
try { try {
await this.onActivate(); await this.onActivate();
this.isEnabled = true; this._isEnabled = true;
this[Disposers].push(...await register(this)); this[Disposers].push(...await register(this));
logger.info(`[EXTENSION]: enabled ${this.name}@${this.version}`); logger.info(`[EXTENSION]: enabled ${this.name}@${this.version}`);
@ -100,11 +105,11 @@ export class LensExtension {
@action @action
async disable() { async disable() {
if (!this.isEnabled) { if (!this._isEnabled) {
return; return;
} }
this.isEnabled = false; this._isEnabled = false;
try { try {
await this.onDeactivate(); await this.onDeactivate();