All files / modules/state StateManager.ts

100% Statements 20/20
100% Branches 6/6
100% Functions 6/6
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                    6x                 6x 6x 6x 6x 6x             6x 1x 1x             6x 1x 1x             6x 1x 1x             6x 1x 1x 6x   1x  
import { DevicesStore } from "~/types/DevicesStore";
import { HubDevice } from "~/types/HubDevice";
import { SocketConnectionStatus } from "~/types/SocketConnection";
import { SocketConnectionStore } from "~/types/SocketConnectionStore";
import { TokenStore } from "~/types/TokenStore";
 
/**
 * Manages the state of the application by interacting with token, connection, and device states.
 * Provides methods to get and update the token, connection status, and device list.
 */
class StateManager {
	/**
	 * Creates an instance of StateManager.
	 * Initializes with the given stores for token, connection, and device states.
	 *
	 * @param tokenState - Store for managing the authentication token state.
	 * @param connectionState - Store for managing the socket connection state.
	 * @param deviceState - Store for managing the devices state.
	 */
	constructor(
		private tokenState: TokenStore,
		private connectionState: SocketConnectionStore,
		private deviceState: DevicesStore,
	) {}
 
	/**
	 * Retrieves the current authentication token from the token store.
	 *
	 * @returns {string | null} The current token, or null if no token is stored.
	 */
	public getToken = (): string | null => {
		return this.tokenState.getToken();
	};
 
	/**
	 * Updates the authentication token in the token store.
	 *
	 * @param {string} value - The new token value to store.
	 */
	public setToken = (value: string) => {
		this.tokenState.updateToken(value);
	};
 
	/**
	 * Updates the socket connection state in the connection store.
	 *
	 * @param {SocketConnectionStatus} value - The new connection status.
	 */
	public setConnectionState = (value: SocketConnectionStatus) => {
		this.connectionState.updateSocketConnection(value);
	};
 
	/**
	 * Updates the device list in the device store.
	 *
	 * @param {HubDevice[]} value - The new list of devices to store.
	 */
	public setDevices = (value: HubDevice[]) => {
		this.deviceState.updateDevices(value);
	};
}
 
export default StateManager;