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

Register/reregister injectables by id.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
Panu Horsmalahti 2022-11-17 17:20:25 +02:00
parent 7a8c3effd0
commit 2b3862b39e
2 changed files with 40 additions and 5 deletions

View File

@ -2,7 +2,9 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Injectable } from "@ogre-tools/injectable";
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { difference, find, map } from "lodash";
import { reaction, runInAction } from "mobx";
import { disposer } from "../../../common/utils/disposer";
import type { LensExtension } from "../../lens-extension";
@ -13,6 +15,16 @@ export interface Extension {
deregister: () => void;
}
const idsToInjectables = (ids: string[], injectables: Injectable<any, any, any>[]) => ids.map(id => {
const injectable = find(injectables, { id });
if (!injectable) {
throw new Error(`Injectable ${id} not found`);
}
return injectable;
});
const extensionInjectable = getInjectable({
id: "extension",
@ -34,13 +46,20 @@ const extensionInjectable = getInjectable({
// we need to update the registered injectables with a reaction every time they change
reaction(
() => Array.isArray(injectables) ? injectables : injectables.get(),
(currentInjectables, previousInjectables) => {
// On the second reaction remove the previously registered injectables to avoid duplicate injectables
if (previousInjectables) {
childDi.deregister(...previousInjectables);
(currentInjectables, previousInjectables = []) => {
// Register new injectables and deregister removed injectables by id
const currentIds = map(currentInjectables, "id");
const previousIds = map(previousInjectables, "id");
const idsToAdd = difference(currentIds, previousIds);
const idsToRemove = previousIds.filter(previousId => !currentIds.includes(previousId));
if (idsToRemove.length > 0) {
childDi.deregister(...idsToInjectables(idsToRemove, previousInjectables));
}
childDi.register(...currentInjectables);
if (idsToAdd.length > 0) {
childDi.register(...idsToInjectables(idsToAdd, currentInjectables));
}
}, {
fireImmediately: true,
},

View File

@ -258,5 +258,21 @@ describe("preferences: extension adding tray items", () => {
),
).toBeNull();
});
it("given items are removed and one added, it's shown", async () => {
menuItems.replace([]);
menuItems.push({
id: "some-added",
label: "some-added",
click: () => {},
visible: computed(() => true),
});
expect(
builder.tray.get(
"some-added-tray-menu-item-for-extension-some-extension",
),
).not.toBeNull();
});
});
});