From be2694bd368b09bdbb8ea5bb53157c4826d7f0e8 Mon Sep 17 00:00:00 2001 From: zhengzhuolan Date: Thu, 11 Sep 2025 09:31:41 +0800 Subject: [PATCH] add Emitter interface Signed-off-by: zhengzhuolan --- api/@ohos.events.emitter.d.ts | 155 +++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 1 deletion(-) diff --git a/api/@ohos.events.emitter.d.ts b/api/@ohos.events.emitter.d.ts index b67693c8d0..69fd73646a 100644 --- a/api/@ohos.events.emitter.d.ts +++ b/api/@ohos.events.emitter.d.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -793,6 +793,159 @@ declare namespace emitter { */ data?: T; } + + /** + * Provides methods for creating and managing independent event emitter instances. + * Each instance maintains its own set of event listeners and can emit events independently. + * + * @class Emitter + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + export class Emitter { + /** + * Creates a new independent Emitter instance. + * + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + constructor(); + + /** + * Subscribes to an event in persistent manner and executes a callback after the event is received. + * + * @param { string } eventId - Event to subscribe to in persistent manner. The value cannot be an empty string and exceed 10240 bytes. + * @param { Callback } callback - Callback to be executed when the event is received. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + on(eventId: string, callback: Callback): void; + + /** + * Subscribes to an event in persistent manner and executes a callback after the event is received. + * + * @param { string } eventId - Event to subscribe to in persistent manner. The value cannot be an empty string and exceed 10240 bytes. + * @param { Callback> } callback - Callback to be executed when the event is received. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + on(eventId: string, callback: Callback>): void; + + /** + * Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed. + * + * @param { string } eventId - Event to subscribe to in one-shot manner. The value cannot be an empty string and exceed 10240 bytes. + * @param { Callback } callback - Callback to be executed when the event is received. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + once(eventId: string, callback: Callback): void; + + /** + * Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed. + * + * @param { string } eventId - Event to subscribe to in one-shot manner. The value cannot be an empty string and exceed 10240 bytes. + * @param { Callback> } callback - Callback to be executed when the event is received. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + once(eventId: string, callback: Callback>): void; + + /** + * Unsubscribes from all events with the specified event ID. + * + * @param { string } eventId - Event ID. The value cannot be an empty string and exceed 10240 bytes. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + off(eventId: string): void; + + /** + * Unsubscribes from an event with the specified event ID and processed by the specified callback. + * + * @param { string } eventId - Event ID. The value cannot be an empty string and exceed 10240 bytes. + * @param { Callback } callback - Callback to unregister. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + off(eventId: string, callback: Callback): void; + + /** + * Unsubscribes from an event with the specified event ID and processed by the specified callback. + * + * @param { string } eventId - Event ID. The value cannot be an empty string and exceed 10240 bytes. + * @param { Callback> } callback - Callback to unregister. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + off(eventId: string, callback: Callback>): void; + + /** + * Emits the specified event. + * + * @param { string } eventId - ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. + * @param { EventData } [data] - Data passed in the event. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + emit(eventId: string, data?: EventData): void; + + /** + * Emits the specified event. + * + * @param { string } eventId - ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. + * @param { GenericEventData } [data] - Data passed in the event. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + emit(eventId: string, data?: GenericEventData): void; + + /** + * Emits an event of a specified priority. + * + * @param { string } eventId - ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. + * @param { Options } options - Event emit priority. + * @param { EventData } [data] - Data passed in the event. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + emit(eventId: string, options: Options, data?: EventData): void; + + /** + * Emits an event of a specified priority. + * + * @param { string } eventId - ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. + * @param { Options } options - Event emit priority. + * @param { GenericEventData } [data] - Data passed in the event. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + emit(eventId: string, options: Options, data?: GenericEventData): void; + + /** + * Obtains the number of subscriptions to a specified event. + * + * @param { string } eventId - Event ID. The value cannot be an empty string. + * @returns { number } Returns the number of listener count. + * @syscap SystemCapability.Notification.Emitter + * @atomicservice + * @since arkts {'1.1':'21', '1.2':'21'} + */ + getListenerCount(eventId: string): number; + } } export default emitter; -- Gitee