Skip to content

Commit

Permalink
feat(discord-timer.c): added wrapper functions for common methods ass…
Browse files Browse the repository at this point in the history
…ociated with timers
  • Loading branch information
Anotra committed Apr 13, 2022
1 parent 5ec231b commit 19a50d3
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
63 changes: 63 additions & 0 deletions include/discord.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,69 @@ unsigned discord_timer_ctl(struct discord *client, struct discord_timer *timer);
unsigned discord_timer(struct discord *client, discord_ev_timer cb,
void *data, int64_t delay);

/**
* @brief get the data associated with the timer
*
* @param client the client created with discord_init()
* @param id id of the timer
* @param timer where to copy the timer data to
* @return true on success
* @return false on failure
*/
bool discord_timer_get(struct discord *client, unsigned id,
struct discord_timer *timer);

/**
* @brief starts a timer
*
* @param client the client created with discord_init()
* @param id id of the timer
* @return true on success
* @return false on failure
*/
bool discord_timer_start(struct discord *client, unsigned id);

/**
* @brief stops a timer
*
* @param client the client created with discord_init()
* @param id id of the timer
* @return true on success
* @return false on failure
*/
bool discord_timer_stop(struct discord *client, unsigned id);

/**
* @brief cancels a timer,
* this will delete the timer if DISCORD_TIMER_DELETE_AUTO is enabled
*
* @param client the client created with discord_init()
* @param id id of the timer
* @return true on success
* @return false on failure
*/
bool discord_timer_cancel(struct discord *client, unsigned id);

/**
* @brief deletes a timer
*
* @param client the client created with discord_init()
* @param id id of the timer
* @return true on success
* @return false on failure
*/
bool discord_timer_delete(struct discord *client, unsigned id);

/**
* @brief cancels, and deletes a timer
*
* @param client the client created with discord_init()
* @param id id of the timer
* @return true on success
* @return false on failure
*/
bool discord_timer_cancel_and_delete(struct discord *client, unsigned id);

/** @example timers.c
* Demonstrates the Timer API for callback scheduling */

Expand Down
65 changes: 65 additions & 0 deletions src/discord-timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,68 @@ discord_internal_timer(struct discord *client, discord_ev_timer cb,
{
return _discord_timer(client, &client->timers.internal, cb, data, delay);
}

bool
discord_timer_get(struct discord *client, unsigned id,
struct discord_timer *timer)
{
if (!id) return 0;
return priority_queue_get(client->timers.user.q, id, NULL, timer);
}

bool
discord_timer_start(struct discord *client, unsigned id)
{
struct discord_timer timer;
if (discord_timer_get(client, id, &timer)) {
if (timer.delay < 0)
timer.delay = 0;
return discord_timer_ctl(client, &timer);
}
return false;
}

bool
discord_timer_stop(struct discord *client, unsigned id)
{
struct discord_timer timer;
if (discord_timer_get(client, id, &timer)) {
int64_t disabled = -1;
return priority_queue_update(client->timers.user.q,
id, &disabled, &timer);
}
return false;
}

static bool
discord_timer_add_flags(struct discord *client, unsigned id,
enum discord_timer_flags flags)
{
struct discord_timer timer;
if (discord_timer_get(client, id, &timer)) {
timer.flags |= flags;
int64_t run_now = 0;
return priority_queue_update(client->timers.user.q,
id, &run_now, &timer);
}
return false;
}

bool
discord_timer_cancel(struct discord *client, unsigned id)
{
return discord_timer_add_flags(client, id, DISCORD_TIMER_CANCELED);
}

bool
discord_timer_delete(struct discord *client, unsigned id)
{
return discord_timer_add_flags(client, id, DISCORD_TIMER_DELETE);
}

bool
discord_timer_cancel_and_delete(struct discord *client, unsigned id)
{
return discord_timer_add_flags(client, id, DISCORD_TIMER_DELETE
| DISCORD_TIMER_CANCELED);
}

0 comments on commit 19a50d3

Please sign in to comment.