Toggle navigation
Toggle navigation
This project
Loading...
Sign in
developOne
/
harmonyPool
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
zhangbo1_wd
2024-04-15 18:39:44 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
ac4aa31280126c65963f57ee66c10a6c6ba6189c
ac4aa312
1 parent
b093a586
优化通信工具
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
11 deletions
sight_harmony/commons/wdKit/src/main/ets/utils/EmitterUtils.ts → sight_harmony/commons/wdKit/src/main/ets/utils/EmitterUtils.ets
sight_harmony/commons/wdKit/src/main/ets/utils/EmitterUtils.ts → sight_harmony/commons/wdKit/src/main/ets/utils/EmitterUtils.
e
ts
View file @
ac4aa31
import emitter from '@ohos.events.emitter';
import
HashMap
from
'@ohos.util.HashMap'
;
const TAG: string = 'EmitterUtils';
/**
* 线程间通信简单工具
* TODO 待优化
*/
export class EmitterUtils {
/**
...
...
@@ -23,15 +21,17 @@ export class EmitterUtils {
/**
* 发送消息
* @param eventId 事件id
* @param
data
数据
* @param
str 字符串
数据
*/
static
sendEvent
(
eventId
:
number
,
data
:
{
[
key
:
string
]
:
any
;
}
)
{
static sendEvent(eventId: number,
str?: string
) {
let event: emitter.InnerEvent = {
eventId: eventId,
priority: emitter.EventPriority.LOW
};
let eventData: emitter.EventData = {
data
:
data
data: {
jsonStr: str
}
};
emitter.emit(event, eventData);
}
...
...
@@ -41,18 +41,29 @@ export class EmitterUtils {
* @param eventId 事件id
* @param callback 回调函数
*/
static
receiveEvent
(
eventId
:
number
,
callback
:
(
data
?:
{
[
key
:
string
]:
any
;
}
)
=>
void
)
{
static receiveEvent(eventId: number, callback: (
str?: string
) => void) {
let event: emitter.InnerEvent = {
eventId: eventId
};
// 收到eventId为1的事件后执行该回调
let
callback1
=
(
eventData
:
emitter
.
EventData
):
void
=>
{
callback
(
eventData
.
data
)
// 收到eventId事件后执行该回调
let callback1 = (eventData?: emitter.EventData): void => {
if (eventData && eventData.data) {
try {
let jsonObject: EmitterBean = JSON.parse(JSON.stringify(eventData.data))
callback(jsonObject.jsonStr)
} catch (err) {
callback()
}
} else {
callback()
}
};
// 订阅eventId为1的事件
// 订阅eventId事件
emitter.on(event, callback1);
}
}
interface EmitterBean {
jsonStr: string
}
...
...
Please
register
or
login
to post a comment