Merge pull request #176 from hybridgroup/remove-class-cruft-and-namespace
Remove class cruft and namespace from Basestar
This commit is contained in:
commit
2958f148f3
|
@ -8,7 +8,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
require('./basestar');
|
||||
var Basestar = require('./basestar');
|
||||
|
||||
var namespace = require('node-namespace');
|
||||
|
||||
// The Adaptor class is a base class for Adaptor classes in external Cylon
|
||||
|
@ -63,5 +64,5 @@ namespace("Cylon", function() {
|
|||
|
||||
return Adaptor;
|
||||
|
||||
})(Cylon.Basestar);
|
||||
})(Basestar);
|
||||
});
|
||||
|
|
165
lib/basestar.js
165
lib/basestar.js
|
@ -9,99 +9,94 @@
|
|||
"use strict";
|
||||
|
||||
require('./utils');
|
||||
var namespace = require('node-namespace');
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
// Basestar is a base class to be used when writing external Cylon adaptors and
|
||||
// drivers. It provides some useful base methods and functionality
|
||||
//
|
||||
// It also extends EventEmitter, so child classes are capable of emitting events
|
||||
// for other parts of the system to handle.
|
||||
namespace("Cylon", function() {
|
||||
this.Basestar = (function(klass) {
|
||||
subclass(Basestar, klass);
|
||||
// Basestar is a base class to be used when writing external Cylon adaptors and
|
||||
// drivers. It provides some useful base methods and functionality
|
||||
//
|
||||
// It also extends EventEmitter, so child classes are capable of emitting events
|
||||
// for other parts of the system to handle.
|
||||
var Basestar;
|
||||
|
||||
function Basestar(opts) {
|
||||
this.self = this;
|
||||
module.exports = Basestar = function Basestar(opts) {
|
||||
this.self = this;
|
||||
}
|
||||
|
||||
subclass(Basestar, EventEmitter);
|
||||
|
||||
// Public: Proxies calls from all methods in the object to a target object
|
||||
//
|
||||
// methods - array of methods to proxy
|
||||
// target - object to proxy methods to
|
||||
// source - object to proxy methods from
|
||||
// force - whether or not to overwrite existing method definitions
|
||||
//
|
||||
// Returns the klass where the methods have been proxied
|
||||
Basestar.prototype.proxyMethods = function(methods, target, source, force) {
|
||||
if (force == null) { force = false; }
|
||||
return proxyFunctionsToObject(methods, target, source, force);
|
||||
};
|
||||
|
||||
// Public: Defines an event handler that proxies events from a source object
|
||||
// to a target object
|
||||
//
|
||||
// opts - object containing options:
|
||||
// - targetEventName or eventName - event that should be emitted from the
|
||||
// target
|
||||
// - target - object to proxy event to
|
||||
// - source - object to proxy event from
|
||||
// - sendUpdate - whether or not to send an 'update' event
|
||||
//
|
||||
// Returns the source
|
||||
Basestar.prototype.defineEvent = function(opts) {
|
||||
var self = this;
|
||||
|
||||
opts.sendUpdate = opts.sendUpdate || false;
|
||||
opts.targetEventName = opts.targetEventName || opts.eventName;
|
||||
|
||||
opts.source.on(opts.eventName, function() {
|
||||
var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
|
||||
args.unshift(opts.targetEventName);
|
||||
opts.target.emit.apply(opts.target, args);
|
||||
|
||||
if (opts.sendUpdate) {
|
||||
args.unshift('update');
|
||||
opts.target.emit.apply(opts.target, args);
|
||||
}
|
||||
});
|
||||
|
||||
// Public: Proxies calls from all methods in the object to a target object
|
||||
//
|
||||
// methods - array of methods to proxy
|
||||
// target - object to proxy methods to
|
||||
// source - object to proxy methods from
|
||||
// force - whether or not to overwrite existing method definitions
|
||||
//
|
||||
// Returns the klass where the methods have been proxied
|
||||
Basestar.prototype.proxyMethods = function(methods, target, source, force) {
|
||||
if (force == null) { force = false; }
|
||||
return proxyFunctionsToObject(methods, target, source, force);
|
||||
};
|
||||
return opts.source;
|
||||
};
|
||||
|
||||
// Public: Defines an event handler that proxies events from a source object
|
||||
// to a target object
|
||||
//
|
||||
// opts - object containing options:
|
||||
// - targetEventName or eventName - event that should be emitted from the
|
||||
// target
|
||||
// - target - object to proxy event to
|
||||
// - source - object to proxy event from
|
||||
// - sendUpdate - whether or not to send an 'update' event
|
||||
//
|
||||
// Returns the source
|
||||
Basestar.prototype.defineEvent = function(opts) {
|
||||
var self = this;
|
||||
// Public: Creates an event handler that proxies events from an adaptor's
|
||||
// 'connector' (reference to whatever module is actually talking to the hw)
|
||||
// to the adaptor's associated connection.
|
||||
//
|
||||
// opts - hash of opts to be passed to defineEvent()
|
||||
//
|
||||
// Returns this.connector
|
||||
Basestar.prototype.defineAdaptorEvent = function(opts) {
|
||||
if (typeof opts === 'string') { opts = { eventName: opts }; }
|
||||
|
||||
opts.sendUpdate = opts.sendUpdate || false;
|
||||
opts.targetEventName = opts.targetEventName || opts.eventName;
|
||||
opts['source'] = this.connector;
|
||||
opts['target'] = this.connection;
|
||||
|
||||
opts.source.on(opts.eventName, function() {
|
||||
var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
|
||||
args.unshift(opts.targetEventName);
|
||||
opts.target.emit.apply(opts.target, args);
|
||||
return this.defineEvent(opts);
|
||||
};
|
||||
|
||||
if (opts.sendUpdate) {
|
||||
args.unshift('update');
|
||||
opts.target.emit.apply(opts.target, args);
|
||||
}
|
||||
});
|
||||
// Public: Creates an event handler that proxies events from an device's
|
||||
// 'connector' (reference to whatever module is actually talking to the hw)
|
||||
// to the device's associated connection.
|
||||
//
|
||||
// opts - hash of opts to be passed to defineEvent()
|
||||
//
|
||||
// Returns this.connection
|
||||
Basestar.prototype.defineDriverEvent = function(opts) {
|
||||
if (typeof opts === 'string') { opts = { eventName: opts }; }
|
||||
|
||||
return opts.source;
|
||||
};
|
||||
opts['source'] = this.connection;
|
||||
opts['target'] = this.device;
|
||||
|
||||
// Public: Creates an event handler that proxies events from an adaptor's
|
||||
// 'connector' (reference to whatever module is actually talking to the hw)
|
||||
// to the adaptor's associated connection.
|
||||
//
|
||||
// opts - hash of opts to be passed to defineEvent()
|
||||
//
|
||||
// Returns this.connector
|
||||
Basestar.prototype.defineAdaptorEvent = function(opts) {
|
||||
if (typeof opts === 'string') { opts = { eventName: opts }; }
|
||||
|
||||
opts['source'] = this.connector;
|
||||
opts['target'] = this.connection;
|
||||
|
||||
return this.defineEvent(opts);
|
||||
};
|
||||
|
||||
// Public: Creates an event handler that proxies events from an device's
|
||||
// 'connector' (reference to whatever module is actually talking to the hw)
|
||||
// to the device's associated connection.
|
||||
//
|
||||
// opts - hash of opts to be passed to defineEvent()
|
||||
//
|
||||
// Returns this.connection
|
||||
Basestar.prototype.defineDriverEvent = function(opts) {
|
||||
if (typeof opts === 'string') { opts = { eventName: opts }; }
|
||||
|
||||
opts['source'] = this.connection;
|
||||
opts['target'] = this.device;
|
||||
|
||||
return this.defineEvent(opts);
|
||||
};
|
||||
|
||||
return Basestar;
|
||||
|
||||
})(EventEmitter);
|
||||
});
|
||||
return this.defineEvent(opts);
|
||||
};
|
||||
|
|
|
@ -8,7 +8,8 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
require('./basestar');
|
||||
var Basestar = require('./basestar');
|
||||
|
||||
var namespace = require('node-namespace');
|
||||
|
||||
// The Driver class is a base class for Driver classes in external Cylon
|
||||
|
@ -65,5 +66,5 @@ namespace("Cylon", function() {
|
|||
|
||||
return Driver;
|
||||
|
||||
})(Cylon.Basestar);
|
||||
})(Basestar);
|
||||
});
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
"use strict";
|
||||
|
||||
source('basestar');
|
||||
var Basestar = source('basestar');
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
describe('Basestar', function() {
|
||||
describe('constructor', function() {
|
||||
it('assigns @self to the instance of the Basestar class', function() {
|
||||
var instance = new Cylon.Basestar();
|
||||
var instance = new Basestar();
|
||||
expect(instance.self).to.be.eql(instance);
|
||||
});
|
||||
});
|
||||
|
@ -15,36 +15,26 @@ describe('Basestar', function() {
|
|||
describe('#proxyMethods', function() {
|
||||
var methods = ['asString', 'toString', 'returnString'];
|
||||
|
||||
var ProxyClass = (function() {
|
||||
function ProxyClass() {}
|
||||
var ProxyClass = function ProxyClass() {}
|
||||
|
||||
ProxyClass.prototype.asString = function() {
|
||||
return "[object ProxyClass]";
|
||||
};
|
||||
ProxyClass.prototype.asString = function() {
|
||||
return "[object ProxyClass]";
|
||||
};
|
||||
|
||||
ProxyClass.prototype.toString = function() {
|
||||
return "[object ProxyClass]";
|
||||
};
|
||||
ProxyClass.prototype.toString = function() {
|
||||
return "[object ProxyClass]";
|
||||
};
|
||||
|
||||
ProxyClass.prototype.returnString = function(string) {
|
||||
return string;
|
||||
};
|
||||
ProxyClass.prototype.returnString = function(string) {
|
||||
return string;
|
||||
};
|
||||
|
||||
return ProxyClass;
|
||||
var TestClass = function TestClass() {
|
||||
this.testInstance = new ProxyClass;
|
||||
this.proxyMethods(methods, this.testInstance, this, true);
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
var TestClass = (function(_super) {
|
||||
subclass(TestClass, _super);
|
||||
|
||||
function TestClass() {
|
||||
this.testInstance = new ProxyClass;
|
||||
this.proxyMethods(methods, this.testInstance, this, true);
|
||||
}
|
||||
|
||||
return TestClass;
|
||||
|
||||
})(Cylon.Basestar);
|
||||
subclass(TestClass, Basestar);
|
||||
|
||||
it('can alias methods', function() {
|
||||
var testclass = new TestClass;
|
||||
|
@ -66,28 +56,22 @@ describe('Basestar', function() {
|
|||
});
|
||||
|
||||
describe("#defineEvent", function() {
|
||||
var ProxyClass = (function(klass) {
|
||||
subclass(ProxyClass, klass);
|
||||
function ProxyClass() {}
|
||||
return ProxyClass;
|
||||
})(Cylon.Basestar);
|
||||
|
||||
var EmitterClass = (function(klass) {
|
||||
subclass(EmitterClass, klass);
|
||||
var ProxyClass = function ProxyClass() {};
|
||||
|
||||
function EmitterClass(update) {
|
||||
update || (update = false);
|
||||
this.proxy = new ProxyClass();
|
||||
this.defineEvent({
|
||||
eventName: "testevent",
|
||||
source: this,
|
||||
target: this.proxy,
|
||||
sendUpdate: update
|
||||
});
|
||||
}
|
||||
var EmitterClass = function EmitterClass(update) {
|
||||
update || (update = false);
|
||||
this.proxy = new ProxyClass();
|
||||
this.defineEvent({
|
||||
eventName: "testevent",
|
||||
source: this,
|
||||
target: this.proxy,
|
||||
sendUpdate: update
|
||||
});
|
||||
}
|
||||
|
||||
return EmitterClass;
|
||||
})(Cylon.Basestar);
|
||||
subclass(ProxyClass, Basestar);
|
||||
subclass(EmitterClass, Basestar);
|
||||
|
||||
it("proxies events from one class to another", function() {
|
||||
var eventSpy = spy(),
|
||||
|
@ -127,7 +111,7 @@ describe('Basestar', function() {
|
|||
var basestar;
|
||||
|
||||
before(function() {
|
||||
basestar = new Cylon.Basestar();
|
||||
basestar = new Basestar();
|
||||
basestar.connector = new EventEmitter();
|
||||
basestar.connection = new EventEmitter();
|
||||
});
|
||||
|
@ -159,7 +143,7 @@ describe('Basestar', function() {
|
|||
var basestar;
|
||||
|
||||
before(function() {
|
||||
basestar = new Cylon.Basestar();
|
||||
basestar = new Basestar();
|
||||
basestar.connection = new EventEmitter();
|
||||
basestar.device = new EventEmitter();
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue