import * as plugins from './smartstate.plugins'; import { StatePart } from './smartstate.classes.statepart'; /** * Smartstate takes care of providing state */ export class Smartstate { public statePartMap: { [key: string]: StatePart } = {}; constructor() {} public getStatePart( statePartNameArg: string & StatePartNameType, initialArg?: PayloadType ): StatePart { if (this.statePartMap[statePartNameArg as any]) { if (initialArg) { throw new Error( `${statePartNameArg} already exists, yet you try to set an initial state again` ); } return this.statePartMap[statePartNameArg] as StatePart; } else { if (!initialArg) { throw new Error( `${statePartNameArg} does not yet exist, yet you don't provide an initial state` ); } return this.createStatePart(statePartNameArg, initialArg); } } /** * creates a statepart * @param statePartName * @param initialPayloadArg */ private createStatePart( statePartName: StatePartNameType, initialPayloadArg: PayloadType ): StatePart { const newState = new StatePart(statePartName); newState.setState(initialPayloadArg); this.statePartMap[statePartName as any] = newState; return newState; } }