Skip to content

Instantly share code, notes, and snippets.

@thejhh
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save thejhh/edbd00b6c8a4f60460db to your computer and use it in GitHub Desktop.

Select an option

Save thejhh/edbd00b6c8a4f60460db to your computer and use it in GitHub Desktop.
Building remote version of local JavaScript [hidden] "class"
var Socket = require('src/backend/io/Socket.js');
/** RemoteSocket */
function RemoteSocket(socket) {
debug.assert(socket).is('object');
debug.assert(socket.$id).is('uuid');
debug.assert(socket.worker).is('string');
debug.assert(socket.id).is('string');
this._$id = socket.$id;
this._worker = socket.worker;
this._id = socket.id;
}
/** Build methods for RemoteSocket prototype */
function build_remote_socket_methods() {
ARRAY(Object.keys(Socket.prototype)).forEach(function create_method(method_name) {
RemoteSocket.prototype[method_name] = function method_wrapper() {
var self = this;
var args = Array.prototype.slice.call(arguments);
debug.assert(self._worker).is('string');
debug.assert(self._$id).is('uuid');
return WORKERS.fetch(self._worker).then(function(w) {
var url = 'http://' + w.hostname + ':' + w.port + '/api/internal/exec';
return internal_exec(url, function(context) {
var args = '?';
var $id = args.shift();
return context.cache.get_local_socket($id).then(function(socket) {
return socket.apply(undefined, args);
});
}, [self._$id].concat(args));
});
};
});
}
// ..and actually build these here.
build_remote_socket_methods();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment