0.2.3 • Published 10 months ago

msgm v0.2.3

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

msgm 不再更新

msgm 是一个拥抱类型安全的发布订阅模式库 并且可以控制优先级

推荐使用yx-pubsub库 它包含了msgm和yxmsg两个库的功能 并且更加安全与灵活

https://www.npmjs.com/package/yx-pubsub

安装

npm

npm i msgm

yarn

yarn add msgm

pnpm

pnpm add msgm

推荐使用TypeScript, 开箱即用 + 方便的类型配置

初始化

// 导入 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 1 hello 2 hello 3

注销

// 通过回调函数来注销消息
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>();
// ...

如果不需要类型安全 推荐使用yxmsg库 它同样支持优先级控制

npm: https://www.npmjs.com/package/yxmsg

github: https://github.com/yxdtg/yxmsg

类型安全

打开msgm.ts文件 到达文件底部

你大概会看到这样一些内容

/**
 * 类型配置-测试
 * 建议自己新建一个配置文件
 */
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 };
}
/**
 * --------------------
 */

看到 "event" 和 "hello" 以及后面的内容了吗

没错,我想你已经猜到了,它们就是保证类型安全的关键。

你可以编写自己的消息类型,使其在注册,注销,发射的过程中保持愉快和安全。

0.2.3

10 months ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.4

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago