Skip to content

Commit

Permalink
feat: rewrite for 1.0.0
Browse files Browse the repository at this point in the history
BREAKING CHANGE: api change
  • Loading branch information
pooya parsa committed Feb 11, 2019
1 parent a947b64 commit 88decae
Show file tree
Hide file tree
Showing 13 changed files with 3,117 additions and 2,750 deletions.
7 changes: 0 additions & 7 deletions .babelrc

This file was deleted.

1 change: 1 addition & 0 deletions .eslintrc.yml
@@ -0,0 +1 @@
extends: standard
51 changes: 40 additions & 11 deletions README.md
Expand Up @@ -6,7 +6,7 @@
[![npm](https://img.shields.io/npm/dt/hable.svg?style=flat-square)](https://www.npmjs.com/package/hable)
[![size](http://img.badgesize.io/https://unpkg.com/hable/dist/hable.cjs.min.js?compression=gzip&style=flat-square)](https://unpkg.com/hable)

> A simpler tapable alternative, which can be used to create hooks for plugins.
> Awaitable hooks for Node.js
## Install

Expand Down Expand Up @@ -39,8 +39,8 @@ export default class Foo extends Hookable {
// Call and wait for `hook1` hooks (if any) sequential
await this.callHook('hook1')

// Call and wait for `hook2` hooks (if any) in paraller
await this.callHookAsync('hook2')
// Call and wait for `hook2` hooks (if any) in parallel
await this.callHookParallel('hook2')
}
}
```
Expand All @@ -62,19 +62,48 @@ lib.hookObj({

## Hookable class

**private functions**

* `async callHook(name, ...args)`: Used by class itself to **sequentially** call handlers of a specific hook.
* `async callHookAsync(name, ...args)`: Same as `callHook` but calls handlers in **parallel**.
### `hook (name, fn)`

**public functions**
Register a handler for a specific hook. `fn` can be a single function or an array.

* `hook(name, fn)`: Used by plugins to register a handler for an specific hook. `fn` can be a single function or an array.
* `hookObj(hooksObj)`: Register many hooks using an object.
### `addHooks(configHooks)`

**private attributes**
Flatten and register hooks object.

* `$hooks`: An object which maps from each hook name to it's handlers.
Example:

```js
hookable.addHooks({
test: {
before: () => {},
after: () => {}
}
})

```

This registers `test:before` and `test:after` hooks at bulk.

### `async callHook (name, ...args)`

Used by class itself to **sequentially** call handlers of a specific hook.

### `deprecateHook (old, name)`

Deprecate hook called `old` in flavor of `name` hook.

### `clearHook (name)`

Clear all hooks for a specific hook.

### `clearHooks ()`

Clear all hooks registered in the class.

### `flatHooks (hooksObj)`

Register many hooks using an object.

## Credits

Expand Down
7 changes: 7 additions & 0 deletions jest.config.js
@@ -0,0 +1,7 @@
module.exports = {
'testEnvironment': 'node',
'collectCoverage': true,
'collectCoverageFrom': [
'lib/**/*.js'
]
}
60 changes: 60 additions & 0 deletions lib/hookable.js
@@ -0,0 +1,60 @@
const { serial } = require('items-promise')
const consola = require('consola')
const { flatHooks } = require('./utils')

module.exports = class Hookable {
constructor () {
this._hooks = {}
this._deprecatedHooks = {}

this.hook = this.hook.bind(this)
this.callHook = this.callHook.bind(this)
}

hook (name, fn) {
if (!name || typeof fn !== 'function') {
return
}

if (this._deprecatedHooks[name]) {
consola.warn(`${name} hook has been deprecated, please use ${this._deprecatedHooks[name]}`)
name = this._deprecatedHooks[name]
}

this._hooks[name] = this._hooks[name] || []
this._hooks[name].push(fn)
}

deprecateHook (old, name) {
this._deprecatedHooks[old] = name
}

addHooks (configHooks) {
const hooks = flatHooks(configHooks)
for (const key in hooks) {
this.hook(key, hooks[key])
}
}

async callHook (name, ...args) {
if (!this._hooks[name]) {
return
}
try {
await serial(this._hooks[name], fn => fn(...args))
} catch (err) {
name !== 'error' && await this.callHook('error', err)
consola.error(err)
}
}

clearHook (name) {
if (name) {
delete this._hooks[name]
}
}

clearHooks () {
this._hooks = {}
}
}
12 changes: 12 additions & 0 deletions lib/utils.js
@@ -0,0 +1,12 @@
exports.flatHooks = function flatHooks (configHooks, hooks = {}, parentName) {
for (const key in configHooks) {
const subHook = configHooks[key]
const name = parentName ? `${parentName}:${key}` : key
if (typeof subHook === 'object' && subHook !== null) {
flatHooks(subHook, hooks, name)
} else {
hooks[name] = subHook
}
}
return hooks
}
44 changes: 26 additions & 18 deletions package.json
@@ -1,16 +1,20 @@
{
"name": "hable",
"version": "0.0.7",
"description": "A simpler tapable alternative, which can be used to create hooks for plugins",
"author": "Pooya Parsa <pooya@pi0.ir>",
"description": "Awaitable hooks for Node.js",
"cintributors": [
"Sebastien Chopin (@Atinux)",
"Clark Du (@clarkdo)",
"Pooya Parsa <pooya@pi0.ir>"
],
"license": "MIT",
"homepage": "https://github.com/pi0/hable#readme",
"homepage": "https://github.com/jsless/hable#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/pi0/hable.git"
"url": "git+https://github.com/jsless/hable.git"
},
"bugs": {
"url": "https://github.com/pi0/hable/issues"
"url": "https://github.com/jsless/hable/issues"
},
"keywords": [
"tapable",
Expand All @@ -19,26 +23,30 @@
"plugin",
"hookable"
],
"main": "dist/hable.cjs.js",
"module": "dist/hable.es.js",
"jsnext:main": "dist/hable.es.js",
"main": "lib/hable.js",
"files": [
"lib"
],
"scripts": {
"build": "bili --format es,cjs,cjs-min --inline",
"test": "jest",
"dev": "npm run build --watch",
"release": "npm run build && standard-version",
"release2": "git push --follow-tags && npm publish"
"test": "yarn lint && jest",
"lint": "eslint",
"prepublish": "yarn test",
"release": "standard-version && git push --follow-tags && npm publish"
},
"dependencies": {
"consola": "^2.4.0",
"items-promise": "^1.0.0"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-jest": "^22.1.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"bili": "^2.2.3",
"codecov": "^3.0.0",
"jest": "^22.1.4",
"eslint": "^5.13.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jest": "^22.2.2",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.1.0",
"standard-version": "^4.3.0"
}
}
11 changes: 11 additions & 0 deletions renovate.json
@@ -0,0 +1,11 @@
{
"extends": [
"@nuxtjs"
],
"baseBranches": [
"dev"
],
"ignoreDeps": [
"thread-loader"
]
}
42 changes: 0 additions & 42 deletions src/index.js

This file was deleted.

4 changes: 4 additions & 0 deletions test/.eslintrc.yml
@@ -0,0 +1,4 @@
plugins:
- jest
env:
jest/globals: true
53 changes: 0 additions & 53 deletions test/hable.spec.js

This file was deleted.

0 comments on commit 88decae

Please sign in to comment.