Newer
Older
import { infoMessage, warnMessage } from '@helper/debug'
import { QueryActionCreatorResult } from '@reduxjs/toolkit/query'
import { Category, CategoryType } from '@shared/types/category.type'
type Routine = QueryActionCreatorResult<any>
interface RoutineState {
routines: Record<CategoryType, Entity | null>
}
const initalState: RoutineState = {
routines: {
DEVICE: null,
TABLE: null,
TAB: null
}
* Routine manager is a singleton that holds all running routines.
* The redux store holds any persistable information about the routines.
* The routine objects itself are stored in the RoutineManager.
*/
const add = (routine: Routine, category: CategoryType): boolean => {
const entity: Entity = {
state.routines = {
...state.routines,
[category]: entity
}
infoMessage("Routine subscribed to category " + category)
Object.keys(state.routines)
.forEach((category) => {
unsubscribe(category as CategoryType)
})
* @returns returns true if the routine was stopped, false if it was not found
*/
const unsubscribe = (category: CategoryType): boolean => {
const entity = state.routines[category]
if (entity) {
entity.routine.unsubscribe()
state.routines[category] = null
infoMessage("Routine unsubscribed from category " + category)
if (!!entity) {
warnMessage("Desired routine to unsubscribe does not exist in category " + Category[category])
}