An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/kaelzhang/progress-hooks.svg?branch=master)](https://travis-ci.org/kaelzhang/progress-hooks)
[![Coverage](https://codecov.io/gh/kaelzhang/progress-hooks/branch/master/graph/badge.svg)](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 applied

car.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