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

@mollahdev/hooks-js

v1.0.0

Published

A simple and efficient EventManager for JavaScript

Downloads

9

Readme

Hooks JS

Available as an NPM Package.

Install with NPM:

npm i @mollahdev/hooks-js

A simple and efficient event manager for JavaScript. This package assumes that your code will run in an ES2015+ environment.

API Usage

  • addAction( 'hookName', callback, priority )
  • doAction( 'hookName', data )
  • removeAction( 'hookName', callback )
  • addFilter( 'hookName', callback, priority )
  • applyFilters( 'hookName', data )
  • removeFilter( 'hookName', callback )

addAction Hook

addAction is the hook that the hooks-js execute when you call the doAction. The callback function of addAction expect some data that will be passed from doAction. The data can be optional.

import { addAction } from '@mollahdev/hooks-js';
// or
import hooks from '@mollahdev/hooks-js';

const callback = function (data) {
    // do something with the data
};
// You can use any hook name you want but that must be a string type
addAction('core/ready', callback);
// or
/*
 * The last parameter is optional, it's default value is 10 (must be number type).
 * It helps you to set priority
 **/
hooks.addAction('core/ready', callback, 50);

doAction Hook

doAction is the hook that the hooks-js use to run all the addActions you used in your application. You can pass any typeof of data you want.

import { doAction } from '@mollahdev/hooks-js';
// or
import hooks from '@mollahdev/hooks-js';

// Use the same hook name that you used for addAction
doAction('core/ready', { user: 'Jhone Doe' });
// or
hooks.doAction('core/ready');

addFilter Hook

addFilter hook allows you to modify the data that has been passed through applyFilters hook. addFilter hooks should be declared before applyFilters. The callback of addFilter receives the data used in applyFilters, the callback must return the data, You can modify the data before returning.

import { addFilter } from '@mollahdev/hooks-js';
// or
import hooks from '@mollahdev/hooks-js';

const callback = (user) => {
    user.hasAccess = false;
    return user;
};
addFilter('namespace/current-user', callback);
// or
hooks.addFilter('namespace.user', callback, 90);

applyFilters Hook

This hook invokes all the callback attached to addFilter hook. From the addFilter hook you can modify the data passing throw applyFilters.

import { applyFilters } from '@mollahdev/hooks-js';
// or
import hooks from '@mollahdev/hooks-js';

const user = applyFilters('namespace/current-user', {
    _id: 'something',
    name: 'Jhone Doe',
});

console.log(user); // { _id: 'something', name: 'Jhone Doe', hasAccess: false }
// or

const anotherUser = hooks.applyFilters('namespace.user', {
    role: 'editor',
});

console.log(anotherUser); // {role: 'editor', hasAccess: false}

removeAction/removeFilter Hook

Remove any added addAction/addFilter with given hook name and same callback

import { removeAction, removeFilter } from '@mollahdev/hooks-js';
// or
import hooks from '@mollahdev/hooks-js';

// Use the same hook name and callback that you used for addAction/addFilter
removeAction('your-hook-name', callback);
removeFilter('your-hook-name', callback);
// or
hooks.removeAction('core/hook-name', callback);
hooks.removeFilter('core/hook-name', callback);