Coding a Re-Entrant Timer


#1

I wanted to fish for better ideas on how to make a resetable timer. A timer that can be reset to its original (or some new value) if called while running. In :ue4: C++ I’d do something like this:
GetWorld()->GetTimerManager().SetTimer(TimerHandle, this, &Class::Function, Time, false);

Here’s the seemingly obvious implementation I came up with.

// Some control variable defined somewhere
!reset? : false

// The timer coroutine
loop
[
  race
  [
    _wait(timer_duration)
    reset?._wait_true(0)
  ]

  [exit] when not reset?
  reset? := false
]

#2

Here’s another implementation that uses an InvokedCoroutine:

// Some InvokedCoroutine initialized in constructor
@co : branch[_wait(-1)]

// The entry point for controlling the timer
(Real time) 
[
  if @co.valid?
  [
    @co.abort
  ]
  @co : branch[_timer(time)]
]

// _timer
(Real time) 
[
  _wait(time)
  // do something here
]