
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@mollahdev/hooks-js
Advanced tools
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.
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 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 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);
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}
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);
FAQs
A simple and efficient EventManager for JavaScript
We found that @mollahdev/hooks-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.