From 92842efb6b0fe7658ceaccb00ad46d2696cd4c46 Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Sat, 18 Jul 2020 16:44:50 +0530 Subject: [PATCH] refactor: improve code structure --- npm-audit.html | 2 +- src/Hooks/index.ts | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/npm-audit.html b/npm-audit.html index 68d0001..bd7f03f 100644 --- a/npm-audit.html +++ b/npm-audit.html @@ -55,7 +55,7 @@
- July 18th 2020, 10:34:55 am + July 18th 2020, 11:14:52 am

Last updated

diff --git a/src/Hooks/index.ts b/src/Hooks/index.ts index 7935bea..e04ef99 100644 --- a/src/Hooks/index.ts +++ b/src/Hooks/index.ts @@ -52,6 +52,13 @@ export class Hooks { return handler } + /** + * Returns handlers set for a given action or undefined + */ + private getActionHandlers(lifecycle: 'before' | 'after', action: string) { + return this.hooks[lifecycle].get(action) + } + /** * Adds the resolved handler to the actions set */ @@ -60,7 +67,7 @@ export class Hooks { action: string, handler: HooksHandler | IocResolverLookupNode ) { - const handlers = this.hooks[lifecycle].get(action) + const handlers = this.getActionHandlers(lifecycle, action) if (handlers) { handlers.add(handler) @@ -77,7 +84,7 @@ export class Hooks { action: string, handler: HooksHandler | string ): boolean { - const handlers = this.hooks[lifecycle].get(action) + const handlers = this.getActionHandlers(lifecycle, action) if (!handlers) { return false } @@ -101,7 +108,7 @@ export class Hooks { action: string, handler: HooksHandler | string ): void { - const handlers = this.hooks[lifecycle].get(action) + const handlers = this.getActionHandlers(lifecycle, action) if (!handlers) { return } @@ -110,7 +117,8 @@ export class Hooks { } /** - * Remove a pre-registered handler + * Remove all handlers for a given action or lifecycle. If action is not + * defined, then all actions for that given lifecycle are removed */ public clear(lifecycle: 'before' | 'after', action?: string): void { if (!action) { @@ -143,7 +151,7 @@ export class Hooks { * Executes the hook handler for a given action and lifecycle */ public async exec(lifecycle: 'before' | 'after', action: string, ...data: any[]): Promise { - const handlers = this.hooks[lifecycle].get(action) + const handlers = this.getActionHandlers(lifecycle, action) if (!handlers) { return } @@ -152,7 +160,6 @@ export class Hooks { if (typeof handler === 'function') { await handler(...data) } else { - this.ensureResolver() await this.resolver!.call(handler, undefined, data) } }