22var ng = angular . module ( 'btford.socket-io' , [ ] )
33ng . provider ( 'socketFactory' , function ( ) {
44 this . $get = function ( $rootScope ) {
5-
6- var obj = { }
7- obj . events = { }
8- obj . emits = { }
9-
10- // intercept 'on' calls and capture the callbacks
11- obj . on = function ( eventName , callback ) {
12- if ( ! this . events [ eventName ] ) this . events [ eventName ] = [ ]
13- this . events [ eventName ] . push ( callback )
14- }
15-
16- // intercept 'emit' calls from the client and record them to assert against in the test
17- obj . emit = function ( eventName ) {
18- var args = Array . prototype . slice . call ( arguments , 1 )
19-
20- if ( ! this . emits [ eventName ] )
21- this . emits [ eventName ] = [ ]
22- this . emits [ eventName ] . push ( args )
23- }
24-
25- //simulate an inbound message to the socket from the server (only called from the test)
26- obj . receive = function ( eventName ) {
27- var args = Array . prototype . slice . call ( arguments , 1 )
28-
29- if ( this . events [ eventName ] ) {
30-
31- angular . forEach ( this . events [ eventName ] , function ( callback ) {
32- $rootScope . $apply ( function ( ) {
33-
34- callback . apply ( this , args )
35- } )
36- } )
37- }
38- }
39-
40- return obj
41- }
42- } )
5+ return function socketFactory ( ) {
6+ var obj = { } ;
7+ obj . events = { } ;
8+ obj . emits = { } ;
9+
10+ // intercept 'on' calls and capture the callbacks
11+ obj . on = function ( eventName , callback ) {
12+ if ( ! this . events [ eventName ] ) this . events [ eventName ] = [ ] ;
13+ this . events [ eventName ] . push ( callback ) ;
14+ } ;
15+
16+ // intercept 'emit' calls from the client and record them to assert against in the test
17+ obj . emit = function ( eventName ) {
18+ var args = Array . prototype . slice . call ( arguments , 1 ) ;
19+
20+ if ( ! this . emits [ eventName ] )
21+ this . emits [ eventName ] = [ ] ;
22+ this . emits [ eventName ] . push ( args ) ;
23+ } ;
24+
25+ //simulate an inbound message to the socket from the server (only called from the test)
26+ obj . receive = function ( eventName ) {
27+ var args = Array . prototype . slice . call ( arguments , 1 ) ;
28+
29+ if ( this . events [ eventName ] ) {
30+ angular . forEach ( this . events [ eventName ] , function ( callback ) {
31+ $rootScope . $apply ( function ( ) {
32+ callback . apply ( this , args )
33+ } ) ;
34+ } ) ;
35+ } ;
36+ } ;
37+
38+ return obj ;
39+ } ;
40+ } ;
41+ } ) ;
0 commit comments