npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

msgm

v0.2.1

Published

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

Downloads

705

Readme

msgm

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

安装

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 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>();
// ...

如果不需要类型安全 推荐使用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" 以及后面的内容了吗

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

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