@fdmg/resubstitute

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

Pub/Sub drop-in replacement for Microsoft/ReSub

原始使用文档
文档目录6 节
  1. Usage
  2. Creating Stores
  3. Subscribe
  4. Trigger blocks
  5. Subscribe to specific keys
  6. Throttled events

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

ReSubstitute

React State manager drop-in replacement for Microsoft/ReSub

Usage

  1. npm i -D @fdmg/resubstitute

Creating Stores

A store extends the ReSubstitute class. It is a class in which you can store whatever values you want. Typically you'll implement setters and getters to set or get some value from the store. In the setter-function you'll end by calling this.trigger(). This will notify all subscribers to the change.

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

Subscribe

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

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

Trigger blocks

Sometimes you may want to do lot of consecutive updates but you don't necessarily need to notify all subscribers for each of those updates. Then you can use the static function ReSubstitute.pushTriggerBlock() to push a block onto the stack. As long as there is a block on the stack, triggers will not cause subscribers to be notified.

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');

Bypass trigger blocks

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');

Subscribe to specific keys

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]);

Throttled events

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 是什么?

Pub/Sub drop-in replacement for Microsoft/ReSub

如何安装 @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 和提交记录判断。