Skip to content

Commit

Permalink
feat: dpp::co_timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Jul 16, 2023
1 parent c730286 commit b56918c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ class DPP_EXPORT cluster {
*/
bool stop_timer(timer t);

#ifdef DPP_CORO
/**
* @brief Start a one-time timer. Use the co_await keyword on its return value to suspend the coroutine until the timer ends
*
* @param seconds How long to run the timer for
* @return awaitable<timer> co_await-able object holding the timer_handle
*/
awaitable<timer> co_timer(uint64_t seconds);
#endif

/**
* @brief Get the dm channel for a user id
*
Expand Down
11 changes: 11 additions & 0 deletions src/dpp/cluster/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ void cluster::tick_timers() {
}
}

#ifdef DPP_CORO
awaitable<timer> cluster::co_timer(uint64_t seconds) {
return {[this, seconds] (auto &&cb) {
start_timer([this, cb](dpp::timer handle) {
cb(handle);
stop_timer(handle);
}, seconds);
}};
}
#endif

oneshot_timer::oneshot_timer(class cluster* cl, uint64_t duration, timer_callback_t callback) : owner(cl) {
/* Create timer */
th = cl->start_timer([callback, this](dpp::timer timer_handle) {
Expand Down

0 comments on commit b56918c

Please sign in to comment.