store.js 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. export class Store {
  2. constructor () {
  3. this.store = {};
  4. this.nextStore = null;
  5. }
  6. extendStore (nextStore) {
  7. return Object.assign(new Store, {
  8. store: this.store,
  9. nextStore: nextStore
  10. });
  11. }
  12. findVariable (id) {
  13. if(!this.store[id]) {
  14. if (this.nextStore === null) {
  15. throw new Error("Undefined variable: " + id);
  16. } else {
  17. return this.nextStore.findVariable(id);
  18. }
  19. }
  20. return this.store[id];
  21. }
  22. isDefined (id) {
  23. if(!this.store[id]) {
  24. if (this.nextStore === null) {
  25. return false;
  26. } else {
  27. return this.nextStore.isDefined(id);
  28. }
  29. }
  30. return true;
  31. }
  32. updateVariable (id, storeObj) {
  33. if(!this.isDefined(id)) {
  34. this.store[id] = storeObj;
  35. } else {
  36. const old = this.findVariable(id);
  37. if (storeObj.type !== old.type) {
  38. throw new Error(`${storeObj.value} is not compatible with ${old.type}`);
  39. } else {
  40. this.store[id] = storeObj;
  41. }
  42. }
  43. }
  44. }