latte_web2
Version:
65 lines (63 loc) • 1.83 kB
JavaScript
(function(define) { 'use strict';
define("latte_web/server/rpc/master", ["require", "exports", "module", "window"],
function(require, exports, module, window) {
var latte_lib = require("latte_lib")
, ORPC = require("./orpc");
function RPC(methods) {
this.workers = [];
ORPC.call(this, methods);
};
latte_lib.inherits(RPC, ORPC);
(function(){
this.removeWorker = function(worker) {
var self = this;
var index = self.workers.indexOf(worker);
if(index != -1) {
latte_lib.removeLocalArray(self.workers, index);
}
}
this.addWorker = function(worker) {
ORPC.prototype.addWorker.call(this, worker);
this.workers.push(worker);
}
this.CallAll = function(method, params, socket, cb) {
var self = this;
if(latte_lib.isFunction(socket)) { cb = socket; socket = null; }
if(cb) {
var funcs = this.workers.map(function(worker) {
return function(callback) {
self.Call(worker, method, params, socket, function(error, data, socket) {
callback(null, {
error: error,
data: data,
worker: worker
});
});
};
});
latte_lib.async.parallel(funcs, function(err, all) {
cb(err, all);
});
}else{
this.workers.forEach(function(worker) {
self.Call(worker, method, params, socket);
});
}
}
this.Call = function(worker, method, params, socket, cb) {
var id = ++this.id;
if(latte_lib.isFunction(socket)) {
cb = socket;
socket = null;
}
worker.send({
method: method,
params: params,
id: id
},socket);
cb && this.once(id, cb);
}
}).call(RPC.prototype);
module.exports = RPC;
});
})(typeof define === "function"? define: function(name, reqs, factory) { factory(require, exports, module); });