Skip to content

Commit

Permalink
fix: handle deprecated hooks after being registred
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 23, 2022
1 parent 0d56527 commit 23d9ff4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/hookable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,18 @@ export class Hookable <

deprecateHook <NameT extends HookNameT> (name: NameT, deprecated: DeprecatedHook<HooksT>) {
this._deprecatedHooks[name] = deprecated
const _hooks = this._hooks[name] || []
this._hooks[name] = undefined
for (const hook of _hooks) {
this.hook(name, hook as any)
}
}

deprecateHooks (deprecatedHooks: Record<HookNameT, DeprecatedHook<HooksT>>) {
Object.assign(this._deprecatedHooks, deprecatedHooks)
for (const name in deprecatedHooks) {
this.deprecateHook(name, deprecatedHooks[name])
}
}

addHooks (configHooks: NestedHooks<HooksT>) {
Expand Down
10 changes: 10 additions & 0 deletions test/hookable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ describe('core: hookable', () => {
expect(hook._hooks.c).toEqual([expect.any(Function), expect.any(Function), expect.any(Function)])
})

test('should handle deprecation after registering', () => {
const hook = createHooks()
hook.hook('a', () => { })
hook.hook('b', () => { })
hook.deprecateHook('a', 'b')
expect(console.warn).toBeCalledWith('a hook has been deprecated, please use b')
expect(hook._hooks.a).toBeUndefined()
expect(hook._hooks.b).toEqual([expect.any(Function), expect.any(Function)])
})

test('deprecateHooks', () => {
const hook = createHooks()
hook.deprecateHooks({
Expand Down

0 comments on commit 23d9ff4

Please sign in to comment.