Skip to content

Instantly share code, notes, and snippets.

@chemdemo
Created April 17, 2014 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemdemo/10978154 to your computer and use it in GitHub Desktop.
Save chemdemo/10978154 to your computer and use it in GitHub Desktop.
Test postMessage
// see => http://jsperf.com/postmessage
var echoSetTimeout = (function() {
return function(msg, cb) {
setTimeout(cb, 0);
}
})();
var echoWorker = (function() {
var code = 'onmessage = function(e) { postMessage(e.data) };';
var blobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
if(!blobBuilder) {
return echoSetTimeout;
}
var bb = new (blobBuilder)();
bb.append(code);
var blobURL = (window.URL || window.webkitURL || window.mozURL).createObjectURL(bb.getBlob());
var worker = new Worker(blobURL);
return function(msg, cb) {
worker.onmessage = cb;
worker.postMessage(msg);
};
})();
var echoIFramePostMessage = (function() {
var iframe = document.createElement('iframe');
window.onload = function() {
iframe.style.display = 'none';
document.body.appendChild(iframe);
iframe.contentDocument.write('<html><head><script>onmessage = function(e) { e.source.postMessage(e.data, \'*\') };<'+'/script><'+'/head><'+'/html>');
};
return function(msg, cb) {
window.onmessage = cb;
iframe.contentWindow.postMessage(msg, '*');
};
})();
var echoPostMessage = (function() {
return function(msg, cb) {
async = false;
window.onmessage = cb;
window.postMessage(msg, '*');
async = true;
};
})();
var payload = {
shortString: 'test',
simple: {
foo: "bar",
arr: []
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment