Showing
1 changed file
with
22 additions
and
11 deletions
| 1 | import emitter from '@ohos.events.emitter'; | 1 | import emitter from '@ohos.events.emitter'; |
| 2 | -import HashMap from '@ohos.util.HashMap'; | ||
| 3 | 2 | ||
| 4 | const TAG: string = 'EmitterUtils'; | 3 | const TAG: string = 'EmitterUtils'; |
| 5 | 4 | ||
| 6 | /** | 5 | /** |
| 7 | * 线程间通信简单工具 | 6 | * 线程间通信简单工具 |
| 8 | - * TODO 待优化 | ||
| 9 | */ | 7 | */ |
| 10 | export class EmitterUtils { | 8 | export class EmitterUtils { |
| 11 | /** | 9 | /** |
| @@ -23,15 +21,17 @@ export class EmitterUtils { | @@ -23,15 +21,17 @@ export class EmitterUtils { | ||
| 23 | /** | 21 | /** |
| 24 | * 发送消息 | 22 | * 发送消息 |
| 25 | * @param eventId 事件id | 23 | * @param eventId 事件id |
| 26 | - * @param data 数据 | 24 | + * @param str 字符串数据 |
| 27 | */ | 25 | */ |
| 28 | - static sendEvent(eventId: number, data: { [key: string]: any; }) { | 26 | + static sendEvent(eventId: number, str?: string) { |
| 29 | let event: emitter.InnerEvent = { | 27 | let event: emitter.InnerEvent = { |
| 30 | eventId: eventId, | 28 | eventId: eventId, |
| 31 | priority: emitter.EventPriority.LOW | 29 | priority: emitter.EventPriority.LOW |
| 32 | }; | 30 | }; |
| 33 | let eventData: emitter.EventData = { | 31 | let eventData: emitter.EventData = { |
| 34 | - data: data | 32 | + data: { |
| 33 | + jsonStr: str | ||
| 34 | + } | ||
| 35 | }; | 35 | }; |
| 36 | emitter.emit(event, eventData); | 36 | emitter.emit(event, eventData); |
| 37 | } | 37 | } |
| @@ -41,18 +41,29 @@ export class EmitterUtils { | @@ -41,18 +41,29 @@ export class EmitterUtils { | ||
| 41 | * @param eventId 事件id | 41 | * @param eventId 事件id |
| 42 | * @param callback 回调函数 | 42 | * @param callback 回调函数 |
| 43 | */ | 43 | */ |
| 44 | - static receiveEvent(eventId: number, callback: (data?: { [key: string]: any; }) => void) { | 44 | + static receiveEvent(eventId: number, callback: (str?: string) => void) { |
| 45 | let event: emitter.InnerEvent = { | 45 | let event: emitter.InnerEvent = { |
| 46 | eventId: eventId | 46 | eventId: eventId |
| 47 | }; | 47 | }; |
| 48 | 48 | ||
| 49 | - // 收到eventId为1的事件后执行该回调 | ||
| 50 | - let callback1 = (eventData: emitter.EventData): void => { | ||
| 51 | - callback(eventData.data) | 49 | + // 收到eventId事件后执行该回调 |
| 50 | + let callback1 = (eventData?: emitter.EventData): void => { | ||
| 51 | + if (eventData && eventData.data) { | ||
| 52 | + try { | ||
| 53 | + let jsonObject: EmitterBean = JSON.parse(JSON.stringify(eventData.data)) | ||
| 54 | + callback(jsonObject.jsonStr) | ||
| 55 | + } catch (err) { | ||
| 56 | + callback() | ||
| 57 | + } | ||
| 58 | + } else { | ||
| 59 | + callback() | ||
| 60 | + } | ||
| 52 | }; | 61 | }; |
| 53 | - | ||
| 54 | - // 订阅eventId为1的事件 | 62 | + // 订阅eventId事件 |
| 55 | emitter.on(event, callback1); | 63 | emitter.on(event, callback1); |
| 56 | } | 64 | } |
| 57 | } | 65 | } |
| 58 | 66 | ||
| 67 | +interface EmitterBean { | ||
| 68 | + jsonStr: string | ||
| 69 | +} |
-
Please register or login to post a comment