# @fdmg/resubstitute 中文文档

规范地址：https://npmcn.com/package/%40fdmg/resubstitute/

版本：0\.1\.55
npm 元数据更新时间：2026-10-30T11:44:50.446Z
中文译文更新时间：2026-11-16T00:27:55.756Z
软件包许可：ISC

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40fdmg/resubstitute
原文对照：https://npmcn.com/package/%40fdmg/resubstitute/?lang=original
项目主页：https://github.com/FDMediagroep/ReSubstitute#readme
源码仓库：https://github.com/FDMediagroep/ReSubstitute

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：17 kB，5 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2021-06-23T09:15:21.748Z
- 最新版本发布：2026-10-30T11:44:50.095Z
- 近 30 天下载量：1971（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：63（近 12 个月 10 个）

## 中文简介

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

## 安装

```sh
npm install @fdmg/resubstitute
```

也可以使用：`pnpm add @fdmg/resubstitute`、`yarn add @fdmg/resubstitute`、`bun add @fdmg/resubstitute`

## README

[![Node.js CI](https://github.com/FDMediagroep/ReSubstitute/actions/workflows/node.js.yml/badge.svg)](https://github.com/FDMediagroep/ReSubstitute/actions/workflows/node.js.yml) [![Coverage Status](https://coveralls.io/repos/github/FDMediagroep/ReSubstitute/badge.svg?branch=main)](https://coveralls.io/github/FDMediagroep/ReSubstitute?branch=main) [![CodeQL](https://github.com/FDMediagroep/ReSubstitute/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/FDMediagroep/ReSubstitute/actions/workflows/codeql-analysis.yml) [![Node.js Package](https://github.com/FDMediagroep/ReSubstitute/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/FDMediagroep/ReSubstitute/actions/workflows/npm-publish.yml) [![Bundle-size minified+gzip](https://img.shields.io/bundlephobia/minzip/@fdmg/resubstitute)](https://bundlephobia.com/result?p=@fdmg/resubstitute) [![NPM](https://img.shields.io/npm/v/@fdmg/resubstitute?color=blue)](https://npmjs.com/package/@fdmg%2Fresubstitute)

# 替换

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

## 使用量

1.  `npm i -D @fdmg/resubstitute`

### 创建存储

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

`ThemeStore.ts`

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

### 订阅

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

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

### 触发块

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

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

#### 绕行触发块

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

### 订阅特定的密钥

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

### 被勒令停止的活动

```javascript
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 是什么？

微软/ 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 和提交记录判断。

