12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import { Modes } from './../modes';
- export class Store {
- constructor() {
- this.store = {};
- this.nextStore = null;
- this.mode = Modes.RUN;
- }
- extendStore (nextStore) {
- this.nextStore = nextStore;
- }
- applyStore (id) {
- if(!this.store[id]) {
- if (this.nextStore !== null) {
- return this.nextStore.applyStore(id);
- } else {
- // TODO: better error message
- throw new Error(`Variable ${id} not found.`);
- }
- }
- const val = this.store[id];
- if (val.isRef) {
- return val.getRefObj();
- }
- return this.store[id];
- }
- updateStore (id, stoObj) {
- if(!this.store[id]) {
- if(this.nextStore !== null) {
- this.nextStore.updateStore(id, stoObj);
- return this;
- } else {
- // TODO: better error message
- throw new Error(`Variable ${id} not found.`);
- }
- } else {
- const oldObj = this.store[id];
- if(oldObj.readOnly) {
- // TODO: better error message
- throw new Error("Cannot change value of a read only variable: " + id);
- }
- if(oldObj.isCompatible(stoObj)) {
- if(oldObj.isRef) {
- oldObj.updateRef(stoObj);
- return this;
- }
- stoObj.setID(id);
- this.store[id] = Object.freeze(stoObj);
- return this;
- } else {
- // TODO: better error message
- throw new Error(`${oldObj.type} is not compatible with the value given`);
- }
- }
- }
- //In case of future use of ref, it needs to have a special function to update the storeRefObject
- // and no the StoreObject refferenced by it
- // updateStoreRef(id, stoObjAddress) {...}
- insertStore (id, stoObj) {
- if (this.store[id]) {
- // TODO: better error message
- throw new Error(`${id} is already defined`);
- }
- stoObj.setID(id);
- this.store[id] = Object.freeze(stoObj);
- return this;
- }
- }
|