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

use collector of each .original #17

Merged
merged 3 commits into from
Feb 12, 2020
Merged
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
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 });
};