Skip to content

when 分支与信号聚合

可视化边界:Mermaid 可以帮助看懂路由关系;导出配置时仍以具名条件与稳定事件名为准。

when() 的含义是“当指定信号到达时,启动这条链路”。

1. 路由与聚合图

如何阅读这张图

  • when() 先决定监听哪类信号,再决定多信号聚合模式。
  • and / or / simple_or 不是不同的 API 家族,而是相同 when() 的不同聚合语义。

2. 监听业务事件

python
flow.when("UserFeedback").to(finalize).end()

3. 监听状态变化

python
flow.when({"runtime_data": "profile"}).to(handle_profile)
flow.when({"flow_data": "feature_flag"}).to(handle_flag)

4. 监听 collect 结果

.collect("plan", "read") 发出的内部收敛事件名是:

  • Collect-plan

因此也可以写:

python
flow.when({"collect": "plan"}).to(handle_collected_plan)

5. 多信号模式

mode="and"

所有信号都到齐才继续:

python
flow.when({"event": ["A", "B"]}, mode="and").to(after_both)

mode="or"

任一信号到达即继续,并返回 (type, event, value)

python
flow.when({"event": ["A", "B"]}, mode="or").to(handle_first)

mode="simple_or"

任一信号到达即继续,但只传递 value

python
flow.when({"event": ["A", "B"]}, mode="simple_or").to(handle_value_only)

6. 当前最佳实践

  • 外部系统监听业务事件名,而不是内部 chunk trigger
  • when() 分支若负责最终结果,应显式 .end()set_result()
  • and 模式只用于确实需要“都到齐”的前置条件