Skip to content

Commit

Permalink
feat: deprecateHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed Aug 21, 2019
1 parent 2860582 commit 62f2d38
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ Used by class itself to **sequentially** call handlers of a specific hook.

### `deprecateHook (old, name)`

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

### `deprecateHooks (hooks)`

Deprecate all hooks from an object (keys are old and values or newer ones).

### `clearHook (name)`

Expand Down
6 changes: 6 additions & 0 deletions lib/hookable.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ module.exports = class Hookable {
this._deprecatedHooks[old] = name
}

deprecateHooks (deprecations) {
Object.entries(deprecations).forEach(([old, name]) => {
this.deprecateHook(old, name)
})
}

addHooks (configHooks) {
const hooks = flatHooks(configHooks)
for (const key in hooks) {
Expand Down
13 changes: 13 additions & 0 deletions test/hookable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ describe('core: hookable', () => {
expect(hook._hooks['test:before']).toEqual([expect.any(Function)])
})

test('deprecateHooks', () => {
const hook = new Hookable()
hook.deprecateHooks({
'test:hook': 'test:before'
})

hook.hook('test:hook', () => { })

expect(console.warn).toBeCalledWith('test:hook hook has been deprecated, please use test:before')
expect(hook._hooks['test:hook']).toBeUndefined()
expect(hook._hooks['test:before']).toEqual([expect.any(Function)])
})

test('should call registered hook', async () => {
const hook = new Hookable()
hook.hook('test:hook', () => console.log('test:hook called'))
Expand Down

0 comments on commit 62f2d38

Please sign in to comment.