-
Notifications
You must be signed in to change notification settings - Fork 0
/
aop.ts
49 lines (49 loc) · 1.44 KB
/
aop.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
/**
* @packageDocumentation
* @module @tomato-js/function
*/
import { isFunction } from "@tomato-js/shared";
interface AopEvent {
args: any[];
value?: any;
}
type AopType = "before" | "after" | "around" | "afterReturn";
function shouldExecBefore(type: AopType) {
return ["before", "around"].includes(type);
}
function shouldExecAfter(type: AopType) {
return ["after", "around"].includes(type);
}
function shouldExecAfterReturn(type: AopType) {
return ["afterReturn"].includes(type);
}
/**
* AOP增强底层函数
*
* 脚本举例
* ```
* import { aop } from '@tomato-js/function'
* let origin = (str) => console.log(str);
* const target = ({args,value}) => console.log('around');
* origin = aop(origin, target,'around');
* origin('current');//around current around
* ```
*
* @param origin - 原始函数
* @param target - 注入的切片函数
* @param aopType - 切片方案,支持after/before/around/afterReturn
* @returns 新函数
*/
export function aop(origin: Function, target: Function, aopType: AopType) {
if (!isFunction(origin) || !isFunction(target)) {
throw new TypeError("Expected a function");
}
return function(...args: any) {
const event: AopEvent = { args };
if (shouldExecBefore(aopType)) target(event);
event.value = origin.apply(null, event.args);
if (shouldExecAfter(aopType)) target(event);
if (shouldExecAfterReturn(aopType)) return target(event.value);
return event.value;
};
}