Raxol.Core.Utils.TimerUtils (Raxol v2.0.1)
View SourceConsolidated timer utilities for standardized timer management across Raxol.
Provides a unified interface for common timer patterns:
- Periodic timers (cleanup, health checks, optimization)
- Delayed execution timers
- Debounced timers
- Timer cancellation and management
This consolidates the 84+ timer patterns found across the codebase.
Summary
Functions
Cancels all timers in a state's timers map.
Cancels a timer if it exists and is valid.
Creates a debounced timer that cancels the previous timer of the same key. Useful for file watching, input handling, etc.
Gets a standard interval by name.
Standard timer intervals used across the application.
Cancels an existing timer and starts a new one (common pattern for debouncing).
Starts a delayed timer that sends a message after a specified delay.
Starts a periodic timer that sends messages at regular intervals.
Helper for common periodic timer patterns with state management.
Types
@type timer_opts() :: [interval: pos_integer(), message: term(), debounce_key: term()]
@type timer_ref() :: reference() | nil
@type timer_type() :: :periodic | :delayed | :debounced
Functions
@spec cancel_all_timers(map()) :: :ok
Cancels all timers in a state's timers map.
Examples
# In terminate/2 callback
TimerUtils.cancel_all_timers(state)
@spec cancel_timer(timer_ref()) :: :ok
Cancels a timer if it exists and is valid.
Examples
timer_ref = TimerUtils.start_delayed(self(), :cleanup, 5000)
:ok = TimerUtils.cancel_timer(timer_ref)
Creates a debounced timer that cancels the previous timer of the same key. Useful for file watching, input handling, etc.
State should have a timers map: %{timers: %{}}
Examples
# In GenServer state
new_state = TimerUtils.debounce_timer(
state,
:file_reload,
self(),
{:reload_file, path},
1000
)
@spec get_interval(atom()) :: pos_integer()
Gets a standard interval by name.
Examples
interval = TimerUtils.get_interval(:health_check) # 30_000
timer_ref = TimerUtils.start_periodic(self(), :health_check, interval)
Standard timer intervals used across the application.
@spec restart_timer(timer_ref(), pid(), term(), pos_integer()) :: timer_ref()
Cancels an existing timer and starts a new one (common pattern for debouncing).
Examples
# Debounced file reload - cancel previous and start new
timer_ref = TimerUtils.restart_timer(old_timer, self(), {:reload_file, path}, 1000)
@spec start_delayed(pid(), term(), pos_integer()) :: timer_ref()
Starts a delayed timer that sends a message after a specified delay.
Examples
# Cleanup after 60 seconds
timer_ref = TimerUtils.start_delayed(self(), :cleanup_timer, 60_000)
# Flush data after 1 second
timer_ref = TimerUtils.start_delayed(self(), :flush, 1_000)
@spec start_periodic(pid(), term(), pos_integer()) :: timer_ref()
Starts a periodic timer that sends messages at regular intervals.
Examples
# Health check every 30 seconds
timer_ref = TimerUtils.start_periodic(self(), :perform_health_check, 30_000)
# Performance monitoring every 5 seconds
timer_ref = TimerUtils.start_periodic(self(), :monitor_performance, 5_000)
Helper for common periodic timer patterns with state management.
Examples
# Start health check timer and update state
new_state = TimerUtils.start_periodic_with_state(
state,
:health_timer,
self(),
:perform_health_check,
:health_check
)