sframeChan Uint8Array without conversion

pull/1/head
yflory 4 years ago
parent 004c242f63
commit 682e722d72

@ -8,19 +8,17 @@ define([
var e = err || (data && data.error); var e = err || (data && data.error);
if (e) { return void cb(e); } if (e) { return void cb(e); }
if (!data || typeof(data) !== "object") { return void cb('EINVAL'); } if (!data || typeof(data) !== "object") { return void cb('EINVAL'); }
var u8 = Uint8Array.from(data); cb(null, data);
cb(null, u8); }, { raw: true });
});
}; };
var setBlobCache = function (id, u8, cb) { var setBlobCache = function (id, u8, cb) {
var array = [].slice.call(u8);
sframeChan.query('Q_SET_BLOB_CACHE', { sframeChan.query('Q_SET_BLOB_CACHE', {
id: id, id: id,
u8: array u8: u8
}, function (err, data) { }, function (err, data) {
var e = err || (data && data.error) || undefined; var e = err || (data && data.error) || undefined;
cb(e); cb(e);
}); }, { raw: true });
}; };

@ -591,7 +591,7 @@ var factory = function () {
var getCache = function () { var getCache = function () {
var c = cache[uid]; var c = cache[uid];
if (!c || !c.promise || !c.mt) { console.error(uid);return; } if (!c || !c.promise || !c.mt) { return; }
return c; return c;
}; };

@ -65,11 +65,13 @@ define([
cb(undefined, data.content, msg); cb(undefined, data.content, msg);
}; };
evReady.reg(function () { evReady.reg(function () {
postMsg(JSON.stringify({ var toSend = {
txid: txid, txid: txid,
content: content, content: content,
q: q q: q,
})); raw: opts.raw
};
postMsg(opts.raw ? toSend : JSON.stringify(toSend));
}); });
}; };
@ -84,12 +86,13 @@ define([
// If the type is a query, your handler will be invoked with a reply function that takes // If the type is a query, your handler will be invoked with a reply function that takes
// one argument (the content to reply with). // one argument (the content to reply with).
chan.on = function (queryType, handler, quiet) { chan.on = function (queryType, handler, quiet) {
var h = function (data, msg) { var h = function (data, msg, raw) {
handler(data.content, function (replyContent) { handler(data.content, function (replyContent) {
postMsg(JSON.stringify({ var toSend = {
txid: data.txid, txid: data.txid,
content: replyContent content: replyContent
})); };
postMsg(raw ? toSend : JSON.stringify(toSend));
}, msg); }, msg);
}; };
(handlers[queryType] = handlers[queryType] || []).push(h); (handlers[queryType] = handlers[queryType] || []).push(h);
@ -150,7 +153,7 @@ define([
onMsg.reg(function (msg) { onMsg.reg(function (msg) {
if (!chanLoaded) { return; } if (!chanLoaded) { return; }
if (!msg.data || msg.data === '_READY') { return; } if (!msg.data || msg.data === '_READY') { return; }
var data = JSON.parse(msg.data); var data = typeof(msg.data) === "object" ? msg.data : JSON.parse(msg.data);
if (typeof(data.ack) !== "undefined") { if (typeof(data.ack) !== "undefined") {
if (acks[data.txid]) { acks[data.txid](!data.ack); } if (acks[data.txid]) { acks[data.txid](!data.ack); }
} else if (typeof(data.q) === 'string') { } else if (typeof(data.q) === 'string') {
@ -163,7 +166,7 @@ define([
})); }));
} }
handlers[data.q].forEach(function (f) { handlers[data.q].forEach(function (f) {
f(data || JSON.parse(msg.data), msg); f(data || JSON.parse(msg.data), msg, data && data.raw);
data = undefined; data = undefined;
}); });
} else { } else {

@ -683,15 +683,12 @@ define([
sframeChan.on('Q_GET_BLOB_CACHE', function (data, cb) { sframeChan.on('Q_GET_BLOB_CACHE', function (data, cb) {
Utils.Cache.getBlobCache(data.id, function (err, obj) { Utils.Cache.getBlobCache(data.id, function (err, obj) {
if (err) { return void cb({error: err}); } if (err) { return void cb({error: err}); }
var arr = [].slice.call(obj); cb(obj);
cb(arr);
}); });
}); });
sframeChan.on('Q_SET_BLOB_CACHE', function (data, cb) { sframeChan.on('Q_SET_BLOB_CACHE', function (data, cb) {
if (!data || !data.u8 || typeof(data.u8) !== "object") { return void cb({error: 'EINVAL'}); } if (!data || !data.u8 || typeof(data.u8) !== "object") { return void cb({error: 'EINVAL'}); }
var arr = data.u8; Utils.Cache.setBlobCache(data.id, data.u8, function (err) {
var u8 = Uint8Array.from(arr);
Utils.Cache.setBlobCache(data.id, u8, function (err) {
if (err) { return void cb({error: err}); } if (err) { return void cb({error: err}); }
cb(); cb();
}); });

@ -33,7 +33,7 @@ define([
// loading screen setup. // loading screen setup.
var done = waitFor(); var done = waitFor();
var onMsg = function (msg) { var onMsg = function (msg) {
var data = JSON.parse(msg.data); var data = typeof(msg.data) === "object" ? msg.data : JSON.parse(msg.data);
if (data.q !== 'READY') { return; } if (data.q !== 'READY') { return; }
window.removeEventListener('message', onMsg); window.removeEventListener('message', onMsg);
var _done = done; var _done = done;

Loading…
Cancel
Save