From b362b49f590d70b5dd757f0ccff03f87fc4031ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Berthommier?= Date: Sat, 28 Sep 2019 14:07:38 +0200 Subject: [PATCH 1/2] allow to unshift hooks --- packages/commons/src/hooks.ts | 33 +++++++++++++++++++++++++++-- packages/commons/test/hooks.test.ts | 16 ++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/commons/src/hooks.ts b/packages/commons/src/hooks.ts index f8d3882db2..fd0087a2ef 100644 --- a/packages/commons/src/hooks.ts +++ b/packages/commons/src/hooks.ts @@ -73,6 +73,8 @@ export function convertHookData (obj: any) { if (Array.isArray(obj)) { hook = { all: obj }; + } else if (obj.$unshift || obj.$push) { + hook = { all: obj }; } else if (typeof obj !== 'object') { hook = { all: [ obj ] }; } else { @@ -139,6 +141,33 @@ export function processHooks (hooks: any[], initialHookObject: any) { }); } +function registerHooks (myHooks: any, hooks: any): any { + if (Array.isArray(hooks)) { + hooks.forEach(hook => { + if (typeof hook === 'function') { + myHooks.push(hook); + } else { + if (hook.$push) { + if (Array.isArray(hook.$push)) { + myHooks.push.apply(myHooks, hook.$push); + } else { + myHooks.push(hook.$push); + } + } + if (hook.$unshift) { + if (Array.isArray(hook.$unshift)) { + myHooks.unshift.apply(myHooks, hook.$unshift); + } else { + myHooks.unshift(hook.$unshift); + } + } + } + }); + } else if (hooks) { + return registerHooks(myHooks, [hooks]); + } +} + // Add `.hooks` functionality to an object export function enableHooks (obj: any, methods: string[], types: string[]) { if (typeof obj.hooks === 'function') { @@ -178,11 +207,11 @@ export function enableHooks (obj: any, methods: string[], types: string[]) { const myHooks = this.__hooks[type][method] || (this.__hooks[type][method] = []); if (hooks.all) { - myHooks.push.apply(myHooks, hooks.all); + registerHooks(myHooks, hooks.all); } if (hooks[method]) { - myHooks.push.apply(myHooks, hooks[method]); + registerHooks(myHooks, hooks[method]); } }); }); diff --git a/packages/commons/test/hooks.test.ts b/packages/commons/test/hooks.test.ts index d110639701..a0f253060b 100644 --- a/packages/commons/test/hooks.test.ts +++ b/packages/commons/test/hooks.test.ts @@ -350,6 +350,22 @@ describe('hook utilities', () => { expect(base.__hooks.dummy.testMethod).to.deep.equal([ fn ]); }); + it('unshifts hook with custom type and `all` method', () => { + expect(typeof base.hooks).to.equal('function'); + + const fn = function () {}; + const fn2 = function () {}; + const fn3 = function () {}; + + base.hooks({ dummy: [fn] }); + base.hooks({ dummy: { + all: { $unshift: [fn2] } + } }); + base.hooks({ dummy: { $unshift: [fn3] } }); + + expect(base.__hooks.dummy.testMethod).to.deep.equal([ fn3, fn2, fn ]); + }); + it('registers hook with custom type and specific method', () => { base.hooks({ dummy: { From b5333ebf3acb57dd5dd5d29a1f39f1af4f969975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Berthommier?= Date: Wed, 9 Oct 2019 22:20:01 +0200 Subject: [PATCH 2/2] simplify registerHooks --- packages/commons/src/hooks.ts | 40 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/packages/commons/src/hooks.ts b/packages/commons/src/hooks.ts index fd0087a2ef..56a8657009 100644 --- a/packages/commons/src/hooks.ts +++ b/packages/commons/src/hooks.ts @@ -142,30 +142,28 @@ export function processHooks (hooks: any[], initialHookObject: any) { } function registerHooks (myHooks: any, hooks: any): any { - if (Array.isArray(hooks)) { - hooks.forEach(hook => { - if (typeof hook === 'function') { - myHooks.push(hook); - } else { - if (hook.$push) { - if (Array.isArray(hook.$push)) { - myHooks.push.apply(myHooks, hook.$push); - } else { - myHooks.push(hook.$push); - } + const hooksToRegister = Array.isArray(hooks) ? hooks : [hooks]; + + hooksToRegister.forEach(hook => { + if (typeof hook === 'function') { + myHooks.push(hook); + } else { + if (hook.$push) { + if (Array.isArray(hook.$push)) { + myHooks.push.apply(myHooks, hook.$push); + } else { + myHooks.push(hook.$push); } - if (hook.$unshift) { - if (Array.isArray(hook.$unshift)) { - myHooks.unshift.apply(myHooks, hook.$unshift); - } else { - myHooks.unshift(hook.$unshift); - } + } + if (hook.$unshift) { + if (Array.isArray(hook.$unshift)) { + myHooks.unshift.apply(myHooks, hook.$unshift); + } else { + myHooks.unshift(hook.$unshift); } } - }); - } else if (hooks) { - return registerHooks(myHooks, [hooks]); - } + } + }); } // Add `.hooks` functionality to an object