iii-state-reactions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseState Reactions
状态反应
Comparable to: Firebase onSnapshot, Convex mutations
类似技术:Firebase onSnapshot、Convex mutations
Key Concepts
核心概念
Use the concepts below when they fit the task. Not every state reaction needs all of them.
- A state trigger fires whenever a value changes within a watched scope
- The handler receives describing the change
{ new_value, old_value, key, event_type } - condition_function_id gates execution — the reaction only fires if the condition returns truthy
- Multiple reactions can independently watch the same scope
- Reactions fire on ,
state::set, andstate::updateoperationsstate::delete
当任务适配时使用以下概念,并非所有状态反应都需要用到全部概念。
- state trigger(状态触发器):当监视作用域内的值发生变更时触发
- 处理器会接收描述变更的 参数
{ new_value, old_value, key, event_type } - condition_function_id:用于控制执行——仅当条件返回真值时,反应才会触发
- 多个反应可以独立监视同一作用域
- 反应会在 、
state::set和state::update操作时触发state::delete
Architecture
架构
state::set / state::update / state::delete
→ StateModule emits change event
→ registerTrigger type:'state' (scope match)
→ condition_function_id check (if configured)
→ registerFunction handler ({ new_value, old_value, key, event_type })
state::set / state::update / state::delete
→ StateModule 发出变更事件
→ registerTrigger type:'state'(作用域匹配)
→ condition_function_id 检查(若已配置)
→ registerFunction 处理器 ({ new_value, old_value, key, event_type })
iii Primitives Used
iii 使用的原语
| Primitive | Purpose |
|---|---|
| Define the reaction handler |
| Watch a scope for changes |
| Scope filter and optional condition gate |
Event payload: | Change details passed to the handler |
| 原语 | 用途 |
|---|---|
| 定义反应处理器 |
| 监视作用域内的变更 |
| 作用域过滤器和可选的条件控制 |
事件负载: | 传递给处理器的变更详情 |
Reference Implementation
参考实现
See ../references/state-reactions.js for the full working example — a reaction that watches a state scope and fires side effects when values change, with an optional condition gate.
Also available in Python: ../references/state-reactions.py
Also available in Rust: ../references/state-reactions.rs
完整可运行示例请查看 ../references/state-reactions.js——这是一个监视状态作用域并在值变更时触发副作用的反应,带有可选的条件控制。
同时提供Python版本:../references/state-reactions.py
以及Rust版本:../references/state-reactions.rs
Common Patterns
常见模式
Code using this pattern commonly includes, when relevant:
- — worker initialization
registerWorker(url, { workerName }) - — define the reaction handler
registerFunction(id, handler) - — watch for changes
registerTrigger({ type: 'state', config: { scope, key, condition_function_id } }) - /
payload.new_value— compare before and afterpayload.old_value - — distinguish between set, update, and delete events
payload.event_type - — write derived state from the reaction
trigger({ function_id: 'state::set', payload }) - — structured logging per reaction
const logger = new Logger()
相关代码通常包含以下内容(视情况使用):
- —— 工作进程初始化
registerWorker(url, { workerName }) - —— 定义反应处理器
registerFunction(id, handler) - —— 监视变更
registerTrigger({ type: 'state', config: { scope, key, condition_function_id } }) - /
payload.new_value—— 比较变更前后的值payload.old_value - —— 区分 set、update 和 delete 事件
payload.event_type - —— 通过反应写入派生状态
trigger({ function_id: 'state::set', payload }) - —— 为每个反应添加结构化日志
const logger = new Logger()
Adapting This Pattern
模式适配
Use the adaptations below when they apply to the task.
- Set to watch a specific domain (e.g.
scope,orders)user-profiles - Use to narrow reactions to a single key within a scope
key - Add a to filter — only react when the condition function returns truthy
condition_function_id - Chain reactions by writing state in one handler that triggers another reaction on a different scope
当任务符合以下场景时,可进行相应适配:
- 设置 以监视特定领域(例如
scope、orders)user-profiles - 使用 将反应范围缩小到作用域内的单个键
key - 添加 进行过滤——仅当条件函数返回真值时才触发反应
condition_function_id - 通过在一个处理器中写入状态,触发另一个作用域上的反应,实现反应链式调用
Engine Configuration
引擎配置
StateModule must be enabled in iii-config.yaml for state triggers to fire. See ../references/iii-config.yaml for the full annotated config reference.
要使状态触发器生效,必须在 iii-config.yaml 中启用 StateModule。完整带注释的配置参考请查看 ../references/iii-config.yaml
Pattern Boundaries
模式边界
- If the task is about directly reading or writing state without reactions, prefer .
iii-state-management - If the task needs conditional trigger logic shared across trigger types, prefer .
iii-trigger-conditions - Stay with when the primary need is automatic side effects on state changes.
iii-state-reactions
- 如果任务仅涉及直接读写状态而无需反应,建议使用 。
iii-state-management - 如果任务需要跨触发器类型共享条件触发逻辑,建议使用 。
iii-trigger-conditions - 当主要需求是在状态变更时自动触发副作用时,应使用 。
iii-state-reactions
When to Use
使用场景
- Use this skill when the task is primarily about in the iii engine.
iii-state-reactions - Triggers when the request directly asks for this pattern or an equivalent implementation.
- 当任务主要涉及 iii 引擎中的 时,使用此技能。
iii-state-reactions - 当请求直接要求实现此模式或等效功能时触发。
Boundaries
边界限制
- Never use this skill as a generic fallback for unrelated tasks.
- You must not apply this skill when a more specific iii skill is a better fit.
- Always verify environment and safety constraints before applying examples from this skill.
- 切勿将此技能作为无关任务的通用 fallback 方案。
- 当有更合适的特定 iii 技能时,不得使用此技能。
- 在应用此技能中的示例前,务必验证环境和安全约束。