• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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
16import { Callback } from './basic';
17
18/**
19 * Provides methods for sending and processing in-process events.
20 *
21 * @since 7
22 * @syscap SystemCapability.Notification.Emitter
23 * @import import events_emitter from '@ohos.emitter';
24 * @permission N/A
25 */
26declare namespace emitter {
27  /**
28   * Subscribe to a certain event in persistent manner and receives the event callback.
29   *
30   * @since 7
31   * @param event indicate event to subscribe to.
32   * @param callback indicate callback used to receive the event.
33   * @returns -
34   */
35  function on(event: InnerEvent, callback: Callback<EventData>): void;
36
37  /**
38   * Subscribe to a certain event in one-shot manner and unsubscribe from it
39   * after the event callback is received.
40   *
41   * @since 7
42   * @param event indicate event to subscribe to in one shot.
43   * @param callback indicate callback used to receive the event.
44   * @returns -
45   */
46  function once(event: InnerEvent, callback: Callback<EventData>): void;
47
48  /**
49   * Unsubscribe from an event.
50   *
51   * @since 7
52   * @param eventId indicate ID of the event to unsubscribe from.
53   * @returns -
54   */
55  function off(eventId: number): void;
56
57  /**
58   * Emits an event to the event queue.
59   *
60   * @since 7
61   * @param event indicate event to emit.
62   * @param data indicate data carried by the event.
63   * @returns -
64   */
65  function emit(event: InnerEvent, data?: EventData): void;
66
67  /**
68   * Describes data passed in the event.
69   */
70  export interface EventData {
71    /**
72     * Data carried by the event.
73     */
74    data?: {[key: string]: any};
75  }
76
77  /**
78   * Describes an intra-process event.
79   */
80  export interface InnerEvent {
81    /**
82     * Event ID, which is used to identify an event.
83     */
84    eventId: number;
85
86    /**
87     * Emit priority of the event. The default priority is {@link EventPriority.LOW}.
88     */
89    priority?: EventPriority;
90  }
91
92  /**
93   * Indicates the emit priority of the event.
94   */
95  export enum EventPriority {
96    /**
97     * Indicates that the event will be emitted immediately.
98     */
99    IMMEDIATE = 0,
100
101    /**
102     * Indicates that the event will be emitted before low-priority events.
103     */
104    HIGH,
105
106    /**
107     * Indicates that the event will be emitted before idle-priority events. By default, an event is in LOW priority.
108     */
109    LOW,
110
111    /**
112     * Indicates that the event will be emitted after all the other events.
113     */
114    IDLE,
115  }
116}
117
118export default emitter;