提交 eb6ac3af 编写于 作者: W wusongqing

updated docs

Signed-off-by: Nwusongqing <wusongqing@huawei.com>
上级 3d43f67e
......@@ -3,6 +3,7 @@
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A Worker thread is an independent thread running in parallel with the main thread. The thread that creates the Worker thread is referred to as a host thread. The URL file passed in during Worker creation is executed in the Worker thread. The Worker thread can process time-consuming operations, but cannot directly operate the UI.
## Modules to Import
......@@ -10,30 +11,31 @@
import worker from '@ohos.worker';
```
## System Capabilities
SystemCapability.Utils.Lang
## Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| parentPort | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscope) | Yes| Yes| Object of the worker thread used to communicate with the host thread.|
**System capability**: SystemCapability.Utils.Lang
| Name | Type | Readable| Writable| Description |
| ---------- | --------------------------------------------------------- | ---- | ---- | ------------------------------------ |
| parentPort | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscope) | Yes | Yes | Object of the Worker thread used to communicate with the host thread.|
## WorkerOptions
Provides options that can be set for the worker to create.
Provides options that can be set for the **Worker** instance to create.
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| type | "classic" | Yes| Yes| Mode in which the worker executes the script.|
| name | string | Yes| Yes| Name of the worker.|
| Name| Type | Readable| Writable| Description |
| ---- | --------- | ---- | ---- | ---------------------- |
| type | "classic" | Yes | Yes | Mode in which the Worker thread executes the script.|
| name | string | Yes | Yes | Name of the Worker thread. |
## Worker
Before using the following methods, you must construct a worker instance. The **Worker** class inherits from [EventTarget](#eventtarget).
Before using the following APIs, you must construct a **Worker** instance. The **Worker** class inherits from [EventTarget](#eventtarget).
### constructor
......@@ -42,18 +44,20 @@ constructor(scriptURL: string, options?: WorkerOptions)
A constructor used to create a **Worker** instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| scriptURL | string | Yes| URL of the script to be executed by the worker. 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.|
| options | [WorkerOptions](#workeroptions) | No| Options that can be set for the worker.|
| 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.|
| options | [WorkerOptions](#workeroptions) | No | Options that can be set for the **Worker** instance. |
**Return value**
| Type| Description|
| -------- | -------- |
| worker | Returns the **Worker** instance created; returns **undefined** if the **Worker** instance fails to be created.|
| Type | Description |
| ------ | --------------------------------------------------------- |
| Worker | Returns the **Worker** instance created; returns **undefined** if the **Worker** instance fails to be created.|
**Example**
......@@ -66,14 +70,16 @@ const workerInstance = new worker.Worker("workers/worker.js", {name:"first worke
postMessage(message: Object, options?: PostMessageOptions): void
Sends a message to the worker thread. The data is transferred using the structured clone algorithm.
Sends a message to the Worker thread. The data is transferred using the structured clone algorithm.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| message | Object | Yes| Data to be sent to the worker.|
| options | [PostMessageOptions](#postmessageoptions) | No| **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
| Name | Type | Mandatory| Description |
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| message | Object | Yes | Data to be sent to the Worker thread. |
| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
**Example**
......@@ -81,6 +87,7 @@ Sends a message to the worker thread. The data is transferred using the structur
const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.postMessage("hello world");
```
```
const workerInstance= new worker.Worker("workers/worker.js");
var buffer = new ArrayBuffer(8);
......@@ -92,14 +99,16 @@ workerInstance.postMessage(buffer, [buffer]);
on(type: string, listener: EventListener): void
Adds an event listener to the worker.
Adds an event listener for the Worker instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event to listen for.|
| listener | [EventListener](#eventlistener) | Yes| Callback to invoke when an event of the specified type occurs.|
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ---------------- |
| type | string | Yes | Type of the event to listen for.|
| listener | [EventListener](#eventlistener) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example**
......@@ -115,14 +124,16 @@ workerInstance.on("alert", (e)=>{
once(type: string, listener: EventListener): void
Adds an event listener to the worker and removes the event listener automatically after it is invoked once.
Adds an event listener for the Worker thread and removes the event listener after it is invoked once.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event to listen for.|
| listener | [EventListener](#eventlistener) | Yes| Callback to invoke when an event of the specified type occurs.|
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ---------------- |
| type | string | Yes | Type of the event to listen for.|
| listener | [EventListener](#eventlistener) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example**
......@@ -138,14 +149,16 @@ workerInstance.once("alert", (e)=>{
off(type: string, listener?: EventListener): void
Removes an event listener for the worker.
Removes an event listener for the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event for which the event listener is removed.|
| listener | [EventListener](#eventlistener) | No| Callback of the event listener to remove.|
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ---------------------- |
| type | string | Yes | Type of the event for which the event listener is removed. |
| listener | [EventListener](#eventlistener) | No | Callback of the event listener to remove.|
**Example**
......@@ -159,7 +172,9 @@ workerInstance.off("alert");
terminate(): void
Terminates the worker thread to stop the worker from receiving messages.
Terminates the Worker thread to stop it from receiving messages.
**System capability**: SystemCapability.Utils.Lang
**Example**
......@@ -173,13 +188,15 @@ workerInstance.terminate()
onexit?: (code: number) =&gt; void
Defines the event handler to be called when the worker exits. The handler is executed in the host thread.
Defines the event handler to be called when the Worker thread exits. The handler is executed in the host thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| code | number | No| Code indicating the worker exit state.|
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------ |
| code | number | No | Code indicating the Worker thread exit state.|
**Example**
......@@ -195,13 +212,15 @@ workerInstance.onexit = function(e) {
onerror?: (err: ErrorEvent) =&gt; void
Defines the event handler to be called when an exception occurs during worker execution. The event handler is executed in the host thread.
Defines the event handler to be called when an exception occurs during Worker execution. The event handler is executed in the host thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| err | [ErrorEvent](#errorevent) | No| Error data.|
| Name| Type | Mandatory| Description |
| ------ | ------------------------- | ---- | ---------- |
| err | [ErrorEvent](#errorevent) | No | Error data.|
**Example**
......@@ -217,13 +236,15 @@ workerInstance.onerror = function(e) {
onmessage?: (event: MessageEvent) =&gt; void
Defines the event handler to be called when the host thread receives a message created by itself and sent by the worker through the **parentPort.postMessage**. The event handler is executed in the host thread.
Defines the event handler to be called when the host thread receives a message created by itself and sent by the Worker thread through the **parentPort.postMessage**. The event handler is executed in the host thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | [MessageEvent](#messageevent) | No| Message received.|
| Name| Type | Mandatory| Description |
| ------ | ----------------------------- | ---- | ---------------------- |
| event | [MessageEvent](#messageevent) | No | Message received.|
**Example**
......@@ -239,13 +260,15 @@ workerInstance.onmessage = function(e) {
onmessageerror?: (event: MessageEvent) =&gt; void
Defines the event handler to be called when the worker receives a message that cannot be serialized. The event handler is executed in the host thread.
Defines the event handler to be called when the Worker thread receives a message that cannot be serialized. The event handler is executed in the host thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | [MessageEvent](#messageevent) | No| Error data.|
| Name| Type | Mandatory| Description |
| ------ | ----------------------------- | ---- | ---------- |
| event | [MessageEvent](#messageevent) | No | Error data.|
**Example**
......@@ -264,14 +287,16 @@ workerInstance.onmessageerror= function(e) {
addEventListener(type: string, listener: EventListener): void
Adds an event listener to the worker.
Adds an event listener for the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event to listen for.|
| listener | [EventListener](#eventlistener) | Yes| Callback to invoke when an event of the specified type occurs.|
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ---------------- |
| type | string | Yes | Type of the event to listen for.|
| listener | [EventListener](#eventlistener) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example**
......@@ -287,14 +312,16 @@ workerInstance.addEventListener("alert", (e)=>{
removeEventListener(type: string, callback?: EventListener): void
Removes an event listener for the worker.
Removes an event listener for the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event for which the event listener is removed.|
| callback | [EventListener](#eventlistener) | No| Callback of the event listener to remove.|
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ---------------------- |
| type | string | Yes | Type of the event for which the event listener is removed. |
| callback | [EventListener](#eventlistener) | No | Callback of the event listener to remove.|
**Example**
......@@ -308,18 +335,20 @@ workerInstance.removeEventListener("alert")
dispatchEvent(event: Event): boolean
Dispatches the event defined for the worker.
Dispatches the event defined for the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | [Event](#event) | Yes| Event to dispatch.|
| Name| Type | Mandatory| Description |
| ------ | --------------- | ---- | ---------------- |
| event | [Event](#event) | Yes | Event to dispatch.|
**Return value**
| Type| Description|
| -------- | -------- |
| Type | Description |
| ------- | ------------------------------- |
| boolean | Returns **true** if the event is dispatched successfully; returns **false** otherwise.|
**Example**
......@@ -334,7 +363,9 @@ workerInstance.dispatchEvent({type:"alert"})
removeAllListener(): void
Removes all event listeners for the worker.
Removes all event listeners for the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Example**
......@@ -346,21 +377,23 @@ workerInstance.removeAllListener({type:"alert"})
## DedicatedWorkerGlobalScope
Implements communication between the worker thread and the host thread. The **postMessage** API is used to send messages to the host thread, and the **close** API is used to terminate the worker thread. The **DedicatedWorkerGlobalScope** class inherits from [WorkerGlobalScope](#workerglobalscope).
Implements communication between the Worker thread and the host thread. The **postMessage** API is used to send messages to the host thread, and the **close** API is used to terminate the Worker thread. The **DedicatedWorkerGlobalScope** class inherits from [WorkerGlobalScope](#workerglobalscope).
### postMessage
postMessage(message: Object, options?: PostMessageOptions): void
Sends a message to the host thread from the worker.
Sends a message to the host thread from the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| message | Object | Yes| Data to be sent to the worker.|
| options | [PostMessageOptions](#postmessageoptions) | No| **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
| Name | Type | Mandatory| Description |
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| message | Object | Yes | Data to be sent to the Worker thread. |
| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
**Example**
......@@ -373,6 +406,7 @@ workerInstance.onmessage = function(e) {
console.log("receive data from worker.js")
}
```
```
// worker.js
import worker from '@ohos.worker';
......@@ -387,7 +421,9 @@ parentPort.onmessage = function(e){
close(): void
Closes the worker thread to stop the worker from receiving messages.
Terminates the Worker thread to stop it from receiving messages.
**System capability**: SystemCapability.Utils.Lang
**Example**
......@@ -396,6 +432,7 @@ Closes the worker thread to stop the worker from receiving messages.
import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js")
```
```
// worker.js
import worker from '@ohos.worker';
......@@ -410,13 +447,15 @@ parentPort.onmessage = function(e) {
onmessage?: (event: MessageEvent) =&gt; void
Defines the event handler to be called when the worker thread receives a message sent by the host thread through **worker.postMessage**. The event handler is executed in the worker thread.
Defines the event handler to be called when the Worker thread receives a message sent by the host thread through **worker.postMessage**. The event handler is executed in the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | [MessageEvent](#messageevent) | No| Message received.|
| Name| Type | Mandatory| Description |
| ------ | ----------------------------- | ---- | ---------------------- |
| event | [MessageEvent](#messageevent) | No | Message received.|
**Example**
......@@ -426,6 +465,7 @@ import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js")
workerInstance.postMessage("hello world")
```
```
// worker.js
import worker from '@ohos.worker';
......@@ -440,13 +480,15 @@ parentPort.onmessage = function(e) {
onmessageerror?: (event: MessageEvent) =&gt; void
Defines the event handler to be called when the worker receives a message that cannot be deserialized. The event handler is executed in the worker thread.
Defines the event handler to be called when the Worker thread receives a message that cannot be deserialized. The event handler is executed in the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | [MessageEvent](#messageevent) | No| Error data.|
| Name| Type | Mandatory| Description |
| ------ | ----------------------------- | ---- | ---------- |
| event | [MessageEvent](#messageevent) | No | Error data.|
**Example**
......@@ -455,6 +497,7 @@ Defines the event handler to be called when the worker receives a message that c
import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js")
```
```
// worker.js
import worker from '@ohos.worker';
......@@ -469,19 +512,23 @@ parentPort.onmessageerror= function(e) {
Specifies the object whose ownership needs to be transferred during data transfer. The object must be **ArrayBuffer**.
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| transfer | Object[] | Yes| Yes| **ArrayBuffer** array used to transfer the ownership.|
**System capability**: SystemCapability.Utils.Lang
| Name | Type| Readable| Writable| Description |
| -------- | -------- | ---- | ---- | --------------------------------- |
| transfer | Object[] | Yes | Yes | **ArrayBuffer** array used to transfer the ownership.|
## Event
Defines the event.
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| type | string | Yes| No| Type of the event.|
| timeStamp | number | Yes| No| Timestamp (accurate to millisecond) when the event is created.|
**System capability**: SystemCapability.Utils.Lang
| Name | Type| Readable| Writable| Description |
| --------- | -------- | ---- | ---- | ---------------------------------- |
| type | string | Yes | No | Type of the event. |
| timeStamp | number | Yes | No | Timestamp (accurate to millisecond) when the event is created.|
## EventListener
......@@ -493,16 +540,18 @@ Implements event listening.
Specifies the callback to invoke.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| evt | [Event](#event) | Yes| Event class for the callback to invoke.|
| Name| Type | Mandatory| Description |
| ------ | --------------- | ---- | -------------- |
| evt | [Event](#event) | Yes | Event class for the callback to invoke.|
**Return value**
| Type| Description|
| -------- | -------- |
| Type | Description |
| ------------------------------------- | ------------------------------- |
| void&nbsp;\|&nbsp;Promise&lt;void&gt; | Returns no value or returns a **Promise**.|
**Example**
......@@ -517,57 +566,66 @@ workerInstance.addEventListener("alert", (e)=>{
## ErrorEvent
Provides detailed information about the exception occurred during worker execution. The **ErrorEvent** class inherits from [Event](#event).
Provides detailed information about the exception occurred during Worker execution. The **ErrorEvent** class inherits from [Event](#event).
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| message | string | Yes| No| Information about the exception.|
| filename | string | Yes| No| File where the exception is located.|
| lineno | number | Yes| No| Number of the line where the exception is located.|
| colno | number | Yes| No| Number of the column where the exception is located.|
| error | Object | Yes| No| Type of the exception.|
| Name | Type| Readable| Writable| Description |
| -------- | -------- | ---- | ---- | -------------------- |
| message | string | Yes | No | Information about the exception.|
| filename | string | Yes | No | File where the exception is located.|
| lineno | number | Yes | No | Number of the line where the exception is located. |
| colno | number | Yes | No | Number of the column where the exception is located. |
| error | Object | Yes | No | Type of the exception. |
## MessageEvent
Holds the data transferred between worker threads.
Holds the data transferred between Worker threads.
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| data | T | Yes| No| Data transferred between threads.|
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description |
| ---- | -------- | ---- | ---- | ------------------ |
| data | T | Yes | No | Data transferred between threads.|
## WorkerGlobalScope
Defines the running environment of the worker thread. The **WorkerGlobalScope** class inherits from [EventTarget](#eventtarget).
## WorkerGlobalScope
Defines the running environment of the Worker thread. The **WorkerGlobalScope** class inherits from [EventTarget](#eventtarget).
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| name | string | Yes| No| Worker name specified when there is a new worker.|
| self | [WorkerGlobalScope](#workerglobalscope)&nbsp;&amp;&nbsp;typeof&nbsp;globalThis | Yes| No| WorkerGlobalScope.|
**System capability**: SystemCapability.Utils.Lang
| Name| Type | Readable| Writable| Description |
| ---- | ------------------------------------------------------------ | ---- | ---- | --------------------------------------- |
| name | string | Yes | No | **Worker** instance specified when there is a new **Worker** instance.|
| self | [WorkerGlobalScope](#workerglobalscope)&nbsp;&amp;&nbsp;typeof&nbsp;globalThis | Yes | No | WorkerGlobalScope. |
### onerror
onerror?: (ev: ErrorEvent) =&gt; void
Defines the event handler to be called when an exception occurs during worker execution. The event handler is executed in the worker thread.
Defines the event handler to be called when an exception occurs during Worker execution. The event handler is executed in the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| ev | [ErrorEvent](#errorevent) | No| Error data.|
| Name| Type | Mandatory| Description |
| ------ | ------------------------- | ---- | ---------- |
| ev | [ErrorEvent](#errorevent) | No | Error data.|
**Example**
```
// main.js
import worker from '@ohos.worker';
const workerInstance = new worker.Worker("workers/worker.js")
```
```
// worker.js
import worker from '@ohos.worker';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册