forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 279
/
ChannelUtils.js
41 lines (34 loc) · 1.57 KB
/
ChannelUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* global Window, Uint8Array */
define(function (require, exports, module) {
"use strict";
var UUID = require("bramble/thirdparty/uuid.core");
// We have to be careful trying to transfer ArrayBuffer in postMessage
// in Blink (and also Safari, it seems):
// https://code.google.com/p/chromium/issues/detail?id=334408&q=transferable&colspec=ID%20Pri%20M%20Week%20ReleaseBlock%20Cr%20Status%20Owner%20Summary%20OS%20Modified
//
// Here we try to transfer an ArrayBuffer as the first thing we do, and see
// if we get it back correctly.
function checkArrayBufferTransfer(port, callback) {
var numbers = [1, 2, 3, 4];
var data = new Uint8Array(numbers);
var buffer = data.buffer;
function checkArrayBufferTransferHandler(e) {
port.removeEventListener("message", checkArrayBufferTransferHandler, false);
try {
var a = new Uint8Array(numbers);
var b = new Uint8Array(e.data.buffer);
callback(null, a.length === b.length &&
a[0] === b[0] &&
a[1] === b[1] &&
a[2] === b[2] &&
a[3] === b[3]);
} catch(err) {
callback(null, false);
}
}
port.addEventListener("message", checkArrayBufferTransferHandler, false);
port.postMessage({buffer: buffer}, [buffer]);
}
exports.checkArrayBufferTransfer = checkArrayBufferTransfer;
exports.UUID = UUID;
});