WIP: refactoring rpc code
This commit is contained in:
parent
afcc888a8b
commit
d3d9e2e771
@ -6,8 +6,9 @@ define([
|
|||||||
'/common/test.js',
|
'/common/test.js',
|
||||||
'/common/common-hash.js',
|
'/common/common-hash.js',
|
||||||
'/common/common-thumbnail.js',
|
'/common/common-thumbnail.js',
|
||||||
|
'/common/wire.js',
|
||||||
'/common/flat-dom.js',
|
'/common/flat-dom.js',
|
||||||
], function ($, Hyperjson, Sortify, Drive, Test, Hash, Thumb, Flat) {
|
], function ($, Hyperjson, Sortify, Drive, Test, Hash, Thumb, Wire, Flat) {
|
||||||
window.Hyperjson = Hyperjson;
|
window.Hyperjson = Hyperjson;
|
||||||
window.Sortify = Sortify;
|
window.Sortify = Sortify;
|
||||||
|
|
||||||
@ -30,7 +31,7 @@ define([
|
|||||||
|
|
||||||
ASSERTS.forEach(function (f, index) {
|
ASSERTS.forEach(function (f, index) {
|
||||||
f(function (err) {
|
f(function (err) {
|
||||||
console.log("test " + index);
|
//console.log("test " + index);
|
||||||
done(err, index);
|
done(err, index);
|
||||||
}, index);
|
}, index);
|
||||||
});
|
});
|
||||||
@ -235,6 +236,50 @@ define([
|
|||||||
return cb(true);
|
return cb(true);
|
||||||
}, "version 2 hash failed to parse correctly");
|
}, "version 2 hash failed to parse correctly");
|
||||||
|
|
||||||
|
assert(function (cb) {
|
||||||
|
Wire.create({
|
||||||
|
constructor: function (cb) {
|
||||||
|
var service = function (type, data, cb) {
|
||||||
|
switch (type) {
|
||||||
|
case "HEY_BUDDY":
|
||||||
|
return cb(void 0, "SALUT!");
|
||||||
|
default:
|
||||||
|
cb("ERROR");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var respond;
|
||||||
|
cb(void 0, {
|
||||||
|
send: function (raw, cb) {
|
||||||
|
try {
|
||||||
|
var parsed = JSON.parse(raw);
|
||||||
|
var txid = parsed.txid;
|
||||||
|
var message = parsed.message;
|
||||||
|
setTimeout(function () {
|
||||||
|
service(message.command, message.content, function (e, result) {
|
||||||
|
respond(JSON.stringify({
|
||||||
|
txid: txid,
|
||||||
|
error: e,
|
||||||
|
content: result,
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (e) { console.error("PEWPEW"); }
|
||||||
|
},
|
||||||
|
receive: function (f) {
|
||||||
|
respond = f;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}, function (e, rpc) {
|
||||||
|
if (e) { return cb(false); }
|
||||||
|
rpc.send('HEY_BUDDY', null, function (e, out) {
|
||||||
|
if (e) { return void cb(false); }
|
||||||
|
if (out === 'SALUT!') { cb(true); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, "Test rpc factory");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
assert(function (cb) {
|
assert(function (cb) {
|
||||||
var getBlob = function (url, cb) {
|
var getBlob = function (url, cb) {
|
||||||
|
|||||||
94
www/common/wire.js
Normal file
94
www/common/wire.js
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
define([
|
||||||
|
|
||||||
|
], function () {
|
||||||
|
var Wire = {};
|
||||||
|
|
||||||
|
/* MISSION: write a generic RPC framework
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
|
||||||
|
* some transmission methods can be interrupted
|
||||||
|
* handle disconnects and reconnects
|
||||||
|
* handle callbacks
|
||||||
|
* configurable timeout
|
||||||
|
* Service should expose 'addClient' method
|
||||||
|
* and handle broadcast
|
||||||
|
|
||||||
|
|
||||||
|
*
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
var uid = function () {
|
||||||
|
return Number(Math.floor(Math.random () *
|
||||||
|
Number.MAX_SAFE_INTEGER)).toString(32);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
opt = {
|
||||||
|
send: function () {
|
||||||
|
|
||||||
|
},
|
||||||
|
receive: function () {
|
||||||
|
|
||||||
|
},
|
||||||
|
constructor: function (cb) {
|
||||||
|
cb(void 0 , {
|
||||||
|
send: function (content, cb) {
|
||||||
|
|
||||||
|
},
|
||||||
|
receive: function () {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
Wire.create = function (opt, cb) {
|
||||||
|
var ctx = {};
|
||||||
|
var pending = ctx.pending = {};
|
||||||
|
ctx.connected = false;
|
||||||
|
|
||||||
|
var rpc = {};
|
||||||
|
|
||||||
|
opt.constructor(function (e, service) {
|
||||||
|
if (e) { return setTimeout(function () { cb(e); }); }
|
||||||
|
|
||||||
|
rpc.send = function (type, data, cb) {
|
||||||
|
var txid = uid();
|
||||||
|
if (typeof(cb) !== 'function') {
|
||||||
|
throw new Error('expected callback');
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.pending[txid] = function (err, response) {
|
||||||
|
cb(err, response);
|
||||||
|
};
|
||||||
|
|
||||||
|
service.send(JSON.stringify({
|
||||||
|
txid: txid,
|
||||||
|
message: {
|
||||||
|
command: type,
|
||||||
|
content: data,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
service.receive(function (raw) {
|
||||||
|
try {
|
||||||
|
var data = JSON.parse(raw);
|
||||||
|
var txid = data.txid;
|
||||||
|
if (!txid) { throw new Error('NO_TXID'); }
|
||||||
|
var cb = pending[txid];
|
||||||
|
if (data.error) { return void cb(data.error); }
|
||||||
|
cb(void 0, data.content);
|
||||||
|
} catch (e) { console.error("UNHANDLED_MESSAGE", data); }
|
||||||
|
});
|
||||||
|
|
||||||
|
cb(void 0, rpc);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
return Wire;
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user