-
Notifications
You must be signed in to change notification settings - Fork 37
/
sync_advices.ts
47 lines (41 loc) · 1.54 KB
/
sync_advices.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
import { Advice } from '../core/advice';
import { Metadata } from '../core/metadata';
export class BeforeAdvice extends Advice {
apply(target: Function, metadata: Metadata) {
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
this.invoke(target, metadata);
return metadata.method.result;
}
}
export class AfterAdvice extends Advice {
apply(target: Function, metadata: Metadata) {
this.invoke(target, metadata);
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
return metadata.method.result;
}
}
export class AroundAdvice extends Advice {
apply(target: Function, metadata: Metadata) {
// if the user called metadata.method.complete(), then he's handling return value himself;
// else he called metadata.method.invoke() & we'll handle it unless metadata.method.proceed is false
const result = this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
if (metadata.method.proceed) {
this.invoke(target, metadata);
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
} else {
metadata.method.result = metadata.method.result || result;
}
return metadata.method.result;
}
}
export class OnThrowAdvice extends Advice {
apply(target: Function, metadata: Metadata) {
try {
this.invoke(target, metadata);
} catch (e) {
metadata.method.exception = e;
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
}
return metadata.method.result;
}
}