1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.tasks.delayedtasks;
7 
8 import diamond.core.apptype;
9 
10 static if (isWeb)
11 {
12 
13   public import core.time : Duration, dur, weeks, days, hours, minutes, seconds,
14                             msecs, usecs, hnsecs, nsecs;
15 
16   import diamond.tasks.core;
17 
18   /**
19   * Executes a delayed asynchronous task.
20   * Params:
21   *   delay = The time to delay the task.
22   *   task =  The task to execute.
23   *   args =  The arguments to pass to the task.
24   */
25   void delayTask(ARGS...)(Duration delay, void delegate(ARGS) @safe task, auto ref ARGS args)
26   {
27     sleep(delay);
28 
29     executeTask(task, args);
30   }
31 
32   /**
33   * Executes a delayed asynchronous task.
34   * Params:
35   *   delay = The time to delay the task.
36   *   task =  The task to execute.
37   *   args =  The arguments to pass to the task.
38   */
39   void delayTask(ARGS...)(Duration delay, void delegate(ARGS) @system task, auto ref ARGS args) @system
40   {
41     sleep(delay);
42 
43     executeTask(task, args);
44   }
45 
46   /**
47   * Executes a delayed asynchronous task.
48   * Params:
49   *   delay = The time to delay the task.
50   *   task =  The task to execute.
51   *   args =  The arguments to pass to the task.
52   */
53   void delayTask(CALLABLE, ARGS...)(Duration delay, CALLABLE task, auto ref ARGS args)
54   if (!is(CALLABLE : void delegate(ARGS)) && is(typeof(CALLABLE.init(ARGS.init))))
55   {
56     sleep(delay);
57 
58     executeTask(task, args);
59   }
60 }