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

mhook

v1.0.1

Published

Middleware like hooks for node.js

Downloads

7,543

Readme

mhook

Middleware like hooks for node.js which is useful for building some relations between entities (ODM, ORM, etc).

Build Status

Installation

npm install mhook

Usage

Define your actions, bind hooks on them, trigger actions. When you trigger an action all binded hooks will be sequentially executed. Be careful, hook function should return promise or call provided callback, otherwise trigger will hang.


var Hook = require('mhook').Hook;


var hook = new Hook(['beforeUpdate', 'afterUpdate', 'afterRemove']);

// add hook with callback
hook.on('beforeUpdate', function(done) {
	// do something, and then say that you done,
	// pass error as first argument to done
	done();
});

// add another hook with promise
hook.on('beforeUpdate', function() {
	return Promise.resolve();
});

// trigger `action` - executes all two hooks
hook.trigger('beforeUpdate', [], function(err) {
	// this function will be called after all
	// hooks done or one of them fail
});

// trigger also return promise
hook.trigger('beforeUpdate', []).then(function(err) {
	// all hooks successfully done
}).catch(function(err) {
	// one of them fail
});

for getting on and trigger methods to your object you can inherits from Hook


var Hook = require('mhook').Hook,
	inherits = require('util').inherits;


function Model() {
	// apply parent cunstructor
	Hook.call(this, ['beforeUpdate', 'afterUpdate', 'afterRemove']);
}

// inherits from Hook
inherits(Model, Hook);

// now we can use `on` and `trigger` as own methods
Model.prototype.update = function(obj, callback) {
	this.trigger('beforeUpdate', [obj], function(err) {
		if (err) {callback(err); return}
		// update
		// trigger afterUpdate, etc
	});
};


var model = new Model();

model.on('beforeUpdate', function(obj, done) {
	console.log('before update object: ', obj);
	done();
});

Api

Hook()

Hook constructor

accepts array of string actions - possible actions which could be used at on and trigger

Hook.on(action:String, hook:Function)

Bind hook on action

Hook.trigger(action:String, hookArgs:Array, [callback]:Function)

Trigger some action with hookArgs (arguments which will be passed to every hook function). If callback (accepts error as first argument) function presents it will be called after hooks execution. Return promise which will be resolved after hooks execution.

Running test

Into cloned repository run npm run test