WebsockexNova.Helpers.StateHelpers (WebsockexNova v0.1.1)
View SourceHelper 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
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.
@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.
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.
Parameters
state
- The current state maphandler_type
- The type of handler (must be an atom, e.g., :auth_handler, :error_handler)handler_module
- The module that implements the handler behaviorhandler_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.
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.
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.
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.
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.
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.