1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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<string, Rountangle>;
- 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;
- }
- }
- }
|