在Javascript ES6中,在浏览器中,我想使用" Transferable"将自定义类对象传输给Web worker。接口。这可能吗?我可以找到有关ArrayBuffer对象的文档,但不能找到自定义类对象的文档。
这不是How to pass custom class instances through Web-Workers?的重复,因为我的问题是关于Transferable接口的。我想将自定义类实例传递给worker而不复制它。
答案 0 :(得分:2)
我已经以不同的方式几次回答了这个问题。抱歉,您对本次查询的特定版本的回答肯定是否。
有几个原因。
DispatchQueue.main.async(execute: {
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIViewController()
window.windowLevel = UIWindow.Level.alert+1
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction2 = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert2.addAction(defaultAction2)
window.makeKeyAndVisible()
window.rootViewController?.present(alert2, animated: true, completion: nil)
})
的代码实际上都只是现有结构化克隆算法的开销,
如果您真的想要,我不确定您应该这么做。
想象一个这样的类:
ArrayBuffer
它的属性存储在数组缓冲区中,您可以传输它。但这并没有太大用处,要使其正常工作,我们需要确保可以从接收到的数组缓冲区中构造它。可以肯定地做到这一点:
class Vector2 {
constructor(existing) {
this._data = new Float64Array(2);
}
get x() {
return this._data[0];
}
set x(x) {
this._data[0] = x;
}
get y() {
return this._data[1];
}
set y(y) {
this._data[1] = y;
}
}
现在,您甚至可以通过从子类传递更大的数组缓冲区来对其进行子类化,在该子类中,父级和子级的属性都适合:
class Vector2 {
constructor(existing) {
if(existing instanceof ArrayBuffer) {
this.load(existing);
}
else {
this.init();
}
}
/*
* Loads from existing buffer
* @param {ArrayBuffer} existing
*/
load(existing) {
this._data = existing;
this.initProperties();
}
init() {
// 16 bytes, 8 for each Float64
this._data = new ArrayBuffer(16);
this.initProperties();
}
initProperties() {
this._coordsView = new Float64Array(this._data, 0, 2);
}
get x() {
return this._coordsView[0];
}
set x(x) {
this._coordsView[0] = x;
}
get y() {
return this._coordsView[1];
}
set y(y) {
this._coordsView[1] = y;
}
}
一个简单的测试:
class Vector2Altitude extends Vector2 {
constructor(existing) {
super(existing instanceof ArrayBuffer ? existing : new ArrayBuffer(16 + 8));
this._altitudeView = new Float64Array(this._data, 16, 1);
}
get altitude() {
return this._altitudeView[0];
}
set altitude(alt) {
this._altitudeView[0] = alt;
}
}
要真正使用它,您需要解决许多其他问题,并从根本上实现对复杂对象的内存分配。