Skip to content

Commit

Permalink
refactor: add debug calls
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 15, 2023
1 parent 1787dbc commit d33e4b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"prepublishOnly": "npm run build",
"lint": "eslint . --ext=.ts",
"sync-labels": "github-label-sync --labels .github/labels.json poppinss/hooks",
"quick:test": "node --loader=ts-node/esm bin/test.ts"
"quick:test": "cross-env NODE_DEBUG=poppinss:hooks node --loader=ts-node/esm bin/test.ts"
},
"keywords": [
"hooks",
Expand All @@ -50,6 +50,7 @@
"@swc/core": "^1.3.100",
"@types/node": "^20.10.4",
"c8": "^8.0.1",
"cross-env": "^7.0.3",
"del-cli": "^5.1.0",
"eslint": "^8.55.0",
"github-label-sync": "^2.3.1",
Expand Down
15 changes: 15 additions & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
* file that was distributed with this source code.
*/

import { debuglog } from 'node:util'
import { HookHandler, CleanupHandler, HookHandlerProvider } from './types.js'

const debug = debuglog('poppinss:hooks')

/**
* Runner allows running a set of specific hook handlers for a given
* event. You can grab the instance of the runner using the "hook.runner" method.
Expand Down Expand Up @@ -79,9 +82,11 @@ export class Runner<HookArgs extends any[], CleanUpArgs extends any[]> {
*/
without(handlersToIgnore?: string[]): this {
if (!handlersToIgnore) {
debug('skipping all hooks')
this.#skipAllHooks = true
} else {
this.#skipAllHooks = false
debug('skipping %O hooks', handlersToIgnore)
this.#handlersToIgnore = handlersToIgnore
}

Expand All @@ -101,14 +106,23 @@ export class Runner<HookArgs extends any[], CleanUpArgs extends any[]> {
return
}

debug('running hooks')

const handlers = reverse ? Array.from(this.#hookHandlers).reverse() : this.#hookHandlers
for (let handler of handlers) {
if (this.#filter(handler.name)) {
if (handler.name) {
debug('running hook %s', handler.name)
}

const result = await (typeof handler === 'function'
? handler(...data)
: handler.handle(this.action, ...data))

if (typeof result === 'function') {
if (handler.name) {
debug('cleanup scheduled by %s hook', handler.name)
}
this.#cleanupHandlers.push(result)
}
}
Expand Down Expand Up @@ -138,6 +152,7 @@ export class Runner<HookArgs extends any[], CleanUpArgs extends any[]> {
}

this.#state = 'cleanup_initiated'
debug('performing cleanup')

let startIndex = this.#cleanupHandlers.length
while (startIndex--) {
Expand Down

0 comments on commit d33e4b9

Please sign in to comment.