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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mini-dev/event

v0.1.0

Published

小程序(微信/支付宝/抖音)带挂起补投的事件总线(event bus)—— 零依赖核心,不接管页面生命周期

Readme

@mini-dev/event

license platform: 小程序 deps: zero

@mini-dev 小程序开发工具箱的一员 —— 面向微信 / 支付宝 / 抖音原生小程序的带挂起/补投的事件总线。零运行时依赖,不知道「页面」为何物。

原生小程序没有跨页 / 全局的发布-订阅机制,跨页通信只能靠 getCurrentPages() 取上页实例、getApp().globalData 手写,都无 off / once、无类型、易泄漏。更棘手的是投递时机:页 A 打开页 B,B 广播时 A 已不可见,此刻若立即执行 A 的 handler(可能 setData 一个不可见页),顺序与正确性都可疑。

@mini-dev/event 用作用域(scope)的活跃态决定投递时机:活跃 scope 立即收,非活跃 scope 挂起、activate() 时补投最新值,dispose() 时退订。核心只提供这套机制与显式的状态转移方法(activate / deactivate / dispose),不碰任何页面实例、不 hook 任何生命周期——把「什么时候活跃/非活跃/销毁」交给调用方或 @mini-dev/event-hook 决定。这是它区别于一切通用 emitter(mitt / eventemitter3 / Taro / uni)的核心。

目录

  1. 安装
  2. 快速开始
  3. 核心概念:作用域与活跃态投递
  4. API 参考
  5. 三种用法:应用级 / 手动 scope / $events
  6. 类型安全
  7. 非目标

1. 安装

npm install @mini-dev/event

发布产物同时提供 CommonJS 与 ESM 两份构建(dist/cjs / dist/esm,均 .js 后缀 + per-format .d.ts);miniprogram 字段指向 dist/cjs,供微信开发者工具「构建 npm」消费。各平台需分别开启 npm 支持:微信 · 支付宝 · 抖音。

2. 快速开始

核心不接管生命周期,由你把页面的 onShow/onHide/onUnload 接到 scope 的转移方法上(想省这几行、自动接线,见 @mini-dev/event-hook):

const { events } = require('@mini-dev/event');

Page({
    data: { last: '未收到' },
    onLoad() {
        this.scope = events.scope();
        this.scope.on('cart:change', (count) => this.setData({ last: '购物车 ' + count }));
    },
    onShow() { this.scope.activate(); },    // 变活跃:补投挂起期间最新值
    onHide() { this.scope.deactivate(); },  // 变非活跃:后续 emit 挂起
    onUnload() { this.scope.dispose(); }    // 销毁:退订本页全部订阅
});

// 另一个页面广播:A 若非活跃,此值被挂起,A activate 时补投最新一条
events.emit('cart:change', 3);

3. 核心概念:作用域与活跃态投递

一次 hub.scope() 得到一个自持活跃态的作用域(EventScope),初始为活跃。投递按订阅是否属于某 scope、该 scope 是否活跃、是否 eager 决定:

flowchart TD
    A[emit event, payload] --> B[遍历该 event 的订阅]
    B --> C{订阅属于某 scope?}
    C -- 否, 应用级 --> D[立即执行]
    C -- 是 --> E{scope 活跃 或 声明 eager?}
    E -- 是 --> D
    E -- 否 --> F[按 event 覆盖式暂存最新 payload]
    F -.-> G[scope.activate 时补投最新一条]
    B -.-> H[scope.dispose: 退订本 scope 全部订阅]

三条规则要点:

  • 不属于任何 scope 的订阅(hub.on 直接订阅)恒立即投递 —— 应用级广播,如全局登录态。
  • 属于某 scope 的订阅:scope 活跃时立即投递;非活跃时按 event 覆盖式暂存最新一条,activate() 时补投。挂起期间的中间值被丢弃,只补最后一条。
  • eager: true 的订阅即便 scope 非活跃也立即执行 —— 用于「已登出,清资源」这类必须即时处理、且不碰 UI 的事件。
  • 遍历订阅前取快照,handler 内增删订阅不影响本轮投递。单个 handler 抛错被隔离,经 onError 上报,不影响其余 handler。

⚠️ eager 的 handler 在 scope 非活跃时仍可能 setData 一个不可见页,请只用于非 UI 的即时处理。

4. API 参考

4.1 events 与 createEventHub

import { events, createEventHub } from '@mini-dev/event';
  • events —— 默认共享单例,多数场景直接用它。
  • createEventHub<Events>() —— 创建互相隔离的独立总线实例。

下文用 hub 泛指任一 EventHub 实例(events 或 createEventHub() 的产物),用 scope 泛指 hub.scope() 得到的 EventScope。它们是行文约定,不是导出名。

4.2 EventHub

| 方法 | 说明 | | --- | --- | | on(event, handler, options?) | 订阅;返回退订函数。options: { eager?, onError? } | | once(event, handler, options?) | 订阅一次,触发后自动退订;返回退订函数 | | off(event, handler) | 应用级退订:按引用摘掉 hub.on/once 的直接订阅,不触碰任何 scope 的订阅。scope 内退订用 on 返回的退订函数或 scope.dispose() | | emit(event, payload) | 广播 | | clear(event?) | 清空某 event 的全部订阅(含各 scope 对应的挂起值);省略 event 清空全部 | | scope() | 新建一个自持活跃态的作用域 EventScope(见 4.3)| | onError? | 全局错误回调 (err, event) => void,被 handler 抛错时调用 |

4.3 EventScope

hub.scope() 返回的作用域,自持活跃态,转移由调用方显式驱动:

| 方法 | 说明 | | --- | --- | | on(event, handler, options?) | 订阅,归属本 scope;options: { eager?, onError? } | | once(event, handler, options?) | 同上,触发一次 | | emit(event, payload) | 广播(委托到父总线,与 hub.emit 等价,天然全局)| | activate() | 变为活跃:补投挂起期间每个 event 的最新一条 | | deactivate() | 变为非活跃:后续 emit 对本 scope 非 eager 订阅挂起(覆盖式最新)| | dispose() | 退订本 scope 的全部订阅 |

5. 三种用法:应用级 / 手动 scope / $events

// A. 应用级(不绑定生命周期):恒立即投递,适合全局广播
events.on('user:login', handler);

// B. 手动 scope(零依赖):你自己把生命周期接到转移方法上
const scope = events.scope();
scope.on('cart:change', handler);
// 在页面 onShow/onHide/onUnload 里分别调 scope.activate()/deactivate()/dispose()

// C. this.$events(开箱即用,需装 @mini-dev/event-hook):生命周期自动接线
this.$events.on('cart:change', handler);

C 层由伴生包 @mini-dev/event-hook 提供:createPageEventHook() 返回一个覆盖 onLoad/onShow/onHide/onUnload 的 @mini-dev/hook decoration,由你 _Page.use(createPageEventHook()) 叠加后,每个 Page 自动获得已接好线的 this.$events。核心包不依赖它,也不依赖 @mini-dev/hook。

6. 类型安全

用泛型事件表约束 event 名与 payload 类型(相对通用 emitter 的主要优势):

import { createEventHub } from '@mini-dev/event';

interface Events {
    'user:login': { uid: string };
    'cart:change': number;
}

const typed = createEventHub<Events>();
typed.on('user:login', (p) => console.log(p.uid)); // p 推断为 { uid: string }
typed.emit('cart:change', 3);                        // payload 受检,传字符串会报错

事件表用普通接口即可,无需字符串索引签名。默认单例 events 按 Record<string, unknown> 输入,适合不声明事件表的应用级用法。

7. 非目标

秉持工具箱「每个库只解决一件事」「增强不归一」的原则,本库刻意不做:

  • 生命周期接线 —— 核心只暴露 activate/deactivate/dispose,不 hook、不改写任何页面实例的方法。接线由 @mini-dev/event-hook 或使用方负责。
  • 状态管理 —— 无 state / getter / 响应式订阅。
  • 跨页导航传参 —— 那是 @mini-dev/router / @mini-dev/unipath / 微信 EventChannel 的职责。
  • 异步队列 / 消息代理 / 跨启动持久化。
  • Sticky(粘性事件) —— 订阅晚于发布也能拿最后一次值,规划中,v1 不做。
  • 通配符 / 命名空间匹配(如 on('user:*'))—— v1 不做。
  • 平台归一 / 运行时探测 —— 核心不碰 wx / my / tt 全局,纯内存实现,天然跨端。