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