青岛网站制作辰星辰seo网络公司
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);}}
具体的使用