fix(core): update
This commit is contained in:
@@ -5,11 +5,11 @@ import { Task } from '@push.rocks/taskbuffer';
|
||||
|
||||
export type TKeyValueStore = 'custom' | 'userHomeDir';
|
||||
|
||||
export interface IKvStoreConstructorOptions {
|
||||
export interface IKvStoreConstructorOptions<T> {
|
||||
typeArg: TKeyValueStore;
|
||||
identityArg: string;
|
||||
customPath?: string;
|
||||
mandatoryKeys?: string[];
|
||||
mandatoryKeys?: Array<keyof T>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -17,9 +17,9 @@ export interface IKvStoreConstructorOptions {
|
||||
*/
|
||||
export class KeyValueStore<T = any> {
|
||||
private dataObject: Partial<T> = {};
|
||||
private deletedObject: any = {};
|
||||
private mandatoryKeys: Set<string> = new Set();
|
||||
public changeSubject = new plugins.smartrx.rxjs.Subject();
|
||||
private deletedObject: Partial<T> = {};
|
||||
private mandatoryKeys: Set<keyof T> = new Set();
|
||||
public changeSubject = new plugins.smartrx.rxjs.Subject<Partial<T>>();
|
||||
|
||||
private storedStateString: string = '';
|
||||
public syncTask = new Task({
|
||||
@@ -28,12 +28,11 @@ export class KeyValueStore<T = any> {
|
||||
bufferMax: 1,
|
||||
execDelay: 0,
|
||||
taskFunction: async () => {
|
||||
|
||||
this.dataObject = {
|
||||
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
||||
...this.dataObject,
|
||||
};
|
||||
for (const key of Object.keys(this.deletedObject)) {
|
||||
for (const key of Object.keys(this.deletedObject) as Array<keyof T>) {
|
||||
delete this.dataObject[key];
|
||||
}
|
||||
this.deletedObject = {};
|
||||
@@ -89,7 +88,7 @@ export class KeyValueStore<T = any> {
|
||||
* @param identityArg
|
||||
* @param customPath Optional custom path for the keyValue store
|
||||
*/
|
||||
constructor(optionsArg: IKvStoreConstructorOptions) {
|
||||
constructor(optionsArg: IKvStoreConstructorOptions<T>) {
|
||||
if (optionsArg.customPath && optionsArg.typeArg !== 'custom') {
|
||||
throw new Error('customPath can only be provided if typeArg is custom');
|
||||
}
|
||||
@@ -108,7 +107,7 @@ export class KeyValueStore<T = any> {
|
||||
/**
|
||||
* reads all keyValue pairs at once and returns them
|
||||
*/
|
||||
public async readAll() {
|
||||
public async readAll(): Promise<Partial<T>> {
|
||||
await this.syncTask.trigger();
|
||||
return this.dataObject;
|
||||
}
|
||||
@@ -116,21 +115,21 @@ export class KeyValueStore<T = any> {
|
||||
/**
|
||||
* reads a keyValueFile from disk
|
||||
*/
|
||||
public async readKey(keyArg: string) {
|
||||
public async readKey<K extends keyof T>(keyArg: K): Promise<T[K]> {
|
||||
await this.syncTask.trigger();
|
||||
return this.dataObject[keyArg];
|
||||
return this.dataObject[keyArg] as T[K];
|
||||
}
|
||||
|
||||
/**
|
||||
* writes a specific key to the keyValueStore
|
||||
*/
|
||||
public async writeKey(keyArg: string, valueArg: any) {
|
||||
public async writeKey<K extends keyof T>(keyArg: K, valueArg: T[K]): Promise<void> {
|
||||
await this.writeAll({
|
||||
[keyArg]: valueArg,
|
||||
});
|
||||
} as unknown as Partial<T>);
|
||||
}
|
||||
|
||||
public async deleteKey(keyArg: string) {
|
||||
public async deleteKey<K extends keyof T>(keyArg: K): Promise<void> {
|
||||
this.deletedObject[keyArg] = this.dataObject[keyArg];
|
||||
await this.syncTask.trigger();
|
||||
}
|
||||
@@ -138,7 +137,7 @@ export class KeyValueStore<T = any> {
|
||||
/**
|
||||
* writes all keyValue pairs in the object argument
|
||||
*/
|
||||
public async writeAll(keyValueObject: { [key: string]: any }) {
|
||||
public async writeAll(keyValueObject: Partial<T>): Promise<void> {
|
||||
this.dataObject = { ...this.dataObject, ...keyValueObject };
|
||||
await this.syncTask.trigger();
|
||||
}
|
||||
@@ -146,7 +145,7 @@ export class KeyValueStore<T = any> {
|
||||
/**
|
||||
* wipes a key value store from disk
|
||||
*/
|
||||
public async wipe() {
|
||||
public async wipe(): Promise<void> {
|
||||
this.dataObject = {};
|
||||
await plugins.smartfile.fs.remove(this.filePath);
|
||||
}
|
||||
@@ -154,11 +153,11 @@ export class KeyValueStore<T = any> {
|
||||
/**
|
||||
* resets the KeyValueStore to the initial state by syncing first, deleting all keys, and then triggering a sync again
|
||||
*/
|
||||
public async reset() {
|
||||
public async reset(): Promise<void> {
|
||||
await this.syncTask.trigger(); // Sync to get the latest state
|
||||
|
||||
// Delete all keys from the dataObject and add them to deletedObject
|
||||
for (const key of Object.keys(this.dataObject)) {
|
||||
for (const key of Object.keys(this.dataObject) as Array<keyof T>) {
|
||||
this.deletedObject[key] = this.dataObject[key];
|
||||
delete this.dataObject[key];
|
||||
}
|
||||
@@ -166,21 +165,21 @@ export class KeyValueStore<T = any> {
|
||||
await this.syncTask.trigger(); // Sync again to reflect the deletion
|
||||
}
|
||||
|
||||
private setMandatoryKeys(keys: string[]) {
|
||||
private setMandatoryKeys(keys: Array<keyof T>) {
|
||||
keys.forEach(key => this.mandatoryKeys.add(key));
|
||||
}
|
||||
|
||||
public async getMissingMandatoryKeys(): Promise<string[]> {
|
||||
public async getMissingMandatoryKeys(): Promise<Array<keyof T>> {
|
||||
await this.readAll();
|
||||
return Array.from(this.mandatoryKeys).filter(key => !(key in this.dataObject));
|
||||
}
|
||||
|
||||
public async waitForKeysPresent(keysArg: string[]): Promise<void> {
|
||||
public async waitForKeysPresent<K extends keyof T>(keysArg: K[]): Promise<void> {
|
||||
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||
if (missingKeys.length === 0) {
|
||||
return;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const subscription = this.changeSubject.subscribe(() => {
|
||||
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||
if (missingKeys.length === 0) {
|
||||
@@ -190,4 +189,9 @@ export class KeyValueStore<T = any> {
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
|
||||
await this.waitForKeysPresent([keyArg]);
|
||||
return this.readKey(keyArg);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user