Skip to content

Commit

Permalink
Add the timer function to TaskManager
Browse files Browse the repository at this point in the history
We need a timer function in the operating or test environment.
This function is delegate called repeatedly or after a certain time.
  • Loading branch information
TrustHenry committed May 26, 2020
1 parent 3c79429 commit 6001ec3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
33 changes: 33 additions & 0 deletions source/agora/common/Task.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ module agora.common.Task;

import core.time;

/// Whether the timer periodic type
public enum Periodic : bool
{
No,
Yes
}

/// Exposes primitives to run tasks through Vibe.d
public class TaskManager
{
Expand Down Expand Up @@ -51,4 +58,30 @@ public class TaskManager
static import vibe.core.core;
vibe.core.core.sleep(dur);
}

/***************************************************************************
Run an asynchronous task after a given time.
The task will first run after the given `timeout`, and
can either repeat or run only once (the default).
Use the `setTimer` vibe.d's
Params:
timeout = Determines the minimum amount of time that elapses before
the timer fires.
dg = This delegate will be called when the timer fires.
periodic = Speficies if the timer fires repeatedly or only once.
***************************************************************************/

public void timer (Duration timeout, void delegate() dg,
Periodic periodic = Periodic.No)
{
assert(dg !is null, "Cannot call this delegate if null");

static import vibe.core.core;
vibe.core.core.setTimer(timeout, dg, periodic);
}
}

22 changes: 22 additions & 0 deletions source/agora/test/Base.d
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,28 @@ public class LocalRestTaskManager : TaskManager
{
geod24.LocalRest.sleep(dur);
}

/***************************************************************************
Run an asynchronous task after a given time in LocalRest's
The task will first run after the given `timeout`, and
can either repeat or run only once (the default).
Works similarly to Vibe.d's `setTimer`.
Params:
timeout = Determines the minimum amount of time that elapses before
the timer fires.
dg = This delegate will be called when the timer fires.
periodic = Speficies if the timer fires repeatedly or only once.
***************************************************************************/

public override void timer (Duration timeout, void delegate() dg,
Periodic periodic = Periodic.No)
{
geod24.LocalRest.setTimer(timeout, dg, periodic);
}
}

/// A ban manager with a fake clock for reliable unittesting
Expand Down

0 comments on commit 6001ec3

Please sign in to comment.