smartcontext/ts/logcontext.classes.asyncstore.ts

51 lines
1.2 KiB
TypeScript

import * as plugins from './logcontext.plugins.js';
export class AsyncStore {
private parentStore?: AsyncStore;
private deletedKeys: string[] = [];
private dataObject: {[key: string]: any} = {};
constructor(parentStore?: AsyncStore) {
this.parentStore = parentStore;
}
private cleanUp() {
for (const key of this.deletedKeys) {
if (this.parentStore && this.parentStore.get(key)) {
// ok still valid
} else {
delete this.deletedKeys[key];
}
}
}
public add(keyArg: string, objectArg: any) {
this.cleanUp();
if (this.deletedKeys.includes(keyArg)) {
this.deletedKeys = this.deletedKeys.filter((key) => key !== keyArg);
}
this.dataObject[keyArg] = objectArg;
}
public delete(paramName: string) {
this.cleanUp();
if (this.parentStore.get(paramName)) {
this.deletedKeys.push(paramName);
}
delete this.dataObject[paramName];
}
public get(paramName: string) {
this.cleanUp();
if (this.deletedKeys.includes(paramName)) {
return undefined;
}
return this.dataObject[paramName] || this.parentStore?.get(paramName);
}
public getAll() {
this.cleanUp();
return {...this.dataObject, ...(this.parentStore?.getAll() || {})};
}
}