未验证 提交 b9c90d02 编写于 作者: O openharmony_ci 提交者: Gitee

!10739 add special case for worker.postMessage

Merge pull request !10739 from wangzhaoyong/master
......@@ -704,6 +704,56 @@ parentPort.onerror = function(e){
| ArrayBuffer | 提供transfer能力 | 是 |
| TypedArray | | 是 |
特例:传递通过自定义class创建出来的object时,不会发生序列化错误,但是自定义class的属性(如Function)无法通过序列化传递。
```js
// main.js
import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.postMessage("message from main to worker");
workerInstance.onmessage = function(d) {
// 当worker线程传递obj2时,data即为obj2。data没有Init、SetName的方法
let data = d.data;
}
```
```js
// worker.js
import worker from '@ohos.worker';
const parentPort = worker.parentPort;
class MyModel {
Init() {
this.name = "wzy"
this.age = 18
}
SetName() {
this.name = "WZY"
}
}
parentPort.onmessage = function(d) {
console.log("worker.js onmessage");
let data = d.data;
let func1 = function() {
console.log("post message is function");
}
let obj1 = {
"index": 2,
"name1": "zhangshan",
setName() {
this.index = 3;
}
}
let obj2 = new MyModel();
// parentPort.postMessage(func1); 传递func1发生序列化错误
// parentPort.postMessage(obj1); 传递obj1发生序列化错误
parentPort.postMessage(obj2); // 传递obj2不会发生序列化错误
}
parentPort.onmessageerror = function(e) {
console.log("worker.js onmessageerror");
}
parentPort.onerror = function(e) {
console.log("worker.js onerror");
}
```
### 内存模型
Worker基于Actor并发模型实现。在Worker的交互流程中,JS主线程可以创建多个Worker子线程,各个Worker线程间相互隔离,并通过序列化传递对象,等到Worker线程完成计算任务,再把结果返回给主线程。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册