From c43997845b34ca2753e36d0af455c3fe2e9f6281 Mon Sep 17 00:00:00 2001 From: Gloria Date: Sat, 15 Jul 2023 10:43:08 +0800 Subject: [PATCH] Update docs against 20033+20435 Signed-off-by: wusongqing --- .../reference/apis/js-apis-display.md | 46 ++-- .../reference/apis/js-apis-screen.md | 112 ++++---- .../reference/apis/js-apis-screenshot.md | 22 +- .../reference/apis/js-apis-worker.md | 251 +++++++++--------- 4 files changed, 211 insertions(+), 220 deletions(-) diff --git a/en/application-dev/reference/apis/js-apis-display.md b/en/application-dev/reference/apis/js-apis-display.md index 15f5d8bdbc..fe1425c33c 100644 --- a/en/application-dev/reference/apis/js-apis-display.md +++ b/en/application-dev/reference/apis/js-apis-display.md @@ -36,10 +36,10 @@ Describes a rectangle on the display. | Name | Type| Readable| Writable| Description | | ------ | -------- | ---- | ---- | ------------------ | -| left | number | Yes | Yes | Left boundary of the rectangle, in pixels.| -| top | number | Yes | Yes | Top boundary of the rectangle, in pixels.| -| width | number | Yes | Yes | Width of the rectangle, in pixels. | -| height | number | Yes | Yes | Height of the rectangle, in pixels. | +| left | number | Yes | Yes | Left boundary of the rectangle, in pixels. The value must be an integer.| +| top | number | Yes | Yes | Top boundary of the rectangle, in pixels. The value must be an integer.| +| width | number | Yes | Yes | Width of the rectangle, in pixels. The value must be an integer. | +| height | number | Yes | Yes | Height of the rectangle, in pixels. The value must be an integer. | ## WaterfallDisplayAreaRects9+ @@ -183,7 +183,7 @@ Checks whether there is a visible privacy window on a display. The privacy windo | Name| Type | Mandatory| Description | | ------ | ------------------------- | ---- |----------| -| id | number | Yes | ID of the display.| +| id | number | Yes | ID of the display. The value must be an integer.| **Return value** @@ -235,10 +235,10 @@ Subscribes to display changes. **Parameters** -| Name| Type| Mandatory| Description| -| -------- | -------- | -------- | -------- | +| Name| Type| Mandatory| Description | +| -------- | -------- | -------- |---------------------------------------------------------------------------------------------------------------------------------| | type | string | Yes| Event type.
- **add**, indicating the display addition event. Example: event that a display is connected.
- **remove**, indicating the display removal event. Example: event that a display is disconnected.
- **change**, indicating the display change event. Example: event that the display orientation is changed.| -| callback | Callback<number> | Yes| Callback used to return the ID of the display.| +| callback | Callback<number> | Yes| Callback used to return the ID of the display, which is an integer. | **Example** @@ -266,7 +266,7 @@ Unsubscribes from display changes. | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | type | string | Yes| Event type.
- **add**, indicating the display addition event. Example: event that a display is connected.
- **remove**, indicating the display removal event. Example: event that a display is disconnected.
- **change**, indicating the display change event. Example: event that the display orientation is changed.| -| callback | Callback<number> | No| Callback used to return the ID of the display.| +| callback | Callback<number> | No| Callback used to return the ID of the display, which is an integer.| **Example** @@ -409,21 +409,21 @@ Before calling any API in **Display**, you must use [getAllDisplays()](#displayg **System capability**: SystemCapability.WindowManager.WindowManager.Core -| Name| Type| Readable| Writable| Description| -| -------- | -------- | -------- | -------- | -------- | -| id | number | Yes| No| ID of the display.| -| name | string | Yes| No| Name of the display.| -| alive | boolean | Yes| No| Whether the display is alive.| -| state | [DisplayState](#displaystate) | Yes| No| State of the display.| -| refreshRate | number | Yes| No| Refresh rate of the display.| +| Name| Type| Readable| Writable| Description | +| -------- | -------- | -------- | -------- |---------------------------------------------------------------------------------------------------------------| +| id | number | Yes| No| ID of the display. The value must be an integer. | +| name | string | Yes| No| Name of the display. | +| alive | boolean | Yes| No| Whether the display is alive. | +| state | [DisplayState](#displaystate) | Yes| No| State of the display. | +| refreshRate | number | Yes| No| Refresh rate of the display. The value must be an integer. | | rotation | number | Yes| No| Screen rotation angle of the display.
The value **0** indicates that the screen of the display rotates by 0°.
The value **1** indicates that the screen of the display rotates by 90°.
The value **2** indicates that the screen of the display rotates by 180°.
The value **3** indicates that the screen of the display rotates by 270°.| -| width | number | Yes| No| Width of the display, in pixels.| -| height | number | Yes| No| Height of the display, in pixels.| -| densityDPI | number | Yes| No| Screen density of the display, that is, the number of dots per inch. Generally, the value is **160** or **480**.| -| densityPixels | number | Yes| No| Logical density of the display, which is a scaling coefficient independent of the pixel unit. Generally, the value is **1** or **3**.| -| scaledDensity | number | Yes| No| Scaling factor for fonts displayed on the display. Generally, the value is the same as that of **densityPixels**.| -| xDPI | number | Yes| No| Exact physical dots per inch of the screen in the horizontal direction.| -| yDPI | number | Yes| No| Exact physical dots per inch of the screen in the vertical direction.| +| width | number | Yes| No| Width of the display, in pixels. The value must be an integer. | +| height | number | Yes| No| Height of the display, in pixels. The value must be an integer. | +| densityDPI | number | Yes| No| Screen density of the display, that is, the number of dots per inch. The value must be a floating point number. Generally, the value is **160.0** or **480.0**. | +| densityPixels | number | Yes| No| Logical density of the display, which is a scaling coefficient independent of the pixel unit. The value must be a floating point number. Generally, the value is **1.0** or **3.0**. | +| scaledDensity | number | Yes| No| Scaling factor for fonts displayed on the display. The value must be a floating point number. Generally, the value is the same as that of **densityPixels**. | +| xDPI | number | Yes| No| Exact physical dots per inch of the screen in the horizontal direction. The value must be a floating point number. | +| yDPI | number | Yes| No| Exact physical dots per inch of the screen in the vertical direction. The value must be a floating point number. | ### getCutoutInfo9+ getCutoutInfo(callback: AsyncCallback<CutoutInfo>): void diff --git a/en/application-dev/reference/apis/js-apis-screen.md b/en/application-dev/reference/apis/js-apis-screen.md index 8b9085aebe..6141468576 100644 --- a/en/application-dev/reference/apis/js-apis-screen.md +++ b/en/application-dev/reference/apis/js-apis-screen.md @@ -95,10 +95,10 @@ Subscribes to events related to the screen state. **Parameters** -| Name | Type | Mandatory| Description | -| --------- | ---------------------- | ---- | ------------------------------------------------------------ | +| Name | Type | Mandatory| Description | +| --------- | ---------------------- | ---- | ----------------------------------------------------------- | | eventType | string | Yes | Event type.
- **connect**: an event indicating that the screen is connected.
- **disconnect**: an event indicating that the screen is disconnected.
- **change**: an event indicating that the screen state changes.| -| callback | Callback<number> | Yes | Callback used to return the screen ID. | +| callback | Callback<number> | Yes | Callback used to return the screen ID, which is an integer. | **Example** @@ -126,7 +126,7 @@ Unsubscribes from events related to the screen state. | Name | Type | Mandatory| Description | | --------- | ---------------------- | ---- | ------------------------------------------------------------ | | eventType | string | Yes | Event type.
- **connect**: an event indicating that the screen is connected.
- **disconnect**: an event indicating that the screen is disconnected.
- **change**: an event indicating that the screen state changes.| -| callback | Callback<number> | No | Callback used to return the screen ID. | +| callback | Callback<number> | No | Callback used to return the screen ID, which is an integer. | **Example** @@ -151,10 +151,10 @@ Sets the screen to the expanded mode. This API uses an asynchronous callback to **Parameters** -| Name | Type | Mandatory| Description | -| -------- | ------------------------------------------ | ---- | -------------------------------- | -| options | Array<[ExpandOption](#expandoption)> | Yes | Parameters for expanding the screen. | -| callback | AsyncCallback<number> | Yes | Callback used to return the group ID of the expanded screens.| +| Name | Type | Mandatory| Description | +| -------- | ------------------------------------------ | ---- |----------------------------| +| options | Array<[ExpandOption](#expandoption)> | Yes | Parameters for expanding the screen. | +| callback | AsyncCallback<number> | Yes | Callback used to return the group ID of the expanded screens, which is an integer.| **Error codes** @@ -198,9 +198,9 @@ Sets the screen to the expanded mode. This API uses a promise to return the resu **Return value** -| Type | Description | -| --------------------- | ----------------------------------- | -| Promise<number> | Promise used to return the group ID of the expanded screens.| +| Type | Description | +| --------------------- |---------------------------------| +| Promise<number> | Promise used to return the group ID of the expanded screens, which is an integer.| **Error codes** @@ -234,11 +234,11 @@ Sets screen mirroring. This API uses an asynchronous callback to return the resu **Parameters** -| Name | Type | Mandatory| Description | -| ------------ | --------------------------- | ---- |-----------------| -| mainScreen | number | Yes | ID of the primary screen. | -| mirrorScreen | Array<number> | Yes | IDs of secondary screens. | -| callback | AsyncCallback<number> | Yes | Callback used to return the group ID of the secondary screens.| +| Name | Type | Mandatory| Description | +| ------------ | --------------------------- | ---- |--------------------| +| mainScreen | number | Yes | ID of the primary screen. The value must be an integer. | +| mirrorScreen | Array<number> | Yes | IDs of secondary screens. Each ID must be an integer.| +| callback | AsyncCallback<number> | Yes | Callback used to return the group ID of the secondary screens, which is an integer. | **Error codes** @@ -276,16 +276,16 @@ Sets screen mirroring. This API uses a promise to return the result. **Parameters** -| Name | Type | Mandatory| Description | -| ------------ | ------------------- | ---- |-----------| -| mainScreen | number | Yes | ID of the primary screen. | -| mirrorScreen | Array<number> | Yes | IDs of secondary screens.| +| Name | Type | Mandatory| Description | +| ------------ | ------------------- | ---- |--------------------| +| mainScreen | number | Yes | ID of the primary screen. The value must be an integer. | +| mirrorScreen | Array<number> | Yes | IDs of secondary screens. Each ID must be an integer.| **Return value** -| Type | Description | -| --------------------- | ----------------------------------- | -| Promise<number> | Promise used to return the group ID of the secondary screens.| +| Type | Description | +| --------------------- |---------------------------------| +| Promise<number> | Promise used to return the group ID of the secondary screens, which is an integer.| **Error codes** @@ -424,7 +424,7 @@ Destroys a virtual screen. This API uses an asynchronous callback to return the | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------------------------------------ | -| screenId | number | Yes | Screen ID. | +| screenId | number | Yes | Screen ID. The value must be an integer. | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the virtual screen is destroyed, **err** is **undefined**; otherwise, **err** is an error object.| **Error codes** @@ -464,7 +464,7 @@ Destroys a virtual screen. This API uses a promise to return the result. | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ---------- | -| screenId | number | Yes | Screen ID.| +| screenId | number | Yes | Screen ID. The value must be an integer.| **Return value** @@ -509,7 +509,7 @@ Sets the surface for a virtual screen. This API uses an asynchronous callback to | Name | Type | Mandatory| Description | | --------- | ------------------------- | ---- | ------------------------------------------------------------ | -| screenId | number | Yes | Screen ID. | +| screenId | number | Yes | Screen ID. The value must be an integer. | | surfaceId | string | Yes | Surface ID. | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the virtual screen surface is successfully set, **err** is **undefined**; otherwise, **err** is an error object.| @@ -553,7 +553,7 @@ Sets the surface for a virtual screen. This API uses a promise to return the res | Name | Type | Mandatory| Description | | --------- | ------ | ---- | ------------- | -| screenId | number | Yes | Screen ID. | +| screenId | number | Yes | Screen ID. The value must be an integer. | | surfaceId | string | Yes | Surface ID.| **Return value** @@ -711,9 +711,9 @@ Defines the parameters for expanding a screen. | Name | Type| Readable| Writable| Description | | -------- | -------- | ---- | ---- | ------------------- | -| screenId | number | Yes | Yes | Screen ID. | -| startX | number | Yes | Yes | Start X coordinate of the screen.| -| startY | number | Yes | Yes | Start Y coordinate of the screen.| +| screenId | number | Yes | Yes | Screen ID. The value must be an integer. | +| startX | number | Yes | Yes | Start X coordinate of the screen. The value must be an integer.| +| startY | number | Yes | Yes | Start Y coordinate of the screen. The value must be an integer.| ## VirtualScreenOption @@ -721,13 +721,13 @@ Defines virtual screen parameters. **System capability**: SystemCapability.WindowManager.WindowManager.Core -| Name | Type| Readable| Writable| Description | -| --------- | -------- | ---- | ---- | ------------------------- | -| name | string | Yes | Yes | Name of a virtual screen. | -| width | number | Yes | Yes | Width of the virtual screen, in pixels.| -| height | number | Yes | Yes | Height of the virtual screen, in pixels.| -| density | number | Yes | Yes | Density of the virtual screen. | -| surfaceId | string | Yes | Yes | Surface ID of the virtual screen.| +| Name | Type| Readable| Writable| Description | +| --------- | -------- | ---- | ---- |--------------------------| +| name | string | Yes | Yes | Name of a virtual screen. | +| width | number | Yes | Yes | Width of the virtual screen, in pixels. The value must be an integer.| +| height | number | Yes | Yes | Height of the virtual screen, in pixels. The value must be an integer.| +| density | number | Yes | Yes | Density of the virtual screen. The value must be a floating point number. | +| surfaceId | string | Yes | Yes | Surface ID of the virtual screen. | ## Screen @@ -737,13 +737,13 @@ Before calling any API in **Screen**, you must use **[getAllScreens()](#screenge **System capability**: SystemCapability.WindowManager.WindowManager.Core -| Name | Type | Readable| Writable| Description | -| ----------------- | ---------------------------------------------- | ---- | ---- | ---------------------- | -| id | number | Yes | No | Screen ID. | -| parent | number | Yes | No | ID of the group to which a screen belongs. | -| supportedModeInfo | Array<[ScreenModeInfo](#screenmodeinfo)> | Yes | No | Mode set supported by the screen. | -| activeModeIndex | number | Yes | No | Index of the active screen mode. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware.| -| orientation | [Orientation](#orientation) | Yes | No | Screen orientation. | +| Name | Type | Readable| Writable| Description | +| ----------------- | ---------------------------------------------- | ---- | ---- |-------------------------------------------------------------| +| id | number | Yes | No | Screen ID. The value must be an integer. | +| parent | number | Yes | No | ID of the group to which a screen belongs. The value must be an integer. | +| supportedModeInfo | Array<[ScreenModeInfo](#screenmodeinfo)> | Yes | No | Mode set supported by the screen. | +| activeModeIndex | number | Yes | No | Index of the active screen mode. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware. The value must be an integer.| +| orientation | [Orientation](#orientation) | Yes | No | Screen orientation. | ### setOrientation @@ -833,7 +833,7 @@ Sets the active mode of the screen. This API uses an asynchronous callback to re | Name | Type | Mandatory| Description | | --------- | ------------------------- | ---- | ------------------------------------------------------------ | -| modeIndex | number | Yes | Index of the mode to set. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware.| +| modeIndex | number | Yes | Index of the mode to set. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware. The value must be an integer.| | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the active mode is successfully set, **err** is **undefined**; otherwise, **err** is an error object.| **Error codes** @@ -871,7 +871,7 @@ Sets the active mode of the screen. This API uses a promise to return the result | Name | Type | Mandatory| Description | | --------- | ------ | ---- | ---------- | -| modeIndex | number | Yes | Index of the mode to set. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware.| +| modeIndex | number | Yes | Index of the mode to set. The current value and value range of this parameter vary according to the screen resolution, refresh rate, and device hardware. The value must be an integer.| **Return value** @@ -911,9 +911,9 @@ Sets the pixel density of the screen. This API uses an asynchronous callback to **System capability**: SystemCapability.WindowManager.WindowManager.Core -| Name | Type | Mandatory| Description | -| ---------- | ------------------------- | ---- | ------------------------------------------------------------ | -| densityDpi | number | Yes | Pixel density. The value ranges from 80 to 640. | +| Name | Type | Mandatory| Description | +| ---------- | ------------------------- | ---- |------------------------------------------| +| densityDpi | number | Yes | Pixel density. The value must be an integer in the range [80, 640]. | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the pixel density is successfully set, **err** is **undefined**; otherwise, **err** is an error object.| **Error codes** @@ -949,9 +949,9 @@ Sets the pixel density of the screen. This API uses a promise to return the resu **System capability**: SystemCapability.WindowManager.WindowManager.Core -| Name | Type | Mandatory| Description | -| ---------- | ------ | ---- | ---------------------------------- | -| densityDpi | number | Yes | Pixel density. The value ranges from 80 to 640.| +| Name | Type | Mandatory| Description | +| ---------- | ------ | ---- |------------------------------------| +| densityDpi | number | Yes | Pixel density. The value must be an integer in the range [80, 640].| **Return value** @@ -1005,7 +1005,7 @@ Defines the screen mode information. | Name | Type| Readable| Writable| Description | | ----------- | -------- | ---- | ---- | -------------------------------------------------- | -| id | number | Yes | Yes | Mode ID. The supported mode is determined by the device resolution and refresh rate.| -| width | number | Yes | Yes | Width of the screen, in pixels. | -| height | number | Yes | Yes | Height of the screen, in pixels. | -| refreshRate | number | Yes | Yes | Screen refresh rate. | +| id | number | Yes | Yes | Mode ID. The supported mode is determined by the device resolution and refresh rate. The value must be an integer.| +| width | number | Yes | Yes | Width of the screen, in pixels. The value must be an integer. | +| height | number | Yes | Yes | Height of the screen, in pixels. The value must be an integer. | +| refreshRate | number | Yes | Yes | Refresh rate of the screen. The value must be an integer. | diff --git a/en/application-dev/reference/apis/js-apis-screenshot.md b/en/application-dev/reference/apis/js-apis-screenshot.md index 9f71307457..7cb437acb0 100644 --- a/en/application-dev/reference/apis/js-apis-screenshot.md +++ b/en/application-dev/reference/apis/js-apis-screenshot.md @@ -25,8 +25,8 @@ Describes screenshot options. | ---------------------- | ------------- | ---- | ------------------------------------------------------------ | | screenRect | [Rect](#rect) | No | Region of the screen to capture. If this parameter is null, the full screen will be captured. | | imageSize | [Size](#size) | No | Size of the screen region to capture. If this parameter is null, the full screen will be captured. | -| rotation | number | No | Rotation angle of the screenshot. Currently, the value can be **0** only. The default value is **0**. | -| displayId8+ | number | No | ID of the [display](js-apis-display.md#display) device on which the screen region is to be captured.| +| rotation | number | No | Rotation angle of the screenshot. Currently, the value can be **0** only. The default value is **0**. The value must be an integer. | +| displayId8+ | number | No | ID of the [display](js-apis-display.md#display) device on which the screen region is to be captured. The value must be an integer.| ## Rect @@ -37,10 +37,10 @@ Describes the region of the screen to capture. | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | -| left | number | Yes | Left boundary of the screen region to capture, in pixels.| -| top | number | Yes | Top boundary of the screen region to capture, in pixels.| -| width | number | Yes | Width of the screen region to capture, in pixels.| -| height | number | Yes | Height of the screen region to capture, in pixels.| +| left | number | Yes | Left boundary of the screen region to capture, in pixels. The value must be an integer.| +| top | number | Yes | Top boundary of the screen region to capture, in pixels. The value must be an integer.| +| width | number | Yes | Width of the screen region to capture, in pixels. The value must be an integer.| +| height | number | Yes | Height of the screen region to capture, in pixels. The value must be an integer.| ## Size @@ -51,8 +51,8 @@ Describes the size of the screen region to capture. | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | -| width | number | Yes | Width of the screen region to capture, in pixels.| -| height | number | Yes | Height of the screen region to capture, in pixels.| +| width | number | Yes | Width of the screen region to capture, in pixels. The value must be an integer.| +| height | number | Yes | Height of the screen region to capture, in pixels. The value must be an integer.| ## screenshot.save @@ -92,7 +92,7 @@ Takes a screenshot and saves it as a **PixelMap** object. This API uses an async console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); return; } - console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); + console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); pixelMap.release(); // Release the memory in time after the PixelMap is used. }); } catch (exception) { @@ -125,7 +125,7 @@ Takes a screenshot and saves it as a **PixelMap** object. This API uses an async console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); return; } - console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); + console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); pixelMap.release(); // Release the memory in time after the PixelMap is used. }); } catch (exception) { @@ -173,7 +173,7 @@ Takes a screenshot and saves it as a **PixelMap** object. This API uses a promis try { let promise = screenshot.save(screenshotOptions); promise.then((pixelMap) => { - console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); + console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); pixelMap.release(); // Release the memory in time after the PixelMap is used. }).catch((err) => { console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); diff --git a/en/application-dev/reference/apis/js-apis-worker.md b/en/application-dev/reference/apis/js-apis-worker.md index c8910b3df6..6d1bff42d4 100644 --- a/en/application-dev/reference/apis/js-apis-worker.md +++ b/en/application-dev/reference/apis/js-apis-worker.md @@ -83,9 +83,9 @@ import worker from '@ohos.worker'; // Create a Worker instance. // In the FA model, the workers directory is at the same level as the pages directory in the entry module. -const workerFAModel01 = new worker.ThreadWorker("workers/worker.js", {name:"first worker in FA model"}); +const workerFAModel01 = new worker.ThreadWorker("workers/worker.ts", {name:"first worker in FA model"}); // In the FA model, the workers directory is at the same level as the parent directory of the pages directory in the entry module. -const workerFAModel02 = new worker.ThreadWorker("../workers/worker.js"); +const workerFAModel02 = new worker.ThreadWorker("../workers/worker.ts"); // In the stage model, the workers directory is at the same level as the pages directory in the entry module. const workerStageModel01 = new worker.ThreadWorker('entry/ets/workers/worker.ts', {name:"first worker in Stage model"}); @@ -115,7 +115,7 @@ In the FA model: "buildOption": { "sourceOption": { "workers": [ - "./src/main/ets/MainAbility/workers/worker.ts" + "./src/main/ets/entryability/workers/worker.ts" ] } } @@ -161,9 +161,9 @@ In the stage model: ### postMessage9+ -postMessage(message: Object, transfer: ArrayBuffer[]): void; +postMessage(message: Object, transfer: ArrayBuffer[]): void -Sends a message to the worker thread. The data type of the message must be sequenceable. For details about the sequenceable data types, see [More Information](#more-information). +Used by the host thread to send a message to the worker thread by transferring object ownership. **System capability**: SystemCapability.Utils.Lang @@ -171,8 +171,8 @@ Sends a message to the worker thread. The data type of the message must be seque | Name | Type | Mandatory| Description | | -------- | ------------- | ---- | ------------------------------------------------------------ | -| message | Object | Yes | Message to be sent to the worker thread. | -| transfer | ArrayBuffer[] | Yes | An **ArrayBuffer** object can be transferred. The value **null** should not be passed in the array.| +| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| +| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the worker thread. After the transfer, the objects are available only in the worker thread. The array cannot be null.| **Error codes** @@ -181,7 +181,7 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco | ID| Error Message | | -------- | ----------------------------------------- | | 10200004 | Worker instance is not running. | -| 10200006 | Serializing an uncaught exception failed. | +| 10200006 | An exception occurred during serialization. | **Example** @@ -196,7 +196,7 @@ workerInstance.postMessage(buffer, [buffer]); postMessage(message: Object, options?: PostMessageOptions): void -Sends a message to the worker thread. The data type of the message must be sequenceable. For details about the sequenceable data types, see [More Information](#more-information). +Used by the host thread to send a message to the worker thread by transferring object ownership or copying data. **System capability**: SystemCapability.Utils.Lang @@ -204,8 +204,8 @@ Sends a message to the worker thread. The data type of the message must be seque | Name | Type | Mandatory| Description | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | -| message | Object | Yes | Message to be sent to the worker thread. | -| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The default value is **undefined**.| +| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| +| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the worker thread and becomes unavailable in the host thread. The objects are available only in the worker thread.
If this parameter is not specified, the default value **undefined** is used, and information is transferred to the worker thread by copying data.| **Error codes** @@ -214,7 +214,7 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco | ID| Error Message | | -------- | ----------------------------------------- | | 10200004 | Worker instance is not running. | -| 10200006 | Serializing an uncaught exception failed. | +| 10200006 | An exception occurred during serialization. | **Example** @@ -617,7 +617,7 @@ workerInstance.dispatchEvent({type:"alert_once", timeStamp:0});// timeStamp is n // The event listener created by on will not be proactively deleted. workerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); workerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); -// The event listener created by addEventListener will be always valid and will not be proactively deleted. +// The event listener created by addEventListener will not be proactively deleted. workerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); workerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); @@ -792,7 +792,7 @@ workerInstance.dispatchEvent({type:"alert_once", timeStamp:0});// timeStamp is n // The event listener created by on will not be proactively deleted. workerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); workerInstance.dispatchEvent({type:"alert_on", timeStamp:0}); -// The event listener created by addEventListener will be always valid and will not be proactively deleted. +// The event listener created by addEventListener will not be proactively deleted. workerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); workerInstance.dispatchEvent({type:"alert_add", timeStamp:0}); @@ -849,16 +849,16 @@ Implements communication between the worker thread and the host thread. The **po postMessage(messageObject: Object, transfer: ArrayBuffer[]): void; -Sends a message to the host thread from the worker thread. +Used by the worker thread to send a message to the host thread by transferring object ownership. **System capability**: SystemCapability.Utils.Lang **Parameters** -| Name | Type | Mandatory| Description | -| -------- | ------------- | ---- | ------------------------------------------------------- | -| message | Object | Yes | Message to be sent to the host thread. | -| transfer | ArrayBuffer[] | Yes | An **ArrayBuffer** object can be transferred. The value **null** should not be passed in the array.| +| Name | Type | Mandatory| Description | +| -------- | ------------- | ---- | ------------------------------------------------------------ | +| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| +| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the host thread. After the transfer, the objects are available only in the host thread. The array cannot be null.| **Error codes** @@ -867,18 +867,18 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco | ID| Error Message | | -------- | ----------------------------------------- | | 10200004 | Worker instance is not running. | -| 10200006 | Serializing an uncaught exception failed. | +| 10200006 | An exception occurred during serialization. | **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ts"); workerInstance.postMessage("hello world"); workerInstance.onmessage = function(e) { // let data = e.data; - console.log("receive data from worker.js"); + console.log("receive data from worker.ts"); } ``` @@ -897,7 +897,7 @@ workerPort.onmessage = function(e){ postMessage(messageObject: Object, options?: PostMessageOptions): void -Sends a message to the host thread from the worker thread. +Used by the worker thread to send a message to the host thread by transferring object ownership or copying data. **System capability**: SystemCapability.Utils.Lang @@ -905,8 +905,8 @@ Sends a message to the host thread from the worker thread. | Name | Type | Mandatory| Description | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | -| message | Object | Yes | Message to be sent to the host thread. | -| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The default value is **undefined**.| +| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| +| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the host thread and becomes unavailable in the worker thread. The objects are available only in the host thread.
If this parameter is not specified, the default value **undefined** is used, and information is transferred to the host thread by copying data.| **Error codes** @@ -915,18 +915,18 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco | ID| Error Message | | -------- | ----------------------------------------- | | 10200004 | Worker instance is not running. | -| 10200006 | Serializing an uncaught exception failed. | +| 10200006 | An exception occurred during serialization. | **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ts"); workerInstance.postMessage("hello world"); workerInstance.onmessage = function(e) { // let data = e.data; - console.log("receive data from worker.js"); + console.log("receive data from worker.ts"); } ``` @@ -936,7 +936,7 @@ import worker from '@ohos.worker'; const workerPort = worker.workerPort; workerPort.onmessage = function(e){ // let data = e.data; - workerPort.postMessage("receive data from main.js"); + workerPort.postMessage("receive data from main thread"); } ``` @@ -960,7 +960,7 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ts"); ``` @@ -1002,7 +1002,7 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ts"); workerInstance.postMessage("hello world"); @@ -1013,7 +1013,7 @@ workerInstance.postMessage("hello world"); import worker from '@ohos.worker'; const workerPort = worker.workerPort; workerPort.onmessage = function(e) { - console.log("receive main.js message"); + console.log("receive main thread message"); } ``` @@ -1045,7 +1045,7 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ts"); ``` @@ -1055,7 +1055,7 @@ const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ts"); import worker from '@ohos.worker'; const parentPort = worker.workerPort; parentPort.onmessageerror = function(e) { - console.log("worker.js onmessageerror") + console.log("worker.ts onmessageerror") } ``` @@ -1130,7 +1130,7 @@ Defines the event handler to be called when an exception occurs during worker ex **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ts") ``` @@ -1140,7 +1140,7 @@ const workerInstance = new worker.ThreadWorker("entry/ets/workers/worker.ts") import worker from '@ohos.worker'; const workerPort = worker.workerPort workerPort.onerror = function(e){ - console.log("worker.js onerror") + console.log("worker.ts onerror") } ``` @@ -1193,9 +1193,9 @@ import worker from '@ohos.worker'; // Create a Worker instance. // In the FA model, the workers directory is at the same level as the pages directory. -const workerFAModel01 = new worker.Worker("workers/worker.js", {name:"first worker in FA model"}); +const workerFAModel01 = new worker.Worker("workers/worker.ts", {name:"first worker in FA model"}); // In the FA model, the workers directory is at the same level as the parent directory of the pages directory. -const workerFAModel02 = new worker.Worker("../workers/worker.js"); +const workerFAModel02 = new worker.Worker("../workers/worker.ts"); // In the stage model, the workers directory is at the same level as the pages directory. const workerStageModel01 = new worker.Worker('entry/ets/workers/worker.ts', {name:"first worker in Stage model"}); @@ -1216,7 +1216,7 @@ In the FA model: "buildOption": { "sourceOption": { "workers": [ - "./src/main/ets/MainAbility/workers/worker.ts" + "./src/main/ets/entryability/workers/worker.ts" ] } } @@ -1256,9 +1256,9 @@ In the stage model: ### postMessage(deprecated) -postMessage(message: Object, transfer: ArrayBuffer[]): void; +postMessage(message: Object, transfer: ArrayBuffer[]): void -Sends a message to the worker thread. The data type of the message must be sequenceable. For details about the sequenceable data types, see [More Information](#more-information). +Used by the host thread to send a message to the worker thread by transferring object ownership. > **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.postMessage9+](#postmessage9) instead. @@ -1267,15 +1267,15 @@ Sends a message to the worker thread. The data type of the message must be seque **Parameters** -| Name | Type | Mandatory| Description | -| -------- | ------------- | ---- | ----------------------------------------------- | -| message | Object | Yes | Message to be sent to the worker thread. | -| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instances that can be transferred.| +| Name | Type | Mandatory| Description | +| -------- | ------------- | ---- | ------------------------------------------------------------ | +| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| +| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the worker thread. After the transfer, the objects are available only in the worker thread. The array cannot be null.| **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); var buffer = new ArrayBuffer(8); workerInstance.postMessage(buffer, [buffer]); @@ -1285,7 +1285,7 @@ workerInstance.postMessage(buffer, [buffer]); postMessage(message: Object, options?: PostMessageOptions): void -Sends a message to the worker thread. The data type of the message must be sequenceable. For details about the sequenceable data types, see [More Information](#more-information). +Used by the host thread to send a message to the worker thread by transferring object ownership or copying data. > **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorker.postMessage9+](#postmessage9-1) instead. @@ -1296,13 +1296,13 @@ Sends a message to the worker thread. The data type of the message must be seque | Name | Type | Mandatory| Description | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | -| message | Object | Yes | Message to be sent to the worker thread. | -| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The default value is **undefined**.| +| message | Object | Yes | Data to be sent to the worker thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| +| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the worker thread and becomes unavailable in the host thread. The objects are available only in the worker thread.
If this parameter is not specified, the default value **undefined** is used, and information is transferred to the worker thread by copying data.| **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.postMessage("hello world"); @@ -1332,7 +1332,7 @@ Adds an event listener for the worker thread. This API provides the same functio **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.on("alert", (e)=>{ console.log("alert listener callback"); }) @@ -1360,7 +1360,7 @@ Adds an event listener for the worker thread and removes the event listener afte **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.once("alert", (e)=>{ console.log("alert listener callback"); }) @@ -1388,7 +1388,7 @@ Removes an event listener for the worker thread. This API provides the same func **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); // Use on, once, or addEventListener to add a listener for the "alert" event, and use off to remove the listener. workerInstance.off("alert"); ``` @@ -1408,7 +1408,7 @@ Terminates the worker thread to stop it from receiving messages. **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.terminate(); ``` @@ -1433,7 +1433,7 @@ Defines the event handler to be called when the worker thread exits. The handler **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.onexit = function(e) { console.log("onexit"); } @@ -1467,7 +1467,7 @@ Defines the event handler to be called when an exception occurs during worker ex **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.onerror = function(e) { console.log("onerror"); } @@ -1489,12 +1489,12 @@ Defines the event handler to be called when the host thread receives a message s | Name| Type | Mandatory| Description | | ------ | ------------------------------ | ---- | ---------------------- | -| event | [MessageEvent](#messageeventt) | Yes | Message received.| +| event | [MessageEvent](#messageeventt)| Yes | Message received.| **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.onmessage = function(e) { // e: MessageEvent. The usage is as follows: // let data = e.data; @@ -1518,12 +1518,12 @@ Defines the event handler to be called when the worker thread receives a message | Name| Type | Mandatory| Description | | ------ | ------------------------------ | ---- | ---------- | -| event | [MessageEvent](#messageeventt) | Yes | Error data.| +| event | [MessageEvent](#messageeventt)| Yes | Error data.| **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.onmessageerror= function(e) { console.log("onmessageerror"); } @@ -1555,7 +1555,7 @@ Adds an event listener for the worker thread. This API provides the same functio **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.addEventListener("alert", (e)=>{ console.log("alert listener callback"); }) @@ -1583,7 +1583,7 @@ Removes an event listener for the worker thread. This API provides the same func **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.addEventListener("alert", (e)=>{ console.log("alert listener callback"); }) @@ -1617,7 +1617,7 @@ Dispatches the event defined for the worker thread. **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet. ``` @@ -1625,7 +1625,7 @@ workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is n The **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows: ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); // Usage 1: workerInstance.on("alert_on", (e)=>{ @@ -1677,7 +1677,7 @@ Removes all event listeners for the worker thread. **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.addEventListener("alert", (e)=>{ console.log("alert listener callback"); }) @@ -1696,7 +1696,7 @@ Implements communication between the worker thread and the host thread. The **po postMessage(messageObject: Object, transfer: Transferable[]): void; -Sends a message to the host thread from the worker thread. +Used by the worker thread to send a message to the host thread by transferring object ownership. > **NOTE**
> This API is deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+.postMessage9+](#postmessage9-2). @@ -1707,14 +1707,14 @@ Sends a message to the host thread from the worker thread. | Name | Type | Mandatory| Description | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | -| messageObject | Object | Yes | Message to be sent to the host thread. | +| messageObject | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| | transfer| Transferable[] | Yes | Currently, this parameter is not supported. | ### postMessage9+ postMessage(messageObject: Object, transfer: ArrayBuffer[]): void; -Sends a message to the host thread from the worker thread. +Used by the worker thread to send a message to the host thread by transferring object ownership. > **NOTE** > @@ -1724,25 +1724,25 @@ Sends a message to the host thread from the worker thread. **Parameters** -| Name | Type | Mandatory| Description | -| -------- | ------------- | ---- | ----------------------------------------------------- | -| message | Object | Yes | Message to be sent to the host thread. | -| transfer | ArrayBuffer[] | Yes | An **ArrayBuffer** object can be transferred. The value **null** should not be passed in the array.| +| Name | Type | Mandatory| Description | +| -------- | ------------- | ---- | ------------------------------------------------------------ | +| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| +| transfer | ArrayBuffer[] | Yes | **ArrayBuffer** instance holding an array of objects for which the ownership is transferred to the host thread. After the transfer, the objects are available only in the host thread. The array cannot be null.| **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.postMessage("hello world"); workerInstance.onmessage = function(e) { // let data = e.data; - console.log("receive data from worker.js"); + console.log("receive data from worker.ts"); } ``` ```js -// worker.js +// worker.ts import worker from '@ohos.worker'; const parentPort = worker.parentPort; parentPort.onmessage = function(e){ @@ -1756,7 +1756,7 @@ parentPort.onmessage = function(e){ postMessage(messageObject: Object, options?: PostMessageOptions): void -Sends a message to the host thread from the worker thread. +Used by the worker thread to send a message to the host thread by transferring object ownership or copying data. > **NOTE**
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [ThreadWorkerGlobalScope9+.postMessage9+](#postmessage9-3). @@ -1767,28 +1767,28 @@ Sends a message to the host thread from the worker thread. | Name | Type | Mandatory| Description | | ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | -| message | Object | Yes | Message to be sent to the host thread. | -| options | [PostMessageOptions](#postmessageoptions) | No | **ArrayBuffer** instances that can be transferred. The default value is **undefined**.| +| message | Object | Yes | Data to be sent to the host thread. The data object must be sequenceable. For details about the supported parameter types, see [Sequenceable Data Types](#sequenceable-data-types).| +| options | [PostMessageOptions](#postmessageoptions) | No | If this parameter is specified, it functions the same as **ArrayBuffer[]**. Specifically, the ownership of the objects in the array is transferred to the host thread and becomes unavailable in the worker thread. The objects are available only in the host thread.
If this parameter is not specified, the default value **undefined** is used, and information is transferred to the host thread by copying data.| **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.postMessage("hello world"); workerInstance.onmessage = function(e) { // let data = e.data; - console.log("receive data from worker.js"); + console.log("receive data from worker.ts"); } ``` ```js -// worker.js +// worker.ts import worker from '@ohos.worker'; const parentPort = worker.parentPort; parentPort.onmessage = function(e){ // let data = e.data; - parentPort.postMessage("receive data from main.js"); + parentPort.postMessage("receive data from main thread"); } ``` @@ -1806,12 +1806,12 @@ Terminates the worker thread to stop it from receiving messages. **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); ``` ```js -// worker.js +// worker.ts import worker from '@ohos.worker'; const parentPort = worker.parentPort; parentPort.onmessage = function(e) { @@ -1836,22 +1836,22 @@ Defines the event handler to be called when the worker thread receives a message | Name| Type | Mandatory| Description | | ------ | ------------------------------------------------------------ | ---- | ------------------------ | | this | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscopedeprecated) | Yes | Caller. | -| ev | [MessageEvent](#messageeventt) | Yes | Message received.| +| ev | [MessageEvent](#messageeventt) | Yes | Message received.| **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.postMessage("hello world"); ``` ```js -// worker.js +// worker.ts import worker from '@ohos.worker'; const parentPort = worker.parentPort; parentPort.onmessage = function(e) { - console.log("receive main.js message"); + console.log("receive main thread message"); } ``` @@ -1872,28 +1872,28 @@ Defines the event handler to be called when the worker thread receives a message | Name| Type | Mandatory| Description | | ------ | ------------------------------ | ---- | ---------- | | this | [DedicatedWorkerGlobalScope](#dedicatedworkerglobalscopedeprecated) | Yes | Caller.| -| ev | [MessageEvent](#messageeventt) | Yes | Error data.| +| ev | [MessageEvent](#messageeventt)| Yes | Error data.| **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); ``` ```js -// worker.js +// worker.ts import worker from '@ohos.worker'; const parentPort = worker.parentPort; parentPort.onmessageerror = function(e) { - console.log("worker.js onmessageerror") + console.log("worker.ts onmessageerror") } ``` ## PostMessageOptions -Specifies the object whose ownership needs to be transferred during data transfer. The object must be **ArrayBuffer**. +Defines the object for which the ownership is to be transferred during data transfer. The object must be an **ArrayBuffer** instance. After the ownership is transferred, the object becomes unavailable in the sender and can be used only in the receiver. **System capability**: SystemCapability.Utils.Lang @@ -1940,7 +1940,7 @@ Implements event listening. **Example** ```js -const workerInstance = new worker.Worker("workers/worker.js"); +const workerInstance = new worker.Worker("workers/worker.ts"); workerInstance.addEventListener("alert", (e)=>{ console.log("alert listener callback"); }) @@ -2010,16 +2010,16 @@ Defines the event handler to be called when an exception occurs during worker ex **Example** ```js -// main.js +// Main thread import worker from '@ohos.worker'; -const workerInstance = new worker.Worker("workers/worker.js") +const workerInstance = new worker.Worker("workers/worker.ts") ``` ```js -// worker.js +// worker.ts import worker from '@ohos.worker'; const parentPort = worker.parentPort parentPort.onerror = function(e){ - console.log("worker.js onerror") + console.log("worker.ts onerror") } ``` @@ -2045,17 +2045,17 @@ Exception: When an object created through a custom class is passed, no serializa > An FA project of API version 9 is used as an example. ```js -// main.js +// Main thread import worker from '@ohos.worker'; -const workerInstance = new worker.ThreadWorker("workers/worker.js"); -workerInstance.postMessage("message from main to worker"); +const workerInstance = new worker.ThreadWorker("workers/worker.ts"); +workerInstance.postMessage("message from main thread to worker"); workerInstance.onmessage = function(d) { // When the worker thread passes obj2, data contains obj2, excluding the Init or SetName method. let data = d.data; } ``` ```js -// worker.js +// worker.ts import worker from '@ohos.worker'; const workerPort = worker.workerPort; class MyModel { @@ -2065,7 +2065,7 @@ class MyModel { } } workerPort.onmessage = function(d) { - console.log("worker.js onmessage"); + console.log("worker.ts onmessage"); let data = d.data; let func1 = function() { console.log("post message is function"); @@ -2083,27 +2083,28 @@ workerPort.onmessage = function(d) { workerPort.postMessage(obj2); // No serialization error occurs when passing obj2. } workerPort.onmessageerror = function(e) { - console.log("worker.js onmessageerror"); + console.log("worker.ts onmessageerror"); } workerPort.onerror = function(e) { - console.log("worker.js onerror"); + console.log("worker.ts onerror"); } ``` ### Memory Model -The worker thread is implemented based on the actor model. In the worker interaction process, the JS main thread can create multiple worker threads, each of which are isolated and transfer data through sequentialization. They complete computing tasks and return the result to the main thread. +The worker thread is implemented based on the actor model. In the worker interaction process, the JS main thread can create multiple worker threads, each of which are isolated and transfer data through serialization. They complete computing tasks and return the result to the main thread. Each actor concurrently processes tasks of the main thread. For each actor, there is a message queue and a single-thread execution module. The message queue receives requests from the main thread and other actors; the single-thread execution module serially processes requests, sends requests to other actors, and creates new actors. These isolated actors use the asynchronous mode and can run concurrently. ### Precautions -- Currently, a maximum of seven workers can co-exist. +- Currently, a maximum of eight workers can co-exist. - In API version 8 and earlier versions, when the number of **Worker** instances exceeds the upper limit, the error "Too many workers, the number of workers exceeds the maximum." is thrown. -- Since API version 9, when the number of **Worker** instances exceeds the upper limit, the business error "Worker initialization failure, the number of workers exceeds the maximum" is thrown. +- Since API version 9, when the number of **Worker** instances exceeds the upper limit, the business error "Worker initialization failure, the number of workers exceeds the maximum." is thrown. - To proactively destroy a worker thread, you can call **terminate()** or **parentPort.close()** of the newly created **Worker** instance. - Since API version 9, if a **Worker** instance in a non-running state (such as destroyed or being destroyed) calls an API, a business error is thrown. - Creating and terminating worker threads consume performance. Therefore, you are advised to manage available workers and reuse them. - Do not use both **new worker.Worker** and **new worker.ThreadWorker** to create a **Worker** project. Otherwise, **Worker** functions abnormally. Since API version 9, you are advised to use [new worker.ThreadWorker](#constructor9). In API version 8 and earlier versions, you are advised to use [new worker.Worker](#constructordeprecated). - When creating a **Worker** project, do not import any UI construction method (such as .ets file) to the worker thread file (for example, **worker.ts** used in this document). Otherwise, the worker module becomes invalid. To check whether any UI construction method has been imported, decompress the generated HAP file, find **worker.js** in the directory where the worker thread is created, and search for the keyword **View** globally. If the keyword exists, a UI construction method has been packaged in **worker.js**. If this is your case, change the directory level of **src** in the statement **import "xxx" from src** in the worker thread file. +- A maximum of 16 MB data can be transferred during inter-thread communication. ## Sample Code > **NOTE**
@@ -2111,15 +2112,10 @@ Each actor concurrently processes tasks of the main thread. For each actor, ther ### FA Model ```js -// main.js (The following assumes that the workers directory and pages directory are at the same level.) +// Main thread (The following assumes that the workers directory and pages directory are at the same level.) import worker from '@ohos.worker'; // Create a Worker instance in the main thread. const workerInstance = new worker.ThreadWorker("workers/worker.ts"); -// Create either a .json or .ts file. -// const workerInstance = new worker.ThreadWorker("workers/worker.js"); - -// In versions earlier than API version 9, use the following to create a Worker instance in the main thread. -// const workerInstance = new worker.Worker("workers/worker.js"); // The main thread transfers information to the worker thread. workerInstance.postMessage("123"); @@ -2128,7 +2124,7 @@ workerInstance.postMessage("123"); workerInstance.onmessage = function(e) { // data carries the information sent by the worker thread. let data = e.data; - console.log("main.js onmessage"); + console.log("main thread onmessage"); // Terminate the Worker instance. workerInstance.terminate(); @@ -2136,7 +2132,7 @@ workerInstance.onmessage = function(e) { // Call onexit(). workerInstance.onexit = function() { - console.log("main.js terminate"); + console.log("main thread terminate"); } ``` ```js @@ -2146,9 +2142,6 @@ import worker from '@ohos.worker'; // Create an object in the worker thread for communicating with the main thread. const workerPort = worker.workerPort -// In versions earlier than API version 9, use the following to create an object in the worker thread for communicating with the main thread. -// const parentPort = worker.parentPort - // The worker thread receives information from the main thread. workerPort.onmessage = function(e) { // data carries the information sent by the main thread. @@ -2169,20 +2162,18 @@ Configuration of the **build-profile.json5** file: "buildOption": { "sourceOption": { "workers": [ - "./src/main/ets/MainAbility/workers/worker.ts" + "./src/main/ets/entryability/workers/worker.ts" ] } } ``` ### Stage Model ```js -// main.js (The following assumes that the workers directory and pages directory are at different levels.) +// Main thread (The following assumes that the workers directory and pages directory are at different levels.) import worker from '@ohos.worker'; // Create a Worker instance in the main thread. const workerInstance = new worker.ThreadWorker("entry/ets/pages/workers/worker.ts"); -// Create either a .json or .ts file. -// const workerInstance = new worker.ThreadWorker("entry/ets/pages/workers/worker.js"); // The main thread transfers information to the worker thread. workerInstance.postMessage("123"); @@ -2191,14 +2182,14 @@ workerInstance.postMessage("123"); workerInstance.onmessage = function(e) { // data carries the information sent by the worker thread. let data = e.data; - console.log("main.js onmessage"); + console.log("main thread onmessage"); // Terminate the Worker instance. workerInstance.terminate(); } // Call onexit(). workerInstance.onexit = function() { - console.log("main.js terminate"); + console.log("main thread terminate"); } ``` ```js -- GitLab