npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hook-master

v1.0.3

Published

Create, remove and trigger synchronous or asynchronous events easily. No dependencies. Less than 100 lines.

Downloads

7

Readme

HookMaster

Create, remove and trigger synchronous or asynchronous events easily. No dependencies. Less than 100 lines.

Why?

Mainly, HookMaster was created to build:

  • events that are declared independently one of each other
  • events that are sync or asynchronously chainable
  • events that can be sorted with custom criterias
  • events that accept common parameters
  • events that pass one to each other some value
  • events that finally return a result

Install

~$ npm install hook-master

Usage

const HookMaster = require("hook-master");
const hook = HookMaster.create({ // This is the default parameter, so you can skip and use simply HookMaster.create()
	sorter: function(a, b) {
	 return a.HOOK_MASTER_METADATA.order >= b.HOOK_MASTER_METADATA.order ? 1 : -1;
	}
});

// Asynchronous event lastly executed by the hook "hello" (because the order is 40):
hook.add("hello", function(result, parameters) {
 return new Promise(function(resolve, reject) {
   return resolve(result + "!");
 });
}, { order: 40 });

// Synchronous event firstly executed by the hook "hello" (because the order is 20):
hook.add("hello", function(result, parameters) {
 return result + "Hell";
}, { order: 20 });

// Synchronous event executed in the second position by the hook "hello" (because the order is 30):
hook.add("hello", function(result, parameters) {
 return result + "o World";
}, { order: 30 });

// Execution of the event calling `hook.trigger(hook name, initial result, ...parameters)`:
hook.trigger("hello", "", []).then((message) => {
 expect(message).to.equal("Hello World!");
});

// In async/await context, you can simply do:
// const message = await hook.trigger("hello", "");

hook.add("bye", function(result, parameters) {
 expect(result).to.equal("Hello World!");
 return result + " Good bye";
});

hook.add("bye", function(result, parameters) {
 expect(result).to.equal("Hello World! Good bye");
 return result + " World!";
});

hook.trigger(["hello", "bye"], "", []).then((message) => {
 expect(message).to.equal("Hello World! Good bye World!");
 return doneTest();
}).catch(console.log);

Take a look to the tests to see a full demo of the API.

API Reference

HookMaster = require("hook-master");

Name: HookMaster

Description: Master class of the API.

Type: class


HookMaster.create(...args)

Name: HookMaster.create

Description: Instantiantes a new HookMaster instance.

Type: static method

Parameter: ...args:Any. Parameters passed to the constructor of the class.

Return: HookMaster:Object. The new HookMaster instance created.


HookMaster.DEFAULT_OPTIONS

Name: HookMaster.DEFAULT_OPTIONS

Description: Object that has the default options that a new HookMaster will take by default.

Type: Object.

Default value: {sorter:function(...) {...}}. The sorter function is responsible of sorting the values when a hook name is triggered.


hookMaster = new HookMaster(options = {})

Name: HookMaster constructor

Description: The constructor method of the HookMaster class. This class can hold an entire set of hooks, each of them identified by an exclusive name, and provided with its own set of events.

Type: constructor method.

Parameter: options:Object. Optional. Options to be passed to the current HookMaster instance.

Return: HookMaster. Returns a new HookMaster instance.


hookMaster.initialize(name)

Name: hookMaster.initialize

Description: Initializes a new hook in the HookMaster instance (by a name).

Type: instance method

Parameter: name:String | names:Array<String>. Name(s) of the new hook to be initialized.

Return: undefined. Nothing.


hookMaster.add(name, event, meta = {})

Name: hookMaster.add

Description: Adds a new hook (event) to the HookMaster instance (by a name) assigning its own metadata (by meta).

Type: instance method

Parameter: name:String. Name of the hook into which the event is going to be added.

Parameter: event:Function. Function that is the event added to the specified hook. This function:

  • receives: 2 parameters.
    • result: the value passed as initialResult by the trigger method, or the result returned by the previous event of this hook.
    • ...parameters (any number of them): the parameters passed through the trigger method.
  • must return one of these:
    • undefined or nothing: this means that the previous result or the initialResult is maintained for the next event call.
    • any value: this option will alter the result received by the next event of the same hook, or the value returned finally by the trigger method (asynchronously, of course).
    • Promise: this option will force the trigger method to resolve the Promise returned by the function, and find out the value that the event is trying to pass to the next event in the chain of events.

Parameter: meta:Object. Optional. Metadata object for the current event. This object is statically added to the event function through its HOOK_MASTER_METADATA property. This data can be useful to remove items by identifiers or other metadata properties, for example.

Return: undefined. Nothing.


hookMaster.remove(name, filter = undefined)

Name: hookMaster.remove

Description: Removes a whole hook or the filtered events of a hook.

Type: instance method

Parameter: name:String. Name of the hook to be removed.

Parameter: filter:Function. Optional. Name of the hook.

Return: undefined. Nothing.


hookMaster.trigger(name, initialResult = undefined, ...parameters)

Name: hookMaster.trigger

Description: Triggers a specific hook. It can pass parameters (which will be shared by all of the events) and an initial result (which will be altered in each event, unless the event returns undefined, or nothing, in which case the result will be maintained).

Type: instance method

Parameter: name:String|Array<String>. Name(s) of the hook(s) to be triggered.

Parameter: initialResult:Any. Result that will be passed through all the events of the hook, allowing a decorator design pattern in every hook.

Parameter: ...parameters:Any. Parameters that all the events of the hook will receive.

Return: result:Promise. Use the then of this Promise to access to the final result of the chained events of the hook. You can use the catch method too, as usual in Promises.

Tests

~$ npm run test

Code coverage

~$ npm run coverage

Document

~$ npm run docs

Conclusion

Simple library to create easily sync/async systems of hooks. It can be useful if you have in mind something pluggable.