WebsockexNova.Helpers.StateHelpers (WebsockexNova v0.1.1)

View Source

Helper functions for working with application state.

This module provides standardized methods for updating and managing different types of handler states within the application.

Summary

Functions

Adds a request to the request buffer. Returns the updated state.

Removes and cancels a timeout by id using the provided cancel_fun. Returns the updated state.

Moves all buffered requests to pending_requests and sets timeouts using the provided make_timer fun. Returns {new_state, sent_requests} where sent_requests is the list of flushed requests.

Retrieves the host from the state, adapter_state, or config, in that order. Returns nil if not found.

Retrieves the port from the state, adapter_state, or config, in that order. Returns nil if not found.

Retrieves the status from the state, adapter_state, or config, in that order. Returns nil if not found.

Stub for handle_ownership_transfer/2. Not yet implemented.

Removes a pending request and its timeout by id. Returns {from, new_state} where from is the original from pid (or nil).

Sets up a handler in the state with its initial state.

Updates the auth handler state within the main state map.

Updates the connection handler state within the main state map.

Updates the error handler state within the main state map.

Updates the message handler state within the main state map.

Updates the subscription handler state within the main state map.

Functions

buffer_request(state, frame, id, from)

@spec buffer_request(map(), term(), term(), term()) :: map()

Adds a request to the request buffer. Returns the updated state.

cancel_timeout(state, id, cancel_fun)

@spec cancel_timeout(map(), term(), (reference() -> any())) :: map()

Removes and cancels a timeout by id using the provided cancel_fun. Returns the updated state.

flush_buffer(state, make_timer_fun)

@spec flush_buffer(map(), (term() -> reference())) :: {map(), list()}

Moves all buffered requests to pending_requests and sets timeouts using the provided make_timer fun. Returns {new_state, sent_requests} where sent_requests is the list of flushed requests.

get_host(arg1)

@spec get_host(map()) :: String.t() | nil

Retrieves the host from the state, adapter_state, or config, in that order. Returns nil if not found.

get_port(arg1)

@spec get_port(map()) :: non_neg_integer() | nil

Retrieves the port from the state, adapter_state, or config, in that order. Returns nil if not found.

get_status(arg1)

@spec get_status(map()) :: atom() | nil

Retrieves the status from the state, adapter_state, or config, in that order. Returns nil if not found.

handle_ownership_transfer(state, info)

Stub for handle_ownership_transfer/2. Not yet implemented.

pop_pending_request(state, id)

@spec pop_pending_request(map(), term()) :: {term() | nil, map()}

Removes a pending request and its timeout by id. Returns {from, new_state} where from is the original from pid (or nil).

setup_handler(state, handler_type, handler_module)

setup_handler(state, handler_type, handler_module, handler_options)

setup_handler(state, handler_type, handler_module, handler_options, callback_name)

@spec setup_handler(map(), atom(), module(), term(), atom()) :: map()

Sets up a handler in the state with its initial state.

Parameters

  • state - The current state map
  • handler_type - The type of handler (must be an atom, e.g., :auth_handler, :error_handler)
  • handler_module - The module that implements the handler behavior
  • handler_options - Options to pass to the handler's init function (if it has one)
  • callback_name - The name of the callback function to call for initializing the handler

Returns

The updated state map with the new handler and its state.

Note

handler_type must be an atom. The handler state will be stored under the key {handler_type, :state} in the handlers map (e.g., {:auth_handler, :state}). This avoids dynamic atom creation and is safe for all inputs.

update_auth_handler_state(state, auth_handler_state)

@spec update_auth_handler_state(map(), term()) :: map()

Updates the auth handler state within the main state map.

The handler state is stored under the key {:auth_handler, :state} in the handlers map.

update_connection_handler_state(state, connection_handler_state)

@spec update_connection_handler_state(map(), term()) :: map()

Updates the connection handler state within the main state map.

The handler state is stored under the key {:connection_handler, :state} in the handlers map.

update_error_handler_state(state, error_handler_state)

@spec update_error_handler_state(map(), term()) :: map()

Updates the error handler state within the main state map.

The handler state is stored under the key {:error_handler, :state} in the handlers map.

update_message_handler_state(state, message_handler_state)

@spec update_message_handler_state(map(), term()) :: map()

Updates the message handler state within the main state map.

The handler state is stored under the key {:message_handler, :state} in the handlers map.

update_subscription_handler_state(state, subscription_handler_state)

@spec update_subscription_handler_state(map(), term()) :: map()

Updates the subscription handler state within the main state map.

The handler state is stored under the key {:subscription_handler, :state} in the handlers map.