Skip to content
This repository has been archived by the owner on Jun 14, 2019. It is now read-only.

Commit

Permalink
feat(): replace console w/ $log
Browse files Browse the repository at this point in the history
Solves #34
  • Loading branch information
knalli committed Dec 18, 2014
1 parent ef4bfe5 commit 3c9be73
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/service.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ angular.module('knalli.angular-vertxbus')
return this
@skipUnauthorizeds.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: provider.skipUnauthorizeds"

@$get = ($rootScope, $q, $interval, $timeout, vertxEventBus) ->
@$get = ($rootScope, $q, $interval, $timeout, vertxEventBus, $log) ->
# Extract options (with defaults)
{ enabled, debugEnabled, prefix, urlServer, urlPath, reconnectEnabled,
sockjsStateInterval, sockjsReconnectInterval, sockjsOptions,
Expand Down Expand Up @@ -181,7 +181,7 @@ angular.module('knalli.angular-vertxbus')
return true
else
# ignore this message
console.debug("[Vert.x EB Service] Message was not sent because login is required") if debugEnabled
$log.debug("[Vert.x EB Service] Message was not sent because login is required") if debugEnabled
return false
wrapFn.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: ensureOpenAuthConnection function wrapper"
ensureOpenConnection wrapFn
Expand All @@ -193,7 +193,7 @@ angular.module('knalli.angular-vertxbus')
# Register a callback handler for the specified address match.
registerHandler : (address, callback) ->
return unless typeof callback is 'function'
console.debug("[Vert.x EB Service] Register handler for #{address}") if debugEnabled
$log.debug("[Vert.x EB Service] Register handler for #{address}") if debugEnabled
return deconstructors.get(callback) if deconstructors.containsKey(callback) # already known
deconstructor = (message, replyTo) ->
callback(message, replyTo)
Expand All @@ -205,7 +205,7 @@ angular.module('knalli.angular-vertxbus')
# Remove a callback handler for the specified address match.
unregisterHandler : (address, callback) ->
return unless typeof callback is 'function'
console.debug("[Vert.x EB Service] Unregister handler for #{address}") if debugEnabled
$log.debug("[Vert.x EB Service] Unregister handler for #{address}") if debugEnabled
vertxEventBus.unregisterHandler address, deconstructors.get(callback)
deconstructors.remove(callback)
return #void
Expand Down
10 changes: 5 additions & 5 deletions src/wrapper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ angular.module('knalli.angular-vertxbus')
Furthermore, the stub supports theses extra APIs:
- recconnect()
###
@$get = ($timeout) ->
@$get = ($timeout, $log) ->
# Extract options (with defaults)
{ enabled, debugEnabled, prefix, urlServer, urlPath, reconnectEnabled,
sockjsStateInterval, sockjsReconnectInterval, sockjsOptions
Expand All @@ -111,18 +111,18 @@ angular.module('knalli.angular-vertxbus')

if enabled and EventBusOriginal
url = "#{urlServer}#{urlPath}"
console.debug("[Vert.x EB Stub] Enabled: connecting '#{url}'") if debugEnabled
$log.debug("[Vert.x EB Stub] Enabled: connecting '#{url}'") if debugEnabled
# Because we have rebuild an EventBus object (because it have to rebuild a SockJS object)
# we must wrap the object. Therefore, we have to mimic the behavior of onopen and onclose each time.
eventBus = null
connect = ->
eventBus = new EventBusOriginal url, undefined, sockjsOptions
eventBus.onopen = ->
console.debug("[Vert.x EB Stub] Connected") if debugEnabled
$log.debug("[Vert.x EB Stub] Connected") if debugEnabled
EventBusStub.onopen() if typeof EventBusStub.onopen is 'function'
return #void
eventBus.onclose = ->
console.debug("[Vert.x EB Stub] Reconnect in #{sockjsReconnectInterval}ms") if debugEnabled
$log.debug("[Vert.x EB Stub] Reconnect in #{sockjsReconnectInterval}ms") if debugEnabled
EventBusStub.onclose() if typeof EventBusStub.onclose is 'function'
$timeout(connect, sockjsReconnectInterval) if reconnectEnabled
return #void
Expand Down Expand Up @@ -168,7 +168,7 @@ angular.module('knalli.angular-vertxbus')
EventBusStub.getOptions.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusStub.getOptions"

else
console.debug("[Vert.x EB Stub] Disabled") if debugEnabled
$log.debug("[Vert.x EB Stub] Disabled") if debugEnabled

return EventBusStub

Expand Down
20 changes: 12 additions & 8 deletions test/unit/angularVertxbusAdapterSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* jshint camelcase: false, undef: true, unused: true, browser: true */
/* global console: false, module: false, describe: false, it: false, expect: false, beforeEach: false, inject: false, SockJS: false */
/* global module: false, describe: false, it: false, expect: false, beforeEach: false, inject: false, SockJS: false */

describe('knalli.angular-vertxbus', function () {

Expand All @@ -20,17 +20,19 @@ describe('knalli.angular-vertxbus', function () {

describe('vertxEventBus', function () {

var vertxEventBus, $timeout, $rootScope;
var vertxEventBus, $timeout, $rootScope, $log;

beforeEach(module('knalli.angular-vertxbus', function (vertxEventBusProvider) {
// Override (improve test running time)
vertxEventBusProvider.useDebug(true).useSockJsReconnectInterval(2000);
}));

beforeEach(inject(function (_vertxEventBus_, _$timeout_, _$rootScope_) {
beforeEach(inject(function (_vertxEventBus_, _$timeout_, _$rootScope_, _$log_) {
vertxEventBus = _vertxEventBus_;
$timeout = _$timeout_;
$rootScope = _$rootScope_;
$log = _$log_;
SockJS.currentMockInstance.$log = $log;
}));

it('should be an object', function () {
Expand Down Expand Up @@ -88,11 +90,11 @@ describe('knalli.angular-vertxbus', function () {
var okClose = false, okOpen = false;
setTimeout(function () {
vertxEventBus.onclose = function () {
console.debug('[TEST] onclose() called');
$log.debug('[TEST] onclose() called');
okClose = true;
};
vertxEventBus.onopen = function () {
console.debug('[TEST] onopen() called');
$log.debug('[TEST] onopen() called');
okOpen = true;
};
vertxEventBus.reconnect();
Expand Down Expand Up @@ -486,23 +488,25 @@ describe('knalli.angular-vertxbus', function () {
});

describe('should not send message', function () {
var vertxEventBus, vertxEventBusService, $rootScope, $timeout, result;
var vertxEventBus, vertxEventBusService, $rootScope, $timeout, $log, result;

beforeEach(module('knalli.angular-vertxbus', function (vertxEventBusProvider, vertxEventBusServiceProvider) {
vertxEventBusProvider.useMessageBuffer(0);
vertxEventBusServiceProvider.requireLogin(true);
}));

beforeEach(inject(function (_vertxEventBus_, _vertxEventBusService_, _$rootScope_, _$timeout_) {
beforeEach(inject(function (_vertxEventBus_, _vertxEventBusService_, _$rootScope_, _$timeout_, _$log_) {
vertxEventBus = _vertxEventBus_;
vertxEventBusService = _vertxEventBusService_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
$log = _$log_;
SockJS.currentMockInstance.$log = $log;
vertxEventBus.readyState = function () {
return vertxEventBus.EventBus.OPEN;
};
vertxEventBus.send = function (address, message, replyHandler) {
console.debug('XY', address, message, replyHandler);
$log.debug('XY', address, message, replyHandler);
result = true;
};
}));
Expand Down
9 changes: 6 additions & 3 deletions test/unit/mock/sockjs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class SockJS
constructor: (@url, @whitelist, @options, mockOptions) ->
SockJS.mockInstances.push this
SockJS.currentMockInstance = this
console.debug "[MOCK] SockJS Constructur(#{url})"
fn = =>
@onopen() if typeof @onopen is 'function'
return
Expand All @@ -23,8 +22,12 @@ class SockJS
else
window.setTimeout fn, 1

log: ->
log = SockJS.currentMockInstance?.$log or window.console
log.debug.apply(this, arguments)

close: (mockOptions)->
console.debug "[MOCK] SockJS.close()"
@log "[MOCK] SockJS.close()"
fn = =>
@onclose() if typeof @onclose is 'function'
return
Expand All @@ -42,7 +45,7 @@ class SockJS
data = @_unwrapFromEvent(message)
catch e
return
console.debug "[MOCK] SockJS.send(#{message})"
@log "[MOCK] SockJS.send(#{message})"
if data.address is 'vertx.basicauthmanager.login'
reply = if @nextLoginState
@_buildLoginReplyAsSuccess(data.body.username, data.body.password)
Expand Down

0 comments on commit 3c9be73

Please sign in to comment.