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

@aventum/hooks

v1.0.9

Published

A universal, lightweight & efficient EventManager/PluginsSystem/MiddlewareManager/ExtendabilitySystem for JavaScript

Downloads

40

Readme

Aventum Hooks

A universal, lightweight & efficient EventManager/PluginsSystem/MiddlewareManager/ExtendabilitySystem for JavaScript built on top of the amazing @wordpress/hooks with one extra addition which is adding asynchronous hooks support.

Installation

npm:

npm install @aventum/hooks --save

CDN:

<!-- unpkg -->
<script src="https://unpkg.com/@aventum/hooks@latest"></script>

Usage

ES2015:

import * as AventumHooks from '@aventum/hooks'
var hooks = AventumHooks.createHooks()

Script:

<script src="path/to/aventum-hooks.js"></script>
<script>
  var hooks = AventumHooks.createHooks()
</script>

Node.js:

var AventumHooks = require('@aventum/hooks')
var hooks = AventumHooks.createHooks()

Examples

These examples are taken from src/test/index.manual.test.js file, you can run this file using node ./src/examples/misc.js if you have Node.js installed, there will be other example files worth to check all of them will be able to run using node ./src/examples/[fileName].js.

Example 1

var hooks = AventumHooks.createHooks()

/**
 * Asynchronous Filters
 */
hooks.addFilter(
  'AwesomeFilter',
  'vendor/plugin/function',
  (content, arg1, arg2) => {
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        resolve(content + arg1 + arg2)
      }, 300)
    })
  },
  10
)

hooks.addFilter(
  'AwesomeFilter',
  'vendor/plugin/function',
  (content, arg1, arg2) => {
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        resolve(content + arg1 + arg2)
      }, 300)
    })
  },
  10
)

hooks.addFilter(
  'AwesomeFilter',
  'vendor/plugin/function',
  (content, arg1, arg2) => {
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        resolve(content + arg1 + arg2)
      }, 300)
    })
  },
  10
)

const AsyncFunction = async () => {
  var result = await hooks.applyFilters('AwesomeFilter', 25, 1, 2)
  console.log(result)
}

AsyncFunction()

The result will be:

34

Example 2

var hooks = AventumHooks.createHooks()

/**
 * Asynchronous Actions
 */

hooks.addAction(
  'AwesomeAction',
  'vendor/plugin/function',
  (arg1, arg2, arg3) => {
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        console.log('Action1', arg1, arg2, arg3)
        resolve(arg1)
      }, 300)
    })
  },
  10
)
hooks.addAction(
  'AwesomeAction',
  'vendor/plugin/function',
  (arg1, arg2, arg3) => {
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        console.log('Action2', arg1, arg2, arg3)
        resolve(arg1)
      }, 300)
    })
  },
  10
)

const AsyncFunction = async () => {
  await hooks.doAction('AwesomeAction', 25, 6, 30)
}

AsyncFunction()

The result will be:

Action1 25 6 30
Action2 25 6 30

Example 3

var hooks = AventumHooks.createHooks()

/**
 * Synchronous Actions
 */
hooks.addAction(
  'AwesomeActionSync',
  'vendor2/plugin/function',
  (arg1, arg2) => {
    console.log('AwesomeActionSync1', arg1, arg2)
  },
  10
)
hooks.addAction(
  'AwesomeActionSync',
  'vendor2/plugin/function',
  (arg1, arg2) => {
    console.log('AwesomeActionSync2', arg1, arg2)
  },
  10
)
hooks.addAction(
  'AwesomeActionSync',
  'vendor2/plugin/function',
  (arg1, arg2) => {
    console.log('AwesomeActionSync3', arg1, arg2)
  },
  10
)

hooks.doActionSync('AwesomeActionSync', 10, 20)

The result will be:

AwesomeActionSync1 10 20
AwesomeActionSync2 10 20
AwesomeActionSync3 10 20

Example 4

var hooks = AventumHooks.createHooks()

/**
 * Synchronous Filters
 */
hooks.addFilter(
  'AwesomeFilterSync',
  'vendor2/plugin/function',
  (content, arg1, arg2) => {
    return content + arg1 + arg2
  },
  10
)
hooks.addFilter(
  'AwesomeFilterSync',
  'vendor2/plugin/function',
  (content, arg1, arg2) => {
    return content + arg1 + arg2
  },
  10
)
hooks.addFilter(
  'AwesomeFilterSync',
  'vendor2/plugin/function',
  (content, arg1, arg2) => {
    return content + arg1 + arg2
  },
  10
)

console.log(hooks.applyFiltersSync('AwesomeFilterSync', 5, 1, 2))

The result will be:

14

API Usage

  • createHooks()
  • addAction( 'hookName', 'namespace', callback, priority )
  • addFilter( 'hookName', 'namespace', callback, priority )
  • removeAction( 'hookName', 'namespace' )
  • removeFilter( 'hookName', 'namespace' )
  • removeAllActions( 'hookName' )
  • removeAllFilters( 'hookName' )
  • doAction( 'hookName', arg1, arg2, moreArgs, finalArg )
  • doActionSync( 'hookName', arg1, arg2, moreArgs, finalArg )
  • applyFilters( 'hookName', content, arg1, arg2, moreArgs, finalArg )
  • applyFiltersSync( 'hookName', content, arg1, arg2, moreArgs, finalArg )
  • doingAction( 'hookName' )
  • doingFilter( 'hookName' )
  • didAction( 'hookName' )
  • didFilter( 'hookName' )
  • hasAction( 'hookName' )
  • hasFilter( 'hookName' )
  • actions
  • filters

The namespace is a unique string that can only contain numbers, letters, dashes, periods, underscores and slashes, it used to identify the callback, the best practice to make it in the form vendor/plugin/function

Events on action/filter add or remove

Whenever an action or filter is added or removed, a matching hookAdded or hookRemoved action is triggered.

  • hookAdded action is triggered when addFilter() or addAction() method is called, passing values for hookName, functionName, callback and priority.
  • hookRemoved action is triggered when removeFilter() or removeAction() method is called, passing values for hookName and functionName.

The all hook

In non-minified builds developers can register a filter or action that will be called on all hooks, for example: addAction( 'all', 'namespace', callbackFunction );. Useful for debugging, the code supporting the all hook is stripped from the production code for performance reasons.

Build & Test Using Docker

docker build -t aventum-hooks .

# Test
docker run -it -v /app/node_modules -v $PWD:/app aventum-hooks npm run test

# Build
docker run -it -v /app/node_modules -v $PWD:/app aventum-hooks npm run build