@vzt7/unhook
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

unhook

npm version npm downloads Github Actions Codecov

@vzt7/unhook is an alternative to tapable.

🦭 Why

  • ✅ Get all returns of each hook.
  • ✅ Types friendly, specify the hook type you want.
  • ✅ Extends the basic hook to customize a hook you need.

📦 Installation

# with pnpm
pnpm install @vzt7/unhook

# with yarn
yarn add @vzt7/unhook

# with npm
npm install @vzt7/unhook

⚡️ Usage

The following hooks you can use.

  • Hook (Abstract)
  • SyncHook
  • SyncBailHook
  • SyncLoopHook
  • SyncWaterfallHook
  • AsyncParallelHook
  • AsyncParallelBailHook
  • AsyncSeriesHook
  • AsyncSeriesBailHook
  • AsyncSeriesLoopHook
  • AsyncSeriesWaterfallHook
import { AsyncSeriesHook } from '@vzt7/unhook';

const hook = new AsyncSeriesHook();

hook.tap('say', () => {
  console.log('Hello World');
});

hook.tap('scream', () => {
  console.log('Hello World!!!');
});

hook.dispatch();

// Hello World
// Hello World!!!

All hooks follow the usage below, only the dispatch method of each hook is different implementation.

import { AsyncSeriesHook } from '@vzt7/unhook';

const hook = new AsyncSeriesHook<(arg0: string) => string>();

hook.tap({ name: 'say', once: true }, (arg0) => {
  console.log('Hello World');
});

hook.tap('scream', (arg0) => {
  console.log('Hello World!!!');
  return 'screaming';
});

hook.tap({ name: 'smile', stage: -10 }, async (arg0) => {
  return 'smiling';
});

hook.tap({ name: 'silent', before: 'scream' }, (arg0) => {
  console.log(arg0);
});

hook.dispatch('Anyone else').then((result) => {
  console.log(result); // ['smiling', undefined, undefined, 'screaming'];
});

// Hello World
// Anyone else
//
// Hello World!!!
// ['smiling', undefined, undefined, 'screaming'];

Extends the basic hook to create your own.

import { Hook } from '@vzt7/unhook';

class CustomHook<Fn extends (...args: any[]) => any> extends Hook<Fn> {
  dispatch(...args: any) {
    return Promise.all<ReturnType<Fn>>(this.taps.map(({ fn }) => fn(...args)));
  }
}

const hook = new CustomHook();

hook.tap('foo', () => {
  return 'foo results';
});

const result = await hook.dispatch(); // ['foo results'];

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Published under WTFPL.

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                   Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

 0. You just DO WHAT THE FUCK YOU WANT TO.

Package Sidebar

Install

npm i @vzt7/unhook

Weekly Downloads

3

Version

0.2.0

License

WTFPL

Unpacked Size

29.5 kB

Total Files

9

Last publish

Collaborators

  • vzt6