2014-02-28 02:58:50 +08:00
|
|
|
"use strict";
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
var EventEmitter = require("events").EventEmitter;
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2015-02-21 10:49:31 +08:00
|
|
|
var Utils = require("./utils"),
|
|
|
|
_ = require("./utils/helpers");
|
2014-06-06 03:11:37 +08:00
|
|
|
|
2014-04-25 03:43:00 +08:00
|
|
|
// 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.
|
2014-06-11 09:44:40 +08:00
|
|
|
var Basestar = module.exports = function Basestar() {
|
2015-05-13 09:47:27 +08:00
|
|
|
Utils.classCallCheck(this, Basestar);
|
2014-06-11 09:44:40 +08:00
|
|
|
};
|
2014-04-25 03:43:00 +08:00
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(Basestar, EventEmitter);
|
2014-04-25 03:43:00 +08:00
|
|
|
|
|
|
|
// 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
|
2014-12-19 05:19:58 +08:00
|
|
|
Basestar.prototype.proxyMethods = Utils.proxyFunctionsToObject;
|
2014-04-25 03:43:00 +08:00
|
|
|
|
2015-03-20 01:42:51 +08:00
|
|
|
// Public: Triggers a callback and emits an event with provided data
|
|
|
|
//
|
|
|
|
// event - name of event to be triggered
|
|
|
|
// callback - callback function to be triggered
|
|
|
|
// ...data - additional arguments to be passed to both event/callback
|
|
|
|
//
|
|
|
|
// Returns nothing
|
2015-03-28 05:18:45 +08:00
|
|
|
Basestar.prototype.respond = function(event, callback, err) {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 3);
|
2015-03-20 01:42:51 +08:00
|
|
|
|
2015-04-15 12:49:12 +08:00
|
|
|
if (err) {
|
2015-03-28 05:36:24 +08:00
|
|
|
this.emit("error", err);
|
2015-03-28 05:18:45 +08:00
|
|
|
} else {
|
|
|
|
this.emit.apply(this, [event].concat(args));
|
|
|
|
}
|
2015-03-20 01:42:51 +08:00
|
|
|
|
|
|
|
if (typeof callback === "function") {
|
2015-03-28 05:18:45 +08:00
|
|
|
callback.apply(this, [err].concat(args));
|
2015-03-20 01:42:51 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 03:43:00 +08:00
|
|
|
// 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
|
2014-12-16 03:15:29 +08:00
|
|
|
// - sendUpdate - whether or not to send an "update" event
|
2014-04-25 03:43:00 +08:00
|
|
|
//
|
|
|
|
// Returns the source
|
|
|
|
Basestar.prototype.defineEvent = function(opts) {
|
|
|
|
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) {
|
2014-12-16 03:15:29 +08:00
|
|
|
args.unshift("update");
|
2014-04-25 03:43:00 +08:00
|
|
|
opts.target.emit.apply(opts.target, args);
|
2014-02-28 02:58:50 +08:00
|
|
|
}
|
2014-04-25 03:43:00 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return opts.source;
|
|
|
|
};
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
// Public: Creates an event handler that proxies events from an adaptor"s
|
|
|
|
// "connector" (reference to whatever module is actually talking to the hw)
|
2014-11-11 07:56:25 +08:00
|
|
|
// to the adaptor
|
2014-04-25 03:43:00 +08:00
|
|
|
//
|
|
|
|
// opts - hash of opts to be passed to defineEvent()
|
|
|
|
//
|
|
|
|
// Returns this.connector
|
|
|
|
Basestar.prototype.defineAdaptorEvent = function(opts) {
|
2014-11-11 07:56:25 +08:00
|
|
|
return this._proxyEvents(opts, this.connector, this);
|
2014-04-25 03:43:00 +08:00
|
|
|
};
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
// Public: Creates an event handler that proxies events from a driver"s
|
2014-11-15 02:56:45 +08:00
|
|
|
// connection to the driver
|
2014-04-25 03:43:00 +08:00
|
|
|
//
|
|
|
|
// opts - hash of opts to be passed to defineEvent()
|
|
|
|
//
|
|
|
|
// Returns this.connection
|
|
|
|
Basestar.prototype.defineDriverEvent = function(opts) {
|
2014-11-15 02:56:45 +08:00
|
|
|
return this._proxyEvents(opts, this.connection, this);
|
2014-10-03 02:14:19 +08:00
|
|
|
};
|
2014-04-25 03:43:00 +08:00
|
|
|
|
2014-10-03 02:14:19 +08:00
|
|
|
Basestar.prototype._proxyEvents = function(opts, source, target) {
|
2015-02-21 10:49:31 +08:00
|
|
|
opts = _.isString(opts) ? { eventName: opts } : opts;
|
2014-10-03 02:14:19 +08:00
|
|
|
|
|
|
|
opts.source = source;
|
2014-12-16 03:15:29 +08:00
|
|
|
opts.target = target;
|
2014-04-25 03:43:00 +08:00
|
|
|
|
|
|
|
return this.defineEvent(opts);
|
2014-12-16 03:15:29 +08:00
|
|
|
};
|