@@ -8,7 +8,7 @@ import { HandlerType } from './types/enums/HandlerType';
8
8
import { SocketControllersOptions } from './types/SocketControllersOptions' ;
9
9
import { ControllerMetadata } from './types/ControllerMetadata' ;
10
10
import { MiddlewareMetadata } from './types/MiddlewareMetadata' ;
11
- import { ActionType } from './types/enums/ActionType ' ;
11
+ import { SocketEventType } from './types/enums/SocketEventType ' ;
12
12
import { ActionMetadata } from './types/ActionMetadata' ;
13
13
import { ParameterMetadata } from './types/ParameterMetadata' ;
14
14
import { ParameterType } from './types/enums/ParameterType' ;
@@ -18,12 +18,13 @@ import { TransformOptions } from './types/TransformOptions';
18
18
import { defaultTransformOptions } from './types/constants/defaultTransformOptions' ;
19
19
import { ActionTransformOptions } from './types/ActionTransformOptions' ;
20
20
import { instanceToPlain , plainToInstance } from 'class-transformer' ;
21
+ import { ScopedContainerGetterParams } from './types/ScopedContainerGetterParams' ;
21
22
import { MiddlewareInterface } from './types/MiddlewareInterface' ;
22
23
23
24
export class SocketControllers {
24
25
public container : { get < T > ( someClass : { new ( ...args : any [ ] ) : T } | Function ) : T } ;
25
- public controllers : HandlerMetadata < any , ControllerMetadata > [ ] ;
26
- public middlewares : HandlerMetadata < MiddlewareInterface , MiddlewareMetadata > [ ] ;
26
+ public controllers : HandlerMetadata < ControllerMetadata > [ ] ;
27
+ public middlewares : HandlerMetadata < MiddlewareMetadata > [ ] ;
27
28
public io : Server ;
28
29
public transformOptions : TransformOptions ;
29
30
@@ -34,23 +35,14 @@ export class SocketControllers {
34
35
...defaultTransformOptions ,
35
36
...options . transformOption ,
36
37
} ;
37
- this . controllers = this . loadHandlers < Function , ControllerMetadata > (
38
- options . controllers || [ ] ,
39
- HandlerType . CONTROLLER
40
- ) ;
41
- this . middlewares = this . loadHandlers < MiddlewareInterface , MiddlewareMetadata > (
42
- options . middlewares || [ ] ,
43
- HandlerType . MIDDLEWARE
44
- ) ;
38
+ this . controllers = this . loadHandlers < ControllerMetadata > ( options . controllers || [ ] , HandlerType . CONTROLLER ) ;
39
+ this . middlewares = this . loadHandlers < MiddlewareMetadata > ( options . middlewares || [ ] , HandlerType . MIDDLEWARE ) ;
45
40
46
41
this . registerMiddlewares ( ) ;
47
42
this . registerControllers ( ) ;
48
43
}
49
44
50
- private loadHandlers < T extends Object , U > (
51
- handlers : Array < Function | string > ,
52
- type : HandlerType
53
- ) : HandlerMetadata < T , U > [ ] {
45
+ private loadHandlers < T extends Object > ( handlers : Array < Function | string > , type : HandlerType ) : HandlerMetadata < T > [ ] {
54
46
const loadedHandlers : Function [ ] = [ ] ;
55
47
56
48
for ( const handler of handlers ) {
@@ -64,7 +56,7 @@ export class SocketControllers {
64
56
return loadedHandlers . map ( handler => {
65
57
return {
66
58
metadata : getMetadata ( handler ) ,
67
- instance : this . container . get ( handler ) ,
59
+ target : handler ,
68
60
} ;
69
61
} ) ;
70
62
}
@@ -101,7 +93,7 @@ export class SocketControllers {
101
93
const middlewaresWithNamespace = middlewares . filter ( middleware => ! ! middleware . metadata . namespace ) ;
102
94
103
95
for ( const middleware of middlewaresWithoutNamespace ) {
104
- this . registerMiddleware ( this . io as unknown as Namespace , middleware . instance ) ;
96
+ this . registerMiddleware ( this . io as unknown as Namespace , middleware ) ;
105
97
}
106
98
107
99
this . io . on ( 'new_namespace' , ( namespace : Namespace ) => {
@@ -116,7 +108,7 @@ export class SocketControllers {
116
108
} ) ;
117
109
118
110
if ( shouldApply ) {
119
- this . registerMiddleware ( namespace , middleware . instance ) ;
111
+ this . registerMiddleware ( namespace , middleware ) ;
120
112
}
121
113
}
122
114
} ) ;
@@ -132,7 +124,7 @@ export class SocketControllers {
132
124
}
133
125
} ) ;
134
126
135
- const controllerNamespaceMap : Record < string , HandlerMetadata < unknown , ControllerMetadata > [ ] > = { } ;
127
+ const controllerNamespaceMap : Record < string , HandlerMetadata < ControllerMetadata > [ ] > = { } ;
136
128
const controllerNamespaceRegExpMap : Record < string , string | RegExp > = { } ;
137
129
138
130
for ( const controller of controllersWithNamespace ) {
@@ -156,18 +148,18 @@ export class SocketControllers {
156
148
}
157
149
}
158
150
159
- private registerController ( socket : Socket , controller : HandlerMetadata < any , ControllerMetadata > ) {
151
+ private registerController ( socket : Socket , controller : HandlerMetadata < ControllerMetadata > ) {
160
152
const connectedAction = Object . values ( controller . metadata . actions || { } ) . find (
161
- action => action . type === ActionType . CONNECT
153
+ action => action . type === SocketEventType . CONNECT
162
154
) ;
163
155
const disconnectedAction = Object . values ( controller . metadata . actions || { } ) . find (
164
- action => action . type === ActionType . DISCONNECT
156
+ action => action . type === SocketEventType . DISCONNECT
165
157
) ;
166
158
const disconnectingAction = Object . values ( controller . metadata . actions || { } ) . find (
167
- action => action . type === ActionType . DISCONNECTING
159
+ action => action . type === SocketEventType . DISCONNECTING
168
160
) ;
169
161
const messageActions = Object . values ( controller . metadata . actions || { } ) . filter (
170
- action => action . type === ActionType . MESSAGE
162
+ action => action . type === SocketEventType . MESSAGE
171
163
) ;
172
164
173
165
if ( connectedAction ) {
@@ -195,20 +187,29 @@ export class SocketControllers {
195
187
messages . push ( ack ) ;
196
188
}
197
189
198
- this . executeAction ( socket , controller , messageAction , messages ) ;
190
+ this . executeAction ( socket , controller , messageAction , messageAction . options . name as string , messages ) ;
199
191
} ) ;
200
192
}
201
193
}
202
194
203
195
private executeAction (
204
196
socket : Socket ,
205
- controller : HandlerMetadata < any , ControllerMetadata > ,
197
+ controller : HandlerMetadata < ControllerMetadata > ,
206
198
action : ActionMetadata ,
199
+ eventName ?: string ,
207
200
data ?: any [ ]
208
201
) {
209
202
const parameters = this . resolveParameters ( socket , controller . metadata , action . parameters || [ ] , data ) ;
210
203
try {
211
- const actionResult = controller . instance [ action . methodName ] ( ...parameters ) ;
204
+ let container = this . container ;
205
+ if ( this . options . scopedContainerGetter ) {
206
+ container = this . options . scopedContainerGetter (
207
+ this . collectScopedContainerParams ( socket , action . type , eventName , data , controller . metadata . namespace )
208
+ ) ;
209
+ }
210
+
211
+ const controllerInstance : any = container . get ( controller . target ) ;
212
+ const actionResult = controllerInstance [ action . methodName ] ( ...parameters ) ;
212
213
Promise . resolve ( actionResult )
213
214
. then ( result => {
214
215
this . handleActionResult ( socket , action , result , ResultType . EMIT_ON_SUCCESS ) ;
@@ -251,9 +252,10 @@ export class SocketControllers {
251
252
}
252
253
}
253
254
254
- private registerMiddleware ( namespace : Namespace , middleware : MiddlewareInterface ) {
255
+ private registerMiddleware ( namespace : Namespace , middleware : HandlerMetadata < MiddlewareMetadata > ) {
255
256
namespace . use ( ( socket : Socket , next : ( err ?: any ) => void ) => {
256
- middleware . use ( socket , next ) ;
257
+ const instance : MiddlewareInterface = this . container . get ( middleware . target ) ;
258
+ instance . use ( socket , next ) ;
257
259
} ) ;
258
260
}
259
261
@@ -334,10 +336,27 @@ export class SocketControllers {
334
336
return value ;
335
337
}
336
338
339
+ private collectScopedContainerParams (
340
+ socket : Socket ,
341
+ eventType : SocketEventType ,
342
+ eventName ?: string ,
343
+ messageBody ?: any [ ] ,
344
+ namespace ?: string | RegExp
345
+ ) : ScopedContainerGetterParams {
346
+ return {
347
+ eventType,
348
+ eventName,
349
+ socket,
350
+ socketIo : this . io ,
351
+ nspParams : this . extractNamespaceParameters ( socket , namespace ) ,
352
+ messageArgs : messageBody ,
353
+ } ;
354
+ }
355
+
337
356
private extractNamespaceParameters (
338
357
socket : Socket ,
339
358
namespace : string | RegExp | undefined ,
340
- parameterMetadata : ParameterMetadata
359
+ parameterMetadata ? : ParameterMetadata
341
360
) {
342
361
const keys : any [ ] = [ ] ;
343
362
const regexp = namespace instanceof RegExp ? namespace : pathToRegexp ( namespace || '/' , keys ) ;
0 commit comments