##// END OF EJS Templates
Merge pull request #7565 from minrk/missing-focusout...
Merge pull request #7565 from minrk/missing-focusout enable keyboard manager when codemirror is focused

File last commit:

r19176:f48e011c
r20137:342cef63 merge
Show More
serialize.js
119 lines | 3.9 KiB | application/javascript | JavascriptLexer
MinRK
use utf8.js...
r18331 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
MinRK
cleanup serialize per review
r18416 'underscore',
], function (_) {
MinRK
use utf8.js...
r18331 "use strict";
MinRK
support binary message from javascript
r18332
var _deserialize_array_buffer = function (buf) {
var data = new DataView(buf);
// read the header: 1 + nbufs 32b integers
MinRK
unsigned ints for offsets...
r18338 var nbufs = data.getUint32(0);
MinRK
support binary message from javascript
r18332 var offsets = [];
var i;
for (i = 1; i <= nbufs; i++) {
MinRK
unsigned ints for offsets...
r18338 offsets.push(data.getUint32(i * 4));
MinRK
support binary message from javascript
r18332 }
MinRK
use TextEncoding for string<->ArrayBuffer...
r18333 var json_bytes = new Uint8Array(buf.slice(offsets[0], offsets[1]));
MinRK
cleanup serialize per review
r18416 var msg = JSON.parse(
MinRK
use TextEncoding for string<->ArrayBuffer...
r18333 (new TextDecoder('utf8')).decode(json_bytes)
MinRK
support binary message from javascript
r18332 );
// the remaining chunks are stored as DataViews in msg.buffers
msg.buffers = [];
var start, stop;
for (i = 1; i < nbufs; i++) {
start = offsets[i];
stop = offsets[i+1] || buf.byteLength;
msg.buffers.push(new DataView(buf.slice(start, stop)));
}
return msg;
};
var _deserialize_binary = function(data, callback) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* deserialize the binary message format
* callback will be called with a message whose buffers attribute
* will be an array of DataViews.
*/
MinRK
support binary message from javascript
r18332 if (data instanceof Blob) {
// data is Blob, have to deserialize from ArrayBuffer in reader callback
var reader = new FileReader();
reader.onload = function () {
var msg = _deserialize_array_buffer(this.result);
callback(msg);
};
reader.readAsArrayBuffer(data);
} else {
// data is ArrayBuffer, can deserialize directly
var msg = _deserialize_array_buffer(data);
MinRK
use utf8.js...
r18331 callback(msg);
MinRK
support binary message from javascript
r18332 }
MinRK
use utf8.js...
r18331 };
var deserialize = function (data, callback) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* deserialize a message and pass the unpacked message object to callback
*/
MinRK
use utf8.js...
r18331 if (typeof data === "string") {
// text JSON message
MinRK
cleanup serialize per review
r18416 callback(JSON.parse(data));
MinRK
use utf8.js...
r18331 } else {
// binary message
_deserialize_binary(data, callback);
}
};
MinRK
support binary message from javascript
r18332 var _serialize_binary = function (msg) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* implement the binary serialization protocol
* serializes JSON message to ArrayBuffer
*/
MinRK
cleanup serialize per review
r18416 msg = _.clone(msg);
MinRK
support binary message from javascript
r18332 var offsets = [];
MinRK
test binary websocket messages...
r18337 var buffers = [];
MinRK
cleanup serialize per review
r18416 msg.buffers.map(function (buf) {
MinRK
test binary websocket messages...
r18337 buffers.push(buf);
});
MinRK
support binary message from javascript
r18332 delete msg.buffers;
MinRK
use TextEncoding for string<->ArrayBuffer...
r18333 var json_utf8 = (new TextEncoder('utf8')).encode(JSON.stringify(msg));
MinRK
support binary message from javascript
r18332 buffers.unshift(json_utf8);
var nbufs = buffers.length;
offsets.push(4 * (nbufs + 1));
var i;
for (i = 0; i + 1 < buffers.length; i++) {
offsets.push(offsets[offsets.length-1] + buffers[i].byteLength);
}
var msg_buf = new Uint8Array(
offsets[offsets.length-1] + buffers[buffers.length-1].byteLength
);
MinRK
unsigned ints for offsets...
r18338 // use DataView.setUint32 for network byte-order
MinRK
support binary message from javascript
r18332 var view = new DataView(msg_buf.buffer);
// write nbufs to first 4 bytes
MinRK
unsigned ints for offsets...
r18338 view.setUint32(0, nbufs);
MinRK
support binary message from javascript
r18332 // write offsets to next 4 * nbufs bytes
for (i = 0; i < offsets.length; i++) {
MinRK
unsigned ints for offsets...
r18338 view.setUint32(4 * (i+1), offsets[i]);
MinRK
support binary message from javascript
r18332 }
// write all the buffers at their respective offsets
for (i = 0; i < buffers.length; i++) {
msg_buf.set(new Uint8Array(buffers[i].buffer), offsets[i]);
}
// return raw ArrayBuffer
return msg_buf.buffer;
};
var serialize = function (msg) {
if (msg.buffers && msg.buffers.length) {
return _serialize_binary(msg);
} else {
return JSON.stringify(msg);
}
};
var exports = {
deserialize : deserialize,
serialize: serialize
MinRK
use utf8.js...
r18331 };
MinRK
support binary message from javascript
r18332 return exports;
MinRK
use utf8.js...
r18331 });