import * as plugins from './smartstate.plugins'; import { StatePart } from './smartstate.classes.statepart'; /** * Smartstate takes care of providing state */ export class Smartstate { statePartMap: { [key: string]: StatePart } = {}; constructor() {} getStatePart( statePartNameArg: 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 any]; } else { if (!initialArg) { throw new Error( `${statePartNameArg} does not yet exist, yet you don't provide an initial state` ); } return this.createStatePart(statePartNameArg, initialArg); } } private createStatePart( statePartName: StatePartNameType, initialPayloadArg: PayloadType ): StatePart { const newState = new StatePart(statePartName); newState.setState(initialPayloadArg); this.statePartMap[statePartName as any] = newState; return newState; } /** * dispatches an action on the main level */ dispatch() {} }