在草稿中修改状态
在 set() 的更新函数中执行 this.user.age += 1,提交后会得到新的不可变快照。未修改的分支仍与上一个快照共享同一引用。
React 组件在 observer() 内追踪渲染时读取的状态路径,普通 useStore() 则订阅整个 store。getter 的计算结果由运行时缓存。使用 Zustand 风格的 create() 创建不可变 store。撤销、持久化和远程同步可通过中间件启用,共享模式还支持在 Worker 中执行 action。
npm install coaction @coaction/react支持ReactVueAngularSvelteSolid
图中的四个组件分别追踪自己读取的状态。每次更新后,动画标出本次重新渲染的组件。
user.name渲染 1 次cart.total渲染 1 次user.age渲染 1 次cart.items渲染 1 次核心模型
组件读取状态时,Coaction 记录它依赖的路径。更新提交后,运行时根据新旧快照判断哪些依赖发生了变化。
在 set() 的更新函数中执行 this.user.age += 1,提交后会得到新的不可变快照。未修改的分支仍与上一个快照共享同一引用。
在 observer() 中读取 user.profile.name 时,Coaction 会记录这条路径。仅修改 user.age 不会因此触发该组件重新渲染。对象属性无需单独声明为信号。
运行时记录 get total() 读取的状态字段,并缓存结果。多个调用方可复用该结果,依赖变化后再次读取时才重新计算。
set() 产出下一份快照和一次提交,按需附带补丁。撤销重做、持久化、同步和 Worker 都建立在它之上。
实时演示
执行下方的 action,可对比同一次更新触发的渲染次数。左列组件通过 observer() 追踪读取路径,右列组件通过 useShop() 订阅整个 store。每个单元格分别计数。
import { create } from '@coaction/react';
export const useShop = create((set) => ({
user: { name: 'Ada', age: 36 },
cart: { items: [{ name: 'Notebook', price: 12 }] },
theme: 'light',
get total() {
return this.cart.items.reduce((sum, item) => sum + item.price, 0);
},
rename(name: string) {
set(() => {
this.user.name = name;
});
},
birthday() {
set(() => {
this.user.age += 1;
});
},
addItem(item: Item) {
set(() => {
this.cart.items.push(item);
});
},
setTheme(theme: 'light' | 'dark') {
set(() => {
this.theme = theme;
});
}
}));| 组件 | observer() | 普通 useShop() |
|---|---|---|
<Avatar />读取 user.name | Ada1 | Ada1 |
<AgeBadge />读取 user.age | 361 | 361 |
<CartTotal />读取 total | $121 | $121 |
<ItemList />读取 cart.items | Notebook1 | Notebook1 |
数字为每个单元格的渲染次数。
React 代码对比
Coaction 示例用 observer() 追踪组件读取的字段,用 getter 缓存 doubled。组件无需手动组合选择器,也无需调用 useShallow 或 useMemo。
import { create, observer } from '@coaction/react';
const useCounter = create((set) => ({
count: 0,
step: 1,
// Cached automatically. Recomputed only when a dependency changes.
get doubled() {
return this.count * 2;
},
increment() {
set(() => {
this.count += this.step; // mutable-looking recipe, immutable next state
});
}
}));
const Counter = observer(() => {
const store = useCounter(); // tracks only the fields this render reads
return (
<button onClick={store.increment}>
{store.count} × 2 = {store.doubled}
</button>
);
});import { create } from 'zustand';
import { useShallow } from 'zustand/react/shallow';
import { useMemo } from 'react';
const useCounter = create((set) => ({
count: 0,
step: 1,
increment: () => set((s) => ({ count: s.count + s.step }))
}));
function Counter() {
const { count, step } = useCounter(
useShallow((s) => ({ count: s.count, step: s.step }))
);
const doubled = useMemo(() => count * 2, [count]);
return (
<button onClick={() => useCounter.getState().increment()}>
{count} × 2 = {doubled}
</button>
);
}状态转换
set() 的更新函数在草稿上修改数据。运行时提交新状态,并可按需生成正向和逆向补丁,供历史记录、同步等集成使用。
set(() => { this.todos.push(todo) })coaction只让读取了变化部分的消费者失效。
@coaction/history撤销时应用逆向补丁,重做时应用正向补丁。需要保留对象引用关系时可使用快照。
@coaction/persist保存选定的状态。恢复时按 merge 策略合并,默认由存储值覆盖同名字段。
@coaction/sync默认持久化待发送的本地变更。拉取远端更新后,按冲突策略在新基线上重放这些变更。
@coaction/yjs把 store 绑定到 Yjs 文档。
coaction/sharedWorker 持有可写状态,每个连接的页面维护自己的镜像。
远程同步适配器FetchCRUDSupabaseFirestoreTanStack Query本地队列存储IndexedDB
共享运行时
符合共享 JSON 约束的状态定义可在 Worker 中复用。通过共享入口连接后,页面从本地镜像读取状态,调用远程 action 时通过 Promise 获取执行结果。
action补丁
Worker 持有状态,页面代理 action 并镜像每次提交。
// worker.ts
import { create } from '@coaction/react/shared';
import { counter } from './counter';
create(counter);
// App.tsx
import { create } from '@coaction/react/shared';
import { counter } from './counter';
const worker = new Worker(new URL('./worker.ts', import.meta.url), {
type: 'module'
});
const useCounter = create(counter, { worker });性能基准
两组基准使用同一个包含 1,000 件商品的购物车。第一组连续读取总价,第二组每次更新一件商品后再读取总价。图表显示两种场景各自的吞吐量。
CoactionZustand
84×对比每次读取都重新计算总价的选择器
1,000 件商品的购物车 · 每秒操作数 · 越高越好
直接读取已保存的总价更快。应用须在每次改变价格、数量或商品明细时同步更新总价。
0.55×每次更新后都读取时,相当于 Zustand 选择器吞吐量的比例
1,000 件商品的购物车 · 每秒操作数 · 越高越好
在这项测试中,Zustand 更新并读取手动维护的总价为每秒 2,897,112 次,Coaction 替换整个对象后读取 getter 为每秒 169 次。只更新单个商品时,可直接在草稿中修改对应元素的字段。
| 用例 | 每秒操作数 | 相对误差界限(RME) |
|---|---|---|
| Coaction 缓存的访问器 getter | 66,109,753 | ±1.67% |
| Coaction 手动声明依赖的计算值 | 33,809,569 | ±1.27% |
| Zustand 选择器重新计算 | 787,472 | ±0.13% |
| Zustand 手动维护的总价字段 | 112,062,542 | ±0.25% |
| 用例 | 每秒操作数 | 相对误差界限(RME) |
|---|---|---|
| Coaction 可变式更新 + 缓存 getter | 48,480 | ±0.81% |
| Coaction 可变式更新 + 手动依赖 | 48,318 | ±0.66% |
| Coaction 整体替换对象 + 缓存 getter | 169 | ±4.15% |
| Zustand 不可变更新 + 选择器重新计算 | 88,113 | ±0.48% |
| Zustand 不可变更新 + 手动维护总价 | 2,897,112 | ±0.53% |
Apple M1 Max,Node 24.16.0,运行时 12ae616,测于 2026-09-11。单机数据,并非发布门槛;不包含 React 渲染。 测试方法 性能指南
适用场景
Coaction 提供 Zustand 风格的 create() 接口,在不可变快照上支持 getter、绑定到状态的 this 和读取依赖追踪。下表对比三种库的状态模型与订阅方式。
| Coaction | Zustand | MobX | |
|---|---|---|---|
| 模型 | 不可变响应式状态 | 极简外部 store | 可变的可观察对象图 |
| 无需选择器的渲染追踪 | observer() 或选择器 | 选择器 + useShallow | observer() |
| get value() 与 this | 支持 | 不支持 | 支持 |
| 派生值记忆化 | 跨独立读取缓存 | useMemo 或 reselect | 默认仅在被观察时 |
| 冻结快照与结构共享 | 支持 | 借助 Immer 或 Mutative | 可变的可观察对象 |
| 状态转换与补丁支持 | 核心能力 | 非核心 | mobx-state-tree |
| Worker / SharedWorker 权威 | 内置共享运行时 | 由应用自行处理 | 由应用自行处理 |
| 本地优先同步 | @coaction/sync | 外部方案 | 取决于所用库 |
创建第一个 store,用 observer() 订阅组件读取的字段。需要保留历史记录、保存状态或与服务端同步时,再添加对应的中间件。
npm install coaction @coaction/react