1 /// Implementation of a clock adjusted programmatically, most for testing.
2 module fluid.time_machine;
3 
4 import core.time;
5 
6 import fluid.node;
7 import fluid.utils;
8 import fluid.types;
9 import fluid.node_chain;
10 
11 import fluid.io.time;
12 
13 @safe:
14 
15 alias timeMachine = nodeBuilder!TimeMachine;
16 
17 /// A time machine makes it possible to programmatically adjust and skip time that is used by Fluid nodes.
18 ///
19 /// The main use of a `TimeMachine` is to artificially control passage of time while running tests. This means
20 /// that a test can imitate a change in time without waiting for it, speeding up tests.
21 ///
22 /// See_Also:
23 ///     `core.time.MonoTime`, `fluid.io.time.TimeIO`
24 class TimeMachine : NodeChain, TimeIO {
25 
26     mixin controlIO;
27 
28     public {
29         MonoTime time;
30     }
31 
32     this(Node next = null) {
33         super(next);
34         this.time = MonoTime.currTime();
35     }
36 
37     override void beforeResize(Vector2) {
38         startIO();
39     }
40 
41     override void afterResize(Vector2) {
42         stopIO();
43     }
44 
45     override MonoTime now() nothrow {
46         return time;
47     }
48 
49     /// Add, or subtract time from the machine's clock.
50     /// Params:
51     ///     rhs = Time value to add or subtract.
52     /// Returns:
53     ///     The same time machine.
54     TimeMachine opOpAssign(string op)(Duration rhs) nothrow {
55         time += rhs;
56         return this;
57     }
58 
59 }