Merge pull request #17 from hybridgroup/26-event-proxy

Updated Basestar to include some proxy event functions and updated spher...
This commit is contained in:
Andrew Stewart 2013-10-31 16:20:49 -07:00
commit a24f30ceec
3 changed files with 79 additions and 3 deletions

49
dist/basestar.js vendored
View File

@ -11,7 +11,8 @@
'use strict';
var EventEmitter, namespace,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice;
namespace = require('node-namespace');
@ -34,6 +35,52 @@
return proxyFunctionsToObject(methods, target, klass, force);
};
Basestar.prototype.proxyEvent = function(eventName, onSource, emitSource, updEvt) {
var _this = this;
if (updEvt == null) {
updEvt = false;
}
return onSource.on(eventName, function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
emitSource.emit(eventName, args);
if (updEvt) {
return emitSource.emit('update', eventName, args);
}
});
};
Basestar.prototype.proxyAdaptorEvent = function(params) {
return this.proxyEvent(params.on, this.connector, this.connection, params.emitUpdate);
};
Basestar.prototype.proxyDriverEvent = function(params) {
return this.proxyEvent(params.on, this.connection, this.device, params.emitUpdate);
};
Basestar.prototype.createEvent = function(onEvent, onSource, emitEvent, emitSource, updEvt) {
var _this = this;
if (updEvt == null) {
updEvt = false;
}
return onSource.on(onEvent, function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
emitSource.emit(emitEvent, args);
if (updEvt) {
return emitSource.emit('update', emitEvent, args);
}
});
};
Basestar.prototype.createAdaptorEvent = function(params) {
return this.createEvent(params.on, this.connector, params.emit, this.connection, params.emitUpdate);
};
Basestar.prototype.createDriverEvent = function(params) {
return this.createEvent(params.on, this.connection, params.emit, this.device, params.emitUpdate);
};
return Basestar;
})(EventEmitter);

View File

@ -9,11 +9,16 @@ Cylon.robot
work: (me) ->
me.sphero.on('connect', ->
me.sphero.on 'connect', ->
Logger.info('Setting up Collision Detection...')
me.sphero.detectCollisions()
me.sphero.setRGB(0x00FF00)
)
me.sphero.on 'update', (data) ->
Logger.info("Update event eventName -> #{ data } ")
Logger.info("Update event args -> ")
Logger.info(data)
me.sphero.on 'message', (data) ->
me.sphero.setRGB(0x0000FF)

View File

@ -22,3 +22,27 @@ namespace 'Cylon', ->
proxyMethods: (methods, target, klass, force = false) ->
proxyFunctionsToObject(methods, target, klass, force)
proxyEvent: (eventName, onSource, emitSource, updEvt = false) ->
onSource.on(eventName, (args...) =>
emitSource.emit(eventName, args)
emitSource.emit('update', eventName, args) if updEvt
)
proxyAdaptorEvent: (params) ->
@proxyEvent(params.on, @connector, @connection, params.emitUpdate)
proxyDriverEvent: (params) ->
@proxyEvent(params.on, @connection, @device, params.emitUpdate)
createEvent: (onEvent, onSource, emitEvent, emitSource, updEvt = false ) ->
onSource.on(onEvent, (args...) =>
emitSource.emit(emitEvent, args)
emitSource.emit('update', emitEvent, args) if updEvt
)
createAdaptorEvent: (params) ->
@createEvent(params.on, @connector, params.emit, @connection, params.emitUpdate)
createDriverEvent: (params) ->
@createEvent(params.on, @connection, params.emit, @device, params.emitUpdate)