Merge branch 'main' of http://192.168.1.42/developOne/harmonyPool
Showing
3 changed files
with
71 additions
and
1 deletions
| @@ -38,4 +38,8 @@ export { NumberFormatterUtils } from './src/main/ets/utils/NumberFormatterUtils' | @@ -38,4 +38,8 @@ export { NumberFormatterUtils } from './src/main/ets/utils/NumberFormatterUtils' | ||
| 38 | 38 | ||
| 39 | // export { PermissionUtils } from './src/main/ets/utils/PermissionUtils' | 39 | // export { PermissionUtils } from './src/main/ets/utils/PermissionUtils' |
| 40 | 40 | ||
| 41 | -export { ErrorToastUtils } from './src/main/ets/utils/ErrorToastUtils' | ||
| 41 | +export { ErrorToastUtils } from './src/main/ets/utils/ErrorToastUtils' | ||
| 42 | + | ||
| 43 | +export { EmitterUtils } from './src/main/ets/utils/EmitterUtils' | ||
| 44 | + | ||
| 45 | +export { EmitterEventId } from './src/main/ets/utils/EmitterEventId' |
| 1 | +import emitter from '@ohos.events.emitter'; | ||
| 2 | +import HashMap from '@ohos.util.HashMap'; | ||
| 3 | + | ||
| 4 | +const TAG: string = 'EmitterUtils'; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 线程间通信简单工具 | ||
| 8 | + * TODO 待优化 | ||
| 9 | + */ | ||
| 10 | +export class EmitterUtils { | ||
| 11 | + /** | ||
| 12 | + * 发送空消息 | ||
| 13 | + * @param eventId 事件id | ||
| 14 | + */ | ||
| 15 | + static sendEmptyEvent(eventId: number) { | ||
| 16 | + let event: emitter.InnerEvent = { | ||
| 17 | + eventId: eventId, | ||
| 18 | + priority: emitter.EventPriority.LOW | ||
| 19 | + }; | ||
| 20 | + emitter.emit(event); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * 发送消息 | ||
| 25 | + * @param eventId 事件id | ||
| 26 | + * @param data 数据 | ||
| 27 | + */ | ||
| 28 | + static sendEvent(eventId: number, data: { [key: string]: any; }) { | ||
| 29 | + let event: emitter.InnerEvent = { | ||
| 30 | + eventId: eventId, | ||
| 31 | + priority: emitter.EventPriority.LOW | ||
| 32 | + }; | ||
| 33 | + let eventData: emitter.EventData = { | ||
| 34 | + data: data | ||
| 35 | + }; | ||
| 36 | + emitter.emit(event, eventData); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * 接收消息 | ||
| 41 | + * @param eventId 事件id | ||
| 42 | + * @param callback 回调函数 | ||
| 43 | + */ | ||
| 44 | + static receiveEvent(eventId: number, callback: (data?: { [key: string]: any; }) => void) { | ||
| 45 | + let event: emitter.InnerEvent = { | ||
| 46 | + eventId: eventId | ||
| 47 | + }; | ||
| 48 | + | ||
| 49 | + // 收到eventId为1的事件后执行该回调 | ||
| 50 | + let callback1 = (eventData: emitter.EventData): void => { | ||
| 51 | + callback(eventData.data) | ||
| 52 | + }; | ||
| 53 | + | ||
| 54 | + // 订阅eventId为1的事件 | ||
| 55 | + emitter.on(event, callback1); | ||
| 56 | + } | ||
| 57 | +} | ||
| 58 | + |
-
Please register or login to post a comment