Skip to content

Commit

Permalink
fix: use collector of each .original (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
bertho-zero committed Feb 12, 2020
1 parent b89d044 commit 33fd2cb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
14 changes: 4 additions & 10 deletions packages/hooks/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import { Middleware } from './compose';
export const HOOKS: string = Symbol('@feathersjs/hooks') as any;
export const CONTEXT: string = Symbol('@feathersjs/hooks/context') as any;

function walkOriginal (fn: any, method: any, res: any[] = []): any {
return typeof fn.original === 'function'
? walkOriginal(fn.original, method, [...res, ...method(fn)])
: [...res, ...method(fn)];
}

/**
* @param target The target object or function
* @param middleware
Expand Down Expand Up @@ -79,10 +73,10 @@ export type HookSettings<T = any> = Array<Middleware<T>>|Partial<Omit<FunctionHo
context: ContextUpdater<T>|Array<ContextUpdater<T>>;
}>;

export function defaultCollectMiddleware<T = any> (self: any, fn: any, _args: any[]) {
export function defaultCollectMiddleware<T = any> (self: any, fn: any, args: any[]): Middleware[] {
return [
...getMiddleware<T>(self),
...walkOriginal(fn, getMiddleware)
...(fn && typeof fn.collect === 'function' ? fn.collect(fn, fn.original, args) : getMiddleware(fn))
];
}

Expand All @@ -99,10 +93,10 @@ export function normalizeOptions<T = any> (opts: any): FunctionHookOptions<T> {
return { middleware, context: contextUpdaters, collect };
}

export function collectContextUpdaters<T = any> (self: any, fn: any, _args: any[]) {
export function collectContextUpdaters<T = any> (self: any, fn: any, args: any[]): ContextUpdater[] {
return [
...getContextUpdater<T>(self),
...walkOriginal(fn, getContextUpdater)
...(fn.original ? collectContextUpdaters(fn, fn.original, args) : getContextUpdater(fn))
];
}

Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ export const functionHooks = <F, T = any>(original: F, opts: HookSettings<T>) =>
registerContextUpdater(wrapper, updateContext);
registerMiddleware(wrapper, middleware);

return Object.assign(wrapper, { original });
return Object.assign(wrapper, { original, collect });
};

0 comments on commit 33fd2cb

Please sign in to comment.