@epic-web/remember

中文文档
1.1.0发布于 2024/05/29MITTypeScriptES Module零依赖近 30 天下载 19.7万

Simple, type-safe, "singleton" implementation.

原始使用文档
文档目录5 节
  1. The problem
  2. This solution
  3. Usage
  4. License
  5. Credit

🤔 @epic-web/remember

Simple, type-safe, "singleton" implementation.

For when your "hot module replacement" involves re-evaluating a module, but you don't want to actually re-evaluate a portion of it.

npm install @epic-web/remember

Build Status GPL 3.0 License Code of Conduct

The problem

You're using a framework like Remix with --manual mode and re-evaluating your modules on every change. But you have some state that you don't want to lose between changes. For example:

  • Database connections
  • In-memory caches

This solution

This was copy/paste/modified/tested from @jenseng's abuse-the-platform demo (ISC). It's basically a type-safe singleton implementation that you can use to keep state between module re-evaluations.

Usage

import { remember } from '@epic-web/remember'

export const prisma = remember('prisma', () => new PrismaClient())

Keep in mind that any changes you make within that callback will not be reflected when the module is re-evaluated (that's the whole point). So if you need to change the callback, then you'll need to restart your server.

Forget a value

It might be required to explicitly forget a value if it gets outdated, a memorized connection gets lost or memorized instance closes/errors/etc.

import { remember, forget } from '@epic-web/remember'

export const server = remember('server', () =>
    http.createServer().listen('8080')
        .on('close', () => forget('server')))

License

MIT

Credit

The original code was written by @jenseng and then I modified it and published it to fit my needs.

版本与发布记录

@epic-web/remember 共发布 4 个版本,首次发布于 2023/09/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2024/05/292 年前
1.0.22023/09/113 年前
1.0.12023/09/103 年前
1.0.02023/09/103 年前

关于 @epic-web/remember 的常见问题

@epic-web/remember 是什么?

Simple, type-safe, "singleton" implementation.

如何安装 @epic-web/remember?

在项目目录运行 npm install @epic-web/remember,或使用 pnpm add / yarn add / bun add。

@epic-web/remember 支持 TypeScript 吗?

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

@epic-web/remember 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@epic-web/remember 可以商用吗?

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

@epic-web/remember 的下载量有多少?

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

@epic-web/remember 还在维护吗?

最新版本 1.1.0 发布于 2024/05/29(2 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/09/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。