Mar*_*der 6 javascript buffer data-transfer web-worker arraybuffer
由于Web-Worker JSON在线程之间序列化数据,所以这样的东西不起作用:
worker.js
function Animal() {}
Animal.prototype.foobar = function() {}
self.onmessage = function(e) {
self.postMessage({animal: new Animal()})
}
Run Code Online (Sandbox Code Playgroud)
main.js
let worker = new Worker('worker.js')
worker.onmessage = function(e) {
console.log(e.data)
}
worker.postMessage('go!')
Run Code Online (Sandbox Code Playgroud)
结果将是foobar原型方法丢失的简单对象.
是否可以将自定义对象传回主线程而不会丢失其原型方法?就像,这可能ArrayBuffer吗?我不熟悉那些东西,所以我有点迷茫.
在 onmessage 中,您 JSON.parse(m,reviver)
function Animal(name, age){
var private_name = name;
this.public_age = age;
this.log = function(){
console.log('Animal', private_name, this.public_age);
}
this.toJson = function(){
return JSON.stringify({
__type__:'Animal', // name of class
__args__:[this.public_age, private_name] // same args that construct
});
}
}
Animal.prototype.age = function(){
return this.public_age;
}
var a = new Animal('boby', 6);
worker.postMessage(JSON.stringify(a));
function reviver(o){
if(o.__type__){
var constructor=reviver.register[o.__type__];
if(!constructor) throw Error('__type__ not recognized');
var newObject = {};
return constructor.apply(newObject, o.__args__);
}
return o;
}
reviver.register={}; // you can register any classes
reviver.register['Animal'] = Animal;
worker.onmessage = function(m){
var a = JSON.parse(e, reviver);
}
Run Code Online (Sandbox Code Playgroud)