• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @file
18 * @kit BasicServicesKit
19 */
20
21import { Callback } from './@ohos.base';
22
23/**
24 * Provides methods for sending and processing in-process events.
25 *
26 * @namespace emitter
27 * @syscap SystemCapability.Notification.Emitter
28 * @since 7
29 */
30/**
31 * Provides methods for sending and processing in-process events.
32 *
33 * @namespace emitter
34 * @syscap SystemCapability.Notification.Emitter
35 * @atomicservice
36 * @since 11
37 */
38/**
39 * Provides methods for sending and processing in-process events.
40 *
41 * @namespace emitter
42 * @syscap SystemCapability.Notification.Emitter
43 * @crossplatform
44 * @atomicservice
45 * @since 12
46 */
47declare namespace emitter {
48  /**
49   * Subscribe to a certain event in persistent manner and receives the event callback.
50   *
51   * @param { InnerEvent } event - indicate event to subscribe to.
52   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
53   * @syscap SystemCapability.Notification.Emitter
54   * @since 7
55   */
56  /**
57   * Subscribe to a certain event in persistent manner and receives the event callback.
58   *
59   * @param { InnerEvent } event - indicate event to subscribe to.
60   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
61   * @syscap SystemCapability.Notification.Emitter
62   * @atomicservice
63   * @since 11
64   */
65  /**
66   * Subscribe to a certain event in persistent manner and receives the event callback.
67   *
68   * @param { InnerEvent } event - indicate event to subscribe to.
69   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
70   * @syscap SystemCapability.Notification.Emitter
71   * @crossplatform
72   * @atomicservice
73   * @since 12
74   */
75  function on(event: InnerEvent, callback: Callback<EventData>): void;
76
77  /**
78   * Subscribe to a event by specific id in persistent manner and receives the event callback.
79   *
80   * @param { string } eventId - indicate ID of the event to subscribe to.
81   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
82   * @syscap SystemCapability.Notification.Emitter
83   * @atomicservice
84   * @since 11
85   */
86  /**
87   * Subscribe to a event by specific id in persistent manner and receives the event callback.
88   *
89   * @param { string } eventId - indicate ID of the event to subscribe to.
90   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
91   * @syscap SystemCapability.Notification.Emitter
92   * @crossplatform
93   * @atomicservice
94   * @since 12
95   */
96  function on(eventId: string, callback: Callback<EventData>): void;
97
98  /**
99   * Subscribe to a event by specific id in persistent manner and receives the event callback.
100   *
101   * @param { string } eventId - indicate ID of the event to subscribe to.
102   * @param { Callback<GenericEventData<T>> } callback - indicate callback used to receive the event.
103   * @syscap SystemCapability.Notification.Emitter
104   * @crossplatform
105   * @atomicservice
106   * @since 12
107   */
108  function on<T>(eventId: string, callback: Callback<GenericEventData<T>>): void;
109
110  /**
111   * Subscribe to a certain event in one-shot manner and unsubscribe from it
112   * after the event callback is received.
113   *
114   * @param { InnerEvent } event - indicate event to subscribe to in one shot.
115   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
116   * @syscap SystemCapability.Notification.Emitter
117   * @since 7
118   */
119  /**
120   * Subscribe to a certain event in one-shot manner and unsubscribe from it
121   * after the event callback is received.
122   *
123   * @param { InnerEvent } event - indicate event to subscribe to in one shot.
124   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
125   * @syscap SystemCapability.Notification.Emitter
126   * @atomicservice
127   * @since 11
128   */
129  /**
130   * Subscribe to a certain event in one-shot manner and unsubscribe from it
131   * after the event callback is received.
132   *
133   * @param { InnerEvent } event - indicate event to subscribe to in one shot.
134   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
135   * @syscap SystemCapability.Notification.Emitter
136   * @crossplatform
137   * @atomicservice
138   * @since 12
139   */
140  function once(event: InnerEvent, callback: Callback<EventData>): void;
141
142  /**
143   * Subscribe to a event by specific id in one-shot manner and unsubscribe from it
144   * after the event callback is received.
145   *
146   * @param { string } eventId - indicate ID of the event to subscribe to in one shot.
147   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
148   * @syscap SystemCapability.Notification.Emitter
149   * @atomicservice
150   * @since 11
151   */
152  /**
153   * Subscribe to a event by specific id in one-shot manner and unsubscribe from it
154   * after the event callback is received.
155   *
156   * @param { string } eventId - indicate ID of the event to subscribe to in one shot.
157   * @param { Callback<EventData> } callback - indicate callback used to receive the event.
158   * @syscap SystemCapability.Notification.Emitter
159   * @crossplatform
160   * @atomicservice
161   * @since 12
162   */
163  function once(eventId: string, callback: Callback<EventData>): void;
164
165  /**
166   * Subscribe to a event by specific id in one-shot manner and unsubscribe from it
167   * after the event callback is received.
168   *
169   * @param { string } eventId - indicate ID of the event to subscribe to in one shot.
170   * @param { Callback<GenericEventData<T>> } callback - indicate callback used to receive the event.
171   * @syscap SystemCapability.Notification.Emitter
172   * @crossplatform
173   * @atomicservice
174   * @since 12
175   */
176  function once<T>(eventId: string, callback: Callback<GenericEventData<T>>): void;
177
178  /**
179   * Unsubscribe from an event.
180   *
181   * @param { number } eventId - indicate ID of the event to unsubscribe from.
182   * @syscap SystemCapability.Notification.Emitter
183   * @since 7
184   */
185  /**
186   * Unsubscribe from an event.
187   *
188   * @param { number } eventId - indicate ID of the event to unsubscribe from.
189   * @syscap SystemCapability.Notification.Emitter
190   * @atomicservice
191   * @since 11
192   */
193  /**
194   * Unsubscribe from an event.
195   *
196   * @param { number } eventId - indicate ID of the event to unsubscribe from.
197   * @syscap SystemCapability.Notification.Emitter
198   * @crossplatform
199   * @atomicservice
200   * @since 12
201   */
202  function off(eventId: number): void;
203
204  /**
205   * Unsubscribe from an event.
206   *
207   * @param { string } eventId - indicate ID of the event to unsubscribe from.
208   * @syscap SystemCapability.Notification.Emitter
209   * @atomicservice
210   * @since 11
211   */
212  /**
213   * Unsubscribe from an event.
214   *
215   * @param { string } eventId - indicate ID of the event to unsubscribe from.
216   * @syscap SystemCapability.Notification.Emitter
217   * @crossplatform
218   * @atomicservice
219   * @since 12
220   */
221  function off(eventId: string): void;
222
223  /**
224   * Unsubscribe from an event.
225   *
226   * @param { number } eventId - indicates ID of the event to unsubscribe from.
227   * @param { Callback<EventData> } callback - indicates callback used to receive the event.
228   * @syscap SystemCapability.Notification.Emitter
229   * @since 10
230   */
231  /**
232   * Unsubscribe from an event.
233   *
234   * @param { number } eventId - indicates ID of the event to unsubscribe from.
235   * @param { Callback<EventData> } callback - indicates callback used to receive the event.
236   * @syscap SystemCapability.Notification.Emitter
237   * @atomicservice
238   * @since 11
239   */
240  /**
241   * Unsubscribe specified callback function  from an event.
242   *
243   * @param { number } eventId - indicates ID of the event to unsubscribe from.
244   * @param { Callback<EventData> } callback - indicates callback used to receive the event.
245   * @syscap SystemCapability.Notification.Emitter
246   * @crossplatform
247   * @atomicservice
248   * @since 12
249   */
250  function off(eventId: number, callback: Callback<EventData>): void;
251
252  /**
253   * Unsubscribe from an event.
254   *
255   * @param { string } eventId - indicates ID of the event to unsubscribe from.
256   * @param { Callback<EventData> } callback - indicates callback used to receive the event.
257   * @syscap SystemCapability.Notification.Emitter
258   * @atomicservice
259   * @since 11
260   */
261  /**
262   * Unsubscribe specified callback function  from an event.
263   *
264   * @param { string } eventId - indicates ID of the event to unsubscribe from.
265   * @param { Callback<EventData> } callback - indicates callback used to receive the event.
266   * @syscap SystemCapability.Notification.Emitter
267   * @crossplatform
268   * @atomicservice
269   * @since 12
270   */
271  function off(eventId: string, callback: Callback<EventData>): void;
272
273  /**
274   * Unsubscribe specified callback function  from an event.
275   *
276   * @param { string } eventId - indicates ID of the event to unsubscribe from.
277   * @param { Callback<GenericEventData<T>> } callback - indicates callback used to receive the event.
278   * @syscap SystemCapability.Notification.Emitter
279   * @crossplatform
280   * @atomicservice
281   * @since 12
282   */
283  function off<T>(eventId: string, callback: Callback<GenericEventData<T>>): void;
284
285  /**
286   * Emits an event to the event queue.
287   *
288   * @param { InnerEvent } event - indicate event to emit.
289   * @param { EventData } [data] - indicate data carried by the event.
290   * @syscap SystemCapability.Notification.Emitter
291   * @since 7
292   */
293  /**
294   * Emits an event to the event queue.
295   *
296   * @param { InnerEvent } event - indicate event to emit.
297   * @param { EventData } [data] - indicate data carried by the event.
298   * @syscap SystemCapability.Notification.Emitter
299   * @atomicservice
300   * @since 11
301   */
302  /**
303   * Emits an event to the event queue.
304   *
305   * @param { InnerEvent } event - indicate event to emit.
306   * @param { EventData } [data] - indicate data carried by the event.
307   * @syscap SystemCapability.Notification.Emitter
308   * @crossplatform
309   * @atomicservice
310   * @since 12
311   */
312  function emit(event: InnerEvent, data?: EventData): void;
313
314  /**
315   * Emits an event by specific id to the event queue.
316   *
317   * @param { string } eventId - indicate ID of the event to emit.
318   * @param { EventData } [data] - indicate data carried by the event.
319   * @syscap SystemCapability.Notification.Emitter
320   * @atomicservice
321   * @since 11
322   */
323  /**
324   * Emits an event by specific id to the event queue.
325   *
326   * @param { string } eventId - indicate ID of the event to emit.
327   * @param { EventData } [data] - indicate data carried by the event.
328   * @syscap SystemCapability.Notification.Emitter
329   * @crossplatform
330   * @atomicservice
331   * @since 12
332   */
333  function emit(eventId: string, data?: EventData): void;
334
335  /**
336   * Emits an event by specific id to the event queue.
337   *
338   * @param { string } eventId - indicate ID of the event to emit.
339   * @param { GenericEventData<T> } [data] - indicate data carried by the event.
340   * @syscap SystemCapability.Notification.Emitter
341   * @crossplatform
342   * @atomicservice
343   * @since 12
344   */
345  function emit<T>(eventId: string, data?: GenericEventData<T>): void;
346
347  /**
348   * Emits an event by specific id to the event queue.
349   *
350   * @param { string } eventId - indicate ID of the event to emit.
351   * @param { Options } options - Indicates the {@link Options} option of the emit priority of the event.
352   * @param { EventData } [data] - indicate data carried by the event.
353   * @syscap SystemCapability.Notification.Emitter
354   * @atomicservice
355   * @since 11
356   */
357  /**
358   * Emits an event by specific id to the event queue.
359   *
360   * @param { string } eventId - indicate ID of the event to emit.
361   * @param { Options } options - Indicates the {@link Options} option of the emit priority of the event.
362   * @param { EventData } [data] - indicate data carried by the event.
363   * @syscap SystemCapability.Notification.Emitter
364   * @crossplatform
365   * @atomicservice
366   * @since 12
367   */
368  function emit(eventId: string, options: Options, data?: EventData): void;
369
370  /**
371   * Emits an event by specific id to the event queue.
372   *
373   * @param { string } eventId - indicate ID of the event to emit.
374   * @param { Options } options - Indicates the {@link Options} option of the emit priority of the event.
375   * @param { GenericEventData<T> } [data] - indicate data carried by the event.
376   * @syscap SystemCapability.Notification.Emitter
377   * @crossplatform
378   * @atomicservice
379   * @since 12
380   */
381  function emit<T>(eventId: string, options: Options, data?: GenericEventData<T>): void;
382
383  /**
384   * Obtains the number of subscribe listener count.
385   *
386   * @param { number | string } eventId - indicates ID of the event to unsubscribe from.
387   * @returns { number } Returns the number of listener count.
388   * @syscap SystemCapability.Notification.Emitter
389   * @atomicservice
390   * @since 11
391   */
392  /**
393   * Obtains the number of subscribe listener count.
394   *
395   * @param { number | string } eventId - indicates ID of the event to unsubscribe from.
396   * @returns { number } Returns the number of listener count.
397   * @syscap SystemCapability.Notification.Emitter
398   * @crossplatform
399   * @atomicservice
400   * @since 12
401   */
402  function getListenerCount(eventId: number | string): number;
403
404  /**
405   * Describes data passed in the event.
406   *
407   * @typedef EventData
408   * @syscap SystemCapability.Notification.Emitter
409   * @since 7
410   */
411  /**
412   * Describes data passed in the event.
413   *
414   * @typedef EventData
415   * @syscap SystemCapability.Notification.Emitter
416   * @atomicservice
417   * @since 11
418   */
419  /**
420   * Describes data passed in the event.
421   *
422   * @typedef EventData
423   * @syscap SystemCapability.Notification.Emitter
424   * @crossplatform
425   * @atomicservice
426   * @since 12
427   */
428  export interface EventData {
429    /**
430     * Data carried by the event.
431     *
432     * @type { ?object }
433     * @syscap SystemCapability.Notification.Emitter
434     * @since 7
435     */
436    /**
437     * Data carried by the event.
438     *
439     * @type { ?object }
440     * @syscap SystemCapability.Notification.Emitter
441     * @atomicservice
442     * @since 11
443     */
444    /**
445     * Data carried by the event.
446     *
447     * @type { ?object }
448     * @syscap SystemCapability.Notification.Emitter
449     * @crossplatform
450     * @atomicservice
451     * @since 12
452     */
453    data?: { [key: string]: any };
454  }
455
456  /**
457   * Describes an intra-process event.
458   *
459   * @typedef InnerEvent
460   * @syscap SystemCapability.Notification.Emitter
461   * @since 7
462   */
463  /**
464   * Describes an intra-process event.
465   *
466   * @typedef InnerEvent
467   * @syscap SystemCapability.Notification.Emitter
468   * @atomicservice
469   * @since 11
470   */
471  /**
472   * Describes an intra-process event.
473   *
474   * @typedef InnerEvent
475   * @syscap SystemCapability.Notification.Emitter
476   * @crossplatform
477   * @atomicservice
478   * @since 12
479   */
480  export interface InnerEvent {
481    /**
482     * Event ID, which is used to identify an event.
483     *
484     * @type { number }
485     * @syscap SystemCapability.Notification.Emitter
486     * @since 7
487     */
488    /**
489     * Event ID, which is used to identify an event.
490     *
491     * @type { number }
492     * @syscap SystemCapability.Notification.Emitter
493     * @atomicservice
494     * @since 11
495     */
496    /**
497     * Event ID, which is used to identify an event.
498     *
499     * @type { number }
500     * @syscap SystemCapability.Notification.Emitter
501     * @crossplatform
502     * @atomicservice
503     * @since 12
504     */
505    eventId: number;
506
507    /**
508     * Emit priority of the event. The default priority is {@link EventPriority.LOW}.
509     *
510     * @type { ?EventPriority }
511     * @syscap SystemCapability.Notification.Emitter
512     * @since 7
513     */
514    /**
515     * Emit priority of the event. The default priority is {@link EventPriority.LOW}.
516     *
517     * @type { ?EventPriority }
518     * @syscap SystemCapability.Notification.Emitter
519     * @atomicservice
520     * @since 11
521     */
522    /**
523     * Emit priority of the event. The default priority is {@link EventPriority.LOW}.
524     *
525     * @type { ?EventPriority }
526     * @syscap SystemCapability.Notification.Emitter
527     * @crossplatform
528     * @atomicservice
529     * @since 12
530     */
531    priority?: EventPriority;
532  }
533
534  /**
535   * Indicates the emit priority of the event.
536   *
537   * @enum { number }
538   * @syscap SystemCapability.Notification.Emitter
539   * @since 7
540   */
541  /**
542   * Indicates the emit priority of the event.
543   *
544   * @enum { number }
545   * @syscap SystemCapability.Notification.Emitter
546   * @atomicservice
547   * @since 11
548   */
549  /**
550   * Indicates the emit priority of the event.
551   *
552   * @enum { number }
553   * @syscap SystemCapability.Notification.Emitter
554   * @crossplatform
555   * @atomicservice
556   * @since 12
557   */
558  export enum EventPriority {
559    /**
560     * Indicates that the event will be emitted immediately.
561     *
562     * @syscap SystemCapability.Notification.Emitter
563     * @since 7
564     */
565    /**
566     * Indicates that the event will be emitted immediately.
567     *
568     * @syscap SystemCapability.Notification.Emitter
569     * @atomicservice
570     * @since 11
571     */
572    /**
573     * Indicates that the event will be emitted immediately.
574     *
575     * @syscap SystemCapability.Notification.Emitter
576     * @crossplatform
577     * @atomicservice
578     * @since 12
579     */
580    IMMEDIATE = 0,
581
582    /**
583     * Indicates that the event will be emitted before low-priority events.
584     *
585     * @syscap SystemCapability.Notification.Emitter
586     * @since 7
587     */
588    /**
589     * Indicates that the event will be emitted before low-priority events.
590     *
591     * @syscap SystemCapability.Notification.Emitter
592     * @atomicservice
593     * @since 11
594     */
595    /**
596     * Indicates that the event will be emitted before low-priority events.
597     *
598     * @syscap SystemCapability.Notification.Emitter
599     * @crossplatform
600     * @atomicservice
601     * @since 12
602     */
603    HIGH,
604
605    /**
606     * Indicates that the event will be emitted before idle-priority events. By default, an event is in LOW priority.
607     *
608     * @syscap SystemCapability.Notification.Emitter
609     * @since 7
610     */
611    /**
612     * Indicates that the event will be emitted before idle-priority events. By default, an event is in LOW priority.
613     *
614     * @syscap SystemCapability.Notification.Emitter
615     * @atomicservice
616     * @since 11
617     */
618    /**
619     * Indicates that the event will be emitted before idle-priority events. By default, an event is in LOW priority.
620     *
621     * @syscap SystemCapability.Notification.Emitter
622     * @crossplatform
623     * @atomicservice
624     * @since 12
625     */
626    LOW,
627
628    /**
629     * Indicates that the event will be emitted after all the other events.
630     *
631     * @syscap SystemCapability.Notification.Emitter
632     * @since 7
633     */
634    /**
635     * Indicates that the event will be emitted after all the other events.
636     *
637     * @syscap SystemCapability.Notification.Emitter
638     * @atomicservice
639     * @since 11
640     */
641    /**
642     * Indicates that the event will be emitted after all the other events.
643     *
644     * @syscap SystemCapability.Notification.Emitter
645     * @crossplatform
646     * @atomicservice
647     * @since 12
648     */
649    IDLE,
650  }
651
652  /**
653   * Describe the optional arguments of emit operation.
654   *
655   * @typedef Options
656   * @syscap SystemCapability.Notification.Emitter
657   * @since 11
658   */
659  /**
660   * Describe the optional arguments of emit operation.
661   *
662   * @typedef Options
663   * @syscap SystemCapability.Notification.Emitter
664   * @crossplatform
665   * @atomicservice
666   * @since 12
667   */
668  export interface Options {
669    /**
670     * Emit priority of the event. The default priority is {@link EventPriority.LOW}.
671     *
672     * @type { ?EventPriority }
673     * @syscap SystemCapability.Notification.Emitter
674     * @atomicservice
675     * @since 11
676     */
677    /**
678     * Emit priority of the event. The default priority is {@link EventPriority.LOW}.
679     *
680     * @type { ?EventPriority }
681     * @syscap SystemCapability.Notification.Emitter
682     * @crossplatform
683     * @atomicservice
684     * @since 12
685     */
686    priority?: EventPriority;
687  }
688
689  /**
690   * Describes data passed in the event.
691   *
692   * @typedef GenericEventData<T>
693   * @syscap SystemCapability.Notification.Emitter
694   * @crossplatform
695   * @atomicservice
696   * @since 12
697   */
698  export interface GenericEventData<T> {
699    /**
700     * Data carried by the event.
701     *
702     * @type { ?T }
703     * @syscap SystemCapability.Notification.Emitter
704     * @crossplatform
705     * @atomicservice
706     * @since 12
707     */
708    data?: T;
709  }
710}
711
712export default emitter;
713