https://github.com/kaelzhang/progress-hooks
The manager of sequential hooks to work with tapable
https://github.com/kaelzhang/progress-hooks
tapable
Last synced: 2 months ago
JSON representation
The manager of sequential hooks to work with tapable
- Host: GitHub
- URL: https://github.com/kaelzhang/progress-hooks
- Owner: kaelzhang
- License: other
- Created: 2019-04-08T07:46:29.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-11T08:14:03.000Z (about 6 years ago)
- Last Synced: 2025-03-25T13:16:18.720Z (2 months ago)
- Topics: tapable
- Language: JavaScript
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/kaelzhang/progress-hooks)
[](https://codecov.io/gh/kaelzhang/progress-hooks)# progress-hooks
The manager of sequential hooks to work with [`tapable`](https://www.npmjs.com/package/tapable).
`progress-hooks` applies the taps(plugins) only if the previous hook has been called.
Usually, it used to replace the code slice `this.hooks = {}` of the `tapable` example
## Install
```sh
$ npm i progress-hooks
```## Usage
```js
const {
SyncHook,
AsyncParallelHook
} = require('tapable')
const {
Hooks,
ADD,
CLEAN,
COUNT
} = require('progress-hooks')class Car {
constructor () {
// this.hooks = {
// accelerate: new SyncHook(['newSpeed']),
// brake: new SyncHook()
// }// Instead of the code above, we create hooks by `new Hooks()`
this.hooks = new Hooks({
accelerate: new SyncHook(['newSpeed']),
brake: {
hook: new SyncHook(),
// The car needs to brake twice, then stop
plan: 2
},
stop: new SyncHook()
})
}
}const car = new Car()
let speed = 0// The `LoggerPlugin` method is not actually tapped into the `car.hooks.brake`,
// but instead, it is held by `progress-hooks`
car.hooks.brake.tap('LoggerPlugin', () => {
if (speed === 0) {
throw new Error('can not brake')
}console.log('brake')
})car.hooks.stop.tap('StopEnginePlugin', () => {
console.log('stopped')
})// And it will not be called
car.hooks.brake.call()car.hooks.accelerate.tap('SetterPlugin', newSpeed => {
speed = newSpeed
})car.hook.accelerate.call(120)
// And after `car.hook.accelerate.call()` is invoked,
// The `LoggerPlugin` will be appliedcar.hooks.brake.call()
// prints: 'brake'car.hooks.stop.call()
// nothing `console.log`ged, because of `plan: 2`car.hooks.brake.call()
car.hooks.stop.call()
// prints: stopped
```## new Hooks(rawHooks, options)
- **rawHooks** `{[string]: tapable.Hook | PlannedHook}`
- **options?** `Object`
- **disableAfterCalled?** `boolean=true` If `true`(the default value) the hook will be disabled after called.```ts
interface PlannedHook {
hook: tapable.Hook
// If plan is `2`, then the next hook will not be activated
// until the current hook has been called twice
plan: number
}
```Returns `hooks`
The returned `hooks` is a relatively clean object that we can get all hook names just with `Object.keys(hooks)`:
```js
Object.keys(car.hooks)
// ['accelerate', 'brake']
```### `hooks[ADD](name, hook): void`
Adds a new hook.
```js
const hooks = new Hooks()hooks[ADD]('accelerate', new SyncHook(['newSpeed']))
```### `hooks[CLEAN](): void`
Cleans hook taps if the hook is not enabled, so that we could reload plugins.
## License
MIT