1 /// cycle call 2 module sfw.cycll; 3 4 import std.datetime; 5 import std.exception : enforce; 6 7 /++ For cases when can't use async methods and whey must 8 calls every N hnsecs, but strictly sync 9 +/ 10 struct CyCll 11 { 12 /// returns step for next call 13 Duration delegate() func; 14 /// next std time for call 15 long next = 0; 16 17 /// 18 this(Duration delegate() func) 19 { this.func = enforce(func, "bodyfunc is null"); } 20 21 /// if time is up call func, set next and return true 22 bool opCall() 23 { 24 if (now < next) return false; 25 next = now + func().total!"hnsecs"; 26 return true; 27 } 28 29 /// 30 static auto now() @property { return Clock.currStdTime; } 31 }