Skip to content

Commit

Permalink
refactor: improve code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 18, 2020
1 parent fa08035 commit 92842ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion npm-audit.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h5 class="card-title">
<div class="card">
<div class="card-body">
<h5 class="card-title">
July 18th 2020, 10:34:55 am
July 18th 2020, 11:14:52 am
</h5>
<p class="card-text">Last updated</p>
</div>
Expand Down
19 changes: 13 additions & 6 deletions src/Hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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<void> {
const handlers = this.hooks[lifecycle].get(action)
const handlers = this.getActionHandlers(lifecycle, action)
if (!handlers) {
return
}
Expand All @@ -152,7 +160,6 @@ export class Hooks {
if (typeof handler === 'function') {
await handler(...data)
} else {
this.ensureResolver()
await this.resolver!.call(handler, undefined, data)
}
}
Expand Down

0 comments on commit 92842ef

Please sign in to comment.