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

hookall

v2.1.2

Published

Enhance your program's strength and flexibility by seamlessly hooking into the operation.

Downloads

59

Readme

Hookall

Node.js workflow

Enhance your program's strength and flexibility by seamlessly hooking into the operation.

import { useHookall } from 'hookall'

const hook = useHookall(yourObject)

hook.onBefore('run', async (arr) => {
  arr.push(2)
  return arr
})

hook.onAfter('run', async (arr) => {
  arr.push(4)
  return arr
})

const initial = [1]
const arr = await hook.trigger('run', initial, (arr) => {
  arr.push(3)
  return arr
}) // console: [1, 2, 3, 4]

Attention!
Ver.2 has many differences compared to Ver.1, especially in the preprocessing and post-processing steps. Please refer to the documentation.

Why use Hookall?

Strict type definition with typescript

If you want to support strict type definitions with typescript, you can use the following syntax.

import { writeFile } from 'fs/promises'
import { useHookall, IHookall } from 'hookall'

class FileBuilder {
  private _name: string

  constructor() {
    this._name = ''
  }

  setName(name: string): void {
    this._name = name
    return this
  }

  async make(): Promise<string> {
    const filePath = this._dir+this._name
    const hook = useHookall<Hook>(this)

    return await hook.trigger('make', filePath, async (filePath) => {
      return await writeFile(filePath)
    })
  }
}

type Hook = {
  make: (filePath: string) => Promise<string>
}

const builder = new FileBuilder()
const backupHook = useHookall<Hook>(builder)

hook.onBefore('make', async (filePath) => {
  return filePath.replace('.txt', '.json')
})
hook.onAfter('make', async (filePath) => {
  console.log('file created!')
  return filePath
})

const filePath = await builder.setName('my-file.txt').make()
console.log(filePath) // my-file.json

Work asynchronously

hookall library supports asynchronous.

hook.on('create', async (el) => {
  await doSomething(el)
  return el
})

hook.on('create', async (el) => {
  await doSomethingAnother(el)
  return el
})

console.log('create!')
await hook.trigger('create', element)
console.log('done!')

Data hooking using a life cycle

You can hook into the process using the onBefore and onAfter methods.

const hook = useHookall(someObject)

hook.onBefore('create', async (data) => {
  if (!data) {
    throw new Error('There is no initialization data.')
  }
  return data
})

hook.onAfter('create', async (data) => {
  // ...
})

const initialData = await getFromRemote() // get a null
const err = await hook.trigger('create', initialData, async (initialData) => {
  await doJob(initialData)
  return initialData
}) // Error! There is no initialization data.

How to use

Node.js (cjs)

npm i hookall
import { useHookall } from 'hookall'

Browser (esm)

<script type="module">
  import { useHookall } from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/esm/index.min.js'
</script>

Methods

useHookall (target: object|undefined)

Create hook system. you can pass a target object or undefined. If you pass a object, the hook system will be work for object locally. You're going to want this kind of usage in general.

import { useHookall } from 'hookall'

const element = document.querySelector('your-selector')
const hook = useHookall(element)

hook.onBefore('create', async () => { ... })

If not specified, will be work for global. This is useful when you want to share your work with multiple files.

import { useHookall } from 'hookall'

// file A.ts
const globalHook = useHookall()
globalHook.onBefore('from-B', async (now) => { ... })

// file B.ts
const globalHook = useHookall()
await globalHook.trigger('from-B', Date.now(), () => {
  // ...
})

If you want to use not async, you can use useHookallSync.

import { useHookallSync } from 'hookall'

onBefore (command: string, callback: Function): this

You register a preprocessing function, which is called before the callback function of the trigger method.

The value returned by this function is passed as a parameter to the trigger method's callback function. If you register multiple preprocessing functions, they are executed in order, with each function receiving the value returned by the previous one as a parameter.

onceBefore (command: string, callback: Function): this

Similar to the onBefore method, but it only runs once. For more details, please refer to the onBefore method.

onAfter (command: string, callback: Function): this

You register a post-processing function which is called after the callback function of the trigger method finishes.

This function receives the value returned by the trigger method's callback function as a parameter. If you register multiple post-processing functions, they are executed in order, with each function receiving the value returned by the previous one as a parameter.

onceAfter (command: string, callback: Function): this

Similar to the onAfter method, but it only runs once. For more details, please refer to the onAfter method.

offBefore (command: string, callback?: Function): this

You remove the preprocessing functions registered with onBefore or onceBefore methods.
If you don't specify a callback parameter, it removes all preprocessing functions registered for that command.

offAfter (command: string, callback?: Function): this

You remove the post-preprocessing functions registered with onAfter or onceAfter methods.
If you don't specify a callback parameter, it removes all post-preprocessing functions registered for that command.

trigger (command: string, initialValue: any, callback: Function): Promise<any>

You execute the callback function provided as a parameter. This callback function receives the initialValue parameter.

If preprocessing functions are registered, they run first, and the value returned by the preprocessing functions becomes the initialValue parameter.

After the callback function finishes, post-processing functions are called. These post-processing functions receive the value returned by the callback function as a parameter and run sequentially.

The final value returned becomes the result of the trigger method.

License

MIT License