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

Commit

Permalink
Merge branch 'canary'
Browse files Browse the repository at this point in the history
Conflicts:
	CHANGELOG.md
  • Loading branch information
knalli committed Nov 23, 2014
2 parents a65d222 + 8e5006e commit 8333a1c
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 25 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# angular-vertxbus [![Bower version](https://badge.fury.io/bo/angular-vertxbus.svg)](http://badge.fury.io/bo/angular-vertxbus) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)
# angular-vertxbus [![Bower version](https://badge.fury.io/bo/angular-vertxbus.svg)](http://badge.fury.io/bo/angular-vertxbus) [![npm version](https://badge.fury.io/js/angular-vertxbus.svg)](http://badge.fury.io/js/angular-vertxbus) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)

Client side library using VertX Event Bus as an Angular Service module

Expand Down Expand Up @@ -134,12 +134,12 @@ For each connect or disconnect, a global broadcast will be emitted (on `$rootSco

### Setup for using same callback with different addresses

In some scenarios you will want to use a single callback definition to many different addresses. To do this you will
have to follow this pattern.
In some scenarios you will want to use a single callback definition to many different addresses. To do this you will
have to follow this pattern.

Create a javascript object that defines your callback. Do not attempt to use `.prototype` as the methods defined
as such will always point to the same memory allocation - so will always be found identical when adding to the internal
array.
array.
```javascript
function FunctionHolder(){
"use strict";
Expand All @@ -151,7 +151,7 @@ function FunctionHolder(){
};
```

Then when adding the listeners, you will create a new instance of your object and use your defined function.
Then when adding the listeners, you will create a new instance of your object and use your defined function.
```javascript
var funcHolder = new FunctionHolder();
vertxEventBusService.addListener('address', funcHolder.handler);
Expand Down
8 changes: 4 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"author": "Jan Philipp",
"name": "angular-vertxbus",
"description": "Seed for reusable Angular components.",
"version": "0.8.1",
"description": "AngularJS facade and service acting as a Vert.x SockJS client",
"version": "0.9.0",
"homepage": "http://github.com/knalli/angular-vertxbus",
"main": "./dist/angular-vertxbus.min.js",
"main": "./dist/angular-vertxbus.js",
"ignore": [
".editorconfig",
".jshintrc",
Expand All @@ -20,7 +20,7 @@
},
"dependencies": {
"angular": "~1.2.0",
"sockjs": "~0.3.4",
"sockjs-client": "~0.3.4",
"vertxbus.js": "http://cdnjs.cloudflare.com/ajax/libs/vertx/2.0.0/vertxbus.js"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "angular-vertxbus",
"description": "AngularJS facade and service acting as a Vert.x SockJS client",
"version": "master",
"main": "dist/angular-vertxbus.js",
"repository": "knalli/angular-vertxbus",
"scripts": [
"dist/angular-vertxbus.js"
],
"dependencies": {
"components/angular.js": "~1.2.0"
},
"license": "MIT"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "angular-vertxbus",
"version": "0.8.1",
"description": "Seed for reusable Angular components.",
"main": "dist/angular-vertxbus.min.js",
"version": "0.9.0",
"description": "AngularJS facade and service acting as a Vert.x SockJS client",
"main": "dist/angular-vertxbus.js",
"scripts": {
"prepublish": "bower install",
"test": "grunt install-test && grunt test",
Expand Down
50 changes: 43 additions & 7 deletions src/service.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
angular.module('knalli.angular-vertxbus')
.provider('vertxEventBusService', () ->

CONSTANTS =
MODULE: 'angular-vertxbus'
COMPONENT: 'service'

DEFAULT_OPTIONS =
loginRequired: false
loginBlockForSession: false #NYI
Expand Down Expand Up @@ -88,16 +92,19 @@ angular.module('knalli.angular-vertxbus')
@requireLogin = (value = options.loginRequired) ->
options.loginRequired = value
return this
@requireLogin.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: requireLogin"

# private: NYI
@blockForSession = (value = options.loginBlockForSession) ->
options.loginBlockForSession = value
return this
@blockForSession.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: blockForSession"

# private: NYI
@skipUnauthorizeds = (value = options.skipUnauthorizeds) ->
options.skipUnauthorizeds = value
return this
@skipUnauthorizeds.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: skipUnauthorizeds"

@$get = ($rootScope, $q, $interval, $timeout, vertxEventBus) ->
# Extract options (with defaults)
Expand Down Expand Up @@ -131,6 +138,7 @@ angular.module('knalli.angular-vertxbus')
vertxEventBus.onclose = ->
wrapped.getConnectionState(true)
$rootScope.$broadcast "#{prefix}system.disconnected"
vertxEventBus.onclose.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: 'onclose' handler"

ensureOpenConnection = (fn) ->
if wrapped.getConnectionState() is vertxEventBus.EventBus.OPEN
Expand All @@ -140,20 +148,24 @@ angular.module('knalli.angular-vertxbus')
messageQueueHolder.push(fn)
return true
return false
ensureOpenConnection.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: ensureOpenConnection"

ensureOpenAuthConnection = (fn) ->
unless options.loginRequired
# easy: no login required
ensureOpenConnection fn
else
ensureOpenConnection ->
wrapFn = ->
if validSession
fn()
return true
else
# ignore this message
console.debug("[VertX 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
ensureOpenAuthConnection.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: ensureOpenAuthConnection"

# All utility methods working directly on the event bus object.
# The object "vertxEventBus" must be available.
Expand All @@ -163,10 +175,11 @@ angular.module('knalli.angular-vertxbus')
return unless typeof callback is 'function'
console.debug("[VertX EB Service] Register handler for #{address}") if debugEnabled
return fnWrapperMap.get(callback) if fnWrapperMap.containsKey(callback) # already known
fnWrapperMap.put(callback, (message, replyTo) ->
deconstructor = (message, replyTo) ->
callback(message, replyTo)
$rootScope.$digest()
)
deconstructor.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.registerHandler (deconstructor)"
fnWrapperMap.put(callback, deconstructor)
vertxEventBus.registerHandler address, fnWrapperMap.get(callback)
# Remove a callback handler for the specified address match.
unregisterHandler : (address, callback) ->
Expand All @@ -181,36 +194,48 @@ angular.module('knalli.angular-vertxbus')
# @param timeout an optional number for a timout after which the promise will be rejected
send : (address, message, timeout = 10000) ->
deferred = $q.defer()
dispatched = ensureOpenAuthConnection ->
next = ->
vertxEventBus.send address, message, (reply) ->
if deferred then deferred.resolve reply
# Register timeout for promise rejecting.
if deferred then $timeout (-> deferred.reject()), timeout
next.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.send (ensureOpenAuthConnection callback)"
dispatched = ensureOpenAuthConnection next
if deferred and !dispatched then deferred.reject()
return deferred?.promise
# Publish a message to the specified address (using EventBus.publish).
# @param address a required string for the targeting address in the bus
# @param message a required piece of message data
publish : (address, message) ->
dispatched = ensureOpenAuthConnection ->
next = ->
vertxEventBus.publish address, message
next.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.publish (ensureOpenAuthConnection callback)"
dispatched = ensureOpenAuthConnection next
return dispatched
# Send a login message
# @param username
# @param password
# @param timeout
login : (username, password, timeout = 5000) ->
deferred = $q.defer()
vertxEventBus.login username, password, (reply) ->
next = (reply) ->
if reply?.status is 'ok'
deferred.resolve reply
$rootScope.$broadcast "#{prefix}system.login.succeeded", (status: reply?.status)
else
deferred.reject reply
$rootScope.$broadcast "#{prefix}system.login.failed", (status: reply?.status)
next.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.login (callback)"
vertxEventBus.login username, password, next
$timeout (-> deferred.reject()), timeout
return deferred.promise

util.registerHandler.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.registerHandler"
util.unregisterHandler.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.unregisterHandler"
util.send.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.send"
util.publish.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.publish"
util.login.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: util.login"

# Wrapping methods for the api
wrapped =
# Store of all handlers using as a cache when the event bus is not online
Expand Down Expand Up @@ -266,8 +291,18 @@ angular.module('knalli.angular-vertxbus')
validSession = false
return reply

wrapped.registerHandler.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: wrapped.registerHandler"
wrapped.unregisterHandler.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: wrapped.unregisterHandler"
wrapped.send.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: wrapped.send"
wrapped.publish.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: wrapped.publish"
wrapped.getConnectionState.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: wrapped.getConnectionState"
wrapped.isValidSession.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: wrapped.isValidSession"
wrapped.login.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: wrapped.login"

# Update the current connection state periodially.
$interval (-> wrapped.getConnectionState(true)), sockjsStateInterval
connectionIntervalCheck = -> wrapped.getConnectionState(true)
connectionIntervalCheck.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: periodic connection check"
$interval connectionIntervalCheck, sockjsStateInterval

### building and exposing the actual service API ###
return (
Expand All @@ -284,6 +319,7 @@ angular.module('knalli.angular-vertxbus')
isValidSession : -> validSession
login : wrapped.login
)
@$get.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: initializer"

return #void
)
18 changes: 17 additions & 1 deletion src/wrapper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
angular.module('knalli.angular-vertxbus')
.provider('vertxEventBus', () ->

CONSTANTS =
MODULE: 'angular-vertxbus'
COMPONENT: 'wrapper'

DEFAULT_OPTIONS =
enabled: true
debugEnabled: false
Expand Down Expand Up @@ -123,17 +127,29 @@ angular.module('knalli.angular-vertxbus')
registerHandler: (address, handler) ->
eventBus.registerHandler(address, handler)
### and return the deregister callback ###
return ->
deconstructor = ->
stub.unregisterHandler(address, handler)
return #void
deconstructor.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.registerHandler (deconstructor)"
return deconstructor
unregisterHandler: (address, handler) -> eventBus.unregisterHandler(address, handler)
readyState: -> eventBus.readyState()
### expose current used internal instance of actual EventBus ###
EventBus: EventBus_
getOptions: -> angular.extend({}, options)
stub.reconnect.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.reconnect"
stub.close.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.close"
stub.login.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.login"
stub.send.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.send"
stub.publish.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.publish"
stub.registerHandler.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.registerHandler"
stub.unregisterHandler.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.unregisterHandler"
stub.readyState.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.readyState"
stub.getOptions.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: EventBusWrapper.getOptions"
else
console.debug("[VertX EventBus] Disabled") if debugEnabled
return stub
@$get.displayName = "#{CONSTANTS.MODULE}/#{CONSTANTS.COMPONENT}: initializer"

return #void
)
2 changes: 1 addition & 1 deletion test/e2e/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<span ng-hide="sessionIsValid">Invalid Session</span>
</div>

<script src="bower_components/sockjs/sockjs.js"></script>
<script src="bower_components/sockjs-client/dist/sockjs.js"></script>
<script src="bower_components/vertxbus.js/index.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="temp/src/angular-vertxbus-adapter.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions test_scopes/angular_1.2.x/bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Jan Philipp",
"name": "angular-vertxbus",
"description": "Seed for reusable Angular components.",
"description": "",
"version": "0.0.0",
"homepage": "http://github.com/knalli/angular-vertxbus",
"repository": {
Expand All @@ -10,7 +10,7 @@
},
"dependencies": {
"angular": "~1.2.0",
"sockjs": "~0.3.4",
"sockjs-client": "~0.3.4",
"vertxbus.js": "http://cdnjs.cloudflare.com/ajax/libs/vertx/2.0.0/vertxbus.js"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions test_scopes/angular_1.3.x/bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Jan Philipp",
"name": "angular-vertxbus",
"description": "Seed for reusable Angular components.",
"description": "",
"version": "0.0.0",
"homepage": "http://github.com/knalli/angular-vertxbus",
"repository": {
Expand All @@ -10,7 +10,7 @@
},
"dependencies": {
"angular": "~1.3.0",
"sockjs": "~0.3.4",
"sockjs-client": "~0.3.4",
"vertxbus.js": "http://cdnjs.cloudflare.com/ajax/libs/vertx/2.0.0/vertxbus.js"
},
"devDependencies": {
Expand Down

0 comments on commit 8333a1c

Please sign in to comment.