@fdmg/resubstitute

中文文档
0.1.55发布于 2026/10/30ISCTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 1971

微软/ ReSub 的 Pub/Sub 滴入替换

中文使用文档
文档目录6 节
  1. 使用量
  2. 创建存储
  3. 订阅
  4. 触发块
  5. 订阅特定的密钥
  6. 被勒令停止的活动

Node.js CI Coverage Status CodeQL Node.js Package Bundle-size minified+gzip NPM

替换

对微软/ ReSub 的状态管理器进行反应

使用量

  1. npm i -D @fdmg/resubstitute

创建存储

A 类 store 扩展 ReSubstitute 课。 它是一个类,你可以存储任何你想要的价值观. 通常你会执行 setters 财务报告和审定财务报表 getters 设置或从 store。 。 。 . 在设置功能中,你最后会打电话 this.trigger()。这将通知所有订阅者更改.

ThemeStore.ts

import { ReSubstitute } from '@fdmg/resubstitute';

export enum Theme {
    SYSTEM = 'system',
    DARK = 'dark',
    LIGHT = 'light',
}

class ThemeStore extends ReSubstitute {
    private _theme: Theme = Theme.SYSTEM;

    setTheme(theme: Theme) {
        this._theme = theme;
        this.trigger();
    }

    getTheme() {
        return this._theme;
    }
}

export default new ThemeStore();

订阅

const subscriptionId = ThemeStore.subscribe(() => {
    // Called whenever `this.trigger()` is called within ThemeStore.
});

// Unsubscribe from the ThemeStore
ThemeStore.unsubscribe(subscriptionId);

触发块

有时你可能想要做很多连续的更新,但你并不一定需要通知所有订阅者每个更新. 然后可以使用静态函数 ReSubstitute.pushTriggerBlock() 将一个块推入堆栈。 只要堆栈上有一个块,触发器不会导致用户被通知.

const resub = new ReSubstitute();
resub.subscribe(() => {
    console.log('triggered');
});

// Add global trigger block to the stack
ReSubstitute.pushTriggerBlock();
// Add global trigger block to the stack
ReSubstitute.pushTriggerBlock();
resub.trigger(); // Nothing happens

ReSubstitute.popTriggerBlock();
resub.trigger(); // Nothing happens

ReSubstitute.popTriggerBlock();
resub.trigger(); // console.log('triggered');

绕行触发块

const resub = new ReSubstitute();
const resubBypass = new ReSubstitute(0, true);

resub.subscribe(() => {
    console.log('triggered');
});
resubBypass.subscribe(() => {
    console.log('bypass triggered');
});

// Add global trigger block to the stack
ReSubstitute.pushTriggerBlock();
resub.trigger(); // nothing happens
resubBypass.trigger(); // console.log('bypass triggered');

订阅特定的密钥

const resub = new ReSubstitute();
// Subscribe to global events
resub.subscribe(() => {
    console.log('triggered');
});
// Subscribe only to key
resub.subscribe(() => {
    console.log('only triggered by testKey');
}, 'testKey');

resub.trigger(); // console.log('triggered');
resub.trigger('testKey'); // console.log('only triggered by testKey');

// console.log('triggered');
// console.log('only triggered by testKey');
resub.trigger(['testKey', ReSubstitute.Key_All]);

被勒令停止的活动

const resubThrottled = new ReSubstitute(500);
resubThrottled.subscribe(() => {
    console.log('triggered');
});
resubThrottled.trigger(); // console.log('triggered'); after 500ms
resubThrottled.trigger(); // trigger already scheduled so 2nd trigger is ignored

版本与发布记录

@fdmg/resubstitute 共发布 63 个版本,首次发布于 2021/06/23;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.55latest2026/10/3016 天前
0.1.542026/10/011 个月前
0.1.532026/09/291 个月前
0.1.522026/09/102 个月前
0.1.512026/08/073 个月前
0.1.502026/05/126 个月前

其他发布通道:next → 0.1.32-canary.2。

关于 @fdmg/resubstitute 的常见问题

@fdmg/resubstitute 是什么?

微软/ ReSub 的 Pub/Sub 滴入替换

如何安装 @fdmg/resubstitute?

在项目目录运行 npm install @fdmg/resubstitute,或使用 pnpm add / yarn add / bun add。

@fdmg/resubstitute 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fdmg/resubstitute 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fdmg/resubstitute 可以商用吗?

@fdmg/resubstitute 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fdmg/resubstitute 的下载量有多少?

根据 npm 官方下载统计,@fdmg/resubstitute 在 2026/10/15 – 2026/11/13 这 30 天内被下载 1,971 次,日均约 66 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fdmg/resubstitute 还在维护吗?

最新版本 0.1.55 发布于 2026/10/30(16 天前);至今共发布 63 个版本,其中过去 12 个月发布了 10 个;首次发布于 2021/06/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。