Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
27333efb
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
27333efb
编写于
2月 28, 2023
作者:
O
openharmony_ci
提交者:
Gitee
2月 28, 2023
浏览文件
操作
浏览文件
下载
差异文件
!15286 support new worker scriptURL format cherrypick monthly
Merge pull request !15286 from wangzhaoyong/cherry-pick-1677546668
上级
43313ac1
c3abf9d6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
41 addition
and
33 deletion
+41
-33
zh-cn/application-dev/reference/apis/js-apis-worker.md
zh-cn/application-dev/reference/apis/js-apis-worker.md
+41
-33
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-worker.md
浏览文件 @
27333efb
...
...
@@ -94,6 +94,14 @@ const workerStageModel02 = new worker.ThreadWorker('entry/ets/pages/workers/work
// 理解Stage模型scriptURL的"entry/ets/workers/worker.ts":
// entry: 为module.json5文件中module的name属性对应的值,ets: 表明当前使用的语言。
// scriptURL与worker文件所在的workers目录层级有关,与new worker所在文件无关。
// Stage模型工程esmodule编译场景下,支持新增的scriptURL规格:@bundle:bundlename/entryname/ets/workerdir/workerfile
// @bundle:为固定标签,bundlename为当前应用包名,entryname为当前模块名,ets为当前使用语言
// workerdir为worker文件所在目录,workerfile为worker文件名
// Stage模型-目录同级(entry模块下,workers目录与pages目录同级),假设bundlename是com.example.workerdemo
const
workerStageModel03
=
new
worker
.
ThreadWorker
(
'
@bundle:com.example.workerdemo/entry/ets/workers/worker
'
);
// Stage模型-目录不同级(entry模块下,workers目录是pages目录的子目录),假设bundlename是com.example.workerdemo
const
workerStageModel04
=
new
worker
.
ThreadWorker
(
'
@bundle:com.example.workerdemo/entry/ets/pages/workers/worker
'
);
```
同时,需在工程的模块级build-profile.json5文件的buildOption属性中添加配置信息,主要分为下面两种情况:
...
...
@@ -177,7 +185,7 @@ postMessage(message: Object, transfer: ArrayBuffer[]): void;
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
postMessage
(
"
hello world
"
);
...
...
@@ -212,7 +220,7 @@ postMessage(message: Object, options?: PostMessageOptions): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
postMessage
(
"
hello world
"
);
...
...
@@ -247,7 +255,7 @@ on(type: string, listener: WorkerEventListener): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
on
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -281,7 +289,7 @@ once(type: string, listener: WorkerEventListener): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
once
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -315,7 +323,7 @@ off(type: string, listener?: WorkerEventListener): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
//使用on接口、once接口或addEventListener接口创建“alert”事件,使用off接口删除事件。
workerInstance
.
off
(
"
alert
"
);
```
...
...
@@ -340,7 +348,7 @@ terminate(): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
terminate
();
```
...
...
@@ -371,7 +379,7 @@ Worker对象的onexit属性表示Worker销毁时被调用的事件处理程序
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
onexit
=
function
(
e
)
{
console
.
log
(
"
onexit
"
);
}
...
...
@@ -411,7 +419,7 @@ Worker对象的onerror属性表示Worker在执行过程中发生异常被调用
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
onerror
=
function
(
e
)
{
console
.
log
(
"
onerror
"
);
}
...
...
@@ -444,7 +452,7 @@ Worker对象的onmessage属性表示宿主线程接收到来自其创建的Worke
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
onmessage
=
function
(
e
)
{
// e : MessageEvents, 用法如下:
// let data = e.data;
...
...
@@ -479,7 +487,7 @@ Worker对象的onmessageerror属性表示当Worker对象接收到一条无法被
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
onmessageerror
=
function
(
e
)
{
console
.
log
(
"
onmessageerror
"
);
}
...
...
@@ -512,7 +520,7 @@ addEventListener(type: string, listener: WorkerEventListener): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
addEventListener
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -545,7 +553,7 @@ removeEventListener(type: string, callback?: WorkerEventListener): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
addEventListener
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -584,7 +592,7 @@ dispatchEvent(event: Event): boolean
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
dispatchEvent
({
type
:
"
eventType
"
,
timeStamp
:
0
});
//timeStamp暂未支持。
```
...
...
@@ -592,7 +600,7 @@ workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); //timeStamp暂未
分发事件(dispatchEvent)可与监听接口(on、once、addEventListener)搭配使用,示例如下:
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
//用法一:
workerInstance
.
on
(
"
alert_on
"
,
(
e
)
=>
{
...
...
@@ -651,7 +659,7 @@ removeAllListener(): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
addEventListener
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -687,7 +695,7 @@ addEventListener(type: string, listener: WorkerEventListener): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
addEventListener
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -720,7 +728,7 @@ removeEventListener(type: string, callback?: WorkerEventListener): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
addEventListener
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -759,7 +767,7 @@ dispatchEvent(event: Event): boolean
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
dispatchEvent
({
type
:
"
eventType
"
,
timeStamp
:
0
});
//timeStamp暂未支持。
```
...
...
@@ -767,7 +775,7 @@ workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); //timeStamp暂未
分发事件(dispatchEvent)可与监听接口(on、once、addEventListener)搭配使用,示例如下:
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
//用法一:
workerInstance
.
on
(
"
alert_on
"
,
(
e
)
=>
{
...
...
@@ -826,7 +834,7 @@ removeAllListener(): void
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
addEventListener
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -867,7 +875,7 @@ Worker线程向宿主线程发送消息。
```
js
// main.js
import
worker
from
'
@ohos.worker
'
;
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
postMessage
(
"
hello world
"
);
workerInstance
.
onmessage
=
function
(
e
)
{
// let data = e.data;
...
...
@@ -876,7 +884,7 @@ workerInstance.onmessage = function(e) {
```
```
js
// worker.
j
s
// worker.
t
s
import
worker
from
'
@ohos.worker
'
;
const
workerPort
=
worker
.
workerPort
;
workerPort
.
onmessage
=
function
(
e
){
...
...
@@ -915,7 +923,7 @@ Worker线程向宿主线程发送消息。
```
js
// main.js
import
worker
from
'
@ohos.worker
'
;
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
postMessage
(
"
hello world
"
);
workerInstance
.
onmessage
=
function
(
e
)
{
// let data = e.data;
...
...
@@ -924,7 +932,7 @@ workerInstance.onmessage = function(e) {
```
```
js
// worker.
j
s
// worker.
t
s
import
worker
from
'
@ohos.worker
'
;
const
workerPort
=
worker
.
workerPort
;
workerPort
.
onmessage
=
function
(
e
){
...
...
@@ -955,11 +963,11 @@ close(): void
```
js
// main.js
import
worker
from
'
@ohos.worker
'
;
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
```
```
js
// worker.
j
s
// worker.
t
s
import
worker
from
'
@ohos.worker
'
;
const
workerPort
=
worker
.
workerPort
;
workerPort
.
onmessage
=
function
(
e
)
{
...
...
@@ -997,12 +1005,12 @@ DedicatedWorkerGlobalScope的onmessage属性表示Worker线程收到来自其宿
```
js
// main.js
import
worker
from
'
@ohos.worker
'
;
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
postMessage
(
"
hello world
"
);
```
```
js
// worker.
j
s
// worker.
t
s
import
worker
from
'
@ohos.worker
'
;
const
workerPort
=
worker
.
workerPort
;
workerPort
.
onmessage
=
function
(
e
)
{
...
...
@@ -1040,11 +1048,11 @@ DedicatedWorkerGlobalScope的onmessageerror属性表示当Worker对象接收到
```
js
// main.js
import
worker
from
'
@ohos.worker
'
;
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
```
```
js
// worker.
j
s
// worker.
t
s
import
worker
from
'
@ohos.worker
'
;
const
parentPort
=
worker
.
workerPort
;
parentPort
.
onmessageerror
=
function
(
e
)
{
...
...
@@ -1085,7 +1093,7 @@ parentPort.onmessageerror = function(e) {
**示例:**
```
js
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
);
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
);
workerInstance
.
addEventListener
(
"
alert
"
,
(
e
)
=>
{
console
.
log
(
"
alert listener callback
"
);
})
...
...
@@ -1125,11 +1133,11 @@ GlobalScope的onerror属性表示Worker在执行过程中发生异常被调用
```
js
// main.js
import
worker
from
'
@ohos.worker
'
;
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
workers/worker.j
s
"
)
const
workerInstance
=
new
worker
.
ThreadWorker
(
"
entry/ets/workers/worker.t
s
"
)
```
```
js
// worker.
j
s
// worker.
t
s
import
worker
from
'
@ohos.worker
'
;
const
workerPort
=
worker
.
workerPort
workerPort
.
onerror
=
function
(
e
){
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录