Raxol.Core.Utils.TimerUtils (Raxol v2.0.1)

View Source

Consolidated 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

timer_opts()

@type timer_opts() :: [interval: pos_integer(), message: term(), debounce_key: term()]

timer_ref()

@type timer_ref() :: reference() | nil

timer_type()

@type timer_type() :: :periodic | :delayed | :debounced

Functions

cancel_all_timers(arg1)

@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)

cancel_timer(timer_ref)

@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)

debounce_timer(state, key, pid, message, delay)

@spec debounce_timer(map(), term(), pid(), term(), pos_integer()) :: map()

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
)

get_interval(name)

@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)

intervals()

Standard timer intervals used across the application.

restart_timer(old_timer, pid, message, delay)

@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)

start_delayed(pid, message, delay)

@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)

start_periodic(pid, message, interval)

@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)

start_periodic_with_state(state, timer_key, pid, message, interval_name)

@spec start_periodic_with_state(map(), term(), pid(), term(), atom()) :: map()

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
)