hookified
Event and Middleware Hooks
Last updated 2 years ago by jaredwray .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install hookified 
SYNC missed versions from official npm registry.

site/logo.svg

Event Emitting and Async Middleware Hooks

tests GitHub license codecov npm npm

Features

  • Simple replacement for EventEmitter
  • Async Middleware Hooks for Your Methods
  • ESM / CJS with Types and Nodejs 20+
  • Maintained on a regular basis!

Installation

npm install hookified --save

Usage

This was built because we constantly wanted hooks and events extended on libraires we are building such as Keyv and Cacheable. This is a simple way to add hooks and events to your classes.

import { Hookified } from 'hookified';

class MyClass extends Hookified {
  constructor() {
    super();
  }

  async myMethodEmittingEvent() {
    this.emit('message', 'Hello World'); //using Emittery
  }

  //with hooks you can pass data in and if they are subscribed via onHook they can modify the data
  async myMethodWithHooks() Promise<any> {
    let data = { some: 'data' };
    // do something
    await this.hook('before:myMethod2', data);

    return data;
  }
}

You can even pass in multiple arguments to the hooks:

import { Hookified } from 'hookified';

class MyClass extends Hookified {
  constructor() {
    super();
  }

  async myMethodWithHooks() Promise<any> {
    let data = { some: 'data' };
    let data2 = { some: 'data2' };
    // do something
    await this.hook('before:myMethod2', data, data2);

    return data;
  }
}

Using it in the Browser

<script type="module">
  import { Hookified } from 'https://cdn.jsdelivr.net/npm/hookified/dist/browser/index.js';

  class MyClass extends Hookified {
    constructor() {
      super();
    }

    async myMethodEmittingEvent() {
      this.emit('message', 'Hello World'); //using Emittery
    }

    //with hooks you can pass data in and if they are subscribed via onHook they can modify the data
    async myMethodWithHooks() Promise<any> {
      let data = { some: 'data' };
      // do something
      await this.hook('before:myMethod2', data);

      return data;
    }
  }
</script>

if you are not using ESM modules, you can use the following:

<script src="https://cdn.jsdelivr.net/npm/hookified/dist/browser/index.global.js"></script>
<script>
  class MyClass extends Hookified {
    constructor() {
      super();
    }

    async myMethodEmittingEvent() {
      this.emit('message', 'Hello World'); //using Emittery
    }

    //with hooks you can pass data in and if they are subscribed via onHook they can modify the data
    async myMethodWithHooks() Promise<any> {
      let data = { some: 'data' };
      // do something
      await this.hook('before:myMethod2', data);

      return data;
    }
  }
</script>

API - Hooks

.onHook(eventName, handler)

Subscribe to a hook event.

.removeHook(eventName)

Unsubscribe from a hook event.

.hook(eventName, ...args)

Run a hook event.

.hooks

Get all hooks.

.getHooks(eventName)

Get all hooks for an event.

.clearHooks(eventName)

API - Events

.on(eventName, handler)

Subscribe to an event.

.off(eventName, handler)

Unsubscribe from an event.

.emit(eventName, ...args)

Emit an event.

.listeners(eventName)

Get all listeners for an event.

.removeAllListeners(eventName)

Remove all listeners for an event.

.setMaxListeners(maxListeners: number)

Set the maximum number of listeners and will truncate if there are already too many.

.once(eventName, handler)

Subscribe to an event once.

.prependListener(eventName, handler)

Prepend a listener to an event.

.prependOnceListener(eventName, handler)

Prepend a listener to an event once.

.eventNames()

Get all event names.

.listenerCount(eventName?)

Get the count of listeners for an event or all events if evenName not provided.

.rawListeners(eventName?)

Get all listeners for an event or all events if evenName not provided.

Development and Testing

Hookified is written in TypeScript and tests are written in vitest. To run the tests, use the following command:

To setup the environment and run the tests:

npm i && npm test

To contribute follow the Contributing Guidelines and Code of Conduct.

License

MIT & © Jared Wray

Current Tags

  • 3.1.0                                ...           latest (a month ago)

41 Versions

  • 3.1.0                                ...           a month ago
  • 3.0.4                                ...           2 months ago
  • 3.0.3                                ...           3 months ago
  • 3.0.2                                ...           4 months ago
  • 3.0.1                                ...           5 months ago
  • 3.0.0                                ...           6 months ago
  • 2.2.0                                ...           7 months ago
  • 2.1.1                                ...           8 months ago
  • 2.1.0                                ...           9 months ago
  • 2.0.1                                ...           9 months ago
  • 2.0.0                                ...           9 months ago
  • 1.15.1                                ...           10 months ago
  • 1.15.0                                ...           a year ago
  • 1.14.0                                ...           a year ago
  • 1.13.0                                ...           a year ago
  • 1.12.2                                ...           a year ago
  • 1.12.1                                ...           a year ago
  • 1.12.0                                ...           a year ago
  • 1.11.0                                ...           a year ago
  • 1.10.0                                ...           a year ago
  • 1.9.1                                ...           a year ago
  • 1.9.0                                ...           2 years ago
  • 1.8.2                                ...           2 years ago
  • 1.8.1                                ...           2 years ago
  • 1.8.0                                ...           2 years ago
  • 1.7.1                                ...           2 years ago
  • 1.7.0                                ...           2 years ago
  • 1.6.0                                ...           2 years ago
  • 1.5.1                                ...           2 years ago
  • 1.5.0                                ...           2 years ago
  • 1.4.0                                ...           2 years ago
  • 1.3.0                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.7.1                                ...           2 years ago
  • 0.7.0                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 0.5.1                                ...           2 years ago
  • 0.5.0                                ...           2 years ago
Maintainers (1)
Downloads
Total 107
Today 0
This Week 4
This Month 92
Last Day 0
Last Week 0
Last Month 2
Dependencies (0)
None
Dev Dependencies (7)

© 2010 - cnpmjs.org x YWFE | Home | YWFE