跳到主要内容

空闲定时器

查看 Markdown

这个模式以前叫作不活跃追踪定时器(Inactiveness Tracker Timer)。

当一个动作只应在一段时间没有活动后才发生时,使用这个模式。InactivenessTrackerFlow 从 TrackerStep 开始,等待 Timer 或 Active Channel。如果活动先到达,TrackerStep 转回自身,从而开始一个新的 Timer。如果 Timer 先触发,Flow 转到 ProcessInactiveness。

核心 Step

inactiveness_tracker_flow.pyPython
class TrackerStep(Step[None]):
def __init__(self, process_inactiveness: ProcessInactiveness, active_channel: Channel[None]) -> None:
self.process_inactiveness = process_inactiveness
self.active_channel = active_channel

def wait_for(self, context: Context, input: None) -> Wait:
del context, input
return Wait.any_of(Timer.by_duration(TRACKER_DURATION), self.active_channel.for_one())

def execute(self, context: Context, input: None) -> StepDecision:
del input
if context.has_timer_fired():
return go_to(ProcessInactiveness, None)
return go_to(TrackerStep, None)

例子: examples/python/dex_examples/patterns/inactiveness-tracker-timer/inactiveness_tracker_flow.py

下一步