11
22// Require dependencies
33import uuid from 'uuid' ;
4- import fetch from 'node-fetch' ;
54import config from 'config' ;
65import Daemon from 'daemon' ;
76import session from 'express-session' ;
87import socketio from 'socket.io' ;
98import SessionStore from '@edenjs/session-store' ;
109import cookieParser from 'cookie-parser' ;
1110
12- // Require cache dependencies
13- const calls = cache ( 'calls' ) ;
14-
1511// require models
1612const User = model ( 'user' ) ;
1713
@@ -23,6 +19,7 @@ const aclHelper = helper('user/acl');
2319 *
2420 * @cluster front
2521 * @cluster socket
22+ * @priority 1000
2623 */
2724export default class SocketDaemon extends Daemon {
2825 /**
@@ -75,6 +72,11 @@ export default class SocketDaemon extends Daemon {
7572 * Build chat daemon
7673 */
7774 build ( ) {
75+ // initializing
76+ this . logger . log ( 'info' , 'initializing socket.io' , {
77+ class : this . constructor . name
78+ } ) ;
79+
7880 // Set io
7981 this . __socketIO = socketio ( this . eden . router . app . server ) ;
8082
@@ -318,19 +320,26 @@ export default class SocketDaemon extends Daemon {
318320 // Reload user
319321 if ( user ) await user . refresh ( ) ;
320322
321- // Loop endpoints
322- const matched = calls . filter ( ( call ) => {
323- // Remove call
324- return data . name === call . path ;
323+ // load calls
324+ let call = null ;
325+ const ctrl = Object . values ( this . eden . get ( 'controllers' ) ) . find ( ( ctrl ) => {
326+ // return found
327+ const subCall = ctrl . calls . find ( ( c ) => c . path === data . name ) ;
328+
329+ // set call
330+ if ( subCall ) call = subCall ;
331+
332+ // return sub call
333+ return subCall ;
325334 } ) ;
326335
327- // Loop matched
328- matched . forEach ( async ( call ) => {
336+ // run function
337+ if ( ctrl ) {
329338 // check ACL
330339 if ( call . acl && ! await aclHelper . validate ( user , call . acl ) ) return ;
331340
332341 // get controller
333- const controller = await this . eden . controller ( call . file ) ;
342+ const controller = this . eden . get ( `controller. ${ ctrl . data . file } ` ) ;
334343
335344 // Set opts
336345 const opts = {
@@ -346,11 +355,11 @@ export default class SocketDaemon extends Daemon {
346355 await this . eden . hook ( 'socket.call.opts' , opts ) ;
347356
348357 // Run endpoint
349- const response = await controller [ call . fn ] . apply ( controller , [ ...data . args , opts ] ) ;
358+ const response = await controller [ call . fn ] ( ...data . args , opts ) ;
350359
351360 // Return response
352361 socket . emit ( data . id , response ) ;
353- } ) ;
362+ }
354363 }
355364
356365 /**
@@ -383,7 +392,6 @@ export default class SocketDaemon extends Daemon {
383392 // create faux request and response
384393 let req = { ...socket . request } ;
385394 let res = { ...socket . request . res , send : ( data , code = 200 ) => {
386- console . log ( 'send' , data ) ;
387395 // await text
388396 socket . emit ( data . id , code , JSON . stringify ( data ) ) ;
389397 } , end : ( data , code = 200 ) => {
0 commit comments