RountangleStore.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {RountangleAction} from "./RountangleActions";
  2. import {assertNever} from "../../util/assert";
  3. interface Rountangle {
  4. name: string;
  5. posX: number;
  6. posY: number;
  7. width: number;
  8. height: number;
  9. }
  10. export class RountangleStore {
  11. private state: Record<string, Rountangle>;
  12. private onDispatchListeners: Set<(action: RountangleAction) => void>;
  13. constructor() {
  14. this.state = {};
  15. this.onDispatchListeners = new Set();
  16. }
  17. dispatch(action: RountangleAction) {
  18. switch (action.tag) {
  19. case "createRountangle":
  20. this.state = {
  21. ...this.state,
  22. [action.id]: {name: action.name, posX: action.posX, posY: action.posY, width: action.width, height: action.height}
  23. };
  24. break;
  25. case 'moveRountangle':
  26. this.state = {
  27. ...this.state,
  28. [action.id]: {...this.state[action.id], posX: action.newPosX, posY: action.newPosY}
  29. }
  30. break;
  31. case 'deleteRountangle':
  32. this.state = {
  33. ...(delete this.state[action.id] && this.state)
  34. }
  35. break;
  36. case 'resizeRountangle':
  37. this.state = {
  38. ...this.state,
  39. [action.id]: {...this.state[action.id], width: action.width, height: action.height}
  40. }
  41. break;
  42. default: assertNever(action);
  43. }
  44. this.onDispatchListeners.forEach(listener => listener(action));
  45. }
  46. addOnDispatchListener(newListener: (action: RountangleAction) => void) {
  47. this.onDispatchListeners.add(newListener);
  48. }
  49. getAllRountangles() {
  50. return this.state;
  51. }
  52. getRountangle(id: string) {
  53. if (this.state.hasOwnProperty(id)) {
  54. return this.state[id];
  55. }
  56. else {
  57. return undefined;
  58. }
  59. }
  60. }