thorium-cfx/mono_v2_get_started

Intervals

Marky-S opened this issue ยท 4 comments

Goal

Run intervals such as JavaScript setInterval() directly from scheduler.

Importancy

Quality of Life (QoL)

API and/or potential implementation

int intervalCookie = Scheduler.SetInterval(Action coroutine, ulong interval); // create
...
void Scheduler.ClearInterval(int intervalCookie); // stop and destroy

Extra

No response

Interesting addition, can go together with another request to start a delayed action.

I do want to find different words for interval as it's a misnomer for such operations and definitionally incorrect. Interval (singular) meaning "an intervening time or space" among other similar, i.e.: not repeating, only with time in between. Set and Clear also needs consideration, your comments "create" and "stop and destroy" are already more descriptive.

This could also go as overloads to Scheduler.Schedule, e.g.:

  • Schedule(Action action, ulong delay)
  • Schedule(Action action, ulong delay, bool repeat)

I completely agree with you, it looks much better this way.

But what about stopping and destroying?

If you make it as an overload, then it will be right to give the opportunity to stop and destroy the scheduled execution of both scenarios

May be some kind of Task's CancellationToken, or returning scheduled coroutine handle for deletion

Scheduler.Cancel(int cookie);

Cancel sounds good, will probably get to this in the next set of feature changes

I ended up with the following:

// single delayed execution:
await Coroutine.Schedule(myAction, delay);

Canceler canceler = new Canceler(); // { ThrowOnCancelation = true };
await Coroutine.Schedule(myAction, delay, canceler);


// repeating delayed execution:
await Coroutine.Schedule(myAction, delay, iterations); // repeats x amount of iterations
await Coroutine.Schedule(myAction, delay, Repeat.Infinite); // repeats infinitely

Canceler canceler = new Canceler(); // { ThrowOnCancelation = true };
await Coroutine.Schedule(myAction, delay, iterations, canceler); //  repeats x amount of iterations
await Coroutine.Schedule(myAction, delay, Repeat.Infinite, canceler); // repeats infinitely
  • myAction: Action or Func<Coroutine>
  • delay: ulong
  • iterations: ulong, Repeat.Infinite or ulong.MaxValue for infinite runs/repeatment
  • canceler: CancelerToken
  • await Coroutine.Schedule(...) for async await
  • _ = Coroutine.Schedule(...) for async non-await, equivalent to JS' setTimeout and setInterval