WebsockexNova.Gun.Helpers.StateHelpers (WebsockexNova v0.1.1)

View Source

Helper 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

handle_connection_established(state, gun_pid)

@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 state
  • gun_pid - The Gun connection process PID

Returns

Updated connection state

handle_connection_failure(state, reason)

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 state
  • reason - The failure reason

Returns

Updated connection state

handle_disconnection(state, reason)

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 state
  • reason - The disconnect reason

Returns

Updated connection state

handle_ownership_transfer(state, info)

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:

  1. Validates that required information is present in the info map
  2. Updates the connection state with Gun PID and connection status
  3. Ensures a proper process monitor is established
  4. Migrates active streams information if available

Parameters

  • state - The current connection state
  • info - 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, ...}

handle_reconnection_attempt(state)

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

handle_websocket_upgrade(state, stream_ref)

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 state
  • stream_ref - The stream reference for the WebSocket connection

Returns

Updated connection state