@@ -15,7 +15,13 @@ export interface SocketOptions {
15
15
getParams : ( socket : any ) => RealTimeConnection ;
16
16
}
17
17
18
- export function socket ( { done, emit, socketMap, socketKey, getParams } : SocketOptions ) {
18
+ export function socket ( {
19
+ done,
20
+ emit,
21
+ socketMap,
22
+ socketKey,
23
+ getParams
24
+ } : SocketOptions ) {
19
25
return ( app : Application ) => {
20
26
const leaveChannels = ( connection : RealTimeConnection ) => {
21
27
const { channels } = app ;
@@ -39,53 +45,71 @@ export function socket ({ done, emit, socketMap, socketKey, getParams }: SocketO
39
45
} ) ;
40
46
41
47
// `connection` event
42
- done . then ( provider => provider . on ( 'connection' , ( connection : any ) =>
43
- app . emit ( 'connection' , getParams ( connection ) ) )
48
+ done . then ( ( provider ) =>
49
+ provider . on ( 'connection' , ( connection : any ) =>
50
+ app . emit ( 'connection' , getParams ( connection ) )
51
+ )
44
52
) ;
45
53
46
54
// `socket.emit('methodName', 'serviceName', ...args)` handlers
47
- done . then ( provider => provider . on ( 'connection' , ( connection : any ) => {
48
- for ( const method of app . methods ) {
49
- connection . on ( method , ( ...args : any [ ] ) => {
50
- const path = args . shift ( ) ;
55
+ done . then ( ( provider ) =>
56
+ provider . on ( 'connection' , ( connection : any ) => {
57
+ for ( const method of app . methods ) {
58
+ connection . on ( method , ( ...args : any [ ] ) => {
59
+ const [ path , ...rest ] = args ;
51
60
52
- debug ( `Got '${ method } ' call for service '${ path } '` ) ;
53
- runMethod ( app , getParams ( connection ) , path , method , args ) ;
54
- } ) ;
55
- }
56
-
57
- connection . on ( 'authenticate' , ( ...args : any [ ] ) => {
58
- if ( app . get ( 'defaultAuthentication' ) ) {
59
- debug ( 'Got legacy authenticate event' ) ;
60
- runMethod ( app , getParams ( connection ) , app . get ( 'defaultAuthentication' ) , 'create' , args ) ;
61
+ runMethod ( app , getParams ( connection ) , path , method , rest ) ;
62
+ } ) ;
61
63
}
62
- } ) ;
63
64
64
- connection . on ( 'logout' , ( callback : any ) => {
65
- if ( app . get ( 'defaultAuthentication' ) ) {
66
- debug ( 'Got legacy authenticate event' ) ;
67
- runMethod ( app , getParams ( connection ) , app . get ( 'defaultAuthentication' ) , 'remove' , [ null , { } , callback ] ) ;
68
- }
69
- } ) ;
70
- } ) ) ;
65
+ connection . on ( 'authenticate' , ( ...args : any [ ] ) => {
66
+ if ( app . get ( 'defaultAuthentication' ) ) {
67
+ debug ( 'Got legacy authenticate event' ) ;
68
+ runMethod (
69
+ app ,
70
+ getParams ( connection ) ,
71
+ app . get ( 'defaultAuthentication' ) ,
72
+ 'create' ,
73
+ args
74
+ ) ;
75
+ }
76
+ } ) ;
77
+
78
+ connection . on ( 'logout' , ( callback : any ) => {
79
+ if ( app . get ( 'defaultAuthentication' ) ) {
80
+ debug ( 'Got legacy authenticate event' ) ;
81
+ runMethod (
82
+ app ,
83
+ getParams ( connection ) ,
84
+ app . get ( 'defaultAuthentication' ) ,
85
+ 'remove' ,
86
+ [ null , { } , callback ]
87
+ ) ;
88
+ }
89
+ } ) ;
90
+ } )
91
+ ) ;
71
92
72
93
// Legacy `socket.emit('serviceName::methodName', ...args)` handlers
73
- app . mixins . push ( ( service , path ) => done . then ( provider => {
74
- provider . on ( 'connection' , ( socket : any ) => {
75
- const methods = app . methods . filter ( current =>
76
- // @ts -ignore
77
- typeof service [ current ] === 'function'
78
- ) ;
94
+ app . mixins . push ( ( service , path ) =>
95
+ done . then ( ( provider ) => {
96
+ provider . on ( 'connection' , ( socket : any ) => {
97
+ const methods = app . methods . filter (
98
+ ( current ) =>
99
+ // @ts -ignore
100
+ typeof service [ current ] === 'function'
101
+ ) ;
79
102
80
- for ( const method of methods ) {
81
- const eventName = `${ path } ::${ method } ` ;
103
+ for ( const method of methods ) {
104
+ const eventName = `${ path } ::${ method } ` ;
82
105
83
- socket . on ( eventName , ( ...args : any [ ] ) => {
84
- debug ( `Got legacy method call '${ eventName } '` ) ;
85
- runMethod ( app , getParams ( socket ) , path , method , args ) ;
86
- } ) ;
87
- }
88
- } ) ;
89
- } ) ) ;
106
+ socket . on ( eventName , ( ...args : any [ ] ) => {
107
+ debug ( `Got legacy method call '${ eventName } '` ) ;
108
+ runMethod ( app , getParams ( socket ) , path , method , args ) ;
109
+ } ) ;
110
+ }
111
+ } ) ;
112
+ } )
113
+ ) ;
90
114
} ;
91
115
}
0 commit comments