www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Vibe.d timer - change callback?

reply codic <codic linuxmail.org> writes:
I'd like to be able to change the callback of a vibe.d Timer (eg 
created with 
http://vibe-core.dpldocs.info/v1.9.3/vibe.core.core.createTimer.html) after
creation, something like:

auto timer = createTimer();
timer.rearm(duration, /*...*/);
timer.callback = delegate {
    // things
}

An alternative method would be to recreate the timer, like so:

auto timer = createTimer();
timer.rearm(duration, /*...*/);
auto timer = createTimer();
timer.rearm(duration - timer.elapsed);

However, I can't find an `elapsed` property or similar in the 
documentation.
Aug 25 2020
next sibling parent FreeSlave <freeslave93 gmail.com> writes:
On Tuesday, 25 August 2020 at 18:42:53 UTC, codic wrote:
 I'd like to be able to change the callback of a vibe.d Timer 
 (eg created with 
 http://vibe-core.dpldocs.info/v1.9.3/vibe.core.core.createTimer.html) after
creation, something like:

 auto timer = createTimer();
 timer.rearm(duration, /*...*/);
 timer.callback = delegate {
    // things
 }

 An alternative method would be to recreate the timer, like so:

 auto timer = createTimer();
 timer.rearm(duration, /*...*/);
 auto timer = createTimer();
 timer.rearm(duration - timer.elapsed);

 However, I can't find an `elapsed` property or similar in the 
 documentation.
I did not try it myself, but you can make an object which is accessible from both callback scope and outside scope. Depending on the state of the object it will call different functions. So if you want to change the callback in the meantime, you just change the state of the object.
Aug 27 2020
prev sibling parent James Blachly <james.blachly gmail.com> writes:
On Tuesday, 25 August 2020 at 18:42:53 UTC, codic wrote:
 I'd like to be able to change the callback of a vibe.d Timer 
 (eg created with 
 http://vibe-core.dpldocs.info/v1.9.3/vibe.core.core.createTimer.html) after
creation, something like:

 auto timer = createTimer();
 timer.rearm(duration, /*...*/);
 timer.callback = delegate {
    // things
 }
Would creating a dynamic redirection function as delegate serve your purpose? https://dlang.org/library/std/functional/to_delegate.html You can also pass object having `opCall` to `to_delegate` and th is could also provide dynamic dispatch.
Aug 27 2020