harmony 鸿蒙Publishing Common Events
Publishing Common Events
When to Use
You can use publish() to publish a custom common event, which can carry data for subscribers to parse and process.
NOTE Subscribers can receive sticky common events that have been sent. However, they must subscribe to common events of other types before receiving them. For details about subscription, see Subscribing to Common Events.
Available APIs
For details about the APIs, see commonEventManager.publish.
API | Description |
---|---|
publish(event: string, callback: AsyncCallback |
Publishes a common event. |
publish(event: string, options: CommonEventPublishData, callback: AsyncCallback |
Publishes a common event with given attributes. |
Publishing a Common Event That Does Not Carry Information
Common events that do not carry information can be published only as unordered common events.
- Import the commonEventManager module.
import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG: string = 'ProcessModel';
const DOMAIN_NUMBER: number = 0xFF00;
- Pass in the common event name and callback, and publish the event.
// Publish the common event. Replace the event field with the actual event name.
commonEventManager.publish('event', (err: BusinessError) => {
if (err) {
hilog.error(DOMAIN_NUMBER, TAG, `Publish failed, code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err.message)}`);
} else {
//...
hilog.info(DOMAIN_NUMBER, TAG, `Publish success`);
}
});
Publishing a Common Event That Carries Information
Common events that carry information can be published as unordered, ordered, and sticky common events, which are specified by the isOrdered and isSticky fields of CommonEventPublishData.
- Import the commonEventManager module.
import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG: string = 'ProcessModel';
const DOMAIN_NUMBER: number = 0xFF00;
- Create the public event information to publish.
// Attributes of a common event.
let options: commonEventManager.CommonEventPublishData = {
code: 1, // Result code of the common event.
data: 'initial data', // Initial data of the common event.
};
- Pass in the common event name, attributes of the common event, and callback, and publish the event.
// Publish the common event. Replace the event field with the actual event name.
commonEventManager.publish('event', options, (err: BusinessError) => {
if (err) {
hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish common event. Code is ${err.code}, message is ${err.message}`);
} else {
//...
hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in publishing common event.`);
}
});
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Process and Thread Communication
harmony 鸿蒙Introduction to Common Events
harmony 鸿蒙Removing Sticky Common Events (for System Applications Only)
harmony 鸿蒙Subscribing to Common Events in Static Mode (for System Applications Only)
harmony 鸿蒙Common Event Subscription Overview
harmony 鸿蒙Subscribing to Common Events in Dynamic Mode
harmony 鸿蒙Unsubscribing from Common Events in Dynamic Mode
harmony 鸿蒙Using Emitter for Inter-Thread Communication
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦