Files
purple-explorer/lib/node.js
T
2019-05-27 10:33:22 -07:00

45 lines
957 B
JavaScript

var commands = require('./commands');
var rpc = require('./jsonrpc');
function Client(opts) {
this.rpc = new rpc.Client(opts);
};
Client.prototype.cmd = function() {
var args = [].slice.call(arguments);
var cmd = args.shift();
callRpc(cmd, args, this.rpc);
};
function callRpc (cmd, args, rpc) {
var fn = args[args.length - 1];
// If the last argument is a callback, pop it from the args list
if (typeof fn === 'function') {
args.pop();
} else {
fn = function () {};
}
rpc.call(cmd, args, function () {
var args = [].slice.call(arguments);
args.unshift(null);
fn.apply(this, args);
}, function(err) {
fn(err);
});
};
(function() {
for (var protoFn in commands) {
(function(protoFn) {
Client.prototype[protoFn] = function() {
var args = [].slice.call(arguments);
callRpc(commands[protoFn], args, this.rpc);
};
})(protoFn);
}
})();
module.exports.Client = Client;