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

!2379 Done! 2099 DFX syscap改造

Merge pull request !2379 from wusongqing/TR2099
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > ![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. > 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 ## Modules to Import
...@@ -10,30 +11,31 @@ ...@@ -10,30 +11,31 @@
import worker from '@ohos.worker'; import worker from '@ohos.worker';
``` ```
## System Capabilities
SystemCapability.Utils.Lang
## Attributes ## Attributes
| Name| Type| Readable| Writable| Description| **System capability**: SystemCapability.Utils.Lang
| -------- | -------- | -------- | -------- | -------- |
| parentPort | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscope) | Yes| Yes| Object of the worker thread used to communicate with the host thread.| | Name | Type | Readable| Writable| Description |
| ---------- | --------------------------------------------------------- | ---- | ---- | ------------------------------------ |
| parentPort | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscope) | Yes | Yes | Object of the Worker thread used to communicate with the host thread.|
## WorkerOptions ## 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| | Name| Type | Readable| Writable| Description |
| -------- | -------- | -------- | -------- | -------- | | ---- | --------- | ---- | ---- | ---------------------- |
| type | "classic" | Yes| Yes| Mode in which the worker executes the script.| | type | "classic" | Yes | Yes | Mode in which the Worker thread executes the script.|
| name | string | Yes| Yes| Name of the worker.| | name | string | Yes | Yes | Name of the Worker thread. |
## Worker ## 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 ### constructor
...@@ -42,18 +44,20 @@ constructor(scriptURL: string, options?: WorkerOptions) ...@@ -42,18 +44,20 @@ constructor(scriptURL: string, options?: WorkerOptions)
A constructor used to create a **Worker** instance. A constructor used to create a **Worker** instance.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | 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.| | 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.| | options | [WorkerOptions](#workeroptions) | No | Options that can be set for the **Worker** instance. |
**Return value** **Return value**
| Type| Description| | Type | Description |
| -------- | -------- | | ------ | --------------------------------------------------------- |
| worker | Returns the **Worker** instance created; returns **undefined** if the **Worker** instance fails to be created.| | Worker | Returns the **Worker** instance created; returns **undefined** if the **Worker** instance fails to be created.|
**Example** **Example**
...@@ -66,14 +70,16 @@ const workerInstance = new worker.Worker("workers/worker.js", {name:"first worke ...@@ -66,14 +70,16 @@ const workerInstance = new worker.Worker("workers/worker.js", {name:"first worke
postMessage(message: Object, options?: PostMessageOptions): void 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** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| message | Object | Yes| Data to be sent to the worker.| | 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**.| | options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
**Example** **Example**
...@@ -81,6 +87,7 @@ Sends a message to the worker thread. The data is transferred using the structur ...@@ -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"); 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);
...@@ -92,14 +99,16 @@ workerInstance.postMessage(buffer, [buffer]); ...@@ -92,14 +99,16 @@ workerInstance.postMessage(buffer, [buffer]);
on(type: string, listener: EventListener): void 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** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | -------- | ------------------------------- | ---- | ---------------- |
| type | string | Yes| Type of the event to listen for.| | 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.| | listener | [EventListener](#eventlistener) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example** **Example**
...@@ -115,14 +124,16 @@ workerInstance.on("alert", (e)=>{ ...@@ -115,14 +124,16 @@ workerInstance.on("alert", (e)=>{
once(type: string, listener: EventListener): void 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** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | -------- | ------------------------------- | ---- | ---------------- |
| type | string | Yes| Type of the event to listen for.| | 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.| | listener | [EventListener](#eventlistener) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example** **Example**
...@@ -138,14 +149,16 @@ workerInstance.once("alert", (e)=>{ ...@@ -138,14 +149,16 @@ workerInstance.once("alert", (e)=>{
off(type: string, listener?: EventListener): void 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** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | -------- | ------------------------------- | ---- | ---------------------- |
| type | string | Yes| Type of the event for which the event listener is removed.| | 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.| | listener | [EventListener](#eventlistener) | No | Callback of the event listener to remove.|
**Example** **Example**
...@@ -159,7 +172,9 @@ workerInstance.off("alert"); ...@@ -159,7 +172,9 @@ workerInstance.off("alert");
terminate(): void 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** **Example**
...@@ -173,13 +188,15 @@ workerInstance.terminate() ...@@ -173,13 +188,15 @@ workerInstance.terminate()
onexit?: (code: number) => void onexit?: (code: number) => 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** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | ------ | ---- | ------------------ |
| code | number | No| Code indicating the worker exit state.| | code | number | No | Code indicating the Worker thread exit state.|
**Example** **Example**
...@@ -195,13 +212,15 @@ workerInstance.onexit = function(e) { ...@@ -195,13 +212,15 @@ workerInstance.onexit = function(e) {
onerror?: (err: ErrorEvent) => void onerror?: (err: ErrorEvent) => 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** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | ------------------------- | ---- | ---------- |
| err | [ErrorEvent](#errorevent) | No| Error data.| | err | [ErrorEvent](#errorevent) | No | Error data.|
**Example** **Example**
...@@ -217,13 +236,15 @@ workerInstance.onerror = function(e) { ...@@ -217,13 +236,15 @@ workerInstance.onerror = function(e) {
onmessage?: (event: MessageEvent) => void onmessage?: (event: MessageEvent) => 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** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | ----------------------------- | ---- | ---------------------- |
| event | [MessageEvent](#messageevent) | No| Message received.| | event | [MessageEvent](#messageevent) | No | Message received.|
**Example** **Example**
...@@ -239,13 +260,15 @@ workerInstance.onmessage = function(e) { ...@@ -239,13 +260,15 @@ workerInstance.onmessage = function(e) {
onmessageerror?: (event: MessageEvent) => void onmessageerror?: (event: MessageEvent) => 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** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | ----------------------------- | ---- | ---------- |
| event | [MessageEvent](#messageevent) | No| Error data.| | event | [MessageEvent](#messageevent) | No | Error data.|
**Example** **Example**
...@@ -264,14 +287,16 @@ workerInstance.onmessageerror= function(e) { ...@@ -264,14 +287,16 @@ workerInstance.onmessageerror= function(e) {
addEventListener(type: string, listener: EventListener): void 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** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | -------- | ------------------------------- | ---- | ---------------- |
| type | string | Yes| Type of the event to listen for.| | 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.| | listener | [EventListener](#eventlistener) | Yes | Callback to invoke when an event of the specified type occurs. |
**Example** **Example**
...@@ -287,14 +312,16 @@ workerInstance.addEventListener("alert", (e)=>{ ...@@ -287,14 +312,16 @@ workerInstance.addEventListener("alert", (e)=>{
removeEventListener(type: string, callback?: EventListener): void 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** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | -------- | ------------------------------- | ---- | ---------------------- |
| type | string | Yes| Type of the event for which the event listener is removed.| | 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.| | callback | [EventListener](#eventlistener) | No | Callback of the event listener to remove.|
**Example** **Example**
...@@ -308,18 +335,20 @@ workerInstance.removeEventListener("alert") ...@@ -308,18 +335,20 @@ workerInstance.removeEventListener("alert")
dispatchEvent(event: Event): boolean 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** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | --------------- | ---- | ---------------- |
| event | [Event](#event) | Yes| Event to dispatch.| | event | [Event](#event) | Yes | Event to dispatch.|
**Return value** **Return value**
| Type| Description| | Type | Description |
| -------- | -------- | | ------- | ------------------------------- |
| boolean | Returns **true** if the event is dispatched successfully; returns **false** otherwise.| | boolean | Returns **true** if the event is dispatched successfully; returns **false** otherwise.|
**Example** **Example**
...@@ -334,7 +363,9 @@ workerInstance.dispatchEvent({type:"alert"}) ...@@ -334,7 +363,9 @@ workerInstance.dispatchEvent({type:"alert"})
removeAllListener(): void removeAllListener(): void
Removes all event listeners for the worker. Removes all event listeners for the Worker thread.
**System capability**: SystemCapability.Utils.Lang
**Example** **Example**
...@@ -346,21 +377,23 @@ workerInstance.removeAllListener({type:"alert"}) ...@@ -346,21 +377,23 @@ workerInstance.removeAllListener({type:"alert"})
## DedicatedWorkerGlobalScope ## 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
postMessage(message: Object, options?: PostMessageOptions): void 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** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| message | Object | Yes| Data to be sent to the worker.| | 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**.| | options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The **transferList** array cannot contain **null**.|
**Example** **Example**
...@@ -373,6 +406,7 @@ workerInstance.onmessage = function(e) { ...@@ -373,6 +406,7 @@ workerInstance.onmessage = function(e) {
console.log("receive data from worker.js") console.log("receive data from worker.js")
} }
``` ```
``` ```
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
...@@ -387,7 +421,9 @@ parentPort.onmessage = function(e){ ...@@ -387,7 +421,9 @@ parentPort.onmessage = function(e){
close(): void 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** **Example**
...@@ -396,6 +432,7 @@ Closes the worker thread to stop the worker from receiving messages. ...@@ -396,6 +432,7 @@ Closes the worker thread to stop the worker from receiving messages.
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")
``` ```
``` ```
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
...@@ -410,13 +447,15 @@ parentPort.onmessage = function(e) { ...@@ -410,13 +447,15 @@ parentPort.onmessage = function(e) {
onmessage?: (event: MessageEvent) => void onmessage?: (event: MessageEvent) => 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** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | ----------------------------- | ---- | ---------------------- |
| event | [MessageEvent](#messageevent) | No| Message received.| | event | [MessageEvent](#messageevent) | No | Message received.|
**Example** **Example**
...@@ -426,6 +465,7 @@ import worker from '@ohos.worker'; ...@@ -426,6 +465,7 @@ 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")
``` ```
``` ```
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
...@@ -440,13 +480,15 @@ parentPort.onmessage = function(e) { ...@@ -440,13 +480,15 @@ parentPort.onmessage = function(e) {
onmessageerror?: (event: MessageEvent) => void onmessageerror?: (event: MessageEvent) => 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** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | ----------------------------- | ---- | ---------- |
| event | [MessageEvent](#messageevent) | No| Error data.| | event | [MessageEvent](#messageevent) | No | Error data.|
**Example** **Example**
...@@ -455,6 +497,7 @@ Defines the event handler to be called when the worker receives a message that c ...@@ -455,6 +497,7 @@ Defines the event handler to be called when the worker receives a message that c
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")
``` ```
``` ```
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
...@@ -469,19 +512,23 @@ parentPort.onmessageerror= function(e) { ...@@ -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**. Specifies the object whose ownership needs to be transferred during data transfer. The object must be **ArrayBuffer**.
| Name| Type| Readable| Writable| Description| **System capability**: SystemCapability.Utils.Lang
| -------- | -------- | -------- | -------- | -------- |
| transfer | Object[] | Yes| Yes| **ArrayBuffer** array used to transfer the ownership.| | Name | Type| Readable| Writable| Description |
| -------- | -------- | ---- | ---- | --------------------------------- |
| transfer | Object[] | Yes | Yes | **ArrayBuffer** array used to transfer the ownership.|
## Event ## Event
Defines the event. Defines the event.
| Name| Type| Readable| Writable| Description| **System capability**: SystemCapability.Utils.Lang
| -------- | -------- | -------- | -------- | -------- |
| type | string | Yes| No| Type of the event.| | Name | Type| Readable| Writable| Description |
| timeStamp | number | Yes| No| Timestamp (accurate to millisecond) when the event is created.| | --------- | -------- | ---- | ---- | ---------------------------------- |
| type | string | Yes | No | Type of the event. |
| timeStamp | number | Yes | No | Timestamp (accurate to millisecond) when the event is created.|
## EventListener ## EventListener
...@@ -493,16 +540,18 @@ Implements event listening. ...@@ -493,16 +540,18 @@ Implements event listening.
Specifies the callback to invoke. Specifies the callback to invoke.
**System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | --------------- | ---- | -------------- |
| evt | [Event](#event) | Yes| Event class for the callback to invoke.| | evt | [Event](#event) | Yes | Event class for the callback to invoke.|
**Return value** **Return value**
| Type| Description| | Type | Description |
| -------- | -------- | | ------------------------------------- | ------------------------------- |
| void \| Promise<void> | Returns no value or returns a **Promise**.| | void \| Promise<void> | Returns no value or returns a **Promise**.|
**Example** **Example**
...@@ -517,57 +566,66 @@ workerInstance.addEventListener("alert", (e)=>{ ...@@ -517,57 +566,66 @@ workerInstance.addEventListener("alert", (e)=>{
## ErrorEvent ## 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| | Name | Type| Readable| Writable| Description |
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | ---- | ---- | -------------------- |
| message | string | Yes| No| Information about the exception.| | message | string | Yes | No | Information about the exception.|
| filename | string | Yes| No| File where the exception is located.| | filename | string | Yes | No | File where the exception is located.|
| lineno | number | Yes| No| Number of the line 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.| | colno | number | Yes | No | Number of the column where the exception is located. |
| error | Object | Yes| No| Type of the exception.| | error | Object | Yes | No | Type of the exception. |
## MessageEvent ## MessageEvent
Holds the data transferred between worker threads. Holds the data transferred between Worker threads.
| Name| Type| Readable| Writable| Description| **System capability**: SystemCapability.Utils.Lang
| -------- | -------- | -------- | -------- | -------- |
| data | T | Yes| No| Data transferred between threads.|
| 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 ### Attributes
| Name| Type| Readable| Writable| Description| **System capability**: SystemCapability.Utils.Lang
| -------- | -------- | -------- | -------- | -------- |
| name | string | Yes| No| Worker name specified when there is a new worker.| | Name| Type | Readable| Writable| Description |
| self | [WorkerGlobalScope](#workerglobalscope) & typeof globalThis | Yes| No| WorkerGlobalScope.| | ---- | ------------------------------------------------------------ | ---- | ---- | --------------------------------------- |
| name | string | Yes | No | **Worker** instance specified when there is a new **Worker** instance.|
| self | [WorkerGlobalScope](#workerglobalscope) & typeof globalThis | Yes | No | WorkerGlobalScope. |
### onerror ### onerror
onerror?: (ev: ErrorEvent) => void onerror?: (ev: ErrorEvent) => 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** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type | Mandatory| Description |
| -------- | -------- | -------- | -------- | | ------ | ------------------------- | ---- | ---------- |
| ev | [ErrorEvent](#errorevent) | No| Error data.| | ev | [ErrorEvent](#errorevent) | No | Error data.|
**Example** **Example**
``` ```
// 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")
``` ```
``` ```
// worker.js // worker.js
import worker from '@ohos.worker'; import worker from '@ohos.worker';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册