当前位置: 首页 > news >正文

鸿蒙app开发中Emitter 订阅器

Emitter 是什么?

Emitter是一种作用在进程内的事件处理机制,为应用程序提供订阅事件、发布事件、取消事件订阅的能力。

场景介绍

Emitter用于同一进程内相同线程或不同线程间的事件处理,事件异步执行。使用时需要先订阅一个事件,然后发布该事件,发布完成后Emitter会将已发布的事件分发给订阅者,订阅者就会执行该事件订阅时设置的回调方法。当不需要订阅该事件时应及时取消订阅释放Emitter资源。(成对出现的)

运作机制

Emitter通过维护一个内部事件队列,来进行任务分发。应用需要先订阅某个事件并设置好该事件的回调方法,当应用程序发布事件后,就会往队列里面插入一个事件。任务队列会串行执行队列里面的任务,执行任务时会调用该任务订阅者的回调方法进行事件处理。

理解(就相当于一个全局的状态管理器。   发布一个状态的时候。会在 全局监听这个状态的变化。监听到变化后 执行对应的 回调函数  注意 不用的时候  注意及时的取消订阅)

官方文档

将 emitter 封装成一个类更好的调用

/*
 * Copyright (C) 2024 桃花镇童长老 @pura/harmony-utils
 * 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { emitter } from '@kit.BasicServicesKit';


/**
 * TODO Emitter工具类(进行线程间通信)
 * author: 桃花镇童长老ᥫ᭡
 * since: 2024/05/01
 */
export class EmitterUtil {


  /**
   * 发送事件
   * @param eventId 事件ID,string类型的eventId不支持空字符串。
   * @param data 发送的数据
   * @param EventPriority 事件被发送的优先级
   */
  static post<T>(eventId: string | number, eventData: T, priority: emitter.EventPriority = emitter.EventPriority.HIGH) {
    const genericEventData: emitter.GenericEventData<T> = { data: eventData };
    const options: emitter.Options = { priority: priority };
    emitter.emit(eventId.toString(), options, genericEventData);
  }


  /**
   * 订阅事件
   * @param eventId 事件ID,string类型的eventId不支持空字符串。
   * @param callback 事件的回调处理函数。
   */
  static onSubscribe<T>(eventId: string | number, callback: Callback<T>) {
    emitter.on(eventId.toString(), (eventData: emitter.GenericEventData<T>) => {
      callback(eventData.data);
    });
  }


  /**
   * 单次订阅指定事件
   * @param eventId 事件ID,string类型的eventId不支持空字符串。
   * @param callback 事件的回调处理函数。
   */
  static onceSubscribe<T>(eventId: string | number, callback: Callback<T>) {
    emitter.once(eventId.toString(), (eventData: emitter.GenericEventData<T>) => {
      callback(eventData.data);
    });
  }


  /**
   * 取消事件订阅
   * @param eventId 事件ID,string类型的eventId不支持空字符串。
   */
  static unSubscribe(eventId: string | number) {
    emitter.off(eventId.toString());
  }


  /**
   * 获取指定事件的订阅数
   * @param eventId 事件ID,string类型的eventId不支持空字符串。
   * @returns
   */
  static getListenerCount(eventId: number | string): number {
    return emitter.getListenerCount(eventId);
  }


  /**
   * 订阅事件,支持取消指定事件回调
   * @param eventId 事件ID,string类型的eventId不支持空字符串。
   * @param callback 接收到该事件时需要执行的回调处理函数。
   */
  static on<T>(eventId: string | number, callback: Callback<emitter.GenericEventData<T>>) {
    emitter.on(eventId.toString(), callback);
  }


  /**
   * 单次订阅指定事件,支持取消指定事件回调
   * @param eventId 事件ID,string类型的eventId不支持空字符串。
   * @param callback 接收到该事件时需要执行的回调处理函数。
   */
  static once<T>(eventId: string | number, callback: Callback<emitter.GenericEventData<T>>) {
    emitter.once(eventId.toString(), callback);
  }

  /**
   * 取消事件订阅,支持取消指定事件回调
   * @param eventId 事件ID,string类型的eventId不支持空字符串。
   * @param callback 取消该事件的回调处理函数。
   */
  static off<T>(eventId: string | number, callback: Callback<emitter.GenericEventData<T>>) {
    emitter.off(eventId.toString(), callback);
  }


}

具体的使用

 

相关文章:

  • Reactive编程框架与工具
  • Java 集合介绍
  • Linux--文件系统
  • 第四章 结构化程序设计
  • 【数据挖掘】岭回归(Ridge Regression)和线性回归(Linear Regression)对比实验
  • TBE(TVM的扩展)
  • 滑动窗口滤波
  • OpenIPC开源FPV之Adaptive-Link日志分析
  • 【Linux操作系统】:信号
  • 【Java设计模式】第10章 外观模式讲解
  • C++进阶笔记第一篇:程序的内存模型
  • 简单回溯(组合力扣77)
  • OpenCV 图形API(22)矩阵操作
  • SAP Overview
  • 淘宝 API 高并发优化:突破 QPS 限制的分布式爬虫架构设计
  • java导入excel更新设备经纬度度数或者度分秒
  • UTF-8和GBK编码的区别和详细解释
  • Unity Input 2023 Release-Notes
  • 数据结构第六章(一) -图
  • Dynamics 365 Business Central VS Code AL 开发 多语言的支持
  • 稳住外贸基本盘,这个中部大省出手了
  • 赵作海因病离世,妻子李素兰希望过平静生活
  • 身临其境感受伟人思想力量,“马克思书房”在上海社科馆揭幕
  • 招行:拟出资150亿元全资发起设立金融资产投资公司
  • 特色业务多点开花,苏州银行擦亮金融为民底色
  • 苏丹宣布与阿联酋断交