iii-custom-triggers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCustom Triggers
自定义触发器
Comparable to: Custom event adapters, webhook receivers
类似功能:自定义事件适配器、webhook接收器
Key Concepts
核心概念
Use the concepts below when they fit the task. Not every custom trigger needs all of them.
- registerTriggerType(id, handler) defines a new trigger type with and
registerTriggercallbacksunregisterTrigger - The handler receives a TriggerConfig containing ,
id, andfunction_idconfig - When the external event fires, call to invoke the registered function
iii.trigger(function_id, event) - unregisterTriggerType cleans up when the trigger type is no longer needed
- Do not reuse built-in trigger type names: ,
http,cron,queue,state,streamsubscribe
当任务需要时使用以下概念,并非所有自定义触发器都需要包含全部概念。
- registerTriggerType(id, handler) 用于定义新的触发器类型,包含和
registerTrigger回调函数unregisterTrigger - 处理器会接收包含、
id和function_id的TriggerConfig配置config - 当外部事件触发时,调用来执行已注册的函数
iii.trigger(function_id, event) - unregisterTriggerType 用于在触发器类型不再需要时清理资源
- 请勿复用内置触发器类型名称:、
http、cron、queue、state、streamsubscribe
Architecture
架构
External event source (webhook, file watcher, IoT, CDC, etc.)
→ Custom trigger handler (registerTriggerType)
→ iii.trigger(function_id, event)
→ Registered function processes the event
External event source (webhook, file watcher, IoT, CDC, etc.)
→ Custom trigger handler (registerTriggerType)
→ iii.trigger(function_id, event)
→ Registered function processes the event
iii Primitives Used
使用的iii原语
| Primitive | Purpose |
|---|---|
| Define a new trigger type with lifecycle hooks |
| Clean up a custom trigger type |
| Configuration passed to the trigger handler |
| Fire the registered function when the event occurs |
| 原语 | 用途 |
|---|---|
| 定义带有生命周期钩子的新触发器类型 |
| 清理自定义触发器类型 |
| 传递给触发器处理器的配置 |
| 事件发生时触发已注册的函数 |
Reference Implementation
参考实现
See ../references/custom-triggers.js for the full working example — a custom trigger type that listens for external events and routes them to registered functions.
Also available in Python: ../references/custom-triggers.py
Also available in Rust: ../references/custom-triggers.rs
完整可运行示例请查看 ../references/custom-triggers.js —— 这是一个监听外部事件并将其路由到已注册函数的自定义触发器类型。
同时提供Python版本:../references/custom-triggers.py
以及Rust版本:../references/custom-triggers.rs
Common Patterns
常见模式
Code using this pattern commonly includes, when relevant:
- — worker initialization
registerWorker(url, { workerName }) - — define the custom trigger
registerTriggerType(id, { registerTrigger, unregisterTrigger }) - — called by iii when a function subscribes to this trigger type
registerTrigger(config) - — called by iii when a function unsubscribes
unregisterTrigger(config) - — fire the target function
iii.trigger(config.function_id, eventPayload) - Cleanup logic in (close connections, remove listeners, clear intervals)
unregisterTrigger - — structured logging
const logger = new Logger()
使用此模式的代码通常会包含以下相关内容:
- —— 工作器初始化
registerWorker(url, { workerName }) - —— 定义自定义触发器
registerTriggerType(id, { registerTrigger, unregisterTrigger }) - —— 当函数订阅此触发器类型时,由iii调用
registerTrigger(config) - —— 当函数取消订阅此触发器类型时,由iii调用
unregisterTrigger(config) - —— 触发目标函数
iii.trigger(config.function_id, eventPayload) - 中的清理逻辑(关闭连接、移除监听器、清除定时器)
unregisterTrigger - —— 结构化日志
const logger = new Logger()
Adapting This Pattern
模式适配
Use the adaptations below when they apply to the task.
- Choose a unique trigger type name that describes your event source (e.g. ,
file-watcher,mqtt)db-cdc - In , start the listener (open socket, poll endpoint, subscribe to topic)
registerTrigger - In , tear down the listener to avoid resource leaks
unregisterTrigger - Store active listeners in a map keyed by for clean unregistration
config.id - Pass relevant event data in the payload when calling
iii.trigger(function_id, event)
当任务适用时,可采用以下适配方式:
- 选择一个能描述事件源的唯一触发器类型名称(例如、
file-watcher、mqtt)db-cdc - 在中启动监听器(打开套接字、轮询端点、订阅主题)
registerTrigger - 在中销毁监听器,避免资源泄漏
unregisterTrigger - 将活跃监听器存储在以为键的映射中,以便干净地取消注册
config.id - 调用时,在负载中传递相关事件数据
iii.trigger(function_id, event)
Pattern Boundaries
模式边界
- If the task uses built-in HTTP routes, prefer .
iii-http-endpoints - If the task uses built-in cron schedules, prefer .
iii-cron-scheduling - If the task uses built-in queue triggers, prefer .
iii-queue-processing - Stay with when iii has no built-in trigger type for the event source.
iii-custom-triggers
- 如果任务使用内置HTTP路由,优先使用。
iii-http-endpoints - 如果任务使用内置 cron 调度,优先使用。
iii-cron-scheduling - 如果任务使用内置队列触发器,优先使用。
iii-queue-processing - 当iii没有针对该事件源的内置触发器类型时,才使用。
iii-custom-triggers
When to Use
使用场景
- Use this skill when the task is primarily about in the iii engine.
iii-custom-triggers - Triggers when the request directly asks for this pattern or an equivalent implementation.
- 当任务主要涉及iii引擎中的时,使用此技能。
iii-custom-triggers - 当请求直接要求使用此模式或等效实现时触发。
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技能更适合时,不得应用此技能。
- 在应用此技能中的示例之前,务必验证环境和安全约束。