import {RountangleAction} from "./RountangleActions"; import {assertNever} from "../../util/assert"; interface Rountangle { name: string; posX: number; posY: number; width: number; height: number; } export class RountangleStore { private state: Record; private onDispatchListeners: Set<(action: RountangleAction) => void>; constructor() { this.state = {}; this.onDispatchListeners = new Set(); } dispatch(action: RountangleAction) { switch (action.tag) { case "createRountangle": this.state = { ...this.state, [action.id]: {name: action.name, posX: action.posX, posY: action.posY, width: action.width, height: action.height} }; break; case 'moveRountangle': this.state = { ...this.state, [action.id]: {...this.state[action.id], posX: action.newPosX, posY: action.newPosY} } break; case 'deleteRountangle': this.state = { ...(delete this.state[action.id] && this.state) } break; case 'resizeRountangle': this.state = { ...this.state, [action.id]: {...this.state[action.id], width: action.width, height: action.height} } break; default: assertNever(action); } this.onDispatchListeners.forEach(listener => listener(action)); } addOnDispatchListener(newListener: (action: RountangleAction) => void) { this.onDispatchListeners.add(newListener); } getAllRountangles() { return this.state; } getRountangle(id: string) { if (this.state.hasOwnProperty(id)) { return this.state[id]; } else { return undefined; } } }