/
decorator.ts
56 lines (49 loc) 路 1.36 KB
/
decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as util from '../util'
import { BaseGraph } from './base-graph'
export function hook(
hookName?: string | null,
ignoreNullResult: boolean = false,
) {
return (
target: BaseGraph,
methodName: string,
descriptor: PropertyDescriptor,
) => {
const raw = descriptor.value
const name = hookName || methodName
descriptor.value = function(this: BaseGraph, ...args: any[]) {
const hook = (this.options as any)[name]
if (hook != null) {
this.getNativeValue = raw.bind(this, ...args)
const ret = util.call(hook, this, ...args)
delete this.getNativeValue
if (ret != null || ignoreNullResult) {
return ret
}
}
return raw.call(this, ...args)
}
}
}
export function afterCreate(aopName?: string | null) {
return (
target: BaseGraph,
methodName: string,
descriptor: PropertyDescriptor,
) => {
const raw = descriptor.value
const name = aopName || `on${util.ucFirst(methodName)}`
descriptor.value = function(this: BaseGraph, ...args: any[]) {
const instance = raw.call(this, ...args)
const aop = (this.options as any)[name]
if (aop != null) {
args.unshift(instance)
const restult = util.apply(aop, this, args)
if (restult != null) {
return restult
}
}
return instance
}
}
}