|
| 1 | +import { |
| 2 | + ContextProto, |
| 3 | + Inject, |
| 4 | + SingletonProto, |
| 5 | +} from '@eggjs/tegg'; |
| 6 | +import { |
| 7 | + Advice, |
| 8 | + AdviceContext, |
| 9 | + Crosscut, |
| 10 | + IAdvice, |
| 11 | + Pointcut, |
| 12 | + PointcutType, |
| 13 | +} from '@eggjs/tegg/aop'; |
| 14 | +import assert from 'assert'; |
| 15 | + |
| 16 | +export interface CallTraceMsg { |
| 17 | + className: string; |
| 18 | + methodName: string; |
| 19 | + id: number; |
| 20 | + name: string; |
| 21 | + result?: string; |
| 22 | + adviceParams?: any; |
| 23 | +} |
| 24 | + |
| 25 | +@SingletonProto() |
| 26 | +export class CallTrace { |
| 27 | + msgs: Array<CallTraceMsg> = []; |
| 28 | + |
| 29 | + addMsg(msg: CallTraceMsg) { |
| 30 | + this.msgs.push(msg); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export const pointcutAdviceParams = { |
| 35 | + point: Math.random() |
| 36 | + .toString(), |
| 37 | + cut: Math.random() |
| 38 | + .toString(), |
| 39 | +}; |
| 40 | + |
| 41 | +@Advice() |
| 42 | +export class PointcutAdvice implements IAdvice<Hello> { |
| 43 | + @Inject() |
| 44 | + callTrace: CallTrace; |
| 45 | + |
| 46 | + async beforeCall(ctx: AdviceContext<Hello>): Promise<void> { |
| 47 | + assert.ok(ctx.adviceParams); |
| 48 | + assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); |
| 49 | + this.callTrace.addMsg({ |
| 50 | + className: PointcutAdvice.name, |
| 51 | + methodName: 'beforeCall', |
| 52 | + id: ctx.that.id, |
| 53 | + name: ctx.args[0], |
| 54 | + adviceParams: ctx.adviceParams, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + async afterReturn(ctx: AdviceContext<Hello>, result: any): Promise<void> { |
| 59 | + assert.ok(ctx.adviceParams); |
| 60 | + assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); |
| 61 | + this.callTrace.addMsg({ |
| 62 | + className: PointcutAdvice.name, |
| 63 | + methodName: 'afterReturn', |
| 64 | + id: ctx.that.id, |
| 65 | + name: ctx.args[0], |
| 66 | + result, |
| 67 | + adviceParams: ctx.adviceParams, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + async afterThrow(ctx: AdviceContext<Hello, any>, error: Error): Promise<void> { |
| 72 | + assert.ok(ctx.adviceParams); |
| 73 | + assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); |
| 74 | + this.callTrace.addMsg({ |
| 75 | + className: PointcutAdvice.name, |
| 76 | + methodName: 'afterThrow', |
| 77 | + id: ctx.that.id, |
| 78 | + name: ctx.args[0], |
| 79 | + result: error.message, |
| 80 | + adviceParams: ctx.adviceParams, |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + async afterFinally(ctx: AdviceContext<Hello>): Promise<void> { |
| 85 | + assert.ok(ctx.adviceParams); |
| 86 | + assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); |
| 87 | + this.callTrace.addMsg({ |
| 88 | + className: PointcutAdvice.name, |
| 89 | + methodName: 'afterFinally', |
| 90 | + id: ctx.that.id, |
| 91 | + name: ctx.args[0], |
| 92 | + adviceParams: ctx.adviceParams, |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + async around(ctx: AdviceContext<Hello>, next: () => Promise<any>): Promise<any> { |
| 97 | + assert.ok(ctx.adviceParams); |
| 98 | + assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams); |
| 99 | + ctx.args[0] = `withPointAroundParam(${ctx.args[0]})`; |
| 100 | + const result = await next(); |
| 101 | + return `withPointAroundResult(${result}${JSON.stringify(pointcutAdviceParams)})`; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +@ContextProto() |
| 106 | +export class Hello { |
| 107 | + id = 233; |
| 108 | + |
| 109 | + @Pointcut(PointcutAdvice, { adviceParams: pointcutAdviceParams }) |
| 110 | + async hello(name: string) { |
| 111 | + return `hello ${name}`; |
| 112 | + } |
| 113 | + |
| 114 | + @Pointcut(PointcutAdvice, { adviceParams: pointcutAdviceParams }) |
| 115 | + async helloWithException(name: string) { |
| 116 | + throw new Error(`ops, exception for ${name}`); |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +export const crosscutAdviceParams = { |
| 122 | + cross: Math.random() |
| 123 | + .toString(), |
| 124 | + cut: Math.random() |
| 125 | + .toString(), |
| 126 | +}; |
| 127 | + |
| 128 | +@Crosscut({ |
| 129 | + type: PointcutType.CLASS, |
| 130 | + clazz: Hello, |
| 131 | + methodName: 'hello', |
| 132 | +}, { adviceParams: crosscutAdviceParams }) |
| 133 | +@Advice() |
| 134 | +export class CrosscutAdvice implements IAdvice<Hello, String> { |
| 135 | + @Inject() |
| 136 | + callTrace: CallTrace; |
| 137 | + |
| 138 | + async beforeCall(ctx: AdviceContext<Hello, {}>): Promise<void> { |
| 139 | + assert.ok(ctx.adviceParams); |
| 140 | + assert.deepStrictEqual(ctx.adviceParams, crosscutAdviceParams); |
| 141 | + this.callTrace.addMsg({ |
| 142 | + className: CrosscutAdvice.name, |
| 143 | + methodName: 'beforeCall', |
| 144 | + id: ctx.that.id, |
| 145 | + name: ctx.args[0], |
| 146 | + adviceParams: ctx.adviceParams, |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + async afterReturn(ctx: AdviceContext<Hello>, result: any): Promise<void> { |
| 151 | + assert.ok(ctx.adviceParams); |
| 152 | + assert.deepStrictEqual(ctx.adviceParams, crosscutAdviceParams); |
| 153 | + this.callTrace.addMsg({ |
| 154 | + className: CrosscutAdvice.name, |
| 155 | + methodName: 'afterReturn', |
| 156 | + id: ctx.that.id, |
| 157 | + name: ctx.args[0], |
| 158 | + result, |
| 159 | + adviceParams: ctx.adviceParams, |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + async afterFinally(ctx: AdviceContext<Hello>): Promise<void> { |
| 164 | + assert.ok(ctx.adviceParams); |
| 165 | + assert.deepStrictEqual(ctx.adviceParams, crosscutAdviceParams); |
| 166 | + this.callTrace.addMsg({ |
| 167 | + className: CrosscutAdvice.name, |
| 168 | + methodName: 'afterFinally', |
| 169 | + id: ctx.that.id, |
| 170 | + name: ctx.args[0], |
| 171 | + adviceParams: ctx.adviceParams, |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + async around(ctx: AdviceContext<Hello>, next: () => Promise<any>): Promise<any> { |
| 176 | + assert.ok(ctx.adviceParams); |
| 177 | + assert.deepStrictEqual(ctx.adviceParams, crosscutAdviceParams); |
| 178 | + ctx.args[0] = `withCrosscutAroundParam(${ctx.args[0]})`; |
| 179 | + const result = await next(); |
| 180 | + return `withCrossAroundResult(${result}${JSON.stringify(ctx.adviceParams)})`; |
| 181 | + } |
| 182 | +} |
0 commit comments