{"name":"hookable","version":"6.1.0","keywords":["hook","hookable","plugin","tapable","tappable"],"license":"MIT","_id":"hookable@6.1.0","maintainers":[{"name":"atinux","email":"atinux@gmail.com"},{"name":"pi0","email":"pyapar@gmail.com"}],"homepage":"https://github.com/unjs/hookable#readme","bugs":{"url":"https://github.com/unjs/hookable/issues"},"dist":{"shasum":"f9eb1c39928b5c6db4e8884155d402420030555e","tarball":"http://registry-npm.ywwl.com/hookable/download/hookable-6.1.0.tgz","fileCount":5,"integrity":"sha512-ZoKZSJgu8voGK2geJS+6YtYjvIzu9AOM/KZXsBxr83uhLL++e9pEv/dlgwgy3dvHg06kTz6JOh1hk3C8Ceiymw==","signatures":[{"sig":"MEQCIAVgPW1VCMfdj/X7qETk4dwLuZ86JdLOCedkCw9t5Hb/AiBTl1ZKj8l15M3RvZJnjV7+LvfdYYY2FZT9VZpCBQGhXw==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":24157,"size":7753,"noattachment":false,"key":"/hookable/-/hookable-6.1.0.tgz"},"main":"./dist/index.mjs","type":"module","types":"./dist/index.d.mts","exports":{".":"./dist/index.mjs"},"gitHead":"4ba437469baa4b60a1914033613f4b9d26ca9a2c","scripts":{"dev":"vitest","lint":"oxlint . && oxfmt --check src test","test":"pnpm lint && vitest run --coverage","bench":"node --expose-gc --allow-natives-syntax test/bench.ts","build":"obuild src/index.ts","release":"pnpm test && pnpm build && changelogen --release --publish --push","lint:fix":"oxlint . --fix && oxfmt src test","prepublish":"pnpm build","test:types":"tsgo --noEmit"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"repository":{"url":"git+https://github.com/unjs/hookable.git","type":"git"},"_npmVersion":"11.6.1","description":"Awaitable hook system","directories":{},"sideEffects":false,"_nodeVersion":"24.10.0","_hasShrinkwrap":false,"packageManager":"pnpm@10.32.1","devDependencies":{"vite":"^8.0.0","oxfmt":"^0.40.0","mitata":"^1.0.34","obuild":"^0.4.32","oxlint":"^1.55.0","vitest":"^4.1.0","esbuild":"^0.27.4","typescript":"^5.9.3","@types/node":"^25.5.0","changelogen":"^0.6.2","expect-type":"^1.3.0","hookable-prev":"npm:hookable@^6.0.1","eslint-config-unjs":"^0.6.2","@vitest/coverage-v8":"^4.1.0","@typescript/native-preview":"7.0.0-dev.20260314.1"},"_npmOperationalInternal":{"tmp":"tmp/hookable_6.1.0_1773563188805_0.7011563805517955","host":"s3://npm-registry-packages-npm-production"},"readme":"# Hookable\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![bundle][bundle-src]][bundle-href]\n[![Codecov][codecov-src]][codecov-href]\n[![License][license-src]][license-href]\n\nAwaitable hooks system.\n\n## Install\n\n```bash\nnpx nypm i hookable\n```\n\n## Usage\n\n**Method A: Create a hookable instance:**\n\n```js\nimport { Hookable } from \"hookable\";\n\n// Create a hookable instance\nconst hooks = new Hookable();\n\n// Hook on 'hello'\nhooks.hook(\"hello\", () => {\n  console.log(\"Hello World\");\n});\n\n// Call 'hello' hook\nhooks.callHook(\"hello\");\n```\n\n> [!TIP]\n> You can use `HookableCore` alternatively for less bundle and runtime footprint if simple `hook`/`callHook` functionality is only needed.\n\n**Method B: Extend your base class from Hookable:**\n\n```js\nimport { Hookable } from \"hookable\";\n\nexport default class FooLib extends Hookable {\n  constructor() {\n    // Call to parent to initialize\n    super();\n    // Initialize Hookable with custom logger\n    // super(consola)\n  }\n\n  async someFunction() {\n    // Call and wait for `hook1` hooks (if any) sequential\n    await this.callHook(\"hook1\");\n  }\n}\n```\n\n**Inside plugins, register for any hook:**\n\n```js\nconst lib = new FooLib();\n\n// Register a handler for `hook2`\nlib.hook(\"hook2\", async () => {\n  /* ... */\n});\n\n// Register multiply handlers at once\nlib.addHooks({\n  hook1: async () => {\n    /* ... */\n  },\n  hook2: [\n    /* can be also an array */\n  ],\n});\n```\n\n**Unregistering hooks:**\n\n```js\nconst lib = new FooLib();\n\nconst hook0 = async () => {\n  /* ... */\n};\nconst hook1 = async () => {\n  /* ... */\n};\nconst hook2 = async () => {\n  /* ... */\n};\n\n// The hook() method returns an \"unregister\" function\nconst unregisterHook0 = lib.hook(\"hook0\", hook0);\nconst unregisterHooks1and2 = lib.addHooks({ hook1, hook2 });\n\n/* ... */\n\nunregisterHook0();\nunregisterHooks1and2();\n\n// or\n\nlib.removeHooks({ hook0, hook1 });\nlib.removeHook(\"hook2\", hook2);\n```\n\n**Triggering a hook handler once:**\n\n```js\nconst lib = new FooLib();\n\nconst unregister = lib.hook(\"hook0\", async () => {\n  // Unregister as soon as the hook is executed\n  unregister();\n\n  /* ... */\n});\n```\n\n## Hookable class\n\n### `constructor()`\n\n### `hook (name, fn)`\n\nRegister a handler for a specific hook. `fn` must be a function.\n\nReturns an `unregister` function that, when called, will remove the registered handler.\n\n### `hookOnce (name, fn)`\n\nSimilar to `hook` but unregisters hook once called.\n\nReturns an `unregister` function that, when called, will remove the registered handler before first call.\n\n### `addHooks(configHooks)`\n\nFlatten and register hooks object.\n\nExample:\n\n```js\nhookable.addHooks({\n  test: {\n    before: () => {},\n    after: () => {},\n  },\n});\n```\n\nThis registers `test:before` and `test:after` hooks at bulk.\n\nReturns an `unregister` function that, when called, will remove all the registered handlers.\n\n### `async callHook (name, ...args)`\n\nUsed by class itself to **sequentially** call handlers of a specific hook.\n\n### `callHookWith (name, callerFn)`\n\nIf you need custom control over how hooks are called, you can provide a custom function that will receive an array of handlers of a specific hook.\n\n`callerFn` if a callback function that accepts 3 arguments, `hooks`, `args` and `name`:\n\n- `hooks`: Array of user hooks to be called\n- `args`: Array of arguments that should be passed each time calling a hook\n- `name`: Name of the hook\n\n### `deprecateHook (old, name)`\n\nDeprecate hook called `old` in favor of `name` hook.\n\n### `deprecateHooks (deprecatedHooks)`\n\nDeprecate all hooks from an object (keys are old and values or newer ones).\n\n### `removeHook (name, fn)`\n\nRemove a particular hook handler, if the `fn` handler is present.\n\n### `removeHooks (configHooks)`\n\nRemove multiple hook handlers.\n\nExample:\n\n```js\nconst handler = async () => {\n  /* ... */\n};\n\nhookable.hook(\"test:before\", handler);\nhookable.addHooks({ test: { after: handler } });\n\n// ...\n\nhookable.removeHooks({\n  test: {\n    before: handler,\n    after: handler,\n  },\n});\n```\n\n### `removeAllHooks`\n\nRemove all hook handlers.\n\n### `beforeEach (syncCallback)`\n\nRegisters a (sync) callback to be called before each hook is being called.\n\n```js\nhookable.beforeEach((event) => {\n  console.log(`${event.name} hook is being called with ${event.args}`);\n});\nhookable.hook(\"test\", () => {\n  console.log(\"running test hook\");\n});\n\n// test hook is being called with []\n// running test hook\nawait hookable.callHook(\"test\");\n```\n\n### `afterEach (syncCallback)`\n\nRegisters a (sync) callback to be called after each hook is being called.\n\n```js\nhookable.afterEach((event) => {\n  console.log(`${event.name} hook called with ${event.args}`);\n});\nhookable.hook(\"test\", () => {\n  console.log(\"running test hook\");\n});\n\n// running test hook\n// test hook called with []\nawait hookable.callHook(\"test\");\n```\n\n### `createDebugger`\n\nAutomatically logs each hook that is called and how long it takes to run.\n\n```js\nconst debug = hookable.createDebugger(hooks, { tag: \"something\" });\n\nhooks.callHook(\"some-hook\", \"some-arg\");\n// [something] some-hook: 0.21ms\n\ndebug.close();\n```\n\n## Migration\n\n### From `4.x` to `5.x`\n\n- Type checking improved. You can use `Hookable<T>` or `createHooks<T>()` to provide types interface **([c2e1e22](https://github.com/unjs/hookable/commit/c2e1e223d16e7bf87117cd8d72ad3ba211a333d8))**\n- We no longer provide an IE11 compatible umd build. Instead, you should use an ESM-aware bundler such as webpack or rollup to transpile if needed.\n- Logger param is dropped. We use `console.warn` by default for deprecated hooks.\n- Package now uses named exports. You should import `{ Hookable }` instead of `Hookable` or use new `createHooks` util\n- `mergeHooks` util is exported standalone. You should replace `Hookable.mergeHooks` and `this.mergeHooks` with new `{ mergeHooks }` export\n- In versions < 5.0.0 when using `callHook` if an error happened by one of the hook callbacks, we was handling errors globally and call global `error` hook + `console.error` instead and resolve `callHook` promise! This sometimes makes confusing behavior when we think code worked but it didn't. v5 introduced a breaking change that when a hook throws an error, `callHook` also rejects instead of a global `error` event. This means you should be careful to handle all errors when using `callHook` now.\n\n## Credits\n\nExtracted from [Nuxt](https://github.com/nuxt/nuxt.js) hooks system originally introduced by [Sébastien Chopin](https://github.com/Atinux)\n\nThanks to [Joe Paice](https://github.com/RGBboy) for donating [hookable](https://www.npmjs.com/package/hookable) package name.\n\n## License\n\nMIT - Made with 💖\n\n<!-- Badges -->\n\n[npm-version-src]: https://img.shields.io/npm/v/hookable?style=flat&colorA=18181B&colorB=F0DB4F\n[npm-version-href]: https://npmjs.com/package/hookable\n[npm-downloads-src]: https://img.shields.io/npm/dm/hookable?style=flat&colorA=18181B&colorB=F0DB4F\n[npm-downloads-href]: https://npmjs.com/package/hookable\n[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/hookable/main?style=flat&colorA=18181B&colorB=F0DB4F\n[codecov-href]: https://codecov.io/gh/unjs/h3\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/hookable?style=flat&colorA=18181B&colorB=F0DB4F\n[bundle-href]: https://bundlephobia.com/result?p=hookable\n[license-src]: https://img.shields.io/github/license/unjs/hookable.svg?style=flat&colorA=18181B&colorB=F0DB4F\n[license-href]: https://github.com/unjs/hookable/blob/main/LICENSE\n","publish_time":1773563188960,"_cnpm_publish_time":1773563188960,"dist-tags":{"latest":"6.1.2","rc":"6.0.0-rc.2"}}