提交 b44f71b0 编写于 作者: W wangzhaoyong

add special case for worker.postMessage

issue: https://gitee.com/openharmony/docs/issues/I5WBCBSigned-off-by: Nwangzhaoyong <wangzhaoyong@huawei.com>
Change-Id: I93d20907eedfc6303a695d6c38c598e49142949f
上级 be4f6c10
...@@ -704,6 +704,56 @@ parentPort.onerror = function(e){ ...@@ -704,6 +704,56 @@ parentPort.onerror = function(e){
| ArrayBuffer | 提供transfer能力 | 是 | | ArrayBuffer | 提供transfer能力 | 是 |
| TypedArray | | 是 | | 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线程完成计算任务,再把结果返回给主线程。 Worker基于Actor并发模型实现。在Worker的交互流程中,JS主线程可以创建多个Worker子线程,各个Worker线程间相互隔离,并通过序列化传递对象,等到Worker线程完成计算任务,再把结果返回给主线程。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册