Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
8da511f4
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看板
未验证
提交
8da511f4
编写于
8月 31, 2023
作者:
O
openharmony_ci
提交者:
Gitee
8月 31, 2023
浏览文件
操作
浏览文件
下载
差异文件
!23076 ArkTs适配
Merge pull request !23076 from XKK/cherry-pick-1693012745
上级
61e4acb2
cf59c6bb
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
182 addition
and
172 deletion
+182
-172
en/application-dev/reference/apis/js-apis-commonEvent.md
en/application-dev/reference/apis/js-apis-commonEvent.md
+26
-24
en/application-dev/reference/apis/js-apis-commonEventManager.md
...lication-dev/reference/apis/js-apis-commonEventManager.md
+27
-26
en/application-dev/reference/apis/js-apis-inner-commonEvent-commonEventSubscriber.md
...e/apis/js-apis-inner-commonEvent-commonEventSubscriber.md
+35
-33
zh-cn/application-dev/reference/apis/js-apis-commonEvent.md
zh-cn/application-dev/reference/apis/js-apis-commonEvent.md
+31
-29
zh-cn/application-dev/reference/apis/js-apis-commonEventManager.md
...lication-dev/reference/apis/js-apis-commonEventManager.md
+28
-27
zh-cn/application-dev/reference/apis/js-apis-inner-commonEvent-commonEventSubscriber.md
...e/apis/js-apis-inner-commonEvent-commonEventSubscriber.md
+35
-33
未找到文件。
en/application-dev/reference/apis/js-apis-commonEvent.md
浏览文件 @
8da511f4
...
...
@@ -12,6 +12,8 @@ The **CommonEvent** module provides common event capabilities, including the cap
```
ts
import
CommonEvent
from
'
@ohos.commonEvent
'
;
import
CommonEventManager
from
'
@ohos.commonEventManager
'
;
import
Base
from
'
@ohos.base
'
;
```
## Support
...
...
@@ -43,7 +45,7 @@ Publishes a common event. This API uses an asynchronous callback to return the r
```
ts
// Callback for common event publication
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`publish failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -80,14 +82,14 @@ Publishes a common event with given attributes. This API uses an asynchronous ca
```
ts
// Attributes of a common event.
let
options
=
{
let
options
:
CommonEventManager
.
CommonEventPublishData
=
{
code
:
0
,
// Result code of the common event.
data
:
"
initial data
"
;
// Result data of the common event.
data
:
"
initial data
"
,
// Result data of the common event.
isOrdered
:
true
// The common event is an ordered one.
}
// Callback for common event publication
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`publish failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -125,7 +127,7 @@ Publishes a common event to a specific user. This API uses an asynchronous callb
```
ts
// Callback for common event publication
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`publishAsUser failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -168,13 +170,13 @@ Publishes a common event with given attributes to a specific user. This API uses
```
ts
// Attributes of a common event.
let
options
=
{
let
options
:
CommonEventManager
.
CommonEventPublishData
=
{
code
:
0
,
// Result code of the common event.
data
:
"
initial data
"
,
// Result data of the common event.
}
// Callback for common event publication
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`publishAsUser failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -212,15 +214,15 @@ Creates a subscriber. This API uses an asynchronous callback to return the resul
```
ts
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
events
:
[
"
event
"
]
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Callback for subscriber creation.
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
.
code
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -259,18 +261,18 @@ Creates a subscriber. This API uses a promise to return the result.
**Example**
```
ts
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Create a subscriber.
CommonEvent
.
createSubscriber
(
subscribeInfo
).
then
((
commonEventSubscriber
)
=>
{
CommonEvent
.
createSubscriber
(
subscribeInfo
).
then
((
commonEventSubscriber
:
CommonEventManager
.
CommonEventSubscriber
)
=>
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
});
```
...
...
@@ -297,15 +299,15 @@ Subscribes to common events. This API uses an asynchronous callback to return th
**Example**
```
ts
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Callback for common event subscription.
function
subscribeCB
(
err
,
d
ata
)
{
function
subscribeCB
(
err
:
Base
.
BusinessError
,
data
:
CommonEventManager
.
CommonEventD
ata
)
{
if
(
err
.
code
)
{
console
.
error
(
`subscribe failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -314,7 +316,7 @@ function subscribeCB(err, data) {
}
// Callback for subscriber creation.
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
.
code
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -351,15 +353,15 @@ Unsubscribes from common events. This API uses an asynchronous callback to retur
**Example**
```
ts
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Callback for common event subscription.
function
subscribeCB
(
err
,
d
ata
)
{
function
subscribeCB
(
err
:
Base
.
BusinessError
,
data
:
CommonEventManager
.
CommonEventD
ata
)
{
if
(
err
.
code
)
{
console
.
error
(
`subscribe failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -368,7 +370,7 @@ function subscribeCB(err, data) {
}
// Callback for subscriber creation.
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
.
code
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -380,7 +382,7 @@ function createCB(err, commonEventSubscriber) {
}
// Callback for common event unsubscription.
function
unsubscribeCB
(
err
)
{
function
unsubscribeCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`unsubscribe failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
en/application-dev/reference/apis/js-apis-commonEventManager.md
浏览文件 @
8da511f4
...
...
@@ -10,6 +10,7 @@ The **CommonEventManager** module provides common event capabilities, including
```
ts
import
CommonEventManager
from
'
@ohos.commonEventManager
'
;
import
Base
from
'
@ohos.base
'
;
```
## Support
...
...
@@ -48,7 +49,7 @@ Publishes a common event and executes an asynchronous callback after the event i
```
ts
// Callback for common event publication
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`publish failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -95,14 +96,14 @@ Publishes a common event with given attributes. This API uses an asynchronous ca
```
ts
// Attributes of a common event.
let
options
=
{
let
options
:
CommonEventManager
.
CommonEventPublishData
=
{
code
:
0
,
// Result code of the common event.
data
:
"
initial data
"
,
// Result data of the common event.
isOrdered
:
true
// The common event is an ordered one.
}
// Callback for common event publication
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`publish failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -151,7 +152,7 @@ Publishes a common event to a specific user. This API uses an asynchronous callb
```
ts
// Callback for common event publication
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`publishAsUser failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -205,13 +206,13 @@ Publishes a common event with given attributes to a specific user. This API uses
```
ts
// Attributes of a common event.
let
options
=
{
let
options
:
CommonEventManager
.
CommonEventPublishData
=
{
code
:
0
,
// Result code of the common event.
data
:
"
initial data
"
,
// Result data of the common event.
}
// Callback for common event publication.
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`publishAsUser failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -248,15 +249,15 @@ Creates a subscriber. This API uses an asynchronous callback to return the resul
**Example**
```
ts
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Callback for subscriber creation.
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
!
err
)
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
...
...
@@ -295,18 +296,18 @@ Creates a subscriber. This API uses a promise to return the result.
**Example**
```
ts
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Create a subscriber.
CommonEventManager
.
createSubscriber
(
subscribeInfo
).
then
((
commonEventSubscriber
)
=>
{
CommonEventManager
.
createSubscriber
(
subscribeInfo
).
then
((
commonEventSubscriber
:
CommonEventManager
.
CommonEventSubscriber
)
=>
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
...
...
@@ -341,15 +342,15 @@ Subscribes to common events. This API uses an asynchronous callback to return th
```
ts
// Subscriber information.
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Callback for common event subscription.
function
SubscribeCB
(
err
,
d
ata
)
{
function
SubscribeCB
(
err
:
Base
.
BusinessError
,
data
:
CommonEventManager
.
CommonEventD
ata
)
{
if
(
err
)
{
console
.
error
(
`subscribe failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -358,7 +359,7 @@ function SubscribeCB(err, data) {
}
// Callback for subscriber creation.
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
!
err
)
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
...
...
@@ -409,13 +410,13 @@ Unsubscribes from common events. This API uses an asynchronous callback to retur
**Example**
```
ts
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Callback for common event subscription.
function
subscribeCB
(
err
,
d
ata
)
{
function
subscribeCB
(
err
:
Base
.
BusinessError
,
data
:
CommonEventManager
.
CommonEventD
ata
)
{
if
(
err
)
{
console
.
error
(
`subscribe failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -423,7 +424,7 @@ function subscribeCB(err, data) {
}
}
// Callback for subscriber creation.
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -438,7 +439,7 @@ function createCB(err, commonEventSubscriber) {
}
}
// Callback for common event unsubscription.
function
unsubscribeCB
(
err
)
{
function
unsubscribeCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`unsubscribe failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -493,7 +494,7 @@ Removes a sticky common event. This API uses an asynchronous callback to return
```
ts
CommonEventManager
.
removeStickyCommonEvent
(
"
sticky_event
"
,
(
err
)
=>
{
CommonEventManager
.
removeStickyCommonEvent
(
"
sticky_event
"
,
(
err
:
Base
.
BusinessError
)
=>
{
if
(
err
)
{
console
.
info
(
`Remove sticky event AsyncCallback failed, errCode:
${
err
.
code
}
, errMes:
${
err
.
message
}
`
);
return
;
...
...
@@ -542,7 +543,7 @@ Removes a sticky common event. This API uses a promise to return the result.
```
ts
CommonEventManager
.
removeStickyCommonEvent
(
"
sticky_event
"
).
then
(()
=>
{
console
.
info
(
`Remove sticky event AsyncCallback success`
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
info
(
`Remove sticky event AsyncCallback failed, errCode:
${
err
.
code
}
, errMes:
${
err
.
message
}
`
);
});
```
...
...
@@ -577,7 +578,7 @@ Enables or disables static subscription for the current application. This API us
```
ts
CommonEventManager
.
setStaticSubscriberState
(
true
,
(
err
)
=>
{
CommonEventManager
.
setStaticSubscriberState
(
true
,
(
err
:
Base
.
BusinessError
)
=>
{
if
(
!
err
)
{
console
.
info
(
`Set static subscriber state callback failed, err is null.`
);
return
;
...
...
@@ -627,7 +628,7 @@ Enables or disables static subscription for the current application. This API us
```
ts
CommonEventManager
.
setStaticSubscriberState
(
false
).
then
(()
=>
{
console
.
info
(
`Set static subscriber state promise success`
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
info
(
`Set static subscriber state promise failed, errCode:
${
err
.
code
}
, errMes:
${
err
.
message
}
`
);
});
```
en/application-dev/reference/apis/js-apis-inner-commonEvent-commonEventSubscriber.md
浏览文件 @
8da511f4
...
...
@@ -10,15 +10,17 @@ Before using the **CommonEventSubscriber** module, you must obtain a **subscribe
```
ts
import
CommonEvent
from
'
@ohos.commonEvent
'
;
let
subscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
import
CommonEventManager
from
'
@ohos.commonEventManager
'
;
import
Base
from
'
@ohos.base
'
;
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// Callback for subscriber creation.
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
.
code
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -49,7 +51,7 @@ Obtains the code of this common event. This API uses an asynchronous callback to
```
ts
// Callback for result code obtaining of an ordered common event.
function
getCodeCB
(
err
,
code
)
{
function
getCodeCB
(
err
:
Base
.
BusinessError
,
code
:
number
)
{
if
(
err
.
code
)
{
console
.
error
(
`getCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -76,9 +78,9 @@ Obtains the code of this common event. This API uses a promise to return the res
**Example**
```
ts
subscriber
.
getCode
().
then
((
code
)
=>
{
subscriber
.
getCode
().
then
((
code
:
number
)
=>
{
console
.
info
(
"
getCode
"
+
JSON
.
stringify
(
code
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`getCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -102,7 +104,7 @@ Sets the code for this common event. This API uses an asynchronous callback to r
```
ts
// Callback for result code setting of an ordered common event.
function
setCodeCB
(
err
)
{
function
setCodeCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`setCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -137,7 +139,7 @@ Sets the code for this common event. This API uses a promise to return the resul
```
ts
subscriber
.
setCode
(
1
).
then
(()
=>
{
console
.
info
(
"
setCode
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`setCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -160,7 +162,7 @@ Obtains the data of this common event. This API uses an asynchronous callback to
```
ts
// Callback for result data obtaining of an ordered common event.
function
getDataCB
(
err
,
data
)
{
function
getDataCB
(
err
:
Base
.
BusinessError
,
data
:
string
)
{
if
(
err
.
code
)
{
console
.
error
(
`getData failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -187,9 +189,9 @@ Obtains the data of this common event. This API uses a promise to return the res
**Example**
```
ts
subscriber
.
getData
().
then
((
data
)
=>
{
subscriber
.
getData
().
then
((
data
:
string
)
=>
{
console
.
info
(
"
getData
"
+
JSON
.
stringify
(
data
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`getData failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -213,7 +215,7 @@ Sets the data for this common event. This API uses an asynchronous callback to r
```
ts
// Callback for result data setting of an ordered common event
function
setDataCB
(
err
)
{
function
setDataCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`setCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -248,7 +250,7 @@ Sets the data for this common event. This API uses a promise to return the resul
```
ts
subscriber
.
setData
(
"
publish_data_changed
"
).
then
(()
=>
{
console
.
info
(
"
setData
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`setCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -273,7 +275,7 @@ Sets the code and data for this common event. This API uses an asynchronous call
```
ts
// Callback for code and data setting of an ordered common event.
function
setCodeDataCB
(
err
)
{
function
setCodeDataCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`setCodeAndData failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -309,7 +311,7 @@ Sets the code and data for this common event. This API uses a promise to return
```
ts
subscriber
.
setCodeAndData
(
1
,
"
publish_data_changed
"
).
then
(()
=>
{
console
.
info
(
"
setCodeAndData
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`setCodeAndData failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -332,7 +334,7 @@ Checks whether this common event is an ordered one. This API uses an asynchronou
```
ts
// Callback for checking whether the current common event is an ordered one.
function
isOrderedCB
(
err
,
isOrdered
)
{
function
isOrderedCB
(
err
:
Base
.
BusinessError
,
isOrdered
:
boolean
)
{
if
(
err
.
code
)
{
console
.
error
(
`isOrderedCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -359,9 +361,9 @@ Checks whether this common event is an ordered one. This API uses a promise to r
**Example**
```
ts
subscriber
.
isOrderedCommonEvent
().
then
((
isOrdered
)
=>
{
subscriber
.
isOrderedCommonEvent
().
then
((
isOrdered
:
boolean
)
=>
{
console
.
info
(
"
isOrdered
"
+
JSON
.
stringify
(
isOrdered
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`isOrdered failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -384,7 +386,7 @@ Checks whether this common event is a sticky one. This API uses an asynchronous
```
ts
// Callback for checking whether the current common event is a sticky one.
function
isStickyCB
(
err
,
isSticky
)
{
function
isStickyCB
(
err
:
Base
.
BusinessError
,
isSticky
:
boolean
)
{
if
(
err
.
code
)
{
console
.
error
(
`isStickyCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -411,9 +413,9 @@ Checks whether this common event is a sticky one. This API uses a promise to ret
**Example**
```
ts
subscriber
.
isStickyCommonEvent
().
then
((
isSticky
)
=>
{
subscriber
.
isStickyCommonEvent
().
then
((
isSticky
:
boolean
)
=>
{
console
.
info
(
"
isSticky
"
+
JSON
.
stringify
(
isSticky
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`isSticky failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -436,7 +438,7 @@ Aborts this common event. After the abort, the common event is not sent to the n
```
ts
// Callback for common event aborting.
function
abortCB
(
err
)
{
function
abortCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`abortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -465,7 +467,7 @@ Aborts this common event. After the abort, the common event is not sent to the n
```
ts
subscriber
.
abortCommonEvent
().
then
(()
=>
{
console
.
info
(
"
abortCommonEvent
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`abortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -488,7 +490,7 @@ Clears the aborted state of this common event. This API takes effect only for or
```
ts
// Callback for clearing the aborted state of the current common event.
function
clearAbortCB
(
err
)
{
function
clearAbortCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`clearAbortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -517,7 +519,7 @@ Clears the aborted state of this common event. This API takes effect only for or
```
ts
subscriber
.
clearAbortCommonEvent
().
then
(()
=>
{
console
.
info
(
"
clearAbortCommonEvent
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`clearAbortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -540,7 +542,7 @@ Checks whether this common event is in the aborted state. This API takes effect
```
ts
// Callback for checking whether the current common event is in the aborted state.
function
getAbortCB
(
err
,
abortEvent
)
{
function
getAbortCB
(
err
:
Base
.
BusinessError
,
abortEvent
:
boolean
)
{
if
(
err
.
code
)
{
console
.
error
(
`getAbortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -567,9 +569,9 @@ Checks whether this common event is in the aborted state. This API takes effect
**Example**
```
ts
subscriber
.
getAbortCommonEvent
().
then
((
abortEvent
)
=>
{
subscriber
.
getAbortCommonEvent
().
then
((
abortEvent
:
boolean
)
=>
{
console
.
info
(
"
abortCommonEvent
"
+
JSON
.
stringify
(
abortEvent
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`getAbortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -592,7 +594,7 @@ Obtains the subscriber information. This API uses an asynchronous callback to re
```
ts
// Callback for subscriber information obtaining.
function
getCB
(
err
,
s
ubscribeInfo
)
{
function
getCB
(
err
:
Base
.
BusinessError
,
subscribeInfo
:
CommonEventManager
.
CommonEventS
ubscribeInfo
)
{
if
(
err
.
code
)
{
console
.
error
(
`getSubscribeInfo failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -619,9 +621,9 @@ Obtains the subscriber information. This API uses a promise to return the result
**Example**
```
ts
subscriber
.
getSubscribeInfo
().
then
((
subscribeInfo
)
=>
{
subscriber
.
getSubscribeInfo
().
then
((
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
)
=>
{
console
.
info
(
"
subscribeInfo
"
+
JSON
.
stringify
(
subscribeInfo
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`getSubscribeInfo failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -644,7 +646,7 @@ Finishes this common event. This API takes effect only for ordered common events
```
ts
// Callback for ordered common event finishing.
function
finishCB
(
err
)
{
function
finishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`finishCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -674,7 +676,7 @@ Finishes this common event. This API takes effect only for ordered common events
```
ts
subscriber
.
finishCommonEvent
().
then
(()
=>
{
console
.
info
(
"
FinishCommonEvent
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`finishCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
zh-cn/application-dev/reference/apis/js-apis-commonEvent.md
浏览文件 @
8da511f4
...
...
@@ -11,6 +11,8 @@
```
ts
import
CommonEvent
from
'
@ohos.commonEvent
'
;
import
CommonEventManager
from
'
@ohos.commonEventManager
'
;
import
Base
from
'
@ohos.base
'
;
```
## Support
...
...
@@ -41,7 +43,7 @@ publish(event: string, callback: AsyncCallback\<void>): void
```
ts
//发布公共事件回调
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`publish failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -77,14 +79,14 @@ publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\
```
ts
//公共事件相关信息
let
options
=
{
let
options
:
CommonEventManager
.
CommonEventPublishData
=
{
code
:
0
,
//公共事件的初始代码
data
:
"
initial data
"
,
//公共事件的初始数据
isOrdered
:
true
//有序公共事件
}
//发布公共事件回调
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`publish failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -121,7 +123,7 @@ publishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): vo
```
ts
// 发布公共事件回调
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`publishAsUser failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -163,14 +165,14 @@ publishAsUser(event: string, userId: number, options: CommonEventPublishData, ca
```
ts
// 公共事件相关信息
let
options
=
{
let
options
:
CommonEventManager
.
CommonEventPublishData
=
{
code
:
0
,
// 公共事件的初始代码
data
:
"
initial data
"
,
// 公共事件的初始数据
}
// 发布公共事件回调
function
publishCB
(
err
)
{
if
(
err
.
code
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`publishAsUser failed, code is
${
err
.
code
}
`
);
}
else
{
console
.
info
(
"
publishAsUser
"
);
...
...
@@ -206,15 +208,15 @@ createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallbac
```
ts
let
subscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
// 订阅者信息
let
subscribeInfo
=
{
events
:
[
"
event
"
]
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// 创建订阅者回调
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
.
code
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -252,18 +254,18 @@ createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventS
**示例:**
```
ts
let
subscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
// 订阅者信息
let
subscribeInfo
=
{
events
:
[
"
event
"
]
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// 创建订阅者
CommonEvent
.
createSubscriber
(
subscribeInfo
).
then
((
commonEventSubscriber
)
=>
{
CommonEvent
.
createSubscriber
(
subscribeInfo
).
then
((
commonEventSubscriber
:
CommonEventManager
.
CommonEventSubscriber
)
=>
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
});
```
...
...
@@ -289,15 +291,15 @@ subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEven
**示例:**
```
ts
let
subscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
// 订阅者信息
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// 订阅公共事件回调
function
subscribeCB
(
err
,
d
ata
)
{
function
subscribeCB
(
err
:
Base
.
BusinessError
,
data
:
CommonEventManager
.
CommonEventD
ata
)
{
if
(
err
.
code
)
{
console
.
error
(
`subscribe failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -306,13 +308,13 @@ function subscribeCB(err, data) {
}
// 创建订阅者回调
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
.
code
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
}
else
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
//
订阅公共事件
//
Subscribe to a common event.
CommonEvent
.
subscribe
(
subscriber
,
subscribeCB
);
}
}
...
...
@@ -342,15 +344,15 @@ unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>):
**示例:**
```
ts
let
subscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
// 订阅者信息
let
subscribeInfo
=
{
events
:
[
"
event
"
]
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// 订阅公共事件回调
function
subscribeCB
(
err
,
d
ata
)
{
function
subscribeCB
(
err
:
Base
.
BusinessError
,
data
:
CommonEventManager
.
CommonEventD
ata
)
{
if
(
err
.
code
)
{
console
.
error
(
`subscribe failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -359,20 +361,20 @@ function subscribeCB(err, data) {
}
// 创建订阅者回调
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
.
code
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
}
else
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
//
订阅公共事件
//
Subscribe to a common event.
CommonEvent
.
subscribe
(
subscriber
,
subscribeCB
);
}
}
// 取消订阅公共事件回调
function
unsubscribeCB
(
err
)
{
if
(
err
.
code
)
{
function
unsubscribeCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`unsubscribe failed, code is
${
err
.
code
}
`
);
}
else
{
console
.
info
(
"
unsubscribe
"
);
...
...
zh-cn/application-dev/reference/apis/js-apis-commonEventManager.md
浏览文件 @
8da511f4
...
...
@@ -10,6 +10,7 @@
```
ts
import
CommonEventManager
from
'
@ohos.commonEventManager
'
;
import
Base
from
'
@ohos.base
'
;
```
## Support
...
...
@@ -48,8 +49,8 @@ publish(event: string, callback: AsyncCallback\<void>): void
```
ts
//发布公共事件回调
function
publishCB
(
err
)
{
if
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`publish failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
console
.
info
(
"
publish
"
);
...
...
@@ -95,14 +96,14 @@ publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\
```
ts
//公共事件相关信息
let
options
=
{
let
options
:
CommonEventManager
.
CommonEventPublishData
=
{
code
:
0
,
//公共事件的初始代码
data
:
"
initial data
"
,
//公共事件的初始数据
isOrdered
:
true
//有序公共事件
}
//发布公共事件回调
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`publish failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -151,7 +152,7 @@ publishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): vo
```
ts
//发布公共事件回调
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`publishAsUser failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -205,13 +206,13 @@ publishAsUser(event: string, userId: number, options: CommonEventPublishData, ca
```
ts
//公共事件相关信息
let
options
=
{
let
options
:
CommonEventManager
.
CommonEventPublishData
=
{
code
:
0
,
//公共事件的初始代码
data
:
"
initial data
"
,
//公共事件的初始数据
}
//发布公共事件回调
function
publishCB
(
err
)
{
function
publishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`publishAsUser failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -248,15 +249,15 @@ createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallbac
**示例:**
```
ts
let
subscriber
;
//用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
//用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
//订阅者信息
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
//创建订阅者回调
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
!
err
)
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
...
...
@@ -295,18 +296,18 @@ createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventS
**示例:**
```
ts
let
subscriber
;
//用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
//用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
//订阅者信息
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
//创建订阅者
CommonEventManager
.
createSubscriber
(
subscribeInfo
).
then
((
commonEventSubscriber
)
=>
{
CommonEventManager
.
createSubscriber
(
subscribeInfo
).
then
((
commonEventSubscriber
:
CommonEventManager
.
CommonEventSubscriber
)
=>
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
...
...
@@ -379,15 +380,15 @@ subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEven
```
ts
//订阅者信息
let
subscriber
;
//用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
//用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
//订阅者信息
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
//订阅公共事件回调
function
SubscribeCB
(
err
,
d
ata
)
{
function
SubscribeCB
(
err
:
Base
.
BusinessError
,
data
:
CommonEventManager
.
CommonEventD
ata
)
{
if
(
err
)
{
console
.
error
(
`subscribe failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -396,7 +397,7 @@ function SubscribeCB(err, data) {
}
//创建订阅者回调
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
!
err
)
{
console
.
info
(
"
createSubscriber
"
);
subscriber
=
commonEventSubscriber
;
...
...
@@ -447,13 +448,13 @@ unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>):
**示例:**
```
ts
let
subscriber
;
//用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
//用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
//订阅者信息
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
//订阅公共事件回调
function
subscribeCB
(
err
,
d
ata
)
{
function
subscribeCB
(
err
:
Base
.
BusinessError
,
data
:
CommonEventManager
.
CommonEventD
ata
)
{
if
(
err
)
{
console
.
error
(
`subscribe failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -461,7 +462,7 @@ function subscribeCB(err, data) {
}
}
//创建订阅者回调
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -476,7 +477,7 @@ function createCB(err, commonEventSubscriber) {
}
}
//取消订阅公共事件回调
function
unsubscribeCB
(
err
)
{
function
unsubscribeCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
)
{
console
.
error
(
`unsubscribe failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -531,7 +532,7 @@ removeStickyCommonEvent(event: string, callback: AsyncCallback\<void>): void
```
ts
CommonEventManager
.
removeStickyCommonEvent
(
"
sticky_event
"
,
(
err
)
=>
{
CommonEventManager
.
removeStickyCommonEvent
(
"
sticky_event
"
,
(
err
:
Base
.
BusinessError
)
=>
{
if
(
err
)
{
console
.
info
(
`Remove sticky event AsyncCallback failed, errCode:
${
err
.
code
}
, errMes:
${
err
.
message
}
`
);
return
;
...
...
@@ -580,7 +581,7 @@ removeStickyCommonEvent(event: string): Promise\<void>
```
ts
CommonEventManager
.
removeStickyCommonEvent
(
"
sticky_event
"
).
then
(()
=>
{
console
.
info
(
`Remove sticky event AsyncCallback success`
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
info
(
`Remove sticky event AsyncCallback failed, errCode:
${
err
.
code
}
, errMes:
${
err
.
message
}
`
);
});
```
...
...
@@ -615,7 +616,7 @@ setStaticSubscriberState(enable: boolean, callback: AsyncCallback\<void>): void;
```
ts
CommonEventManager
.
setStaticSubscriberState
(
true
,
(
err
)
=>
{
CommonEventManager
.
setStaticSubscriberState
(
true
,
(
err
:
Base
.
BusinessError
)
=>
{
if
(
!
err
)
{
console
.
info
(
`Set static subscriber state callback failed, err is null.`
);
return
;
...
...
@@ -665,7 +666,7 @@ setStaticSubscriberState(enable: boolean): Promise\<void>;
```
ts
CommonEventManager
.
setStaticSubscriberState
(
false
).
then
(()
=>
{
console
.
info
(
`Set static subscriber state promise success`
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
info
(
`Set static subscriber state promise failed, errCode:
${
err
.
code
}
, errMes:
${
err
.
message
}
`
);
});
```
zh-cn/application-dev/reference/apis/js-apis-inner-commonEvent-commonEventSubscriber.md
浏览文件 @
8da511f4
...
...
@@ -10,15 +10,17 @@
```
ts
import
CommonEvent
from
'
@ohos.commonEvent
'
;
let
subscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
import
CommonEventManager
from
'
@ohos.commonEventManager
'
;
import
Base
from
'
@ohos.base
'
;
let
subscriber
:
CommonEventManager
.
CommonEventSubscriber
;
// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
// 订阅者信息
let
subscribeInfo
=
{
let
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
=
{
events
:
[
"
event
"
]
};
// 创建订阅者回调
function
createCB
(
err
,
c
ommonEventSubscriber
)
{
function
createCB
(
err
:
Base
.
BusinessError
,
commonEventSubscriber
:
CommonEventManager
.
C
ommonEventSubscriber
)
{
if
(
err
.
code
)
{
console
.
error
(
`createSubscriber failed, code is
${
err
.
code
}
`
);
}
else
{
...
...
@@ -49,7 +51,7 @@ getCode(callback: AsyncCallback\<number>): void
```
ts
//获取有序公共事件代码回调
function
getCodeCB
(
err
,
code
)
{
function
getCodeCB
(
err
:
Base
.
BusinessError
,
code
:
number
)
{
if
(
err
.
code
)
{
console
.
error
(
`getCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -76,9 +78,9 @@ getCode(): Promise\<number>
**示例:**
```
ts
subscriber
.
getCode
().
then
((
code
)
=>
{
subscriber
.
getCode
().
then
((
code
:
number
)
=>
{
console
.
info
(
"
getCode
"
+
JSON
.
stringify
(
code
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`getCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -123,7 +125,7 @@ setCode(code: number, callback: AsyncCallback\<void>): void
```
ts
//设置有序公共事件的代码回调
function
setCodeCB
(
err
)
{
function
setCodeCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`setCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -158,7 +160,7 @@ setCode(code: number): Promise\<void>
```
ts
subscriber
.
setCode
(
1
).
then
(()
=>
{
console
.
info
(
"
setCode
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`setCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -207,7 +209,7 @@ getData(callback: AsyncCallback\<string>): void
```
ts
//获取有序公共事件代码数据回调
function
getDataCB
(
err
,
data
)
{
function
getDataCB
(
err
:
Base
.
BusinessError
,
data
:
string
)
{
if
(
err
.
code
)
{
console
.
error
(
`getData failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -234,9 +236,9 @@ getData(): Promise\<string>
**示例:**
```
ts
subscriber
.
getData
().
then
((
data
)
=>
{
subscriber
.
getData
().
then
((
data
:
string
)
=>
{
console
.
info
(
"
getData
"
+
JSON
.
stringify
(
data
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`getData failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -281,7 +283,7 @@ setData(data: string, callback: AsyncCallback\<void>): void
```
ts
//设置有序公共事件的结果数据回调
function
setDataCB
(
err
)
{
function
setDataCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`setCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -316,7 +318,7 @@ setData(data: string): Promise\<void>
```
ts
subscriber
.
setData
(
"
publish_data_changed
"
).
then
(()
=>
{
console
.
info
(
"
setData
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`setCode failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -364,7 +366,7 @@ setCodeAndData(code: number, data: string, callback:AsyncCallback\<void>): void
```
ts
//设置有序公共事件的代码和数据回调
function
setCodeDataCB
(
err
)
{
function
setCodeDataCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`setCodeAndData failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -400,7 +402,7 @@ setCodeAndData(code: number, data: string): Promise\<void>
```
ts
subscriber
.
setCodeAndData
(
1
,
"
publish_data_changed
"
).
then
(()
=>
{
console
.
info
(
"
setCodeAndData
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`setCodeAndData failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -450,7 +452,7 @@ isOrderedCommonEvent(callback: AsyncCallback\<boolean>): void
```
ts
//获取当前公共事件是否为有序事件的回调
function
isOrderedCB
(
err
,
isOrdered
)
{
function
isOrderedCB
(
err
:
Base
.
BusinessError
,
isOrdered
:
boolean
)
{
if
(
err
.
code
)
{
console
.
error
(
`isOrderedCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -479,9 +481,9 @@ isOrderedCommonEvent(): Promise\<boolean>
**示例:**
```
ts
subscriber
.
isOrderedCommonEvent
().
then
((
isOrdered
)
=>
{
subscriber
.
isOrderedCommonEvent
().
then
((
isOrdered
:
boolean
)
=>
{
console
.
info
(
"
isOrdered
"
+
JSON
.
stringify
(
isOrdered
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`isOrdered failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -529,7 +531,7 @@ isStickyCommonEvent(callback: AsyncCallback\<boolean>): void
```
ts
//获取当前公共事件是否为粘性事件的回调
function
isStickyCB
(
err
,
isSticky
)
{
function
isStickyCB
(
err
:
Base
.
BusinessError
,
isSticky
:
boolean
)
{
if
(
err
.
code
)
{
console
.
error
(
`isStickyCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -558,9 +560,9 @@ isStickyCommonEvent(): Promise\<boolean>
**示例:**
```
ts
subscriber
.
isStickyCommonEvent
().
then
((
isSticky
)
=>
{
subscriber
.
isStickyCommonEvent
().
then
((
isSticky
:
boolean
)
=>
{
console
.
info
(
"
isSticky
"
+
JSON
.
stringify
(
isSticky
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`isSticky failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -606,7 +608,7 @@ abortCommonEvent(callback: AsyncCallback\<void>): void
```
ts
//取消当前有序公共事件的回调
function
abortCB
(
err
)
{
function
abortCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`abortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -635,7 +637,7 @@ abortCommonEvent(): Promise\<void>
```
ts
subscriber
.
abortCommonEvent
().
then
(()
=>
{
console
.
info
(
"
abortCommonEvent
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`abortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -672,7 +674,7 @@ clearAbortCommonEvent(callback: AsyncCallback\<void>): void
```
ts
//清除当前公共事件取消状态的回调
function
clearAbortCB
(
err
)
{
function
clearAbortCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`clearAbortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -701,7 +703,7 @@ clearAbortCommonEvent(): Promise\<void>
```
ts
subscriber
.
clearAbortCommonEvent
().
then
(()
=>
{
console
.
info
(
"
clearAbortCommonEvent
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`clearAbortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -738,7 +740,7 @@ getAbortCommonEvent(callback: AsyncCallback\<boolean>): void
```
ts
//获取当前有序公共事件是否取消的回调
function
getAbortCB
(
err
,
abortEvent
)
{
function
getAbortCB
(
err
:
Base
.
BusinessError
,
abortEvent
:
boolean
)
{
if
(
err
.
code
)
{
console
.
error
(
`getAbortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -765,9 +767,9 @@ getAbortCommonEvent(): Promise\<boolean>
**示例:**
```
ts
subscriber
.
getAbortCommonEvent
().
then
((
abortEvent
)
=>
{
subscriber
.
getAbortCommonEvent
().
then
((
abortEvent
:
boolean
)
=>
{
console
.
info
(
"
abortCommonEvent
"
+
JSON
.
stringify
(
abortEvent
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`getAbortCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -811,7 +813,7 @@ getSubscribeInfo(callback: AsyncCallback\<CommonEventSubscribeInfo>): void
```
ts
//获取订阅者信息回调
function
getCB
(
err
,
s
ubscribeInfo
)
{
function
getCB
(
err
:
Base
.
BusinessError
,
subscribeInfo
:
CommonEventManager
.
CommonEventS
ubscribeInfo
)
{
if
(
err
.
code
)
{
console
.
error
(
`getSubscribeInfo failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -838,9 +840,9 @@ getSubscribeInfo(): Promise\<CommonEventSubscribeInfo>
**示例:**
```
ts
subscriber
.
getSubscribeInfo
().
then
((
subscribeInfo
)
=>
{
subscriber
.
getSubscribeInfo
().
then
((
subscribeInfo
:
CommonEventManager
.
CommonEventSubscribeInfo
)
=>
{
console
.
info
(
"
subscribeInfo
"
+
JSON
.
stringify
(
subscribeInfo
));
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`getSubscribeInfo failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
...
...
@@ -884,7 +886,7 @@ finishCommonEvent(callback: AsyncCallback\<void>): void
```
ts
//结束当前有序公共事件的回调
function
finishCB
(
err
)
{
function
finishCB
(
err
:
Base
.
BusinessError
)
{
if
(
err
.
code
)
{
console
.
error
(
`finishCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
else
{
...
...
@@ -914,7 +916,7 @@ finishCommonEvent(): Promise\<void>
```
ts
subscriber
.
finishCommonEvent
().
then
(()
=>
{
console
.
info
(
"
FinishCommonEvent
"
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
Base
.
BusinessError
)
=>
{
console
.
error
(
`finishCommonEvent failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录