iii-reactive-backend

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Reactive Backend

响应式后端

Comparable to: Convex, Firebase, Supabase, Appwrite
同类产品:Convex、Firebase、Supabase、Appwrite

Key Concepts

核心概念

Use the concepts below when they fit the task. Not every reactive backend needs every trigger or realtime surface shown here.
  • State is the "database" — CRUD via
    state::set/get/update/delete/list
  • State triggers fire automatically when any value in a scope changes
  • Side effects (notifications, metrics, stream pushes) are wired reactively, not imperatively
  • Streams deliver real-time updates to connected clients
当以下概念符合任务需求时使用。并非每个响应式后端都需要用到此处展示的所有触发器或实时交互面。
  • State(状态)即“数据库”——通过
    state::set/get/update/delete/list
    实现CRUD操作
  • 状态触发器会在某个作用域内的任意值发生变化时自动触发
  • 副作用(通知、指标统计、流推送)以响应式方式关联,而非命令式
  • 负责向已连接的客户端传递实时更新

Architecture

架构

text
HTTP CRUD endpoints
  → state::set/update/delete (writes to 'todos' scope)
    ↓ (automatic state triggers)
    → on-change → stream::send (push to clients)
    → update-metrics → state::update (aggregate counters)

HTTP GET /metrics → reads from 'todo-metrics' scope
WebSocket clients ← stream 'todos-live'
text
HTTP CRUD endpoints
  → state::set/update/delete (writes to 'todos' scope)
    ↓ (automatic state triggers)
    → on-change → stream::send (push to clients)
    → update-metrics → state::update (aggregate counters)

HTTP GET /metrics → reads from 'todo-metrics' scope
WebSocket clients ← stream 'todos-live'

iii Primitives Used

使用的iii原语

PrimitivePurpose
registerWorker
Initialize the worker and connect to iii
registerFunction
CRUD handlers and reactive side effects
trigger({ function_id: 'state::...', payload })
Database layer
registerTrigger({ type: 'state', config: { scope } })
React to any change in a scope
trigger({ ..., action: TriggerAction.Void() })
Fire-and-forget stream push to clients
registerTrigger({ type: 'http' })
REST endpoints
原语用途
registerWorker
初始化Worker并连接到iii引擎
registerFunction
CRUD处理器和响应式副作用
trigger({ function_id: 'state::...', payload })
数据库层
registerTrigger({ type: 'state', config: { scope } })
响应某个作用域内的任意变化
trigger({ ..., action: TriggerAction.Void() })
以“触发即遗忘”的方式向客户端推送流
registerTrigger({ type: 'http' })
REST端点

Reference Implementation

参考实现

See ../references/reactive-backend.js for the full working example — a real-time todo app with CRUD endpoints, automatic change broadcasting via streams, and reactive aggregate metrics.
查看 ../references/reactive-backend.js 获取完整可运行示例——一个具备CRUD端点、通过流自动广播变更、以及响应式聚合指标统计的实时待办事项应用。

Common Patterns

常见模式

Code using this pattern commonly includes, when relevant:
  • registerWorker(url, { workerName })
    — worker initialization
  • trigger({ function_id: 'state::set/get', payload: { scope, key, value } })
    — CRUD via state module
  • registerTrigger({ type: 'state', function_id, config: { scope } })
    — reactive side effects on state change
  • Event argument destructuring in reactive handlers:
    async (event) => { const { new_value, old_value, key } = event }
  • trigger({ function_id: 'stream::send', payload, action: TriggerAction.Void() })
    — push live updates to clients
  • const logger = new Logger()
    — structured logging inside handlers
当相关时,使用此模式的代码通常包含以下内容:
  • registerWorker(url, { workerName })
    —— Worker初始化
  • trigger({ function_id: 'state::set/get', payload: { scope, key, value } })
    —— 通过state模块实现CRUD
  • registerTrigger({ type: 'state', function_id, config: { scope } })
    —— 状态变化时触发响应式副作用
  • 响应式处理器中的事件参数解构:
    async (event) => { const { new_value, old_value, key } = event }
  • trigger({ function_id: 'stream::send', payload, action: TriggerAction.Void() })
    —— 向客户端推送实时更新
  • const logger = new Logger()
    —— 处理器内的结构化日志

Adapting This Pattern

模式适配

Use the adaptations below when they apply to the task.
  • State triggers fire on any change in the scope — use the
    event
    argument (
    new_value
    ,
    old_value
    ,
    key
    ) to determine what changed
  • Multiple functions can react to the same scope independently (on-change and update-metrics both watch
    todos
    )
  • Stream clients connect via
    ws://host:port/stream/{stream_name}/{group_id}
  • Keep reactive functions fast — offload heavy work to queues if needed
当以下适配方案适用于任务时使用:
  • 状态触发器会在作用域内任意变化时触发——使用
    event
    参数(
    new_value
    old_value
    key
    )判断具体变更内容
  • 多个函数可以独立响应同一作用域的变化(on-change和update-metrics都监听
    todos
    作用域)
  • 流客户端通过
    ws://host:port/stream/{stream_name}/{group_id}
    连接
  • 保持响应式函数运行快速——若有需要,将繁重工作卸载到队列中

Pattern Boundaries

模式边界

  • If the request focuses on registering external/legacy HTTP endpoints via
    registerFunction
    (especially with endpoint lists like
    { path, id }
    plus iteration), prefer
    iii-http-invoked-functions
    .
  • Stay with
    iii-reactive-backend
    when state scopes, state triggers, and live stream updates are the core requirement.
  • 如果需求重点是通过
    registerFunction
    注册外部/遗留HTTP端点(尤其是包含
    { path, id }
    这类端点列表并涉及迭代的场景),请优先使用
    iii-http-invoked-functions
    模式。
  • 当状态作用域、状态触发器和实时流更新是核心需求时,使用
    iii-reactive-backend
    模式。

When to Use

使用场景

  • Use this skill when the task is primarily about
    iii-reactive-backend
    in the iii engine.
  • Triggers when the request directly asks for this pattern or an equivalent implementation.
  • 当任务主要围绕iii引擎中的
    iii-reactive-backend
    展开时,使用此技能。
  • 当请求直接要求实现此模式或等效方案时触发。

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技能更适合时,不得应用此技能。
  • 在应用此技能中的示例前,务必验证环境和安全约束。