Skip to content
Snippets Groups Projects
routine-holder.singleton.ts 1.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { ThunkContainer } from "@shared/types/thunk.type";
    
    interface LocalThunkContainer {
        container: ThunkContainer,
        name: string
    }
    
    export class RoutineHolderSingleton {
        private static instance: RoutineHolderSingleton;
        private routineList: Array<LocalThunkContainer> = []
    
        private constructor() { }
    
        static getInstance(): RoutineHolderSingleton {
            if (!RoutineHolderSingleton.instance) {
                RoutineHolderSingleton.instance = new RoutineHolderSingleton();
            }
            return RoutineHolderSingleton.instance;
        }
    
        registerRoutine(name: string, thunk: ThunkContainer) {
            this.routineList = [...this.routineList, { container: thunk, name }];
        }
    
        getRoutineById(id: number): ThunkContainer {
            const routine = this.routineList.find((thunk) => thunk.container.id === id)
    
            if (!routine) {
                throw new Error('')
                // TODO
            }
    
            return routine.container;
        }
    
    
        getRoutineByName(name: string): ThunkContainer {
            const routine = this.routineList.find((thunk) => thunk.name === name)
    
            if (!routine) {
                throw new Error('')
                // TODO
            }
    
            return routine.container;
        }
    }