'use strict' ; import { app } from '../../config/config' ; const listener = [ ] ; export const cacheListener = [ ] ; const typeMap = [ 'new' , 'change' , 'refresh' , 'remove' ] ; export const socketListener = ( type , namespace , callback ? ) => { if ( ! type || ! namespace || ! app . socket || typeMap . indexOf ( type ) === - 1 ) { return ; } const socket = app . socket ; if ( listener . indexOf ( `:${ type }:${ namespace }` ) !== - 1 ) { return ; } listener . push ( `:${ type }:${ namespace }` ) ; let newMsgCallback = ( data ) => { console . log ( data , `:new:${ namespace }` ) ; } ; let changeMsgCallback = ( data ) => { console . log ( data , `:change:${ namespace }` ) ; } ; let refreshMsgCallback = ( _id ) => { console . log ( _id , `:refresh:${ namespace }` ) ; } ; let removeMsgCallback = ( _id ) => { console . log ( _id , `:remove:${ namespace }` ) ; } ; let changeIsArchivedCallback = ( model , isArchived ) => { console . log ( model , isArchived ) ; } ; let buildCallback = ( type ) => { return ( ... args ) => { if ( callback ) { args . unshift ( type ) ; callback . apply ( null , args ) ; } else { switch ( type ) { case 'new' : newMsgCallback . apply ( null , args ) ; break ; case 'change' : changeMsgCallback . apply ( null , args ) ; break ; case 'refresh' : refreshMsgCallback . apply ( null , args ) ; break ; case 'remove' : removeMsgCallback . apply ( null , args ) ; break ; case 'isArchived' : changeIsArchivedCallback . apply ( null , args ) ; break ; } } } ; } ; if ( name !== 'project' ) { if ( ! socket ) { cacheListener . push ( ( ) => { socket . on ( 'change:isArchived' , buildCallback ( 'change' ) ) ; } ) ; } else { socket . on ( 'change:isArchived' , buildCallback ( 'change' ) ) ; } } if ( ! socket ) { cacheListener . push ( ( ) => { socket . on ( `:${ type }:${ namespace }` , buildCallback ( type ) ) ; } ) ; } else { socket . on ( `:${ type }:${ namespace }` , buildCallback ( type ) ) ; } } ;