WebsockexNova.Gun.Helpers.StateHelpers (WebsockexNova v0.1.0)
View SourceHelper functions for consistent state operations across the codebase.
This module provides a single source of truth for common state update operations, ensuring that state mutations are consistent throughout the application.
It also includes standardized logging for state transitions to maintain consistent log messages and log levels across the application.
Summary
Functions
Updates state for a successful connection.
Updates state for a connection failure.
Updates state for a disconnection event.
Updates state when receiving connection ownership info from another process.
Updates state for a reconnection attempt.
Updates state for a successful WebSocket upgrade.
Functions
@spec handle_connection_established(WebsockexNova.Gun.ConnectionState.t(), pid()) :: WebsockexNova.Gun.ConnectionState.t()
Updates state for a successful connection.
Performs standard updates when a connection is established:
- Updates the Gun PID
- Sets status to :connected
- Resets reconnection attempts
- Logs the connection establishment
Parameters
state
- The current connection stategun_pid
- The Gun connection process PID
Returns
Updated connection state
@spec handle_connection_failure(WebsockexNova.Gun.ConnectionState.t(), term()) :: WebsockexNova.Gun.ConnectionState.t()
Updates state for a connection failure.
Performs standard updates when a connection fails:
- Records the error reason
- Sets status to :error
- Logs the failure with the reason
Parameters
state
- The current connection statereason
- The failure reason
Returns
Updated connection state
@spec handle_disconnection(WebsockexNova.Gun.ConnectionState.t(), term()) :: WebsockexNova.Gun.ConnectionState.t()
Updates state for a disconnection event.
Performs standard updates when a connection is lost:
- Records the disconnect reason
- Sets status to :disconnected
- Logs the disconnection with the reason
Parameters
state
- The current connection statereason
- The disconnect reason
Returns
Updated connection state
@spec handle_ownership_transfer(WebsockexNova.Gun.ConnectionState.t(), map()) :: WebsockexNova.Gun.ConnectionState.t()
Updates state when receiving connection ownership info from another process.
This function is critical for proper Gun process ownership transfer between
processes. When a process transfers Gun ownership, it sends a :gun_info
message containing state details to help rebuild the state in the new owner.
The function:
- Validates that required information is present in the info map
- Updates the connection state with Gun PID and connection status
- Ensures a proper process monitor is established
- Migrates active streams information if available
Parameters
state
- The current connection stateinfo
- Connection info map from previous owner with the following keys::gun_pid
- (required) The Gun process PID:status
- (required) Current connection status:host
- Hostname of connection:port
- Port of connection:path
- Path for WebSocket endpoint:active_streams
- Map of active stream references
Returns
Updated connection state
Examples
iex> handle_ownership_transfer(state, %{gun_pid: pid, status: :connected})
%ConnectionState{gun_pid: pid, status: :connected, ...}
@spec handle_reconnection_attempt(WebsockexNova.Gun.ConnectionState.t()) :: WebsockexNova.Gun.ConnectionState.t()
Updates state for a reconnection attempt.
Performs standard updates when attempting to reconnect:
- Increments the reconnection attempt counter
- Sets status to :reconnecting
- Logs the reconnection attempt with the attempt number
Parameters
state
- The current connection state
Returns
Updated connection state
@spec handle_websocket_upgrade(WebsockexNova.Gun.ConnectionState.t(), reference()) :: WebsockexNova.Gun.ConnectionState.t()
Updates state for a successful WebSocket upgrade.
Performs standard updates when a WebSocket connection is established:
- Updates the stream status to :websocket
- Sets overall connection status to :websocket_connected
- Logs the WebSocket upgrade
Parameters
state
- The current connection statestream_ref
- The stream reference for the WebSocket connection
Returns
Updated connection state