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.
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
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!')
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.
npm i hookall
import { useHookall } from 'hookall'
<script type="module">
import { useHookall } from 'https://cdn.jsdelivr.net/npm/hookall@2.x.x/dist/esm/index.min.js'
</script>
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'
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.
Similar to the onBefore
method, but it only runs once.
For more details, please refer to the onBefore
method.
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.
Similar to the onAfter
method, but it only runs once.
For more details, please refer to the onAfter
method.
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.
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.
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.
MIT License