@ actuallab/ 组合
类型脚本执行 组合核心: 纪念
自动依赖跟踪和串联无效计算。 它给你
Computed<T>,则 @computeMethod 装饰者, ComputedState<T>, (中文(简体) ), MutableState<T>,以及
UIActionTracker ——同样抽象的"融合"在.NET上提供.
这个软件包独立工作(客户端被动状态,不需要服务器). 添加
@actuallab/fusion-rpc 消费
无效的 .NET 服务器的计算服务,以及
@actuallab/fusion-react 让他们成为
在反应.
安装
npm install @actuallab/fusion
无害环境管理首先采用CJS制;船舶自用 .d.ts。仅取决于 @actuallab/core。 。 。 .
计算方法
@computeMethod 相当于 .NET 的 [ComputeMethod] 它包着一个方法 与缓冲和
依赖跟踪。 结果按键 JSON.stringify 辩论.
import { computeMethod } from '@actuallab/fusion';
class CounterService {
private _counters = new Map<string, number>();
@computeMethod
async get(key: string): Promise<number> {
return this._counters.get(key) ?? 0;
}
@computeMethod
async sum(key1: string, key2: string): Promise<number> {
// sum() automatically depends on get(key1) and get(key2)
return (await this.get(key1)) + (await this.get(key2));
}
increment(key: string): void {
this._counters.set(key, (this._counters.get(key) ?? 0) + 1);
(this.get as any).invalidate(key); // cascades into sum()
}
}
没有 Invalidation.Begin() 这里的块: 每个边框计算方法都包含一个
.invalidate(...args) 函数。 对于独立的函数,使用 wrapComputeMethod。 。 。 .
反应状态
ComputedState<T> 每当它使用的东西被作废时即重新计算;
MutableState<T> 并使用同一依赖图.
import { ComputedState, MutableState, FixedDelayer } from '@actuallab/fusion';
const state = new ComputedState(
async () => `Count: ${await counters.get('a')}`,
{ initialValue: 'loading...', updateDelayer: FixedDelayer.get(500) },
);
await state.whenFirstTimeUpdated();
state.value; // "Count: 0" — and it updates on its own from here on
state.dispose(); // required: stops the update loop
const query = new MutableState('');
query.set('fusion'); // invalidates every computation that called query.use()
API 表面
| API 密码 | 说明 |
|---|---|
Computed<T>, (中文(简体) ), ConsistencyState | 缓存计算结果 : .value, (中文(简体) ), .update(), (中文(简体) ), .use(), (中文(简体) ), .invalidate(), (中文(简体) ), .whenInvalidated() |
Computed.capture(fn) | 抓起来 Computed<T> 计算产生的呼叫 |
computeMethod, (中文(简体) ), wrapComputeMethod | 将方法 / 函数转换为计算函数 |
ComputedOptions | 按方法选择,例如:. errorAutoInvalidateDelay |
ComputedRegistry, (中文(简体) ), ComputeFunction, (中文(简体) ), ComputeContext | 内核,用于高级/海关一体化 |
State<T>, (中文(简体) ), ComputedState<T>, (中文(简体) ), MutableState<T> | 反应状态 |
UpdateDelayer, (中文(简体) ), FixedDelayer, (中文(简体) ), UIUpdateDelayer, (中文(简体) ), defaultUpdateDelayer | 重算间距(%% 32 ms 地板, 重试后退) |
UIActionTracker, (中文(简体) ), uiActions | 跟踪运行 UI 命令, 收集错误, 允许即时更新 |
页:1
- 跨度的依赖跟踪
await。 。 。 . 在节点20.16AsyncContext由AsyncLocalStorage,所以它只是工作。 (笑声) 浏览器中的孩子AsyncContext交给你 将方法计算为有线索的论证——接受并转发入巢计算调用. 见 AsyncContext:为什么它很重要。 。 。 . - 处理你的
ComputedState编号. 否则他们的更新循环会持续运行. - 错误. 默认 1 s 后自动验证错误输出( 可配置)
通过
@computeMethod({ errorAutoInvalidateDelay })),各州从不这样做——它们用后退来重试. 取消形状错误永远不会被缓存 .
文档
@actuallab/fusion参考文献 ——完整的API表格和语义- 类型脚本端口概览
- 计算服务 财务报告和审定财务报表 国家 ——概念,以.NET术语表示
- TodoApp 样本( 反应 + Fusion)
许可证
麻省理工学院——见 自由主义。 。 。 .