• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Emitter
2
3> Note: The initial APIs of this module are supported since API version 7.
4
5## Modules to Import
6
7```javascript
8import emitter from '@ohos.events.emitter'
9```
10
11## Required Permissions
12
13None
14
15## EventPriority
16
17Enumerates the event emit priority levels.
18
19| Name     | Value  | Description                                             |
20| --------- | ---- | ------------------------------------------------- |
21| IMMEDIATE | 0    | The event will be emitted immediately.<br>**System capability**: SystemCapability.Notification.Emitter                               |
22| HIGH      | 1    | The event will be emitted before low-priority events.<br>**System capability**: SystemCapability.Notification.Emitter                        |
23| LOW       | 2    | The event will be emitted before idle-priority events. By default, an event is in LOW priority.<br>**System capability**: SystemCapability.Notification.Emitter|
24| IDLE      | 3    | The event will be emitted after all the other events.<br>**System capability**: SystemCapability.Notification.Emitter         |
25
26## emitter.on
27
28on(event: [InnerEvent](#innerevent), callback: Callback\<[EventData](#eventdata)\>): void
29
30Subscribes to an event in persistent manner. This API uses a callback to return the event.
31
32**System capability**: SystemCapability.Notification.Emitter
33
34**Parameters**
35
36| Name  | Type                               | Mandatory| Description                    |
37| -------- | ----------------------------------- | ---- | ------------------------ |
38| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in persistent manner.          |
39| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback used to return the event.|
40
41**Example**
42
43```javascript
44var innerEvent = {
45    eventId: 1
46};
47var callback = (eventData) => {
48    console.info('callback');
49};
50emitter.on(innerEvent, callback);
51```
52
53## emitter.once
54
55once(event: [InnerEvent](#innerevent), callback: Callback\<[EventData](#eventdata)\>): void
56
57Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is received.
58
59**System capability**: SystemCapability.Notification.Emitter
60
61**Parameters**
62
63| Name  | Type                               | Mandatory| Description                    |
64| -------- | ----------------------------------- | ---- | ------------------------ |
65| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in one-shot manner.          |
66| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback used to return the event.|
67
68**Example**
69
70```javascript
71var innerEvent = {
72    eventId: 1
73};
74var callback = (eventData) => {
75    console.info('once callback');
76};
77emitter.once(innerEvent, callback);
78```
79
80## emitter.off
81
82off(eventId: number): void
83
84Unsubscribes from an event.
85
86**System capability**: SystemCapability.Notification.Emitter
87
88**Parameters**
89
90| Name | Type  | Mandatory| Description  |
91| ------- | ------ | ---- | ------ |
92| eventId | number | Yes  | Event ID.|
93
94**Example**
95
96```javascript
97emitter.off(1);
98```
99
100## emitter.emit
101
102emit(event: InnerEvent, data?: EventData): void
103
104Emits an event to the event queue.
105
106**System capability**: SystemCapability.Notification.Emitter
107
108**Parameters**
109
110| Name| Type                     | Mandatory| Description          |
111| ------ | ------------------------- | ---- | -------------- |
112| event  | [InnerEvent](#innerevent) | Yes  | Event to emit.    |
113| data   | [EventData](#eventdata)   | No  | Data carried by the event.|
114
115**Example**
116
117```javascript
118var eventData = {
119    data: {
120        "content": "c",
121        "id": 1,
122    }};
123var innerEvent = {
124    eventId: 1,
125    priority: emitter.EventPriority.HIGH
126};
127emitter.emit(innerEvent, eventData);
128```
129
130## InnerEvent
131
132Describes an intra-process event.
133
134| Name    | Type                       | Readable| Writable| Description                              |
135| -------- | ------------------------------- | ---- | ---- | ---------------------------------- |
136| eventId  | number                          | Yes  | Yes  | Event ID, which is used to identify an event.<br>**System capability**: SystemCapability.Notification.Emitter|
137| priority | [EventPriority](#eventpriority) | Yes  | Yes  | Emit priority of the event.<br>**System capability**: SystemCapability.Notification.Emitter                |
138
139## EventData
140
141Describes the data passed in the event.
142
143| Name| Type          | Readable| Writable| Description          |
144| ---- | ------------------ | ---- | ---- | -------------- |
145| data | [key: string]: any | Yes  | Yes  | Data carried by the event. The data type can be String, Integer, or Boolean.<br>**System capability**: SystemCapability.Notification.Emitter|
146