msgm 是一个拥抱类型安全的发布订阅模式库 并且可以控制优先级
npm
npm i msgm
yarn
yarn add msgm
pnpm
pnpm add msgm
// 导入 msgm类 以及 TypeMap1类型配置项
// 建议自己新建一个配置文件
import { Msgm, TypeMap1} from "msgm";
// 创建一个 msgm 实例 <配置类型接口>
const msg = new Msgm<TypeMap1>();
// 编写一个回调函数
const onEvent = (data) => {
console.log(`onEvent: ${data}`);
};
// 基础注册
msg.on("event", onEvent);
// 注册消息可以返回消息唯一标识
const id = msg.on("event", onEvent);
// 注册并绑定回调目标, 回调目标是在回调函数中的this指向
msg.on("event", onEvent, this);
// 绑定回调目标并且设置优先级
msg.on("event", onEvent, this, 1);
// 可以设置消息优先级, 值越大, 优先级越高
msg.on("hello", () => console.log("hello 1"), 1);
msg.on("hello", () => console.log("hello 2"), 2);
msg.on("hello", () => console.log("hello 3"), 3);
// 这里发射打印结果为 hello 3 hello 2 hello 1
// 通过回调函数来注销消息
msg.off("event", onEvent);
// 通过唯一标识来注销消息
msg.off("event", id);
// 注销绑定回调目标的消息
msg.off("event", onEvent, this);
// 注销一个消息类型的所有消息
msg.off("event");
// 发射消息
msg.emit("event", "hello msgm");
// 发射无数据消息
msg.emit("event");
// 如果要使用无类型的发布订阅 那么也非常简单
const msg = new Msgm<any>();
// ...
github: https://github.com/yxdtg/yxmsg
/**
* 类型配置-测试
* 建议自己新建一个配置文件
*/
export interface TypeMap1 {
["event"]: string;
["hello"]: string;
}
export enum EType {
Create = "Create",
Destroy = "Destroy",
}
export interface TypeMap2 {
[EType.Create]: { name: string, age: number };
[EType.Destroy]: { name: string };
}
/**
* --------------------
*/