diff --git a/src/extensions/renderer-api/catalog.ts b/src/extensions/renderer-api/catalog.ts index 389107c80d..8486eb10d7 100644 --- a/src/extensions/renderer-api/catalog.ts +++ b/src/extensions/renderer-api/catalog.ts @@ -56,8 +56,8 @@ export class CatalogEntityRegistry { * @param onBeforeRun The function that should return a boolean if the onRun of catalog entity should be triggered. * @returns A function to remove that hook */ - addOnBeforeRun(entity: CatalogEntity, onBeforeRun: CatalogEntityOnBeforeRun): Disposer { - return registry.addOnBeforeRun(entity, onBeforeRun); + addOnBeforeRun(onBeforeRun: CatalogEntityOnBeforeRun): Disposer { + return registry.addOnBeforeRun(onBeforeRun); } } diff --git a/src/renderer/api/catalog-entity-registry.ts b/src/renderer/api/catalog-entity-registry.ts index f9c59fce09..1d57693b3f 100644 --- a/src/renderer/api/catalog-entity-registry.ts +++ b/src/renderer/api/catalog-entity-registry.ts @@ -19,7 +19,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import { computed, observable, makeObservable, action, ObservableSet } from "mobx"; +import { computed, observable, makeObservable, action } from "mobx"; import { ipcRendererOn } from "../../common/ipc"; import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntityKindData } from "../../common/catalog"; import "../../common/catalog-entities"; @@ -33,15 +33,13 @@ import { catalogEntityRunContext } from "./catalog-entity"; export type EntityFilter = (entity: CatalogEntity) => any; export type CatalogEntityOnBeforeRun = (entity: CatalogEntity) => boolean | Promise; -type CatalogEntityUid = CatalogEntity["metadata"]["uid"]; - export class CatalogEntityRegistry { @observable protected activeEntityId: string | undefined = undefined; protected _entities = observable.map([], { deep: true }); protected filters = observable.set([], { deep: false, }); - protected onBeforeRunHooks = observable.map>({}, { + protected onBeforeRunHooks = observable.set([], { deep: false, }); @@ -179,23 +177,16 @@ export class CatalogEntityRegistry { } /** - * Add a onBeforeRun hook to a catalog entity. If `onBeforeRun` was previously added then it will not be added again - * @param catalogEntityUid The uid of the catalog entity + * Add a onBeforeRun hook. If `onBeforeRun` was previously added then it will not be added again * @param onBeforeRun The function that should return a boolean if the onRun of catalog entity should be triggered. * @returns A function to remove that hook */ - addOnBeforeRun(entityOrId: CatalogEntity | CatalogEntityUid, onBeforeRun: CatalogEntityOnBeforeRun): Disposer { - logger.debug(`[CATALOG-ENTITY-REGISTRY]: adding onBeforeRun to ${entityOrId}`); + addOnBeforeRun(onBeforeRun: CatalogEntityOnBeforeRun): Disposer { + logger.debug(`[CATALOG-ENTITY-REGISTRY]: adding onBeforeRun hook`); - const id = typeof entityOrId === "string" - ? entityOrId - : entityOrId.getId(); - const hooks = this.onBeforeRunHooks.get(id) ?? - this.onBeforeRunHooks.set(id, observable.set([], { deep: false })).get(id); - - hooks.add(onBeforeRun); - - return once(() => void hooks.delete(onBeforeRun)); + this.onBeforeRunHooks.add(onBeforeRun); + + return once(() => void this.onBeforeRunHooks.delete(onBeforeRun)); } /** @@ -205,14 +196,8 @@ export class CatalogEntityRegistry { */ async onBeforeRun(entity: CatalogEntity): Promise { logger.debug(`[CATALOG-ENTITY-REGISTRY]: run onBeforeRun on ${entity.getId()}`); - - const hooks = this.onBeforeRunHooks.get(entity.getId()); - if (!hooks) { - return true; - } - - for (const onBeforeRun of hooks) { + for (const onBeforeRun of this.onBeforeRunHooks) { try { if (!await onBeforeRun(entity)) { return false; diff --git a/src/renderer/components/+catalog/catalog.test.tsx b/src/renderer/components/+catalog/catalog.test.tsx index ac510d8fb4..89986bbf76 100644 --- a/src/renderer/components/+catalog/catalog.test.tsx +++ b/src/renderer/components/+catalog/catalog.test.tsx @@ -129,7 +129,6 @@ describe("", () => { .mockImplementation(() => catalogEntityItem); catalogEntityRegistry.addOnBeforeRun( - catalogEntityUid, (entity) => { expect(entity).toMatchInlineSnapshot(` Object { @@ -193,7 +192,6 @@ describe("", () => { .mockImplementation(() => catalogEntityItem); catalogEntityRegistry.addOnBeforeRun( - catalogEntityUid, () => { setTimeout(() => { expect(onRun).not.toHaveBeenCalled(); @@ -233,7 +231,6 @@ describe("", () => { .mockImplementation(() => catalogEntityItem); catalogEntityRegistry.addOnBeforeRun( - catalogEntityUid, () => { setTimeout(() => { expect(onRun).not.toHaveBeenCalled(); @@ -273,7 +270,6 @@ describe("", () => { .mockImplementation(() => catalogEntityItem); catalogEntityRegistry.addOnBeforeRun( - catalogEntityUid, async () => { return true; } @@ -308,7 +304,6 @@ describe("", () => { .mockImplementation(() => catalogEntityItem); catalogEntityRegistry.addOnBeforeRun( - catalogEntityUid, async () => { expect(onRun).not.toBeCalled(); @@ -350,7 +345,6 @@ describe("", () => { .mockImplementation(() => catalogEntityItem); catalogEntityRegistry.addOnBeforeRun( - catalogEntityUid, async () => { setTimeout(() => { expect(onRun).not.toHaveBeenCalled();