fix(core): update

This commit is contained in:
2019-09-16 13:18:45 +02:00
parent 82af55fae9
commit c22bbe2daf
3 changed files with 365 additions and 264 deletions

View File

@ -9,14 +9,10 @@ export type TKeyValueStore = 'path' | 'gitProject' | 'custom';
* kvStore is a simple key vlaue store to store data about projects between runs
*/
export class KeyValueStore {
public dataObject: any;
public deletedObject: any = {};
public initialReadTask = new TaskOnce({
taskFunction: async () => {
this.dataObject = plugins.smartfile.fs.toObjectSync(this.filePath);
}
});
private dataObject: any = {};
private deletedObject: any = {};
public syncTask = new Task({
name: 'syncTask',
buffered: true,
bufferMax: 2,
execDelay: 500,
@ -31,8 +27,28 @@ export class KeyValueStore {
this.deletedObject = {};
await plugins.smartfile.memory.toFs(JSON.stringify(this.dataObject), this.filePath);
},
name: 'syncTask'
});
/**
* computes the identity
*/
private initFilePath = () => {
// determine the right base directory
let baseDir: string;
if (this.type === 'custom') {
baseDir = paths.kvCustomDir;
} else if (this.type === 'gitProject') {
baseDir = paths.kvGitDir;
} else if (this.type === 'path') {
baseDir = paths.kvPathDir;
}
this.filePath = plugins.path.join(baseDir, this.identity + '.json');
plugins.smartfile.fs.ensureDirSync(paths.kvCustomDir);
plugins.smartfile.fs.ensureDirSync(paths.kvGitDir);
plugins.smartfile.fs.ensureDirSync(paths.kvPathDir);
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
}
public type: TKeyValueStore; // the type of the kvStore
public identity: string; // the identity of the kvStore
public filePath: string; // the filePath of the kvStore
@ -53,8 +69,7 @@ export class KeyValueStore {
* reads all keyValue pairs at once and returns them
*/
public async readAll() {
await this.initialReadTask.trigger();
this.syncTask.trigger();
await this.syncTask.trigger();
return this.dataObject;
}
@ -62,17 +77,22 @@ export class KeyValueStore {
* reads a keyValueFile from disk
*/
public async readKey(keyArg: string) {
let data = await this.readAll();
return data[keyArg];
await this.syncTask.trigger();
return this.dataObject[keyArg];
}
/**
* writes a specific key to the keyValueStore
*/
async writeKey(keyArg: string, valueArg: any) {
let writeObject: any = {};
writeObject[keyArg] = valueArg;
this.writeAll(writeObject);
public async writeKey(keyArg: string, valueArg: any) {
await this.writeAll({
[keyArg]: valueArg
});
}
public async deleteKey(keyArg: string) {
this.deletedObject[keyArg] = this.dataObject[keyArg];
await this.syncTask.trigger();
}
/**
@ -80,40 +100,14 @@ export class KeyValueStore {
*/
public async writeAll(keyValueObject) {
this.dataObject = {...this.dataObject, ...keyValueObject};
this.syncTask.trigger();
await this.syncTask.trigger();
}
/**
* wipes a key value store from disk
*/
public async wipe() {
for (let key in this.dataObject) {
this.deletedObject[key] = this.dataObject[key];
}
}
/**
* updates a value
*/
public async update(keyObject) {}
/**
* computes the identity
*/
private initFilePath() {
// determine the right base directory
let baseDir: string;
if (this.type === 'custom') {
baseDir = paths.kvCustomDir;
} else if (this.type === 'gitProject') {
baseDir = paths.kvGitDir;
} else if (this.type === 'path') {
baseDir = paths.kvPathDir;
}
this.filePath = plugins.path.join(baseDir, this.identity + '.json');
plugins.smartfile.fs.ensureDirSync(paths.kvCustomDir);
plugins.smartfile.fs.ensureDirSync(paths.kvGitDir);
plugins.smartfile.fs.ensureDirSync(paths.kvPathDir);
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
this.dataObject = {};
await plugins.smartfile.fs.remove(this.filePath);
}
}