Skip to content

Instantly share code, notes, and snippets.

@monjudoh
Created April 4, 2013 11:55
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 monjudoh/5309779 to your computer and use it in GitHub Desktop.
Save monjudoh/5309779 to your computer and use it in GitHub Desktop.
現在の環境でstructured cloneで渡せるもの渡せないものを調べる
(function (global) {
var supports = Object.create(null);
(function __BlobUrls() {
// URLにベンダプレフィクスが付いているのはwebkitだけ!!
var URL = global.URL || global.webkitURL;
supports.BlobUrls = URL !== undefined && URL.createObjectURL;
})();
(function __MessageChannel() {
supports.MessageChannel = !!global['MessageChannel'];
})();
supports.StructuredClone = Object.create(null);
(function (URL,callback) {
function check() {
if (Object.keys(supports.StructuredClone).length === types.length) {
Object.freeze(supports.StructuredClone);
callback(supports.StructuredClone);
}
}
var types = 'Date RegExp Error ArrayBuffer Blob'.split(' ');
var skipTypes = [];
var objects = Object.create(null);
if (global['Blob']) {
try {
objects['Blob'] = (new Blob([''], {type: 'application/javascript'}))
} catch (e) {
skipTypes.push('Blob');
}
}
if (supports.MessageChannel) {
(function () {
var channel = new MessageChannel;
channel.port1.onmessage = function(e){
var type = e.data.type;
supports.StructuredClone[type] = e.data.object instanceof global[type];
check();
};
types.forEach(function(type){
if (skipTypes.indexOf(type) !== -1) {
supports.StructuredClone[type] = false;
return;
}
try {
channel.port2.postMessage({object: objects[type] || (new (global[type])()), type:type});
} catch (e) {
supports.StructuredClone[type] = false;
check();
}
});
})();
return;
}
if (supports.BlobUrls) {
var script = '(' + (function (self) {
self.onmessage = function(e) {
var type = e.data.type;
self.postMessage({
result:e.data.object instanceof self[ type],
type:type
});
};
}).toString() + ')(self);';
var blob = new Blob([script],{type:'application/javascript'});
var worker = new Worker(URL.createObjectURL(blob));
worker.onmessage = function(e) {
supports.StructuredClone[e.data.type] = e.data.result;
check();
};
types.forEach(function(type){
try {
worker.postMessage({object: objects[type] || (new (global[type])()), type:type});
} catch (e) {
supports.StructuredClone[type] = false;
check();
}
});
return;
}
})((global.URL || global.webkitURL),function(StructuredClone){
console.log(JSON.stringify(StructuredClone));
});
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment