Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mollahdev/hooks-js

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mollahdev/hooks-js

A simple and efficient EventManager for JavaScript

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

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);

Keywords

FAQs

Package last updated on 04 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc