1+ ( function ( ) {
2+ var module , __hasProp = { } . hasOwnProperty ;
3+ module = angular . module ( 'knalli.angular-vertxbus' , [ 'ng' ] ) . value ( 'enabled' , true ) . value ( 'debugEnabled' , false ) . value ( 'prefix' , 'vertx-eventbus.' ) . value ( 'urlServer' , '' + location . protocol + '//' + location . hostname + ':' + ( location . port || 80 ) ) . value ( 'urlPath' , '/eventbus' ) . value ( 'reconnectEnabled' , true ) . value ( 'sockjsStateInterval' , 10000 ) . value ( 'sockjsReconnectInterval' , 10000 ) . value ( 'sockjsOptions' , { } ) ;
4+ module . factory ( 'vertxEventBus' , [
5+ '$timeout' ,
6+ 'prefix' ,
7+ 'urlServer' ,
8+ 'urlPath' ,
9+ 'sockjsOptions' ,
10+ 'enabled' ,
11+ 'debugEnabled' ,
12+ 'reconnectEnabled' ,
13+ 'sockjsReconnectInterval' ,
14+ function ( $timeout , prefix , urlServer , urlPath , sockjsOptions , enabled , debugEnabled , reconnectEnabled , sockjsReconnectInterval ) {
15+ var EventBus_ , connect , eventBus , stub , url ;
16+ stub = null ;
17+ EventBus_ = typeof vertx !== 'undefined' && vertx !== null ? vertx . EventBus : void 0 ;
18+ if ( enabled && EventBus_ ) {
19+ url = '' + urlServer + urlPath ;
20+ if ( debugEnabled ) {
21+ console . debug ( '[Vertex EventBus] Enabled: connecting \'' + url + '\'' ) ;
22+ }
23+ eventBus = null ;
24+ connect = function ( ) {
25+ eventBus = new EventBus_ ( url , void 0 , sockjsOptions ) ;
26+ eventBus . onopen = function ( ) {
27+ if ( debugEnabled ) {
28+ console . debug ( '[VertX EventBus] Connected' ) ;
29+ }
30+ if ( typeof stub . onopen === 'function' ) {
31+ stub . onopen ( ) ;
32+ }
33+ } ;
34+ eventBus . onclose = function ( ) {
35+ if ( debugEnabled ) {
36+ console . debug ( '[VertX EventBus] Reconnect in ' + sockjsReconnectInterval + 'ms' ) ;
37+ }
38+ if ( typeof stub . onclose === 'function' ) {
39+ stub . onclose ( ) ;
40+ }
41+ if ( reconnectEnabled ) {
42+ $timeout ( connect , sockjsReconnectInterval ) ;
43+ }
44+ } ;
45+ } ;
46+ connect ( ) ;
47+ stub = {
48+ reconnect : function ( ) {
49+ return eventBus . close ( ) ;
50+ } ,
51+ close : function ( ) {
52+ return eventBus . close ( ) ;
53+ } ,
54+ login : function ( username , password , replyHandler ) {
55+ return eventBus . login ( username , password , replyHandler ) ;
56+ } ,
57+ send : function ( address , message , replyHandler ) {
58+ return eventBus . send ( address , message , replyHandler ) ;
59+ } ,
60+ publish : function ( address , message ) {
61+ return eventBus . publish ( address , message ) ;
62+ } ,
63+ registerHandler : function ( address , handler ) {
64+ return eventBus . registerHandler ( address , handler ) ;
65+ } ,
66+ unregisterHandler : function ( address , handler ) {
67+ return eventBus . unregisterHandler ( address , handler ) ;
68+ } ,
69+ readyState : function ( ) {
70+ return eventBus . readyState ( ) ;
71+ } ,
72+ EventBus : EventBus_
73+ } ;
74+ } else {
75+ if ( debugEnabled ) {
76+ console . debug ( '[VertX EventBus] Disabled' ) ;
77+ }
78+ }
79+ return stub ;
80+ }
81+ ] ) ;
82+ module . service ( 'vertxEventBusService' , [
83+ '$rootScope' ,
84+ '$q' ,
85+ '$interval' ,
86+ '$timeout' ,
87+ 'vertxEventBus' ,
88+ 'prefix' ,
89+ 'enabled' ,
90+ 'sockjsStateInterval' ,
91+ function ( $rootScope , $q , $interval , $timeout , vertxEventBus , prefix , enabled , sockjsStateInterval ) {
92+ var api , connectionState , util , wrapped , _ref ;
93+ connectionState = vertxEventBus != null ? ( _ref = vertxEventBus . EventBus ) != null ? _ref . CLOSED : void 0 : void 0 ;
94+ if ( enabled && vertxEventBus ) {
95+ vertxEventBus . onopen = function ( ) {
96+ var address , callback , callbacks , _i , _len , _ref1 ;
97+ wrapped . getConnectionState ( true ) ;
98+ $rootScope . $broadcast ( '' + prefix + 'system.connected' ) ;
99+ _ref1 = wrapped . handlers ;
100+ for ( address in _ref1 ) {
101+ if ( ! __hasProp . call ( _ref1 , address ) )
102+ continue ;
103+ callbacks = _ref1 [ address ] ;
104+ for ( _i = 0 , _len = callbacks . length ; _i < _len ; _i ++ ) {
105+ callback = callbacks [ _i ] ;
106+ util . registerHandler ( address , callback ) ;
107+ }
108+ }
109+ return $rootScope . $digest ( ) ;
110+ } ;
111+ vertxEventBus . onclose = function ( ) {
112+ wrapped . getConnectionState ( true ) ;
113+ return $rootScope . $broadcast ( '' + prefix + 'system.disconnected' ) ;
114+ } ;
115+ }
116+ util = {
117+ registerHandler : function ( address , callback ) {
118+ if ( typeof callback !== 'function' ) {
119+ return ;
120+ }
121+ if ( debugEnabled ) {
122+ console . debug ( '[VertX EventBus] Register handler for ' + address ) ;
123+ }
124+ return vertxEventBus . registerHandler ( address , function ( message , replyTo ) {
125+ callback ( message , replyTo ) ;
126+ return $rootScope . $digest ( ) ;
127+ } ) ;
128+ } ,
129+ unregisterHandler : function ( address , callback ) {
130+ if ( typeof callback !== 'function' ) {
131+ return ;
132+ }
133+ if ( debugEnabled ) {
134+ console . debug ( '[VertX EventBus] Unregister handler for ' + address ) ;
135+ }
136+ return vertxEventBus . unregisterHandler ( address , callback ) ;
137+ } ,
138+ send : function ( address , message , expectReply , timeout ) {
139+ var deferred ;
140+ if ( timeout == null ) {
141+ timeout = 10000 ;
142+ }
143+ if ( expectReply ) {
144+ deferred = $q . defer ( ) ;
145+ }
146+ vertxEventBus . send ( address , message , function ( reply ) {
147+ if ( deferred ) {
148+ deferred . resolve ( reply ) ;
149+ }
150+ if ( typeof expectReply === 'function' ) {
151+ return expectReply ( reply ) ;
152+ }
153+ } ) ;
154+ if ( deferred ) {
155+ $timeout ( function ( ) {
156+ return deferred . reject ( ) ;
157+ } , timeout ) ;
158+ }
159+ return deferred != null ? deferred . promise : void 0 ;
160+ } ,
161+ publish : function ( address , message ) {
162+ vertxEventBus . publish ( address , message ) ;
163+ return $q . resolve ( ) ;
164+ }
165+ } ;
166+ wrapped = {
167+ handlers : { } ,
168+ registerHandler : function ( address , callback ) {
169+ if ( ! wrapped . handlers [ address ] ) {
170+ wrapped . handlers [ address ] = [ ] ;
171+ }
172+ wrapped . handlers [ address ] . push ( callback ) ;
173+ if ( connectionState === vertxEventBus . EventBus . OPEN ) {
174+ return util . registerHandler ( address , callback ) ;
175+ }
176+ } ,
177+ unregisterHandler : function ( address , callback ) {
178+ var index ;
179+ if ( wrapped . handlers [ address ] && callback ( wrapped . handlers [ address ] ) ) {
180+ index = wrapped . handlers [ address ] . indexOf ( callback ) ;
181+ if ( index > - 1 ) {
182+ wrapped . handlers [ address ] . splice ( index , 1 ) ;
183+ }
184+ }
185+ if ( connectionState === vertxEventBus . EventBus . OPEN ) {
186+ return util . unregisterHandler ( address , callback ) ;
187+ }
188+ } ,
189+ send : function ( address , message , expectReply , timeout ) {
190+ if ( timeout == null ) {
191+ timeout = 10000 ;
192+ }
193+ if ( connectionState === vertxEventBus . EventBus . OPEN ) {
194+ return util . send ( address , message , expectReply , timeout ) ;
195+ } else {
196+ return $q . reject ( ) ;
197+ }
198+ } ,
199+ publish : function ( address , message ) {
200+ if ( connectionState === vertxEventBus . EventBus . OPEN ) {
201+ return util . publish ( address , message ) ;
202+ } else {
203+ return $q . reject ( ) ;
204+ }
205+ } ,
206+ getConnectionState : function ( immediate ) {
207+ if ( enabled && vertxEventBus ) {
208+ if ( immediate ) {
209+ connectionState = vertxEventBus . readyState ( ) ;
210+ }
211+ } else {
212+ connectionState = vertxEventBus . EventBus . CLOSED ;
213+ }
214+ return connectionState ;
215+ }
216+ } ;
217+ $interval ( function ( ) {
218+ return wrapped . getConnectionState ( true ) ;
219+ } , sockjsStateInterval ) ;
220+ api = {
221+ on : wrapped . registerHandler ,
222+ addListener : wrapped . registerHandler ,
223+ un : wrapped . unregisterHandler ,
224+ removeListener : wrapped . unregisterHandler ,
225+ send : wrapped . send ,
226+ publish : wrapped . publish ,
227+ emit : wrapped . publish ,
228+ readyState : wrapped . getConnectionState ,
229+ isEnabled : function ( ) {
230+ return enabled ;
231+ }
232+ } ;
233+ return api ;
234+ }
235+ ] ) ;
236+ } . call ( this ) ) ;
0 commit comments