Emitter provides APIs for sending and processing cross-thread events, including ongoing subscriptions, one-time subscriptions, unsubscriptions, and the dispatch of event queues.
import { emitter } from '@kit.BasicServicesKit';
const TAG: string = 'ThreadModel'; // Define an event with an eventId of 1
let event: emitter.InnerEvent = {
eventId: 1
};
// Execute the callback after receiving an event with eventId 1
let callback = (eventData: emitter.EventData): void => {
this.getUIContext().getPromptAction().showToast({
message: JSON.stringify(eventData)
});
};
// Subscribe to events with eventId 1
emitter.on(event,callback);
this.getUIContext().getPromptAction().showToast({
message: 'subscribe_success'
}); import { emitter } from '@kit.BasicServicesKit'; // Define an event with an eventId of 1 and a priority of Low
let event: emitter.InnerEvent = {
eventId: 1,
priority: emitter.EventPriority.LOW
};
let eventData: emitter.EventData = {
data: {
content: 'c',
id: 1,
isEmpty: false
}
};
// Send an event with eventId 1 and the event content is eventData
emitter.emit(event,eventData); EventHub provides APIs for sending and processing events within the same thread, including event subscription, unsubscription, and triggering. The reference below describes the development procedure by using data synchronization between the UIAbility component and the UI as an example.
Reference