Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to unshift hooks #1590

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions packages/commons/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -139,6 +141,31 @@ export function processHooks (hooks: any[], initialHookObject: any) {
});
}

function registerHooks (myHooks: any, hooks: any): any {
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);
}
}
}
});
}

// Add `.hooks` functionality to an object
export function enableHooks (obj: any, methods: string[], types: string[]) {
if (typeof obj.hooks === 'function') {
Expand Down Expand Up @@ -180,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]);
}
});
});
Expand Down
16 changes: 16 additions & 0 deletions packages/commons/test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down