Created
April 4, 2013 11:55
-
-
Save monjudoh/5309779 to your computer and use it in GitHub Desktop.
現在の環境でstructured cloneで渡せるもの渡せないものを調べる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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