@@ -8,7 +8,7 @@ import { HandlerType } from './types/enums/HandlerType';
88import { SocketControllersOptions } from './types/SocketControllersOptions' ;
99import { ControllerMetadata } from './types/ControllerMetadata' ;
1010import { MiddlewareMetadata } from './types/MiddlewareMetadata' ;
11- import { ActionType } from './types/enums/ActionType ' ;
11+ import { SocketEventType } from './types/enums/SocketEventType ' ;
1212import { ActionMetadata } from './types/ActionMetadata' ;
1313import { ParameterMetadata } from './types/ParameterMetadata' ;
1414import { ParameterType } from './types/enums/ParameterType' ;
@@ -18,12 +18,13 @@ import { TransformOptions } from './types/TransformOptions';
1818import { defaultTransformOptions } from './types/constants/defaultTransformOptions' ;
1919import { ActionTransformOptions } from './types/ActionTransformOptions' ;
2020import { instanceToPlain , plainToInstance } from 'class-transformer' ;
21+ import { ScopedContainerGetterParams } from './types/ScopedContainerGetterParams' ;
2122import { MiddlewareInterface } from './types/MiddlewareInterface' ;
2223
2324export class SocketControllers {
2425 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 > [ ] ;
2728 public io : Server ;
2829 public transformOptions : TransformOptions ;
2930
@@ -34,23 +35,14 @@ export class SocketControllers {
3435 ...defaultTransformOptions ,
3536 ...options . transformOption ,
3637 } ;
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 ) ;
4540
4641 this . registerMiddlewares ( ) ;
4742 this . registerControllers ( ) ;
4843 }
4944
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 > [ ] {
5446 const loadedHandlers : Function [ ] = [ ] ;
5547
5648 for ( const handler of handlers ) {
@@ -64,7 +56,7 @@ export class SocketControllers {
6456 return loadedHandlers . map ( handler => {
6557 return {
6658 metadata : getMetadata ( handler ) ,
67- instance : this . container . get ( handler ) ,
59+ target : handler ,
6860 } ;
6961 } ) ;
7062 }
@@ -101,7 +93,7 @@ export class SocketControllers {
10193 const middlewaresWithNamespace = middlewares . filter ( middleware => ! ! middleware . metadata . namespace ) ;
10294
10395 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 ) ;
10597 }
10698
10799 this . io . on ( 'new_namespace' , ( namespace : Namespace ) => {
@@ -116,7 +108,7 @@ export class SocketControllers {
116108 } ) ;
117109
118110 if ( shouldApply ) {
119- this . registerMiddleware ( namespace , middleware . instance ) ;
111+ this . registerMiddleware ( namespace , middleware ) ;
120112 }
121113 }
122114 } ) ;
@@ -132,7 +124,7 @@ export class SocketControllers {
132124 }
133125 } ) ;
134126
135- const controllerNamespaceMap : Record < string , HandlerMetadata < unknown , ControllerMetadata > [ ] > = { } ;
127+ const controllerNamespaceMap : Record < string , HandlerMetadata < ControllerMetadata > [ ] > = { } ;
136128 const controllerNamespaceRegExpMap : Record < string , string | RegExp > = { } ;
137129
138130 for ( const controller of controllersWithNamespace ) {
@@ -156,18 +148,18 @@ export class SocketControllers {
156148 }
157149 }
158150
159- private registerController ( socket : Socket , controller : HandlerMetadata < any , ControllerMetadata > ) {
151+ private registerController ( socket : Socket , controller : HandlerMetadata < ControllerMetadata > ) {
160152 const connectedAction = Object . values ( controller . metadata . actions || { } ) . find (
161- action => action . type === ActionType . CONNECT
153+ action => action . type === SocketEventType . CONNECT
162154 ) ;
163155 const disconnectedAction = Object . values ( controller . metadata . actions || { } ) . find (
164- action => action . type === ActionType . DISCONNECT
156+ action => action . type === SocketEventType . DISCONNECT
165157 ) ;
166158 const disconnectingAction = Object . values ( controller . metadata . actions || { } ) . find (
167- action => action . type === ActionType . DISCONNECTING
159+ action => action . type === SocketEventType . DISCONNECTING
168160 ) ;
169161 const messageActions = Object . values ( controller . metadata . actions || { } ) . filter (
170- action => action . type === ActionType . MESSAGE
162+ action => action . type === SocketEventType . MESSAGE
171163 ) ;
172164
173165 if ( connectedAction ) {
@@ -195,20 +187,29 @@ export class SocketControllers {
195187 messages . push ( ack ) ;
196188 }
197189
198- this . executeAction ( socket , controller , messageAction , messages ) ;
190+ this . executeAction ( socket , controller , messageAction , messageAction . options . name as string , messages ) ;
199191 } ) ;
200192 }
201193 }
202194
203195 private executeAction (
204196 socket : Socket ,
205- controller : HandlerMetadata < any , ControllerMetadata > ,
197+ controller : HandlerMetadata < ControllerMetadata > ,
206198 action : ActionMetadata ,
199+ eventName ?: string ,
207200 data ?: any [ ]
208201 ) {
209202 const parameters = this . resolveParameters ( socket , controller . metadata , action . parameters || [ ] , data ) ;
210203 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 ) ;
212213 Promise . resolve ( actionResult )
213214 . then ( result => {
214215 this . handleActionResult ( socket , action , result , ResultType . EMIT_ON_SUCCESS ) ;
@@ -251,9 +252,10 @@ export class SocketControllers {
251252 }
252253 }
253254
254- private registerMiddleware ( namespace : Namespace , middleware : MiddlewareInterface ) {
255+ private registerMiddleware ( namespace : Namespace , middleware : HandlerMetadata < MiddlewareMetadata > ) {
255256 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 ) ;
257259 } ) ;
258260 }
259261
@@ -334,10 +336,27 @@ export class SocketControllers {
334336 return value ;
335337 }
336338
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+
337356 private extractNamespaceParameters (
338357 socket : Socket ,
339358 namespace : string | RegExp | undefined ,
340- parameterMetadata : ParameterMetadata
359+ parameterMetadata ? : ParameterMetadata
341360 ) {
342361 const keys : any [ ] = [ ] ;
343362 const regexp = namespace instanceof RegExp ? namespace : pathToRegexp ( namespace || '/' , keys ) ;
0 commit comments