1e41f4b71Sopenharmony_ci# @ohos.events.emitter (Emitter)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **Emitter** module provides the capabilities of sending and processing inter- or intra-thread events in a process. You can use the APIs of this module to subscribe to an event in persistent or one-shot manner, unsubscribe from an event, or emit an event to the event queue.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Modules to Import
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```ts
12e41f4b71Sopenharmony_ciimport { emitter } from '@kit.BasicServicesKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## Required Permissions
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciNone.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## emitter.on
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_cion(event: InnerEvent, callback: Callback\<EventData\>): void
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciSubscribes to an event in persistent manner and executes a callback after the event is received.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**Parameters**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                                                        |
32e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
33e41f4b71Sopenharmony_ci| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in persistent manner. The [EventPriority](#eventpriority) parameter is not required and does not take effect.|
34e41f4b71Sopenharmony_ci| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback to be executed when the event is received.                      |
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci**Example**
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci```ts
39e41f4b71Sopenharmony_cilet innerEvent: emitter.InnerEvent = {
40e41f4b71Sopenharmony_ci  eventId: 1
41e41f4b71Sopenharmony_ci};
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci// Execute the callback after receiving the event whose eventId is 1.
44e41f4b71Sopenharmony_ciemitter.on(innerEvent, () => {
45e41f4b71Sopenharmony_ci  console.info('callback');
46e41f4b71Sopenharmony_ci});
47e41f4b71Sopenharmony_ci```
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci## emitter.on<sup>11+</sup>
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_cion(eventId: string, callback:  Callback\<EventData\>): void
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciSubscribes to an event in persistent manner and executes a callback after the event is received.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**Parameters**
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                                  |
62e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------------------- |
63e41f4b71Sopenharmony_ci| eventId    | string                              | Yes  | Event to subscribe to in persistent manner. The value cannot be an empty string and exceed 10240 bytes.                      |
64e41f4b71Sopenharmony_ci| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback to be executed when the event is received.|
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci**Example**
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci```ts
69e41f4b71Sopenharmony_ci// Execute the callback after receiving the event whose eventId is eventId.
70e41f4b71Sopenharmony_ciemitter.on("eventId", () => {
71e41f4b71Sopenharmony_ci  console.info('callback');
72e41f4b71Sopenharmony_ci});
73e41f4b71Sopenharmony_ci```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci## emitter.on<sup>12+</sup>
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_cion<T\>(eventId: string, callback:  Callback\<GenericEventData<T\>\>): void
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ciSubscribes to an event in persistent manner and executes a callback after the event is received.
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci**Parameters**
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                                  |
88e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------------------- |
89e41f4b71Sopenharmony_ci| eventId    | string                              | Yes  | Event to subscribe to in persistent manner. The value cannot be an empty string and exceed 10240 bytes.                      |
90e41f4b71Sopenharmony_ci| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes  | Callback to be executed when the event is received.|
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci**Example**
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci```ts
95e41f4b71Sopenharmony_ci@Sendable
96e41f4b71Sopenharmony_ciclass Sample {
97e41f4b71Sopenharmony_ci    constructor() {
98e41f4b71Sopenharmony_ci        this.count = 100;
99e41f4b71Sopenharmony_ci    }
100e41f4b71Sopenharmony_ci    printCount() {
101e41f4b71Sopenharmony_ci        console.info('Print count : ' + this.count);
102e41f4b71Sopenharmony_ci    }
103e41f4b71Sopenharmony_ci    count: number;
104e41f4b71Sopenharmony_ci}
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_cilet callback = (eventData: emitter.GenericEventData<Sample>): void => {
107e41f4b71Sopenharmony_ci   let storage: Sample = eventData.data!;
108e41f4b71Sopenharmony_ci   storage.printCount();
109e41f4b71Sopenharmony_ci}
110e41f4b71Sopenharmony_ci// Execute the callback after receiving the event whose eventId is eventId.
111e41f4b71Sopenharmony_ciemitter.on("eventId", callback);
112e41f4b71Sopenharmony_ci```
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci## emitter.once
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_cionce(event: InnerEvent, callback: Callback\<EventData\>): void
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ciSubscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed.
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci**Parameters**
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                                                        |
127e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
128e41f4b71Sopenharmony_ci| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in one-shot manner. The [EventPriority](#eventpriority) parameter is not required and does not take effect.|
129e41f4b71Sopenharmony_ci| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback to be executed when the event is received.                      |
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**Example**
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci```ts
134e41f4b71Sopenharmony_cilet innerEvent: emitter.InnerEvent = {
135e41f4b71Sopenharmony_ci    eventId: 1
136e41f4b71Sopenharmony_ci};
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci// Execute the callback after receiving the event whose eventId is 1.
139e41f4b71Sopenharmony_ciemitter.once(innerEvent, () => {
140e41f4b71Sopenharmony_ci    console.info('once callback');
141e41f4b71Sopenharmony_ci});
142e41f4b71Sopenharmony_ci```
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci## emitter.once<sup>11+</sup>
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_cionce(eventId: string, callback: Callback\<EventData\>): void
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ciSubscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed.
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci**Parameters**
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                                  |
157e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------------------- |
158e41f4b71Sopenharmony_ci| eventId    | string                              | Yes  | Event to subscribe to in one-shot manner. The value cannot be an empty string and exceed 10240 bytes.                      |
159e41f4b71Sopenharmony_ci| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback to be executed when the event is received.|
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci**Example**
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci```ts
164e41f4b71Sopenharmony_ci// Execute the callback after receiving the event whose eventId is eventId.
165e41f4b71Sopenharmony_ciemitter.once("eventId", () => {
166e41f4b71Sopenharmony_ci    console.info('once callback');
167e41f4b71Sopenharmony_ci});
168e41f4b71Sopenharmony_ci```
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci## emitter.once<sup>12+</sup>
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_cionce<T\>(eventId: string, callback: Callback\<GenericEventData<T\>\>): void
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ciSubscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed.
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**Parameters**
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                                  |
183e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------------------- |
184e41f4b71Sopenharmony_ci| eventId    | string                              | Yes  | Event to subscribe to in one-shot manner. The value cannot be an empty string and exceed 10240 bytes.                      |
185e41f4b71Sopenharmony_ci| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes  | Callback to be executed when the event is received.|
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci**Example**
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci```ts
190e41f4b71Sopenharmony_ci@Sendable
191e41f4b71Sopenharmony_ciclass Sample {
192e41f4b71Sopenharmony_ci    constructor() {
193e41f4b71Sopenharmony_ci        this.count = 100;
194e41f4b71Sopenharmony_ci    }
195e41f4b71Sopenharmony_ci    printCount() {
196e41f4b71Sopenharmony_ci        console.info('Print count : ' + this.count);
197e41f4b71Sopenharmony_ci    }
198e41f4b71Sopenharmony_ci    count: number;
199e41f4b71Sopenharmony_ci}
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_cilet callback = (eventData: emitter.GenericEventData<Sample>): void => {
202e41f4b71Sopenharmony_ci   let storage: Sample = eventData.data!;
203e41f4b71Sopenharmony_ci   storage.printCount();
204e41f4b71Sopenharmony_ci}
205e41f4b71Sopenharmony_ci// Execute the callback after receiving the event whose eventId is eventId.
206e41f4b71Sopenharmony_ciemitter.once("eventId", callback);
207e41f4b71Sopenharmony_ci```
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci## emitter.off
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_cioff(eventId: number): void
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ciUnsubscribes from an event.
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ci**Parameters**
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci| Name | Type  | Mandatory| Description    |
222e41f4b71Sopenharmony_ci| ------- | ------ | ---- | -------- |
223e41f4b71Sopenharmony_ci| eventId | number | Yes  | Event ID.|
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci**Example**
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci```ts
228e41f4b71Sopenharmony_ci// Unregister the callbacks of all events whose eventID is 1.
229e41f4b71Sopenharmony_ciemitter.off(1);
230e41f4b71Sopenharmony_ci```
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci## emitter.off<sup>11+</sup>
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_cioff(eventId: string): void
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ciUnsubscribes from an event.
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci**Parameters**
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci| Name | Type  | Mandatory| Description    |
245e41f4b71Sopenharmony_ci| ------- | ------ | ---- | -------- |
246e41f4b71Sopenharmony_ci| eventId | string | Yes  | Event ID. The value cannot be an empty string and exceed 10240 bytes.|
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci**Example**
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci```ts
251e41f4b71Sopenharmony_ci// Unregister the callbacks of all events whose eventID is **eventId**.
252e41f4b71Sopenharmony_ciemitter.off("eventId");
253e41f4b71Sopenharmony_ci```
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci## emitter.off<sup>10+</sup>
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_cioff(eventId: number, callback: Callback\<EventData\>): void
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ciUnsubscribes from an event. If the specified callback has been registered through the **on** or **once** API, it is unregistered. Otherwise, no processing is performed.
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci**Parameters**
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci| Name | Type  | Mandatory| Description  |
268e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------ |
269e41f4b71Sopenharmony_ci| eventId | number | Yes  | Event ID.|
270e41f4b71Sopenharmony_ci| callback | Callback\<[EventData](#eventdata)\> | Yes  |Callback to unregister.  |
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci**Example**
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci```ts
275e41f4b71Sopenharmony_ci// Unregister the emitterCallback callback for the event whose eventID is 1.
276e41f4b71Sopenharmony_ci// If the callback has not been registered, no processing is performed.
277e41f4b71Sopenharmony_ciemitter.off(1, () => {
278e41f4b71Sopenharmony_ci  console.info('callback');
279e41f4b71Sopenharmony_ci});
280e41f4b71Sopenharmony_ci```
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci## emitter.off<sup>11+</sup>
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_cioff(eventId: string, callback: Callback\<EventData\>): void
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ciUnsubscribes from an event. If the specified callback has been registered through the **on** or **once** API, it is unregistered. Otherwise, no processing is performed.
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
291e41f4b71Sopenharmony_ci
292e41f4b71Sopenharmony_ci**Parameters**
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                      |
295e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------- |
296e41f4b71Sopenharmony_ci| eventId  | string                              | Yes  | Event ID. The value cannot be an empty string and exceed 10240 bytes.                  |
297e41f4b71Sopenharmony_ci| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback to unregister.|
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci**Example**
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci```ts
302e41f4b71Sopenharmony_ci// Unregister the emitterCallback callback for the event whose eventID is eventId.
303e41f4b71Sopenharmony_ci// If the callback has not been registered, no processing is performed.
304e41f4b71Sopenharmony_ciemitter.off("eventId", () => {
305e41f4b71Sopenharmony_ci  console.info('callback');
306e41f4b71Sopenharmony_ci});
307e41f4b71Sopenharmony_ci```
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ci## emitter.off<sup>12+</sup>
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_cioff<T\>(eventId: string, callback: Callback\<GenericEventData<T\>\>): void
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ciUnsubscribes from an event. If the specified callback has been registered through the **on** or **once** API, it is unregistered. Otherwise, no processing is performed.
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci**Parameters**
320e41f4b71Sopenharmony_ci
321e41f4b71Sopenharmony_ci| Name  | Type                               | Mandatory| Description                      |
322e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------- |
323e41f4b71Sopenharmony_ci| eventId  | string                              | Yes  | Event ID. The value cannot be an empty string and exceed 10240 bytes.                  |
324e41f4b71Sopenharmony_ci| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes  | Callback to unregister.|
325e41f4b71Sopenharmony_ci
326e41f4b71Sopenharmony_ci**Example**
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci```ts
329e41f4b71Sopenharmony_ci@Sendable
330e41f4b71Sopenharmony_ciclass Sample {
331e41f4b71Sopenharmony_ci    constructor() {
332e41f4b71Sopenharmony_ci        this.count = 100;
333e41f4b71Sopenharmony_ci    }
334e41f4b71Sopenharmony_ci    printCount() {
335e41f4b71Sopenharmony_ci        console.info('Print count : ' + this.count);
336e41f4b71Sopenharmony_ci    }
337e41f4b71Sopenharmony_ci    count: number;
338e41f4b71Sopenharmony_ci}
339e41f4b71Sopenharmony_ci
340e41f4b71Sopenharmony_cilet callback = (eventData: emitter.GenericEventData<Sample>): void => {
341e41f4b71Sopenharmony_ci   let storage: Sample = eventData.data!;
342e41f4b71Sopenharmony_ci   storage.printCount();
343e41f4b71Sopenharmony_ci}
344e41f4b71Sopenharmony_ci// Unregister the callback of the event whose eventID is eventId.
345e41f4b71Sopenharmony_ci// If the callback has not been registered, no processing is performed.
346e41f4b71Sopenharmony_ciemitter.off("eventId", callback);
347e41f4b71Sopenharmony_ci```
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_ci## emitter.emit
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ciemit(event: InnerEvent, data?: EventData): void
352e41f4b71Sopenharmony_ci
353e41f4b71Sopenharmony_ciEmits the specified event.
354e41f4b71Sopenharmony_ci
355e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
356e41f4b71Sopenharmony_ci
357e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
358e41f4b71Sopenharmony_ci
359e41f4b71Sopenharmony_ci**Parameters**
360e41f4b71Sopenharmony_ci
361e41f4b71Sopenharmony_ci| Name| Type                     | Mandatory| Description          |
362e41f4b71Sopenharmony_ci| ------ | ------------------------- | ---- | ------------- |
363e41f4b71Sopenharmony_ci| event  | [InnerEvent](#innerevent) | Yes  | Event to emit, where [EventPriority](#eventpriority) specifies the emit priority of the event.|
364e41f4b71Sopenharmony_ci| data   | [EventData](#eventdata)   | No  | Data passed in the event.|
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_ci**Example**
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_ci```ts
369e41f4b71Sopenharmony_cilet eventData: emitter.EventData = {
370e41f4b71Sopenharmony_ci    data: {
371e41f4b71Sopenharmony_ci        "content": "c",
372e41f4b71Sopenharmony_ci        "id": 1,
373e41f4b71Sopenharmony_ci    }
374e41f4b71Sopenharmony_ci};
375e41f4b71Sopenharmony_ci
376e41f4b71Sopenharmony_cilet innerEvent: emitter.InnerEvent = {
377e41f4b71Sopenharmony_ci    eventId: 1,
378e41f4b71Sopenharmony_ci    priority: emitter.EventPriority.HIGH
379e41f4b71Sopenharmony_ci};
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ciemitter.emit(innerEvent, eventData);
382e41f4b71Sopenharmony_ci```
383e41f4b71Sopenharmony_ci
384e41f4b71Sopenharmony_ci## emitter.emit<sup>11+</sup>
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ciemit(eventId: string, data?: EventData): void
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ciEmits the specified event.
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
393e41f4b71Sopenharmony_ci
394e41f4b71Sopenharmony_ci**Parameters**
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_ci| Name | Type                   | Mandatory| Description            |
397e41f4b71Sopenharmony_ci| ------- | ----------------------- | ---- | ---------------- |
398e41f4b71Sopenharmony_ci| eventId | string                  | Yes  | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes.  |
399e41f4b71Sopenharmony_ci| data    | [EventData](#eventdata) | No  | Data passed in the event.|
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci**Example**
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci```ts
404e41f4b71Sopenharmony_cilet eventData: emitter.EventData = {
405e41f4b71Sopenharmony_ci    data: {
406e41f4b71Sopenharmony_ci        "content": "c",
407e41f4b71Sopenharmony_ci        "id": 1,
408e41f4b71Sopenharmony_ci    }
409e41f4b71Sopenharmony_ci};
410e41f4b71Sopenharmony_ci
411e41f4b71Sopenharmony_ciemitter.emit("eventId", eventData);
412e41f4b71Sopenharmony_ci```
413e41f4b71Sopenharmony_ci
414e41f4b71Sopenharmony_ci## emitter.emit<sup>12+</sup>
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ciemit<T\>(eventId: string, data?: GenericEventData<T\>): void
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ciEmits the specified event.
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
421e41f4b71Sopenharmony_ci
422e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci**Parameters**
425e41f4b71Sopenharmony_ci
426e41f4b71Sopenharmony_ci| Name | Type                   | Mandatory| Description            |
427e41f4b71Sopenharmony_ci| ------- | ----------------------- | ---- | ---------------- |
428e41f4b71Sopenharmony_ci| eventId | string                  | Yes  | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes.  |
429e41f4b71Sopenharmony_ci| data    | [GenericEventData<T\>](#genericeventdatat12) | No  | Data passed in the event.|
430e41f4b71Sopenharmony_ci
431e41f4b71Sopenharmony_ci**Example**
432e41f4b71Sopenharmony_ci
433e41f4b71Sopenharmony_ci```ts
434e41f4b71Sopenharmony_ci@Sendable
435e41f4b71Sopenharmony_ciclass Sample {
436e41f4b71Sopenharmony_ci    constructor() {
437e41f4b71Sopenharmony_ci        this.count = 100;
438e41f4b71Sopenharmony_ci    }
439e41f4b71Sopenharmony_ci    printCount() {
440e41f4b71Sopenharmony_ci        console.info('Print count : ' + this.count);
441e41f4b71Sopenharmony_ci    }
442e41f4b71Sopenharmony_ci    count: number;
443e41f4b71Sopenharmony_ci}
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_ciclass SelfEventData implements emitter.EventData {
446e41f4b71Sopenharmony_ci    data: Sample = new Sample();
447e41f4b71Sopenharmony_ci}
448e41f4b71Sopenharmony_ci
449e41f4b71Sopenharmony_cilet eventData = new SelfEventData();
450e41f4b71Sopenharmony_ciemitter.emit("eventId", eventData);
451e41f4b71Sopenharmony_ci```
452e41f4b71Sopenharmony_ci
453e41f4b71Sopenharmony_ci## emitter.emit<sup>11+</sup>
454e41f4b71Sopenharmony_ci
455e41f4b71Sopenharmony_ciemit(eventId: string, options: Options, data?: EventData): void
456e41f4b71Sopenharmony_ci
457e41f4b71Sopenharmony_ciEmits an event of a specified priority.
458e41f4b71Sopenharmony_ci
459e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
460e41f4b71Sopenharmony_ci
461e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
462e41f4b71Sopenharmony_ci
463e41f4b71Sopenharmony_ci**Parameters**
464e41f4b71Sopenharmony_ci
465e41f4b71Sopenharmony_ci| Name | Type                   | Mandatory| Description            |
466e41f4b71Sopenharmony_ci| ------- | ----------------------- | ---- | ---------------- |
467e41f4b71Sopenharmony_ci| eventId | string                  | Yes  | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes.  |
468e41f4b71Sopenharmony_ci| options | [Options](#options11)   | Yes  | Event emit priority.    |
469e41f4b71Sopenharmony_ci| data    | [EventData](#eventdata) | No  | Data passed in the event.|
470e41f4b71Sopenharmony_ci
471e41f4b71Sopenharmony_ci**Example**
472e41f4b71Sopenharmony_ci
473e41f4b71Sopenharmony_ci```ts
474e41f4b71Sopenharmony_cilet eventData: emitter.EventData = {
475e41f4b71Sopenharmony_ci    data: {
476e41f4b71Sopenharmony_ci        "content": "c",
477e41f4b71Sopenharmony_ci        "id": 1,
478e41f4b71Sopenharmony_ci    }
479e41f4b71Sopenharmony_ci};
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_cilet options: emitter.Options = {
482e41f4b71Sopenharmony_ci    priority: emitter.EventPriority.HIGH
483e41f4b71Sopenharmony_ci};
484e41f4b71Sopenharmony_ci
485e41f4b71Sopenharmony_ciemitter.emit("eventId", options, eventData);
486e41f4b71Sopenharmony_ci```
487e41f4b71Sopenharmony_ci
488e41f4b71Sopenharmony_ci## emitter.emit<sup>12+</sup>
489e41f4b71Sopenharmony_ci
490e41f4b71Sopenharmony_ciemit<T\>(eventId: string, options: Options, data?: GenericEventData<T\>): void
491e41f4b71Sopenharmony_ci
492e41f4b71Sopenharmony_ciEmits an event of a specified priority.
493e41f4b71Sopenharmony_ci
494e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
495e41f4b71Sopenharmony_ci
496e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
497e41f4b71Sopenharmony_ci
498e41f4b71Sopenharmony_ci**Parameters**
499e41f4b71Sopenharmony_ci
500e41f4b71Sopenharmony_ci| Name | Type                   | Mandatory| Description            |
501e41f4b71Sopenharmony_ci| ------- | ----------------------- | ---- | ---------------- |
502e41f4b71Sopenharmony_ci| eventId | string                  | Yes  | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes.  |
503e41f4b71Sopenharmony_ci| options | [Options](#options11)   | Yes  | Event emit priority.    |
504e41f4b71Sopenharmony_ci| data    | [GenericEventData<T\>](#genericeventdatat12) | No  | Data passed in the event.|
505e41f4b71Sopenharmony_ci
506e41f4b71Sopenharmony_ci**Example**
507e41f4b71Sopenharmony_ci
508e41f4b71Sopenharmony_ci```ts
509e41f4b71Sopenharmony_ci@Sendable
510e41f4b71Sopenharmony_ciclass Sample {
511e41f4b71Sopenharmony_ci    constructor() {
512e41f4b71Sopenharmony_ci        this.count = 100;
513e41f4b71Sopenharmony_ci    }
514e41f4b71Sopenharmony_ci    printCount() {
515e41f4b71Sopenharmony_ci        console.info('Print count : ' + this.count);
516e41f4b71Sopenharmony_ci    }
517e41f4b71Sopenharmony_ci    count: number;
518e41f4b71Sopenharmony_ci}
519e41f4b71Sopenharmony_ci
520e41f4b71Sopenharmony_ciclass SelfEventData implements emitter.EventData {
521e41f4b71Sopenharmony_ci    data: Sample = new Sample();
522e41f4b71Sopenharmony_ci}
523e41f4b71Sopenharmony_ci
524e41f4b71Sopenharmony_cilet options: emitter.Options = {
525e41f4b71Sopenharmony_ci    priority: emitter.EventPriority.HIGH
526e41f4b71Sopenharmony_ci};
527e41f4b71Sopenharmony_ci
528e41f4b71Sopenharmony_cilet eventData = new SelfEventData();
529e41f4b71Sopenharmony_ciemitter.emit("eventId", options, eventData);
530e41f4b71Sopenharmony_ci```
531e41f4b71Sopenharmony_ci
532e41f4b71Sopenharmony_ci## emitter.getListenerCount<sup>11+</sup>
533e41f4b71Sopenharmony_ci
534e41f4b71Sopenharmony_cigetListenerCount(eventId: number|string): number
535e41f4b71Sopenharmony_ci
536e41f4b71Sopenharmony_ciObtains the number of subscriptions to a specified event.
537e41f4b71Sopenharmony_ci
538e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
539e41f4b71Sopenharmony_ci
540e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
541e41f4b71Sopenharmony_ci
542e41f4b71Sopenharmony_ci**Parameters**
543e41f4b71Sopenharmony_ci
544e41f4b71Sopenharmony_ci| Name | Type          | Mandatory| Description    |
545e41f4b71Sopenharmony_ci| ------- | -------------- | ---- | -------- |
546e41f4b71Sopenharmony_ci| eventId | number\|string | Yes  | Event ID. The value of the string type cannot be an empty string.|
547e41f4b71Sopenharmony_ci
548e41f4b71Sopenharmony_ci**Example**
549e41f4b71Sopenharmony_ci
550e41f4b71Sopenharmony_ci```ts
551e41f4b71Sopenharmony_cilet count = emitter.getListenerCount("eventId");
552e41f4b71Sopenharmony_ci```
553e41f4b71Sopenharmony_ci
554e41f4b71Sopenharmony_ci## EventPriority
555e41f4b71Sopenharmony_ci
556e41f4b71Sopenharmony_ciEnumerates the event emit priority levels.
557e41f4b71Sopenharmony_ci
558e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
559e41f4b71Sopenharmony_ci
560e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
561e41f4b71Sopenharmony_ci
562e41f4b71Sopenharmony_ci| Name     | Value   | Description                                               |
563e41f4b71Sopenharmony_ci| --------- | ---- | --------------------------------------------------- |
564e41f4b71Sopenharmony_ci| IMMEDIATE | 0    | The event will be emitted immediately.                                |
565e41f4b71Sopenharmony_ci| HIGH      | 1    | The event will be emitted before low-priority events.                          |
566e41f4b71Sopenharmony_ci| LOW       | 2    | The event will be emitted before idle-priority events. By default, an event is in LOW priority.    |
567e41f4b71Sopenharmony_ci| IDLE      | 3    | The event will be emitted after all the other events.            |
568e41f4b71Sopenharmony_ci
569e41f4b71Sopenharmony_ci## InnerEvent
570e41f4b71Sopenharmony_ci
571e41f4b71Sopenharmony_ciDescribes an event to subscribe to or emit. The **EventPriority** settings do not take effect under event subscription.
572e41f4b71Sopenharmony_ci
573e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
574e41f4b71Sopenharmony_ci
575e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
576e41f4b71Sopenharmony_ci
577e41f4b71Sopenharmony_ci| Name    | Type                       | Read Only| Optional| Description                                |
578e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ---- | ------------------------------ |
579e41f4b71Sopenharmony_ci| eventId  | number                          | No  | No  | Event ID.|
580e41f4b71Sopenharmony_ci| priority | [EventPriority](#eventpriority) | No  | Yes  | Emit priority of the event.            |
581e41f4b71Sopenharmony_ci
582e41f4b71Sopenharmony_ci## EventData
583e41f4b71Sopenharmony_ci
584e41f4b71Sopenharmony_ciDescribes the data passed in the event.
585e41f4b71Sopenharmony_ci
586e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
587e41f4b71Sopenharmony_ci
588e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
589e41f4b71Sopenharmony_ci
590e41f4b71Sopenharmony_ci| Name| Type          | Read Only| Optional| Description          |
591e41f4b71Sopenharmony_ci| ---- | ------------------ | ---- | ---- | -------------- |
592e41f4b71Sopenharmony_ci| data | { [key: string]: any } | No  | Yes  | Data passed in the event. The value can be in any of the following types: Array, ArrayBuffer, Boolean, DataView, Date, Error, Map, Number, Object, Primitive (except symbol), RegExp, Set, String, and TypedArray. The maximum data size is 16 MB.|
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_ci## Options<sup>11+</sup>
595e41f4b71Sopenharmony_ci
596e41f4b71Sopenharmony_ciDescribes the event emit priority.
597e41f4b71Sopenharmony_ci
598e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
599e41f4b71Sopenharmony_ci
600e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
601e41f4b71Sopenharmony_ci
602e41f4b71Sopenharmony_ci| Name    | Type                           | Read Only| Optional| Description          |
603e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ---- | -------------- |
604e41f4b71Sopenharmony_ci| priority | [EventPriority](#eventpriority) | No  | Yes  | Event priority.|
605e41f4b71Sopenharmony_ci
606e41f4b71Sopenharmony_ci## GenericEventData<T\><sup>12+</sup>
607e41f4b71Sopenharmony_ci
608e41f4b71Sopenharmony_ciDescribes the generic data passed in the event.
609e41f4b71Sopenharmony_ci
610e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
611e41f4b71Sopenharmony_ci
612e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.Emitter
613e41f4b71Sopenharmony_ci
614e41f4b71Sopenharmony_ci| Name    | Type                           | Read Only| Optional| Description          |
615e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ---- | -------------- |
616e41f4b71Sopenharmony_ci| data | T | No  | Yes  | Data passed in the event. **T**: generic type.|
617