@ecopages/signals

中文文档
0.8.3发布于 2026/11/13MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 6057

Renderer-agnostic signal primitives based on the TC39 Signals proposal

原始使用文档
文档目录7 节
  1. Current Scope
  2. Source Layout
  3. Design Position
  4. TC39 Relationship
  5. API Notes
  6. Example
  7. Limits

Ecopages Signals

@ecopages/signals is a renderer-agnostic signals package that can be used standalone or underneath Radiant.

Its core model is based on the TC39 Signals proposal, with a smaller surface area and a few convenience helpers for application code today.

The public entrypoint remains index.ts, while the implementation now lives in focused modules under src/ so the core runtime, effects, watcher support, and store logic can evolve independently.

Current Scope

This package currently provides:

  • State<T> for writable values
  • Computed<T> for lazily derived values
  • currentComputed() for advanced derived helpers that need the active computed context
  • effect(...) for reactive side effects with scheduled re-execution
  • watch(...) for observing derived values with previous-value access
  • untrack(...) and peek(...) for non-tracking reads
  • subtle.Watcher plus watched and unwatched hooks for low-level invalidation workflows
  • createStore(...) for deep reactive object and array state
  • mapItems(...) for derived lists that map each item once and reuse its result while it stays in the list
  • isStore(...) for detecting signal-backed store proxies
  • snapshot(...) for materializing plain nested data
  • automatic dependency discovery during Computed evaluation
  • subscription support for renderer or framework adapters

Source Layout

The package is organized around a stable public barrel and smaller implementation files:

  • index.ts re-exports the public API and exposes subtle
  • src/types.ts defines the public contracts, options, and low-level symbols
  • src/state.ts implements writable State signals
  • src/computed.ts implements lazy derived Computed signals and active-computation helpers
  • src/effect.ts and src/watch.ts implement effect scheduling and derived-value observation
  • src/watcher.ts implements proposal-shaped low-level watchers
  • src/tracking.ts contains non-tracking read helpers
  • src/store.ts contains deep store proxying and snapshot materialization

Design Position

This package is renderer-agnostic.

  • It does not know about JSX.
  • It does not know about Radiant components.
  • It is meant to work both as a standalone package and underneath adapters in those packages.
flowchart LR
    Set["state.set(value)"] --> Stale["dependent computeds<br/>marked stale"]
    Stale --> Pull["computed.get()<br/>recomputes lazily and caches"]
    Stale --> Notify["subscribers, effects<br/>and watchers notified"]
    Notify --> Adapter["an adapter re-reads and patches,<br/>such as an @ecopages/jsx binding"]

A computed tracks the signals it reads while it evaluates, so dependencies need no declaration. Nothing recomputes until something reads it, and an adapter that subscribes gets pushed the new value to patch its own output.

TC39 Relationship

This package is based on the current TC39 Signals proposal and tracks the same broad model around:

  • State and Computed signal classes
  • lazy pull-based recomputation with cached values
  • automatic dependency discovery during computed evaluation
  • custom equality functions for writable and computed signals
  • untracked reads as an escape hatch

It is not a drop-in implementation of the current proposal draft.

It is best understood as proposal-aligned in its core semantics, but not yet fully API-compatible with the draft surface.

  • It exposes convenience helpers such as effect(...), watch(...), createStore(...), and snapshot(...) directly.
  • It currently exposes manual subscribe(...) hooks for adapter and library integration.
  • It exposes a proposal-shaped subtle.Watcher API, while still keeping the existing convenience helpers.
  • Its subtle.Watcher follows the proposal-style re-arm behavior, where calling watch(...) resets the pending set and notification latch for the next invalidation cycle.
  • It does not yet expose the full TC39 subtle introspection surface.

API Notes

  • subscribe(...) is intended for adapter-style push integration. Application-level derived work is usually better expressed with Computed, effect(...), or watch(...).
  • watch(...) is built on top of a computed signal plus an effect, so it inherits computed equality behavior and effect scheduling. It notifies on change, not per flush: a set followed by a set-back before the scheduler runs does not notify, and previousValue is the last value delivered.
  • subtle.Watcher reports staleness rather than recalculated values. Calling watch(...) is both registration and reset.
  • createStore(...) wraps nested plain objects and arrays, while snapshot(...) detaches the current plain value graph for logging, serialization, or comparison.
  • mapItems(source, project, options?) keeps each item's result by item identity, so only new items are projected and the others keep the same result object. project runs untracked, so a result that reads its item's signals does not re-map the list. A renderer such as @ecopages/jsx skips rows whose value is unchanged; JSX users can use <MapItems each={list}> instead.

Example

import { Computed, State, createStore, effect, watch } from '@ecopages/signals';

const count = new State(0);
const parity = new Computed(() => ((count.get() & 1) === 0 ? 'even' : 'odd'));
const store = createStore({ profile: { name: 'Ada' } });

const dispose = effect(() => {
    console.log(parity.get(), store.profile.name);
});

const stopWatching = watch(
    () => store.profile.name,
    (nextName, previousName) => {
        console.log(previousName, '->', nextName);
    },
);

count.set(1);
store.profile.name = 'Grace';
dispose();
stopWatching();

Limits

This implementation is still smaller than the current TC39 proposal draft.

  • no full TC39 Watcher and subtle semantics surface yet
  • no full proposal-style subtle introspection helpers yet
  • no batching or transaction model
  • no framework-owned disposal tree or component ownership integration yet

Those omissions are deliberate. The goal is to keep a small, useful standalone package while leaving room to align further as the proposal evolves.

版本与发布记录

@ecopages/signals 共发布 58 个版本,首次发布于 2026/04/13;过去 12 个月发布了 58 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.3latest2026/11/13今天
0.8.22026/11/085 天前
0.8.12026/11/085 天前
0.8.02026/11/085 天前
0.7.02026/11/076 天前
0.6.02026/10/2717 天前

其他发布通道:alpha → 0.3.0-alpha.2,beta → 0.3.0-beta.8,rc → 0.3.0-rc.13。

关于 @ecopages/signals 的常见问题

@ecopages/signals 是什么?

Renderer-agnostic signal primitives based on the TC39 Signals proposal

如何安装 @ecopages/signals?

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

@ecopages/signals 支持 TypeScript 吗?

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

@ecopages/signals 支持 ESM 还是 CommonJS?

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

@ecopages/signals 可以商用吗?

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

@ecopages/signals 的下载量有多少?

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

@ecopages/signals 还在维护吗?

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