|
9 | 9 |
|
10 | 10 | export class ExecutionContextHost implements ExecutionContext { |
11 | 11 | private contextType = 'http'; |
| 12 | + private httpCache?: HttpArgumentsHost; |
| 13 | + private rpcCache?: RpcArgumentsHost; |
| 14 | + private wsCache?: WsArgumentsHost; |
12 | 15 |
|
13 | 16 | constructor( |
14 | 17 | private readonly args: any[], |
@@ -41,25 +44,34 @@ export class ExecutionContextHost implements ExecutionContext { |
41 | 44 | } |
42 | 45 |
|
43 | 46 | switchToRpc(): RpcArgumentsHost { |
44 | | - return Object.assign(this, { |
45 | | - getData: () => this.getArgByIndex(0), |
46 | | - getContext: () => this.getArgByIndex(1), |
47 | | - }); |
| 47 | + if (!this.rpcCache) { |
| 48 | + this.rpcCache = Object.assign(this, { |
| 49 | + getData: () => this.getArgByIndex(0), |
| 50 | + getContext: () => this.getArgByIndex(1), |
| 51 | + }); |
| 52 | + } |
| 53 | + return this.rpcCache; |
48 | 54 | } |
49 | 55 |
|
50 | 56 | switchToHttp(): HttpArgumentsHost { |
51 | | - return Object.assign(this, { |
52 | | - getRequest: () => this.getArgByIndex(0), |
53 | | - getResponse: () => this.getArgByIndex(1), |
54 | | - getNext: () => this.getArgByIndex(2), |
55 | | - }); |
| 57 | + if (!this.httpCache) { |
| 58 | + this.httpCache = Object.assign(this, { |
| 59 | + getRequest: () => this.getArgByIndex(0), |
| 60 | + getResponse: () => this.getArgByIndex(1), |
| 61 | + getNext: () => this.getArgByIndex(2), |
| 62 | + }); |
| 63 | + } |
| 64 | + return this.httpCache; |
56 | 65 | } |
57 | 66 |
|
58 | 67 | switchToWs(): WsArgumentsHost { |
59 | | - return Object.assign(this, { |
60 | | - getClient: () => this.getArgByIndex(0), |
61 | | - getData: () => this.getArgByIndex(1), |
62 | | - getPattern: () => this.getArgByIndex(this.getArgs().length - 1), |
63 | | - }); |
| 68 | + if (!this.wsCache) { |
| 69 | + this.wsCache = Object.assign(this, { |
| 70 | + getClient: () => this.getArgByIndex(0), |
| 71 | + getData: () => this.getArgByIndex(1), |
| 72 | + getPattern: () => this.getArgByIndex(this.getArgs().length - 1), |
| 73 | + }); |
| 74 | + } |
| 75 | + return this.wsCache; |
64 | 76 | } |
65 | 77 | } |
0 commit comments