From a73a7903279a78a42d1ac98d3d8a7bea496a2009 Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Thu, 31 Oct 2013 16:15:23 -0600 Subject: [PATCH] Updated Basestar to include some proxy event functions and updated sphero messages example. --- dist/basestar.js | 49 ++++++++++++++++++++++++++++++++- examples/sphero_messages.coffee | 9 ++++-- src/basestar.coffee | 24 ++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/dist/basestar.js b/dist/basestar.js index 3aeb491..c447c09 100644 --- a/dist/basestar.js +++ b/dist/basestar.js @@ -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); diff --git a/examples/sphero_messages.coffee b/examples/sphero_messages.coffee index fd98a29..cfc35cf 100644 --- a/examples/sphero_messages.coffee +++ b/examples/sphero_messages.coffee @@ -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) diff --git a/src/basestar.coffee b/src/basestar.coffee index d222a17..79f1edf 100644 --- a/src/basestar.coffee +++ b/src/basestar.coffee @@ -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)