12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- export class Store {
- constructor () {
- this.store = {};
- this.nextStore = null;
- }
- extendStore (nextStore) {
- return Object.assign(new Store, {
- store: this.store,
- nextStore: nextStore
- });
- }
- findVariable (id) {
- if(!this.store[id]) {
- if (this.nextStore === null) {
- throw new Error("Undefined variable: " + id);
- } else {
- return this.nextStore.findVariable(id);
- }
- }
- return this.store[id];
- }
- isDefined (id) {
- if(!this.store[id]) {
- if (this.nextStore === null) {
- return false;
- } else {
- return this.nextStore.isDefined(id);
- }
- }
- return true;
- }
- updateVariable (id, storeObj) {
- if(!this.isDefined(id)) {
- this.store[id] = storeObj;
- } else {
- const old = this.findVariable(id);
- if (storeObj.type !== old.type) {
- throw new Error(`${storeObj.value} is not compatible with ${old.type}`);
- } else {
- this.store[id] = storeObj;
- }
- }
- }
- }
|