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 './@ohos.base'; 17 18/** 19 * Provides methods for sending and processing in-process events. 20 * 21 * @namespace emitter 22 * @syscap SystemCapability.Notification.Emitter 23 * @since 7 24 */ 25declare namespace emitter { 26 /** 27 * Subscribe to a certain event in persistent manner and receives the event callback. 28 * 29 * @param { InnerEvent } event - indicate event to subscribe to. 30 * @param { Callback<EventData> } callback - indicate callback used to receive the event. 31 * @syscap SystemCapability.Notification.Emitter 32 * @since 7 33 */ 34 function on(event: InnerEvent, callback: Callback<EventData>): void; 35 36 /** 37 * Subscribe to a certain event in one-shot manner and unsubscribe from it 38 * after the event callback is received. 39 * 40 * @param { InnerEvent } event - indicate event to subscribe to in one shot. 41 * @param { Callback<EventData> } callback - indicate callback used to receive the event. 42 * @syscap SystemCapability.Notification.Emitter 43 * @since 7 44 */ 45 function once(event: InnerEvent, callback: Callback<EventData>): void; 46 47 /** 48 * Unsubscribe from an event. 49 * 50 * @param { number } eventId - indicate ID of the event to unsubscribe from. 51 * @syscap SystemCapability.Notification.Emitter 52 * @since 7 53 */ 54 function off(eventId: number): void; 55 56 /** 57 * Unsubscribe from an event. 58 * 59 * @param { number } eventId - indicates ID of the event to unsubscribe from. 60 * @param { Callback<EventData> } callback - indicates callback used to receive the event. 61 * @syscap SystemCapability.Notification.Emitter 62 * @since 10 63 */ 64 function off(eventId: number, callback: Callback<EventData>): void; 65 66 /** 67 * Emits an event to the event queue. 68 * 69 * @param { InnerEvent } event - indicate event to emit. 70 * @param { EventData } [data] - indicate data carried by the event. 71 * @syscap SystemCapability.Notification.Emitter 72 * @since 7 73 */ 74 function emit(event: InnerEvent, data?: EventData): void; 75 76 /** 77 * Describes data passed in the event. 78 * 79 * @typedef EventData 80 * @syscap SystemCapability.Notification.Emitter 81 * @since 7 82 */ 83 export interface EventData { 84 /** 85 * Data carried by the event. 86 * 87 * @type { ?object } 88 * @syscap SystemCapability.Notification.Emitter 89 * @since 7 90 */ 91 data?: { [key: string]: any }; 92 } 93 94 /** 95 * Describes an intra-process event. 96 * 97 * @typedef InnerEvent 98 * @syscap SystemCapability.Notification.Emitter 99 * @since 7 100 */ 101 export interface InnerEvent { 102 /** 103 * Event ID, which is used to identify an event. 104 * 105 * @type { number } 106 * @syscap SystemCapability.Notification.Emitter 107 * @since 7 108 */ 109 eventId: number; 110 111 /** 112 * Emit priority of the event. The default priority is {@link EventPriority.LOW}. 113 * 114 * @type { ?EventPriority } 115 * @syscap SystemCapability.Notification.Emitter 116 * @since 7 117 */ 118 priority?: EventPriority; 119 } 120 121 /** 122 * Indicates the emit priority of the event. 123 * 124 * @enum { number } 125 * @syscap SystemCapability.Notification.Emitter 126 * @since 7 127 */ 128 export enum EventPriority { 129 /** 130 * Indicates that the event will be emitted immediately. 131 * 132 * @syscap SystemCapability.Notification.Emitter 133 * @since 7 134 */ 135 IMMEDIATE = 0, 136 137 /** 138 * Indicates that the event will be emitted before low-priority events. 139 * 140 * @syscap SystemCapability.Notification.Emitter 141 * @since 7 142 */ 143 HIGH, 144 145 /** 146 * Indicates that the event will be emitted before idle-priority events. By default, an event is in LOW priority. 147 * 148 * @syscap SystemCapability.Notification.Emitter 149 * @since 7 150 */ 151 LOW, 152 153 /** 154 * Indicates that the event will be emitted after all the other events. 155 * 156 * @syscap SystemCapability.Notification.Emitter 157 * @since 7 158 */ 159 IDLE, 160 } 161} 162 163export default emitter; 164