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

Change onBeforeRun hook to function

Signed-off-by: Juho Heikka <juho.heikka@gmail.com>
This commit is contained in:
Juho Heikka 2021-10-07 13:27:44 +03:00
parent ae96ae7d50
commit f7ea0b51db
3 changed files with 11 additions and 32 deletions

View File

@ -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. * @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(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";
@ -33,15 +33,13 @@ import { catalogEntityRunContext } from "./catalog-entity";
export type EntityFilter = (entity: CatalogEntity) => any; export type EntityFilter = (entity: CatalogEntity) => any;
export type CatalogEntityOnBeforeRun = (entity: CatalogEntity) => boolean | Promise<boolean>; export type CatalogEntityOnBeforeRun = (entity: CatalogEntity) => boolean | Promise<boolean>;
type CatalogEntityUid = CatalogEntity["metadata"]["uid"];
export class CatalogEntityRegistry { export class CatalogEntityRegistry {
@observable protected activeEntityId: string | undefined = undefined; @observable protected activeEntityId: string | undefined = undefined;
protected _entities = observable.map<string, CatalogEntity>([], { deep: true }); protected _entities = observable.map<string, CatalogEntity>([], { deep: true });
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,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 * 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(); return once(() => void this.onBeforeRunHooks.delete(onBeforeRun));
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));
} }
/** /**
@ -205,14 +196,8 @@ export class CatalogEntityRegistry {
*/ */
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());
if (!hooks) { for (const onBeforeRun of this.onBeforeRunHooks) {
return true;
}
for (const onBeforeRun of hooks) {
try { try {
if (!await onBeforeRun(entity)) { if (!await onBeforeRun(entity)) {
return false; return false;

View File

@ -129,7 +129,6 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
(entity) => { (entity) => {
expect(entity).toMatchInlineSnapshot(` expect(entity).toMatchInlineSnapshot(`
Object { Object {
@ -193,7 +192,6 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
() => { () => {
setTimeout(() => { setTimeout(() => {
expect(onRun).not.toHaveBeenCalled(); expect(onRun).not.toHaveBeenCalled();
@ -233,7 +231,6 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
() => { () => {
setTimeout(() => { setTimeout(() => {
expect(onRun).not.toHaveBeenCalled(); expect(onRun).not.toHaveBeenCalled();
@ -273,7 +270,6 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
async () => { async () => {
return true; return true;
} }
@ -308,7 +304,6 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
async () => { async () => {
expect(onRun).not.toBeCalled(); expect(onRun).not.toBeCalled();
@ -350,7 +345,6 @@ describe("<Catalog />", () => {
.mockImplementation(() => catalogEntityItem); .mockImplementation(() => catalogEntityItem);
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
async () => { async () => {
setTimeout(() => { setTimeout(() => {
expect(onRun).not.toHaveBeenCalled(); expect(onRun).not.toHaveBeenCalled();