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

Change onBeforeRun API (#3981)

Co-authored-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Juho Heikka 2021-10-07 19:43:50 +03:00 committed by GitHub
parent 58f7bcb38d
commit 724b9450a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 59 deletions

View File

@ -0,0 +1,44 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import type { CatalogEntity } from "../catalog";
export class CatalogRunEvent {
#defaultPrevented: boolean;
#target: CatalogEntity;
get defaultPrevented() {
return this.#defaultPrevented;
}
get target() {
return this.#target;
}
constructor({ target }: { target: CatalogEntity }) {
this.#defaultPrevented = false;
this.#target = target;
}
preventDefault() {
this.#defaultPrevented = true;
}
}

View File

@ -51,13 +51,15 @@ export class CatalogEntityRegistry {
} }
/** /**
* Add a onBeforeRun hook to a catalog entity. If `onBeforeRun` was previously added then it will not be added again * Add a onBeforeRun hook to a catalog entities. If `onBeforeRun` was previously
* @param catalogEntityUid The uid of the catalog entity * 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. * @param onBeforeRun The function to be called with a `CatalogRunEvent`
* event target will be the catalog entity. onBeforeRun hook can call event.preventDefault()
* to stop run sequence
* @returns A function to remove that hook * @returns A function to remove that hook
*/ */
addOnBeforeRun(entity: CatalogEntity, onBeforeRun: CatalogEntityOnBeforeRun): Disposer { addOnBeforeRun(onBeforeRun: CatalogEntityOnBeforeRun): Disposer {
return registry.addOnBeforeRun(entity, onBeforeRun); return registry.addOnBeforeRun(onBeforeRun);
} }
} }

View File

@ -19,7 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 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 { ipcRendererOn } from "../../common/ipc";
import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntityKindData } from "../../common/catalog"; import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntityKindData } from "../../common/catalog";
import "../../common/catalog-entities"; import "../../common/catalog-entities";
@ -29,11 +29,10 @@ import { Disposer, iter } from "../utils";
import { once } from "lodash"; import { once } from "lodash";
import logger from "../../common/logger"; import logger from "../../common/logger";
import { catalogEntityRunContext } from "./catalog-entity"; import { catalogEntityRunContext } from "./catalog-entity";
import { CatalogRunEvent } from "../../common/catalog/catalog-run-event";
export type EntityFilter = (entity: CatalogEntity) => any; export type EntityFilter = (entity: CatalogEntity) => any;
export type CatalogEntityOnBeforeRun = (entity: CatalogEntity) => boolean | Promise<boolean>; export type CatalogEntityOnBeforeRun = (event: CatalogRunEvent) => void | Promise<void>;
type CatalogEntityUid = CatalogEntity["metadata"]["uid"];
export class CatalogEntityRegistry { export class CatalogEntityRegistry {
@observable protected activeEntityId: string | undefined = undefined; @observable protected activeEntityId: string | undefined = undefined;
@ -41,7 +40,7 @@ export class CatalogEntityRegistry {
protected filters = observable.set<EntityFilter>([], { protected filters = observable.set<EntityFilter>([], {
deep: false, deep: false,
}); });
protected onBeforeRunHooks = observable.map<CatalogEntityUid, ObservableSet<CatalogEntityOnBeforeRun>>({}, { protected onBeforeRunHooks = observable.set<CatalogEntityOnBeforeRun>([], {
deep: false, deep: false,
}); });
@ -179,49 +178,36 @@ export class CatalogEntityRegistry {
} }
/** /**
* Add a onBeforeRun hook to a catalog entity. If `onBeforeRun` was previously added then it will not be added again * Add a onBeforeRun hook. If `onBeforeRun` was previously added then it will not be added again
* @param catalogEntityUid The uid of the catalog entity
* @param onBeforeRun The function that should return a boolean if the onRun of catalog entity should be triggered. * @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 * @returns A function to remove that hook
*/ */
addOnBeforeRun(entityOrId: CatalogEntity | CatalogEntityUid, onBeforeRun: CatalogEntityOnBeforeRun): Disposer { addOnBeforeRun(onBeforeRun: CatalogEntityOnBeforeRun): Disposer {
logger.debug(`[CATALOG-ENTITY-REGISTRY]: adding onBeforeRun to ${entityOrId}`); logger.debug(`[CATALOG-ENTITY-REGISTRY]: adding onBeforeRun hook`);
const id = typeof entityOrId === "string" this.onBeforeRunHooks.add(onBeforeRun);
? 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 this.onBeforeRunHooks.delete(onBeforeRun));
return once(() => void hooks.delete(onBeforeRun));
} }
/** /**
* Runs all the registered `onBeforeRun` hooks, short circuiting on the first falsy returned/resolved valued * Runs all the registered `onBeforeRun` hooks, short circuiting on the first event that's preventDefaulted
* @param entity The entity to run the hooks on * @param entity The entity to run the hooks on
* @returns Whether the entities `onRun` method should be executed * @returns Whether the entities `onRun` method should be executed
*/ */
async onBeforeRun(entity: CatalogEntity): Promise<boolean> { async onBeforeRun(entity: CatalogEntity): Promise<boolean> {
logger.debug(`[CATALOG-ENTITY-REGISTRY]: run onBeforeRun on ${entity.getId()}`); logger.debug(`[CATALOG-ENTITY-REGISTRY]: run onBeforeRun on ${entity.getId()}`);
const hooks = this.onBeforeRunHooks.get(entity.getId()); const runEvent = new CatalogRunEvent({ target: entity });
if (!hooks) { for (const onBeforeRun of this.onBeforeRunHooks) {
return true; try { 
} await onBeforeRun(runEvent);
for (const onBeforeRun of hooks) {
try {
if (!await onBeforeRun(entity)) {
return false;
}
} catch (error) { } catch (error) {
logger.warn(`[CATALOG-ENTITY-REGISTRY]: entity ${entity.getId()} onBeforeRun threw an error`, error); logger.warn(`[CATALOG-ENTITY-REGISTRY]: entity ${entity.getId()} onBeforeRun threw an error`, error);
}
// If a handler throws treat it as if it has returned `false` if (runEvent.defaultPrevented) {
// Namely: assume that its internal logic has failed and didn't complete as expected
return false; return false;
} }
} }

View File

@ -129,9 +129,8 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, (event) => {
(entity) => { expect(event.target).toMatchInlineSnapshot(`
expect(entity).toMatchInlineSnapshot(`
Object { Object {
"apiVersion": "api", "apiVersion": "api",
"enabled": true, "enabled": true,
@ -159,8 +158,6 @@ describe("<Catalog />", () => {
expect(onRun).toHaveBeenCalled(); expect(onRun).toHaveBeenCalled();
done(); done();
}, 500); }, 500);
return true;
} }
); );
@ -176,7 +173,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
}); });
it("onBeforeRun return false => onRun wont be triggered", (done) => { it("onBeforeRun prevents event => onRun wont be triggered", (done) => {
const catalogCategoryRegistry = new CatalogCategoryRegistry(); const catalogCategoryRegistry = new CatalogCategoryRegistry();
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
@ -193,14 +190,12 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, (e) => {
() => {
setTimeout(() => { setTimeout(() => {
expect(onRun).not.toHaveBeenCalled(); expect(onRun).not.toHaveBeenCalled();
done(); done();
}, 500); }, 500);
e.preventDefault();
return false;
} }
); );
@ -216,7 +211,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
}); });
it("addOnBeforeRun throw an exception => onRun wont be triggered", (done) => { it("addOnBeforeRun throw an exception => onRun will be triggered", (done) => {
const catalogCategoryRegistry = new CatalogCategoryRegistry(); const catalogCategoryRegistry = new CatalogCategoryRegistry();
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
@ -233,10 +228,9 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
() => { () => {
setTimeout(() => { setTimeout(() => {
expect(onRun).not.toHaveBeenCalled(); expect(onRun).toHaveBeenCalled();
done(); done();
}, 500); }, 500);
@ -256,7 +250,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
}); });
it("addOnRunHook return a promise and resolve true => onRun()", (done) => { it("addOnRunHook return a promise and does not prevent run event => onRun()", (done) => {
const catalogCategoryRegistry = new CatalogCategoryRegistry(); const catalogCategoryRegistry = new CatalogCategoryRegistry();
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
@ -273,9 +267,8 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
async () => { async () => {
return true; // no op
} }
); );
@ -291,7 +284,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
}); });
it("addOnRunHook return a promise and resolve false => onRun() wont be triggered", (done) => { it("addOnRunHook return a promise and prevents event wont be triggered", (done) => {
const catalogCategoryRegistry = new CatalogCategoryRegistry(); const catalogCategoryRegistry = new CatalogCategoryRegistry();
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
@ -308,8 +301,7 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, async (e) => {
async () => {
expect(onRun).not.toBeCalled(); expect(onRun).not.toBeCalled();
setTimeout(() => { setTimeout(() => {
@ -317,7 +309,7 @@ describe("<Catalog />", () => {
done(); done();
}, 500); }, 500);
return false; e.preventDefault();
} }
); );
@ -333,7 +325,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
}); });
it("addOnRunHook return a promise and reject => onRun wont be triggered", (done) => { it("addOnRunHook return a promise and reject => onRun will be triggered", (done) => {
const catalogCategoryRegistry = new CatalogCategoryRegistry(); const catalogCategoryRegistry = new CatalogCategoryRegistry();
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
@ -350,10 +342,9 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
async () => { async () => {
setTimeout(() => { setTimeout(() => {
expect(onRun).not.toHaveBeenCalled(); expect(onRun).toHaveBeenCalled();
done(); done();
}, 500); }, 500);