20
20
angular .module (' knalli.angular-vertxbus' )
21
21
.provider (' vertxEventBusService' , () ->
22
22
23
+ CONSTANTS =
24
+ MODULE : ' angular-vertxbus'
25
+ COMPONENT : ' service'
26
+
23
27
DEFAULT_OPTIONS =
24
28
loginRequired : false
25
29
loginBlockForSession : false # NYI
@@ -88,16 +92,19 @@ angular.module('knalli.angular-vertxbus')
88
92
@ requireLogin = (value = options .loginRequired ) ->
89
93
options .loginRequired = value
90
94
return this
95
+ @requireLogin .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : requireLogin"
91
96
92
97
# private: NYI
93
98
@ blockForSession = (value = options .loginBlockForSession ) ->
94
99
options .loginBlockForSession = value
95
100
return this
101
+ @blockForSession .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : blockForSession"
96
102
97
103
# private: NYI
98
104
@ skipUnauthorizeds = (value = options .skipUnauthorizeds ) ->
99
105
options .skipUnauthorizeds = value
100
106
return this
107
+ @skipUnauthorizeds .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : skipUnauthorizeds"
101
108
102
109
@ $get = ($rootScope , $q , $interval , $timeout , vertxEventBus ) ->
103
110
# Extract options (with defaults)
@@ -131,6 +138,7 @@ angular.module('knalli.angular-vertxbus')
131
138
vertxEventBus .onclose = ->
132
139
wrapped .getConnectionState (true )
133
140
$rootScope .$broadcast " #{ prefix} system.disconnected"
141
+ vertxEventBus .onclose .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : 'onclose' handler"
134
142
135
143
ensureOpenConnection = (fn ) ->
136
144
if wrapped .getConnectionState () is vertxEventBus .EventBus .OPEN
@@ -140,20 +148,24 @@ angular.module('knalli.angular-vertxbus')
140
148
messageQueueHolder .push (fn)
141
149
return true
142
150
return false
151
+ ensureOpenConnection .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : ensureOpenConnection"
143
152
144
153
ensureOpenAuthConnection = (fn ) ->
145
154
unless options .loginRequired
146
155
# easy: no login required
147
156
ensureOpenConnection fn
148
157
else
149
- ensureOpenConnection ->
158
+ wrapFn = ->
150
159
if validSession
151
160
fn ()
152
161
return true
153
162
else
154
163
# ignore this message
155
164
console .debug (" [VertX EB Service] Message was not sent because login is required" ) if debugEnabled
156
165
return false
166
+ wrapFn .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : ensureOpenAuthConnection function wrapper"
167
+ ensureOpenConnection wrapFn
168
+ ensureOpenAuthConnection .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : ensureOpenAuthConnection"
157
169
158
170
# All utility methods working directly on the event bus object.
159
171
# The object "vertxEventBus" must be available.
@@ -163,10 +175,11 @@ angular.module('knalli.angular-vertxbus')
163
175
return unless typeof callback is ' function'
164
176
console .debug (" [VertX EB Service] Register handler for #{ address} " ) if debugEnabled
165
177
return fnWrapperMap .get (callback) if fnWrapperMap .containsKey (callback) # already known
166
- fnWrapperMap . put (callback, (message , replyTo ) ->
178
+ deconstructor = (message , replyTo ) ->
167
179
callback (message, replyTo)
168
180
$rootScope .$digest ()
169
- )
181
+ deconstructor .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.registerHandler (deconstructor)"
182
+ fnWrapperMap .put (callback, deconstructor)
170
183
vertxEventBus .registerHandler address, fnWrapperMap .get (callback)
171
184
# Remove a callback handler for the specified address match.
172
185
unregisterHandler : (address , callback ) ->
@@ -181,36 +194,48 @@ angular.module('knalli.angular-vertxbus')
181
194
# @param timeout an optional number for a timout after which the promise will be rejected
182
195
send : (address , message , timeout = 10000 ) ->
183
196
deferred = $q .defer ()
184
- dispatched = ensureOpenAuthConnection ->
197
+ next = ->
185
198
vertxEventBus .send address, message, (reply ) ->
186
199
if deferred then deferred .resolve reply
187
200
# Register timeout for promise rejecting.
188
201
if deferred then $timeout (-> deferred .reject ()), timeout
202
+ next .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.send (ensureOpenAuthConnection callback)"
203
+ dispatched = ensureOpenAuthConnection next
189
204
if deferred and ! dispatched then deferred .reject ()
190
205
return deferred ? .promise
191
206
# Publish a message to the specified address (using EventBus.publish).
192
207
# @param address a required string for the targeting address in the bus
193
208
# @param message a required piece of message data
194
209
publish : (address , message ) ->
195
- dispatched = ensureOpenAuthConnection ->
210
+ next = ->
196
211
vertxEventBus .publish address, message
212
+ next .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.publish (ensureOpenAuthConnection callback)"
213
+ dispatched = ensureOpenAuthConnection next
197
214
return dispatched
198
215
# Send a login message
199
216
# @param username
200
217
# @param password
201
218
# @param timeout
202
219
login : (username , password , timeout = 5000 ) ->
203
220
deferred = $q .defer ()
204
- vertxEventBus . login username, password, (reply ) ->
221
+ next = (reply ) ->
205
222
if reply ? .status is ' ok'
206
223
deferred .resolve reply
207
224
$rootScope .$broadcast " #{ prefix} system.login.succeeded" , (status : reply ? .status )
208
225
else
209
226
deferred .reject reply
210
227
$rootScope .$broadcast " #{ prefix} system.login.failed" , (status : reply ? .status )
228
+ next .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.login (callback)"
229
+ vertxEventBus .login username, password, next
211
230
$timeout (-> deferred .reject ()), timeout
212
231
return deferred .promise
213
232
233
+ util .registerHandler .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.registerHandler"
234
+ util .unregisterHandler .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.unregisterHandler"
235
+ util .send .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.send"
236
+ util .publish .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.publish"
237
+ util .login .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : util.login"
238
+
214
239
# Wrapping methods for the api
215
240
wrapped =
216
241
# Store of all handlers using as a cache when the event bus is not online
@@ -266,8 +291,18 @@ angular.module('knalli.angular-vertxbus')
266
291
validSession = false
267
292
return reply
268
293
294
+ wrapped .registerHandler .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : wrapped.registerHandler"
295
+ wrapped .unregisterHandler .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : wrapped.unregisterHandler"
296
+ wrapped .send .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : wrapped.send"
297
+ wrapped .publish .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : wrapped.publish"
298
+ wrapped .getConnectionState .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : wrapped.getConnectionState"
299
+ wrapped .isValidSession .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : wrapped.isValidSession"
300
+ wrapped .login .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : wrapped.login"
301
+
269
302
# Update the current connection state periodially.
270
- $interval (-> wrapped .getConnectionState (true )), sockjsStateInterval
303
+ connectionIntervalCheck = -> wrapped .getConnectionState (true )
304
+ connectionIntervalCheck .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : periodic connection check"
305
+ $interval connectionIntervalCheck, sockjsStateInterval
271
306
272
307
### building and exposing the actual service API ###
273
308
return (
@@ -284,6 +319,7 @@ angular.module('knalli.angular-vertxbus')
284
319
isValidSession : -> validSession
285
320
login : wrapped .login
286
321
)
322
+ @$get .displayName = " #{ CONSTANTS .MODULE } /#{ CONSTANTS .COMPONENT } : initializer"
287
323
288
324
return # void
289
325
)
0 commit comments