This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+152
View File
@@ -0,0 +1,152 @@
# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## [3.0.0](https://github.com/jsless/hable/compare/v2.3.0...v3.0.0) (2020-02-25)
### Bug Fixes
* revert back hooks ([07f52dc](https://github.com/jsless/hable/commit/07f52dc))
### Features
* advanced deprecation ([5b88628](https://github.com/jsless/hable/commit/5b88628))
* bundle package ([53a2a0e](https://github.com/jsless/hable/commit/53a2a0e))
# [2.3.0](https://github.com/jsless/hable/compare/v2.2.1...v2.3.0) (2019-09-01)
### Features
* hide deprecate warnings on production builds ([0861df3](https://github.com/jsless/hable/commit/0861df3))
## [2.2.1](https://github.com/jsless/hable/compare/v2.2.0...v2.2.1) (2019-08-21)
# [2.2.0](https://github.com/jsless/hable/compare/v2.1.0...v2.2.0) (2019-08-21)
### Features
* deprecateHooks ([62f2d38](https://github.com/jsless/hable/commit/62f2d38))
# [2.1.0](https://github.com/jsless/hable/compare/v2.0.1...v2.1.0) (2019-08-21)
### Features
* optional fatal support for logger ([7c7355d](https://github.com/jsless/hable/commit/7c7355d))
## [2.0.1](https://github.com/jsless/hable/compare/v2.0.0...v2.0.1) (2019-08-21)
# [2.0.0](https://github.com/jsless/hable/compare/v1.0.1...v2.0.0) (2019-08-21)
### Features
* custom logger ([ada6e37](https://github.com/jsless/hable/commit/ada6e37))
### BREAKING CHANGES
* console is replaced by consola by default
## [1.0.1](https://github.com/jsless/hable/compare/v1.0.0...v1.0.1) (2019-03-16)
### Bug Fixes
* fix package.json (2) ([7ff4ce9](https://github.com/jsless/hable/commit/7ff4ce9))
<a name="1.0.0"></a>
# [1.0.0](https://github.com/jsless/hable/compare/v0.0.7...v1.0.0) (2019-02-11)
### Features
* rewrite for 1.0.0 ([88decae](https://github.com/jsless/hable/commit/88decae))
### BREAKING CHANGES
* api change
<a name="0.0.7"></a>
## [0.0.7](https://github.com/pi0/hable/compare/v0.0.6...v0.0.7) (2018-01-28)
### Bug Fixes
* hook with array or falsy key ([7e90de1](https://github.com/pi0/hable/commit/7e90de1))
### Performance Improvements
* use for in for hookObj ([3c8e2e7](https://github.com/pi0/hable/commit/3c8e2e7))
<a name="0.0.6"></a>
## [0.0.6](https://github.com/pi0/hable/compare/v0.0.5...v0.0.6) (2018-01-26)
### Performance Improvements
* reduce transpiled dist size ([df607cf](https://github.com/pi0/hable/commit/df607cf))
<a name="0.0.5"></a>
## [0.0.5](https://github.com/pi0/hable/compare/v0.0.4...v0.0.5) (2018-01-26)
### Bug Fixes
* **package:** lib ~> dist ([34a8d5c](https://github.com/pi0/hable/commit/34a8d5c))
<a name="0.0.4"></a>
## [0.0.4](https://github.com/pi0/hable/compare/v0.0.3...v0.0.4) (2018-01-26)
### Performance Improvements
* handle fn as array faster ([ec35edc](https://github.com/pi0/hable/commit/ec35edc))
<a name="0.0.3"></a>
## [0.0.3](https://github.com/pi0/hable/compare/v0.0.2...v0.0.3) (2018-01-26)
### Bug Fixes
* bind hookObj to this context ([6f6f7bc](https://github.com/pi0/hable/commit/6f6f7bc))
### Performance Improvements
* minor refactor ([e4083aa](https://github.com/pi0/hable/commit/e4083aa))
<a name="0.0.2"></a>
## 0.0.2 (2018-01-26)
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2018 Pooya Parsa <pooya@pi0.ir>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+128
View File
@@ -0,0 +1,128 @@
# Hable
[![CircleCI](https://img.shields.io/circleci/project/github/jsless/hable.svg?style=flat-square)](https://circleci.com/gh/jsless/hable)
[![Codecov](https://img.shields.io/codecov/c/github/jsless/hable.svg?style=flat-square)](https://codecov.io/gh/jsless/hable)
[![npm](https://img.shields.io/npm/v/hable.svg?style=flat-square)](https://www.npmjs.com/package/hable)
[![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)
> Awaitable hooks for Node.js
## Install
Using yarn:
```bash
yarn add hable
```
Using npm:
```bash
npm install hable
```
## Usage
**Extend your base class from Hable:**
```js
import Hookable from 'hable'
export default class Foo extends Hookable {
constructor() {
// Call to parent to initialize
super()
// Initialize Hookable with custom logger
// super(consola)
}
async someFunction() {
// Call and wait for `hook1` hooks (if any) sequential
await this.callHook('hook1')
// Call and wait for `hook2` hooks (if any) in parallel
await this.callHookParallel('hook2')
}
}
```
**Inside plugins, register for any hook:**
```js
const lib = newFooLib()
// Register a handler for `hook2`
lib.hook('hook2', async () => { /* ... */ })
// Register multiply handlers at once
lib.hookObj({
hook1: async () => { /* ... */ },
hook2: [ /* can be also an array */ ]
})
```
## Hookable class
### `constructor(logger)`
Custom logger. Default logger is `console` but you can use your own or [consola](https://github.com/nuxt/consola).
It should be an object implementing following functions:
- warn
- error
- fatal (optional)
### `hook (name, fn)`
Register a handler for a specific hook. `fn` can be a single function or an array.
### `addHooks(configHooks)`
Flatten and register hooks object.
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 favor of `name` hook.
### `deprecateHooks (deprecatedHooks)`
Deprecate all hooks from an object (keys are old and values or newer ones).
### `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
Extracted from [Nuxt.js](github.com/nuxt/nuxt.js) hooks system. Original author: [Sébastien Chopin](https://github.com/Atinux).
## License
MIT
+2
View File
@@ -0,0 +1,2 @@
export default class{constructor(o=console){this._logger=o,this._hooks={},this._deprecatedHooks={},this.hook=this.hook.bind(this),this.callHook=this.callHook.bind(this)}hook(o,t){if(!o||"function"!=typeof t)return;const s=o;let e;for(;this._deprecatedHooks[o];)"string"==typeof(e=this._deprecatedHooks[o])&&(e={to:e}),o=e.to;e&&(e.message?this._logger.warn(e.message):this._logger.warn(`${s} hook has been deprecated`+(e.to?`, please use ${e.to}`:""))),this._hooks[o]=this._hooks[o]||[],this._hooks[o].push(t)}deprecateHook(o,t){this._deprecatedHooks[o]=t}deprecateHooks(o){Object.assign(this._deprecatedHooks,o)}addHooks(o){const t=function o(t,s={},e){for(const h in t){const r=t[h],i=e?`${e}:${h}`:h;"object"==typeof r&&null!==r?o(r,s,i):s[i]=r}return s}(o);for(const o in t)this.hook(o,t[o])}async callHook(o,...t){var s,e;if(this._hooks[o])try{await(s=this._hooks[o],e=(o=>o(...t)),s.reduce((o,t)=>o.then(o=>e(t,o)),Promise.resolve(null)))}catch(t){"error"!==o&&await this.callHook("error",t),this._logger.fatal?this._logger.fatal(t):this._logger.error(t)}}clearHook(o){o&&delete this._hooks[o]}clearHooks(){this._hooks={}}}
//# sourceMappingURL=hable.esm.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"hable.esm.js","sources":["../src/hable.js","../src/utils.js"],"sourcesContent":["import { serial, flatHooks } from './utils'\n\nexport default class Hookable {\n constructor (logger = console) {\n this._logger = logger\n this._hooks = {}\n this._deprecatedHooks = {}\n\n // Allow destructuring hook and callHook functions out of instance object\n this.hook = this.hook.bind(this)\n this.callHook = this.callHook.bind(this)\n }\n\n hook (name, fn) {\n if (!name || typeof fn !== 'function') {\n return\n }\n\n const originalName = name\n let deprecatedHook\n while (this._deprecatedHooks[name]) {\n deprecatedHook = this._deprecatedHooks[name]\n if (typeof deprecatedHook === 'string') {\n deprecatedHook = { to: deprecatedHook }\n }\n name = deprecatedHook.to\n }\n if (deprecatedHook) {\n if (!deprecatedHook.message) {\n this._logger.warn(\n `${originalName} hook has been deprecated` +\n (deprecatedHook.to ? `, please use ${deprecatedHook.to}` : '')\n )\n } else {\n this._logger.warn(deprecatedHook.message)\n }\n }\n\n this._hooks[name] = this._hooks[name] || []\n this._hooks[name].push(fn)\n }\n\n deprecateHook (old, name) {\n this._deprecatedHooks[old] = name\n }\n\n deprecateHooks (deprecatedHooks) {\n Object.assign(this._deprecatedHooks, deprecatedHooks)\n }\n\n addHooks (configHooks) {\n const hooks = flatHooks(configHooks)\n for (const key in hooks) {\n this.hook(key, hooks[key])\n }\n }\n\n async callHook (name, ...args) {\n if (!this._hooks[name]) {\n return\n }\n try {\n await serial(this._hooks[name], fn => fn(...args))\n } catch (err) {\n if (name !== 'error') {\n await this.callHook('error', err)\n }\n if (this._logger.fatal) {\n this._logger.fatal(err)\n } else {\n this._logger.error(err)\n }\n }\n }\n\n clearHook (name) {\n if (name) {\n delete this._hooks[name]\n }\n }\n\n clearHooks () {\n this._hooks = {}\n }\n}\n","export function flatHooks (configHooks, hooks = {}, parentName) {\n for (const key in configHooks) {\n const subHook = configHooks[key]\n const name = parentName ? `${parentName}:${key}` : key\n if (typeof subHook === 'object' && subHook !== null) {\n flatHooks(subHook, hooks, name)\n } else {\n hooks[name] = subHook\n }\n }\n return hooks\n}\n\nexport function serial (tasks, fn) {\n return tasks.reduce((promise, task) => promise.then(previous => fn(task, previous)), Promise.resolve(null))\n}\n"],"names":["constructor","logger","console","_logger","_hooks","_deprecatedHooks","hook","this","bind","callHook","name","fn","originalName","deprecatedHook","to","message","warn","push","deprecateHook","old","deprecateHooks","deprecatedHooks","Object","assign","addHooks","configHooks","hooks","flatHooks","parentName","key","subHook","args","tasks","reduce","promise","task","then","previous","Promise","resolve","err","fatal","error","clearHook","clearHooks"],"mappings":"qBAGEA,YAAaC,EAASC,cACfC,QAAUF,OACVG,OAAS,QACTC,iBAAmB,QAGnBC,KAAOC,KAAKD,KAAKE,KAAKD,WACtBE,SAAWF,KAAKE,SAASD,KAAKD,MAGrCD,KAAMI,EAAMC,OACLD,GAAsB,mBAAPC,eAIdC,EAAeF,MACjBG,OACGN,KAAKF,iBAAiBK,IAEG,iBAD9BG,EAAiBN,KAAKF,iBAAiBK,MAErCG,EAAiB,CAAEC,GAAID,IAEzBH,EAAOG,EAAeC,GAEpBD,IACGA,EAAeE,aAMbZ,QAAQa,KAAKH,EAAeE,cAL5BZ,QAAQa,QACRJ,8BACFC,EAAeC,mBAAqBD,EAAeC,KAAO,WAO5DV,OAAOM,GAAQH,KAAKH,OAAOM,IAAS,QACpCN,OAAOM,GAAMO,KAAKN,GAGzBO,cAAeC,EAAKT,QACbL,iBAAiBc,GAAOT,EAG/BU,eAAgBC,GACdC,OAAOC,OAAOhB,KAAKF,iBAAkBgB,GAGvCG,SAAUC,SACFC,ECnDH,SAASC,EAAWF,EAAaC,EAAQ,GAAIE,OAC7C,MAAMC,KAAOJ,EAAa,OACvBK,EAAUL,EAAYI,GACtBnB,EAAOkB,KAAgBA,KAAcC,IAAQA,EAC5B,iBAAZC,GAAoC,OAAZA,EACjCH,EAAUG,EAASJ,EAAOhB,GAE1BgB,EAAMhB,GAAQoB,SAGXJ,EDyCSC,CAAUF,OACnB,MAAMI,KAAOH,OACXpB,KAAKuB,EAAKH,EAAMG,mBAITnB,KAASqB,GC5CpB,IAAiBC,EAAOrB,KD6CtBJ,KAAKH,OAAOM,aC7CGsB,EDiDLzB,KAAKH,OAAOM,GCjDAC,EDiDOA,CAAAA,GAAMA,KAAMoB,IChDzCC,EAAMC,OAAO,CAACC,EAASC,IAASD,EAAQE,KAAKC,GAAY1B,EAAGwB,EAAME,IAAYC,QAAQC,QAAQ,QDiDjG,MAAOC,GACM,UAAT9B,SACIH,KAAKE,SAAS,QAAS+B,GAE3BjC,KAAKJ,QAAQsC,WACVtC,QAAQsC,MAAMD,QAEdrC,QAAQuC,MAAMF,IAKzBG,UAAWjC,GACLA,UACKH,KAAKH,OAAOM,GAIvBkC,kBACOxC,OAAS"}
+2
View File
@@ -0,0 +1,2 @@
"use strict";function flatHooks(o,s={},t){for(const e in o){const r=o[e],h=t?`${t}:${e}`:e;"object"==typeof r&&null!==r?flatHooks(r,s,h):s[h]=r}return s}function serial(o,s){return o.reduce((o,t)=>o.then(o=>s(t,o)),Promise.resolve(null))}class Hookable{constructor(o=console){this._logger=o,this._hooks={},this._deprecatedHooks={},this.hook=this.hook.bind(this),this.callHook=this.callHook.bind(this)}hook(o,s){if(!o||"function"!=typeof s)return;const t=o;let e;for(;this._deprecatedHooks[o];)"string"==typeof(e=this._deprecatedHooks[o])&&(e={to:e}),o=e.to;e&&(e.message?this._logger.warn(e.message):this._logger.warn(`${t} hook has been deprecated`+(e.to?`, please use ${e.to}`:""))),this._hooks[o]=this._hooks[o]||[],this._hooks[o].push(s)}deprecateHook(o,s){this._deprecatedHooks[o]=s}deprecateHooks(o){Object.assign(this._deprecatedHooks,o)}addHooks(o){const s=flatHooks(o);for(const o in s)this.hook(o,s[o])}async callHook(o,...s){if(this._hooks[o])try{await serial(this._hooks[o],o=>o(...s))}catch(s){"error"!==o&&await this.callHook("error",s),this._logger.fatal?this._logger.fatal(s):this._logger.error(s)}}clearHook(o){o&&delete this._hooks[o]}clearHooks(){this._hooks={}}}module.exports=Hookable;
//# sourceMappingURL=hable.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"hable.js","sources":["../src/utils.js","../src/hable.js"],"sourcesContent":["export function flatHooks (configHooks, hooks = {}, parentName) {\n for (const key in configHooks) {\n const subHook = configHooks[key]\n const name = parentName ? `${parentName}:${key}` : key\n if (typeof subHook === 'object' && subHook !== null) {\n flatHooks(subHook, hooks, name)\n } else {\n hooks[name] = subHook\n }\n }\n return hooks\n}\n\nexport function serial (tasks, fn) {\n return tasks.reduce((promise, task) => promise.then(previous => fn(task, previous)), Promise.resolve(null))\n}\n","import { serial, flatHooks } from './utils'\n\nexport default class Hookable {\n constructor (logger = console) {\n this._logger = logger\n this._hooks = {}\n this._deprecatedHooks = {}\n\n // Allow destructuring hook and callHook functions out of instance object\n this.hook = this.hook.bind(this)\n this.callHook = this.callHook.bind(this)\n }\n\n hook (name, fn) {\n if (!name || typeof fn !== 'function') {\n return\n }\n\n const originalName = name\n let deprecatedHook\n while (this._deprecatedHooks[name]) {\n deprecatedHook = this._deprecatedHooks[name]\n if (typeof deprecatedHook === 'string') {\n deprecatedHook = { to: deprecatedHook }\n }\n name = deprecatedHook.to\n }\n if (deprecatedHook) {\n if (!deprecatedHook.message) {\n this._logger.warn(\n `${originalName} hook has been deprecated` +\n (deprecatedHook.to ? `, please use ${deprecatedHook.to}` : '')\n )\n } else {\n this._logger.warn(deprecatedHook.message)\n }\n }\n\n this._hooks[name] = this._hooks[name] || []\n this._hooks[name].push(fn)\n }\n\n deprecateHook (old, name) {\n this._deprecatedHooks[old] = name\n }\n\n deprecateHooks (deprecatedHooks) {\n Object.assign(this._deprecatedHooks, deprecatedHooks)\n }\n\n addHooks (configHooks) {\n const hooks = flatHooks(configHooks)\n for (const key in hooks) {\n this.hook(key, hooks[key])\n }\n }\n\n async callHook (name, ...args) {\n if (!this._hooks[name]) {\n return\n }\n try {\n await serial(this._hooks[name], fn => fn(...args))\n } catch (err) {\n if (name !== 'error') {\n await this.callHook('error', err)\n }\n if (this._logger.fatal) {\n this._logger.fatal(err)\n } else {\n this._logger.error(err)\n }\n }\n }\n\n clearHook (name) {\n if (name) {\n delete this._hooks[name]\n }\n }\n\n clearHooks () {\n this._hooks = {}\n }\n}\n"],"names":["flatHooks","configHooks","hooks","parentName","key","subHook","name","serial","tasks","fn","reduce","promise","task","then","previous","Promise","resolve","Hookable","constructor","logger","console","_logger","_hooks","_deprecatedHooks","hook","this","bind","callHook","originalName","deprecatedHook","to","message","warn","push","deprecateHook","old","deprecateHooks","deprecatedHooks","Object","assign","addHooks","args","err","fatal","error","clearHook","clearHooks"],"mappings":"aAAO,SAASA,UAAWC,EAAaC,EAAQ,GAAIC,OAC7C,MAAMC,KAAOH,EAAa,OACvBI,EAAUJ,EAAYG,GACtBE,EAAOH,KAAgBA,KAAcC,IAAQA,EAC5B,iBAAZC,GAAoC,OAAZA,EACjCL,UAAUK,EAASH,EAAOI,GAE1BJ,EAAMI,GAAQD,SAGXH,EAGF,SAASK,OAAQC,EAAOC,UACtBD,EAAME,OAAO,CAACC,EAASC,IAASD,EAAQE,KAAKC,GAAYL,EAAGG,EAAME,IAAYC,QAAQC,QAAQ,aCZlFC,SACnBC,YAAaC,EAASC,cACfC,QAAUF,OACVG,OAAS,QACTC,iBAAmB,QAGnBC,KAAOC,KAAKD,KAAKE,KAAKD,WACtBE,SAAWF,KAAKE,SAASD,KAAKD,MAGrCD,KAAMlB,EAAMG,OACLH,GAAsB,mBAAPG,eAIdmB,EAAetB,MACjBuB,OACGJ,KAAKF,iBAAiBjB,IAEG,iBAD9BuB,EAAiBJ,KAAKF,iBAAiBjB,MAErCuB,EAAiB,CAAEC,GAAID,IAEzBvB,EAAOuB,EAAeC,GAEpBD,IACGA,EAAeE,aAMbV,QAAQW,KAAKH,EAAeE,cAL5BV,QAAQW,QACRJ,8BACFC,EAAeC,mBAAqBD,EAAeC,KAAO,WAO5DR,OAAOhB,GAAQmB,KAAKH,OAAOhB,IAAS,QACpCgB,OAAOhB,GAAM2B,KAAKxB,GAGzByB,cAAeC,EAAK7B,QACbiB,iBAAiBY,GAAO7B,EAG/B8B,eAAgBC,GACdC,OAAOC,OAAOd,KAAKF,iBAAkBc,GAGvCG,SAAUvC,SACFC,EAAQF,UAAUC,OACnB,MAAMG,KAAOF,OACXsB,KAAKpB,EAAKF,EAAME,mBAITE,KAASmC,MAClBhB,KAAKH,OAAOhB,aAITC,OAAOkB,KAAKH,OAAOhB,GAAOG,GAAMA,KAAMgC,IAC5C,MAAOC,GACM,UAATpC,SACImB,KAAKE,SAAS,QAASe,GAE3BjB,KAAKJ,QAAQsB,WACVtB,QAAQsB,MAAMD,QAEdrB,QAAQuB,MAAMF,IAKzBG,UAAWvC,GACLA,UACKmB,KAAKH,OAAOhB,GAIvBwC,kBACOxB,OAAS"}
+2
View File
@@ -0,0 +1,2 @@
!function(o,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(o=o||self).hable=e()}(this,function(){"use strict";return class{constructor(o=console){this._logger=o,this._hooks={},this._deprecatedHooks={},this.hook=this.hook.bind(this),this.callHook=this.callHook.bind(this)}hook(o,e){if(!o||"function"!=typeof e)return;const t=o;let s;for(;this._deprecatedHooks[o];)"string"==typeof(s=this._deprecatedHooks[o])&&(s={to:s}),o=s.to;s&&(s.message?this._logger.warn(s.message):this._logger.warn(`${t} hook has been deprecated`+(s.to?`, please use ${s.to}`:""))),this._hooks[o]=this._hooks[o]||[],this._hooks[o].push(e)}deprecateHook(o,e){this._deprecatedHooks[o]=e}deprecateHooks(o){Object.assign(this._deprecatedHooks,o)}addHooks(o){const e=function o(e,t={},s){for(const i in e){const r=e[i],h=s?`${s}:${i}`:i;"object"==typeof r&&null!==r?o(r,t,h):t[h]=r}return t}(o);for(const o in e)this.hook(o,e[o])}async callHook(o,...e){var t,s;if(this._hooks[o])try{await(t=this._hooks[o],s=(o=>o(...e)),t.reduce((o,e)=>o.then(o=>s(e,o)),Promise.resolve(null)))}catch(e){"error"!==o&&await this.callHook("error",e),this._logger.fatal?this._logger.fatal(e):this._logger.error(e)}}clearHook(o){o&&delete this._hooks[o]}clearHooks(){this._hooks={}}}});
//# sourceMappingURL=hable.umd.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"hable.umd.js","sources":["../src/hable.js","../src/utils.js"],"sourcesContent":["import { serial, flatHooks } from './utils'\n\nexport default class Hookable {\n constructor (logger = console) {\n this._logger = logger\n this._hooks = {}\n this._deprecatedHooks = {}\n\n // Allow destructuring hook and callHook functions out of instance object\n this.hook = this.hook.bind(this)\n this.callHook = this.callHook.bind(this)\n }\n\n hook (name, fn) {\n if (!name || typeof fn !== 'function') {\n return\n }\n\n const originalName = name\n let deprecatedHook\n while (this._deprecatedHooks[name]) {\n deprecatedHook = this._deprecatedHooks[name]\n if (typeof deprecatedHook === 'string') {\n deprecatedHook = { to: deprecatedHook }\n }\n name = deprecatedHook.to\n }\n if (deprecatedHook) {\n if (!deprecatedHook.message) {\n this._logger.warn(\n `${originalName} hook has been deprecated` +\n (deprecatedHook.to ? `, please use ${deprecatedHook.to}` : '')\n )\n } else {\n this._logger.warn(deprecatedHook.message)\n }\n }\n\n this._hooks[name] = this._hooks[name] || []\n this._hooks[name].push(fn)\n }\n\n deprecateHook (old, name) {\n this._deprecatedHooks[old] = name\n }\n\n deprecateHooks (deprecatedHooks) {\n Object.assign(this._deprecatedHooks, deprecatedHooks)\n }\n\n addHooks (configHooks) {\n const hooks = flatHooks(configHooks)\n for (const key in hooks) {\n this.hook(key, hooks[key])\n }\n }\n\n async callHook (name, ...args) {\n if (!this._hooks[name]) {\n return\n }\n try {\n await serial(this._hooks[name], fn => fn(...args))\n } catch (err) {\n if (name !== 'error') {\n await this.callHook('error', err)\n }\n if (this._logger.fatal) {\n this._logger.fatal(err)\n } else {\n this._logger.error(err)\n }\n }\n }\n\n clearHook (name) {\n if (name) {\n delete this._hooks[name]\n }\n }\n\n clearHooks () {\n this._hooks = {}\n }\n}\n","export function flatHooks (configHooks, hooks = {}, parentName) {\n for (const key in configHooks) {\n const subHook = configHooks[key]\n const name = parentName ? `${parentName}:${key}` : key\n if (typeof subHook === 'object' && subHook !== null) {\n flatHooks(subHook, hooks, name)\n } else {\n hooks[name] = subHook\n }\n }\n return hooks\n}\n\nexport function serial (tasks, fn) {\n return tasks.reduce((promise, task) => promise.then(previous => fn(task, previous)), Promise.resolve(null))\n}\n"],"names":["constructor","logger","console","_logger","_hooks","_deprecatedHooks","hook","this","bind","callHook","name","fn","originalName","deprecatedHook","to","message","warn","push","deprecateHook","old","deprecateHooks","deprecatedHooks","Object","assign","addHooks","configHooks","hooks","flatHooks","parentName","key","subHook","args","tasks","reduce","promise","task","then","previous","Promise","resolve","err","fatal","error","clearHook","clearHooks"],"mappings":"wMAGEA,YAAaC,EAASC,cACfC,QAAUF,OACVG,OAAS,QACTC,iBAAmB,QAGnBC,KAAOC,KAAKD,KAAKE,KAAKD,WACtBE,SAAWF,KAAKE,SAASD,KAAKD,MAGrCD,KAAMI,EAAMC,OACLD,GAAsB,mBAAPC,eAIdC,EAAeF,MACjBG,OACGN,KAAKF,iBAAiBK,IAEG,iBAD9BG,EAAiBN,KAAKF,iBAAiBK,MAErCG,EAAiB,CAAEC,GAAID,IAEzBH,EAAOG,EAAeC,GAEpBD,IACGA,EAAeE,aAMbZ,QAAQa,KAAKH,EAAeE,cAL5BZ,QAAQa,QACRJ,8BACFC,EAAeC,mBAAqBD,EAAeC,KAAO,WAO5DV,OAAOM,GAAQH,KAAKH,OAAOM,IAAS,QACpCN,OAAOM,GAAMO,KAAKN,GAGzBO,cAAeC,EAAKT,QACbL,iBAAiBc,GAAOT,EAG/BU,eAAgBC,GACdC,OAAOC,OAAOhB,KAAKF,iBAAkBgB,GAGvCG,SAAUC,SACFC,ECnDH,SAASC,EAAWF,EAAaC,EAAQ,GAAIE,OAC7C,MAAMC,KAAOJ,EAAa,OACvBK,EAAUL,EAAYI,GACtBnB,EAAOkB,KAAgBA,KAAcC,IAAQA,EAC5B,iBAAZC,GAAoC,OAAZA,EACjCH,EAAUG,EAASJ,EAAOhB,GAE1BgB,EAAMhB,GAAQoB,SAGXJ,EDyCSC,CAAUF,OACnB,MAAMI,KAAOH,OACXpB,KAAKuB,EAAKH,EAAMG,mBAITnB,KAASqB,GC5CpB,IAAiBC,EAAOrB,KD6CtBJ,KAAKH,OAAOM,aC7CGsB,EDiDLzB,KAAKH,OAAOM,GCjDAC,EDiDOA,CAAAA,GAAMA,KAAMoB,IChDzCC,EAAMC,OAAO,CAACC,EAASC,IAASD,EAAQE,KAAKC,GAAY1B,EAAGwB,EAAME,IAAYC,QAAQC,QAAQ,QDiDjG,MAAOC,GACM,UAAT9B,SACIH,KAAKE,SAAS,QAAS+B,GAE3BjC,KAAKJ,QAAQsC,WACVtC,QAAQsC,MAAMD,QAEdrC,QAAQuC,MAAMF,IAKzBG,UAAWjC,GACLA,UACKH,KAAKH,OAAOM,GAIvBkC,kBACOxC,OAAS"}
+17
View File
@@ -0,0 +1,17 @@
interface hable {
hook (name: string, fn: (...args: any) => Promise<void> | void): void;
deprecateHook (old: string, name: string): void;
deprecateHooks (deprecatedHooks: object): void;
addHooks (configHooks: object): void;
callHook (name: string, ...args: any) : Promise<void>;
clearHook (name: string): void;
clearHooks (): void;
}
export default hable
+92
View File
@@ -0,0 +1,92 @@
{
"_args": [
[
"hable@3.0.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "hable@3.0.0",
"_id": "hable@3.0.0",
"_inBundle": false,
"_integrity": "sha512-7+G0/2/COR8pwteYFqHIVYfQpuEiO2HXwJrhCBJVgrNrl9O5eaUoJVDGXUJX+0RpGncNVTuestexjk1afj01wQ==",
"_location": "/hable",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "hable@3.0.0",
"name": "hable",
"escapedName": "hable",
"rawSpec": "3.0.0",
"saveSpec": null,
"fetchSpec": "3.0.0"
},
"_requiredBy": [
"/@nuxt/cli",
"/@nuxt/core"
],
"_resolved": "https://registry.npmjs.org/hable/-/hable-3.0.0.tgz",
"_spec": "3.0.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"browser": "./dist/hable.umd.js",
"bugs": {
"url": "https://github.com/jsless/hable/issues"
},
"contributors": [
{
"name": "Sebastien Chopin",
"url": "@Atinux"
},
{
"name": "Clark Du",
"url": "@clarkdo"
},
{
"name": "Pooya Parsa",
"url": "@pi0"
}
],
"description": "Awaitable hooks for Node.js",
"devDependencies": {
"babel-jest": "^24.9.0",
"bili": "^4.8.1",
"codecov": "^3.5.0",
"eslint": "^6.3.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^22.17.0",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"jest": "^24.9.0",
"standard-version": "^7.0.0"
},
"files": [
"index.d.ts",
"src",
"dist"
],
"homepage": "https://github.com/jsless/hable#readme",
"keywords": [
"hook",
"hookable",
"plugin",
"tapable",
"tappable"
],
"license": "MIT",
"main": "./dist/hable.js",
"name": "hable",
"repository": {
"type": "git",
"url": "git+https://github.com/jsless/hable.git"
},
"scripts": {
"build": "bili --module-name hable --minify --format cjs,umd,es src/hable.js",
"lint": "eslint src",
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && yarn jest"
},
"types": "./index.d.ts",
"version": "3.0.0"
}
+85
View File
@@ -0,0 +1,85 @@
import { serial, flatHooks } from './utils'
export default class Hookable {
constructor (logger = console) {
this._logger = logger
this._hooks = {}
this._deprecatedHooks = {}
// Allow destructuring hook and callHook functions out of instance object
this.hook = this.hook.bind(this)
this.callHook = this.callHook.bind(this)
}
hook (name, fn) {
if (!name || typeof fn !== 'function') {
return
}
const originalName = name
let deprecatedHook
while (this._deprecatedHooks[name]) {
deprecatedHook = this._deprecatedHooks[name]
if (typeof deprecatedHook === 'string') {
deprecatedHook = { to: deprecatedHook }
}
name = deprecatedHook.to
}
if (deprecatedHook) {
if (!deprecatedHook.message) {
this._logger.warn(
`${originalName} hook has been deprecated` +
(deprecatedHook.to ? `, please use ${deprecatedHook.to}` : '')
)
} else {
this._logger.warn(deprecatedHook.message)
}
}
this._hooks[name] = this._hooks[name] || []
this._hooks[name].push(fn)
}
deprecateHook (old, name) {
this._deprecatedHooks[old] = name
}
deprecateHooks (deprecatedHooks) {
Object.assign(this._deprecatedHooks, deprecatedHooks)
}
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) {
if (name !== 'error') {
await this.callHook('error', err)
}
if (this._logger.fatal) {
this._logger.fatal(err)
} else {
this._logger.error(err)
}
}
}
clearHook (name) {
if (name) {
delete this._hooks[name]
}
}
clearHooks () {
this._hooks = {}
}
}
+16
View File
@@ -0,0 +1,16 @@
export 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
}
export function serial (tasks, fn) {
return tasks.reduce((promise, task) => promise.then(previous => fn(task, previous)), Promise.resolve(null))
}