提交 e286db86 编写于 作者: G Gloria

Update docs against 8608

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 e5d98006
...@@ -8,7 +8,7 @@ The worker thread is an independent thread running in parallel with the main thr ...@@ -8,7 +8,7 @@ The worker thread is an independent thread running in parallel with the main thr
## Modules to Import ## Modules to Import
``` ```js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
``` ```
...@@ -28,10 +28,11 @@ Provides options that can be set for the **Worker** instance to create. ...@@ -28,10 +28,11 @@ Provides options that can be set for the **Worker** instance to create.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
| Name| Type | Readable| Writable| Description | | Name | Type | Readable| Writable| Description |
| ---- | --------- | ---- | ---- | ---------------------- | | ------ | --------- | ---- | ---- | ---------------------- |
| type | "classic" | Yes | Yes | Mode in which the worker thread executes the script.| | type | "classic" | Yes | Yes | Mode in which the worker thread executes the script.|
| name | string | Yes | Yes | Name of the worker thread. | | name | string | Yes | Yes | Name of the worker thread. |
| shared | boolean | Yes | Yes | Whether the worker can be shared.|
## Worker ## Worker
...@@ -51,7 +52,7 @@ A constructor used to create a **Worker** instance. ...@@ -51,7 +52,7 @@ A constructor used to create a **Worker** instance.
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | | --------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| scriptURL | string | Yes | URL of the script to be executed by the Worker thread. The script is stored in the **workers** directory, which is in the same directory as the **pages** directory of the new DevEco Studio project. If the **workers** directory does not exist, you need to create it.| | scriptURL | string | Yes | URL of the script to be executed by the worker thread.<br>In the FA or stage model, DevEco Studio creates a **Worker** project in either of the following scenarios:<br>(a) The **workers** directory is at the same level as the **pages** directory.<br>(b) The **workers** directory is at a different level from the **pages** directory.
| options | [WorkerOptions](#workeroptions) | No | Options that can be set for the **Worker** instance. | | options | [WorkerOptions](#workeroptions) | No | Options that can be set for the **Worker** instance. |
**Return value** **Return value**
...@@ -62,11 +63,68 @@ A constructor used to create a **Worker** instance. ...@@ -62,11 +63,68 @@ A constructor used to create a **Worker** instance.
**Example** **Example**
``` ```js
// Create a worker thread.
// In the FA model, the workers and pages directories are at the same level.
const workerInstance = new worker.Worker("workers/worker.js", {name:"first worker"}); const workerInstance = new worker.Worker("workers/worker.js", {name:"first worker"});
// In the FA model, the workers and pages directories are at different levels.
const workerInstance = new worker.Worker("../workers/worker.js", {name:"first worker"});
// In the stage model, the workers and pages directories are at the same level.
const workerInstance = new worker.Worker('entry/ets/workers/worker.ts');
// In the stage model, the workers and pages directories are at different levels.
const workerInstance = new worker.Worker('entry/ets/pages/workers/worker.ts');
// scriptURL—— Description of "entry/ets/workers/worker.ts".
// entry is the value of the name attribute under module in the module.json5 file.
// ets indicates the programming language in use.
```
Depending on whether the works and pages directories are at the same level, you may need to configure the **buildOption** attribute in the **build-profile.json5** file.
(1) If the workers and pages directories are at the same level, the configuration is optional.
In the FA model:
```json
"buildOption": {
"sourceOption": {
"workers": [
"./src/main/ets/MainAbility/workers/worker.ts"
]
}
}
```
In the stage model:
```json
"buildOption": {
"sourceOption": {
"workers": [
"./src/main/ets/workers/worker.ts"
]
}
}
```
(2) If the workers and pages directories are at different levels, the configuration is mandatory.
In the FA model:
```json
"buildOption": {
"sourceOption": {
"workers": [
"./src/main/ets/workers/worker.ts"
]
}
}
```
In the stage model:
```json
"buildOption": {
"sourceOption": {
"workers": [
"./src/main/ets/pages/workers/worker.ts"
]
}
}
``` ```
### postMessage ### postMessage
postMessage(message: Object, options?: PostMessageOptions): void postMessage(message: Object, options?: PostMessageOptions): void
...@@ -84,12 +142,9 @@ Sends a message to the worker thread. The message data is transferred using the ...@@ -84,12 +142,9 @@ Sends a message to the worker thread. The message data is transferred using the
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js"); const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.postMessage("hello world"); workerInstance.postMessage("hello world");
```
```
const workerInstance= new worker.Worker("workers/worker.js"); const workerInstance= new worker.Worker("workers/worker.js");
var buffer = new ArrayBuffer(8); var buffer = new ArrayBuffer(8);
workerInstance.postMessage(buffer, [buffer]); workerInstance.postMessage(buffer, [buffer]);
...@@ -113,8 +168,8 @@ Adds an event listener for the worker instance. ...@@ -113,8 +168,8 @@ Adds an event listener for the worker instance.
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.on("alert", (e)=>{ workerInstance.on("alert", (e)=>{
console.log("alert listener callback"); console.log("alert listener callback");
}) })
...@@ -138,7 +193,7 @@ Adds an event listener for the worker thread and removes the event listener afte ...@@ -138,7 +193,7 @@ Adds an event listener for the worker thread and removes the event listener afte
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js"); const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.once("alert", (e)=>{ workerInstance.once("alert", (e)=>{
console.log("alert listener callback"); console.log("alert listener callback");
...@@ -163,7 +218,7 @@ Removes an event listener for the worker thread. ...@@ -163,7 +218,7 @@ Removes an event listener for the worker thread.
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js"); const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.off("alert"); workerInstance.off("alert");
``` ```
...@@ -179,9 +234,9 @@ Terminates the worker thread to stop it from receiving messages. ...@@ -179,9 +234,9 @@ Terminates the worker thread to stop it from receiving messages.
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.terminate() workerInstance.terminate();
``` ```
...@@ -201,10 +256,10 @@ Defines the event handler to be called when the worker thread exits. The handler ...@@ -201,10 +256,10 @@ Defines the event handler to be called when the worker thread exits. The handler
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.onexit = function(e) { workerInstance.onexit = function(e) {
console.log("onexit") console.log("onexit");
} }
``` ```
...@@ -225,10 +280,10 @@ Defines the event handler to be called when an exception occurs during worker ex ...@@ -225,10 +280,10 @@ Defines the event handler to be called when an exception occurs during worker ex
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.onerror = function(e) { workerInstance.onerror = function(e) {
console.log("onerror") console.log("onerror");
} }
``` ```
...@@ -249,10 +304,12 @@ Defines the event handler to be called when the host thread receives a message s ...@@ -249,10 +304,12 @@ Defines the event handler to be called when the host thread receives a message s
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.onmessage = function(e) { workerInstance.onmessage = function(e) {
console.log("onerror") // e: MessageEvent<T>. The usage is as follows:
// let data = e.data;
console.log("onmessage");
} }
``` ```
...@@ -273,10 +330,10 @@ Defines the event handler to be called when the worker thread receives a message ...@@ -273,10 +330,10 @@ Defines the event handler to be called when the worker thread receives a message
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.onmessageerror= function(e) { workerInstance.onmessageerror= function(e) {
console.log("onmessageerror") console.log("onmessageerror");
} }
``` ```
...@@ -301,8 +358,8 @@ Adds an event listener for the worker thread. ...@@ -301,8 +358,8 @@ Adds an event listener for the worker thread.
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.addEventListener("alert", (e)=>{ workerInstance.addEventListener("alert", (e)=>{
console.log("alert listener callback"); console.log("alert listener callback");
}) })
...@@ -326,9 +383,9 @@ Removes an event listener for the worker thread. ...@@ -326,9 +383,9 @@ Removes an event listener for the worker thread.
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.removeEventListener("alert") workerInstance.removeEventListener("alert");
``` ```
...@@ -354,9 +411,9 @@ Dispatches the event defined for the worker thread. ...@@ -354,9 +411,9 @@ Dispatches the event defined for the worker thread.
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.dispatchEvent({type:"alert"}) workerInstance.dispatchEvent({type:"alert"});
``` ```
...@@ -370,9 +427,9 @@ Removes all event listeners for the worker thread. ...@@ -370,9 +427,9 @@ Removes all event listeners for the worker thread.
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.removeAllListener({type:"alert"}) workerInstance.removeAllListener();
``` ```
...@@ -383,7 +440,7 @@ Implements communication between the worker thread and the host thread. The **po ...@@ -383,7 +440,7 @@ Implements communication between the worker thread and the host thread. The **po
### postMessage ### postMessage
postMessage(message: Object, options?: PostMessageOptions): void postMessage(messageObject: Object, options?: PostMessageOptions): void
Sends a message to the host thread from the worker thread. Sends a message to the host thread from the worker thread.
...@@ -398,22 +455,23 @@ Sends a message to the host thread from the worker thread. ...@@ -398,22 +455,23 @@ Sends a message to the host thread from the worker thread.
**Example** **Example**
``` ```js
// main.js // main.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.postMessage("hello world") workerInstance.postMessage("hello world");
workerInstance.onmessage = function(e) { workerInstance.onmessage = function(e) {
console.log("receive data from worker.js") // let data = e.data;
console.log("receive data from worker.js");
} }
``` ```
```js
```
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const parentPort = worker.parentPort; const parentPort = worker.parentPort;
parentPort.onmessage = function(e){ parentPort.onmessage = function(e){
parentPort.postMessage("receive data from main.js") // let data = e.data;
parentPort.postMessage("receive data from main.js");
} }
``` ```
...@@ -428,13 +486,12 @@ Terminates the worker thread to stop it from receiving messages. ...@@ -428,13 +486,12 @@ Terminates the worker thread to stop it from receiving messages.
**Example** **Example**
``` ```js
// main.js // main.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
```
``` ```
```js
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const parentPort = worker.parentPort; const parentPort = worker.parentPort;
...@@ -460,19 +517,18 @@ Defines the event handler to be called when the worker thread receives a message ...@@ -460,19 +517,18 @@ Defines the event handler to be called when the worker thread receives a message
**Example** **Example**
``` ```js
// main.js // main.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.postMessage("hello world") workerInstance.postMessage("hello world");
```
``` ```
```js
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const parentPort = worker.parentPort; const parentPort = worker.parentPort;
parentPort.onmessage = function(e) { parentPort.onmessage = function(e) {
console.log("receive main.js message") console.log("receive main.js message");
} }
``` ```
...@@ -493,13 +549,12 @@ Defines the event handler to be called when the worker thread receives a message ...@@ -493,13 +549,12 @@ Defines the event handler to be called when the worker thread receives a message
**Example** **Example**
``` ```js
// main.js // main.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js");
```
``` ```
```js
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const parentPort = worker.parentPort; const parentPort = worker.parentPort;
...@@ -557,7 +612,7 @@ Specifies the callback to invoke. ...@@ -557,7 +612,7 @@ Specifies the callback to invoke.
**Example** **Example**
``` ```js
const workerInstance = new worker.Worker("workers/worker.js"); const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.addEventListener("alert", (e)=>{ workerInstance.addEventListener("alert", (e)=>{
console.log("alert listener callback"); console.log("alert listener callback");
...@@ -621,13 +676,12 @@ Defines the event handler to be called when an exception occurs during worker ex ...@@ -621,13 +676,12 @@ Defines the event handler to be called when an exception occurs during worker ex
**Example** **Example**
``` ```js
// main.js // main.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js") const workerInstance = new worker.Worker("workers/worker.js")
``` ```
```js
```
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
const parentPort = worker.parentPort const parentPort = worker.parentPort
...@@ -635,3 +689,94 @@ parentPort.onerror = function(e){ ...@@ -635,3 +689,94 @@ parentPort.onerror = function(e){
console.log("worker.js onerror") console.log("worker.js onerror")
} }
``` ```
## Sample Code
### FA Model
```js
// main.js (The following assumes that the workers and pages directories are at the same level.)
import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.ts");
// Create either a .json or .ts file.
// const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.postMessage("123");
workerInstance.onmessage = function(e) {
let data = e.data;
console.log("main.js onmessage");
// Call terminate after the worker thread receives messages.
workerInstance.terminate();
}
// Call onexit.
workerInstance.onexit = function() {
console.log("main.js terminate");
}
```
```js
// worker.js
import worker from '@ohos.worker';
const parentPort = worker.parentPort
parentPort.onmessage = function(e) {
let data = e.data;
console.log("worker.js onmessage");
parentPort.postMessage("123")
}
parentPort.onerror= function(e) {
console.log("worker.js onerror");
}
```
Configuration of the **build-profile.json5** file:
```json
"buildOption": {
"sourceOption": {
"workers": [
"./src/main/ets/MainAbility/workers/worker.ts"
]
}
}
```
### Stage Model
```js
// main.js (The following assumes that the workers and pages directories are at different levels.)
import worker from '@ohos.worker';
const workerInstance = new worker.Worker("entry/ets/pages/workers/worker.ts");
// Create either a .json or .ts file.
// const workerInstance = new worker.Worker("entry/ets/pages/workers/worker.js");
workerInstance.postMessage("123");
workerInstance.onmessage = function(e) {
let data = e.data;
console.log("main.js onmessage");
// Call terminate after the worker thread receives messages.
workerInstance.terminate();
}
// Call onexit.
workerInstance.onexit = function() {
console.log("main.js terminate");
}
```
```js
// worker.js
import worker from '@ohos.worker';
const parentPort = worker.parentPort
parentPort.onmessage = function(e) {
let data = e.data;
console.log("worker.js onmessage");
parentPort.postMessage("123")
}
parentPort.onerror= function(e) {
console.log("worker.js onerror");
}
```
Configuration of the **build-profile.json5** file:
```json
"buildOption": {
"sourceOption": {
"workers": [
"./src/main/ets/pages/workers/worker.ts"
]
}
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册