-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d61a574
commit 56819d1
Showing
13 changed files
with
349 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { functionHooks } from './hooks'; | ||
import { setManager, HookOptions, convertOptions } from './base'; | ||
|
||
export const hookDecorator = (managerOrMiddleware?: HookOptions) => { | ||
const wrapper: any = (_target: any, method: string, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any> => { | ||
const manager = convertOptions(managerOrMiddleware); | ||
|
||
if (!descriptor) { | ||
setManager(_target.prototype, manager); | ||
|
||
return _target; | ||
} | ||
|
||
const fn = descriptor.value; | ||
|
||
if (typeof fn !== 'function') { | ||
throw new Error(`Can not apply hooks. '${method}' is not a function`); | ||
} | ||
|
||
descriptor.value = functionHooks(fn, manager.props({ method })); | ||
|
||
return descriptor; | ||
}; | ||
|
||
return wrapper; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Middleware } from './compose'; | ||
import { functionHooks } from './hooks'; | ||
import { setMiddleware, convertOptions, HookOptions } from './base'; | ||
|
||
export type HookMap<O = any> = { | ||
[L in keyof O]?: HookOptions; | ||
} | ||
|
||
export function objectHooks (_obj: any, hooks: HookMap|Middleware[]) { | ||
const obj = typeof _obj === 'function' ? _obj.prototype : _obj; | ||
|
||
if (Array.isArray(hooks)) { | ||
return setMiddleware(obj, hooks); | ||
} | ||
|
||
return Object.keys(hooks).reduce((result, method) => { | ||
const fn = obj[method]; | ||
|
||
if (typeof fn !== 'function') { | ||
throw new Error(`Can not apply hooks. '${method}' is not a function`); | ||
} | ||
|
||
const manager = convertOptions(hooks[method]); | ||
|
||
result[method] = functionHooks(fn, manager.props({ method })); | ||
|
||
return result; | ||
}, obj); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const proto = Object.prototype as any; | ||
// These are non-standard but offer a more reliable prototype based | ||
// lookup for properties | ||
const hasProtoDefinitions = typeof proto.__lookupGetter__ === 'function' && | ||
typeof proto.__defineGetter__ === 'function' && | ||
typeof proto.__defineSetter__ === 'function'; | ||
|
||
export function copyToSelf (target: any) { | ||
// tslint:disable-next-line | ||
for (const key in target) { | ||
if (!target.hasOwnProperty(key)) { | ||
const getter = hasProtoDefinitions ? target.constructor.prototype.__lookupGetter__(key) | ||
: Object.getOwnPropertyDescriptor(target, key); | ||
|
||
if (getter && hasProtoDefinitions) { | ||
target.__defineGetter__(key, getter); | ||
target.__defineSetter__(key, target.constructor.prototype.__lookupSetter__(key)); | ||
} else if (getter) { | ||
Object.defineProperty(target, key, getter); | ||
} else { | ||
target[key] = target[key]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.