This commit is contained in:
2017-07-12 15:30:49 +02:00
parent f0664d4c7d
commit 73c37d8bf8
5 changed files with 317 additions and 153 deletions

View File

@@ -1,54 +1,86 @@
import * as plugins from './npmextra.plugins'
import * as paths from './npmextra.paths'
import { Task } from 'taskbuffer'
export type TKeyValueStore = 'path' | 'gitProject' | 'custom'
/**
* kvStore is a simple key vlaue store to store data about projects between runs
*/
export class KeyValueStore {
dataObject: any
firstReadDeferred = plugins.smartq.defer()
syncTask: plugins.taskbuffer.Task
type: TKeyValueStore // the type of the kvStore
identity: string // the identity of the kvStore
filePath: string // the filePath of the kvStore
constructor(typeArg: TKeyValueStore, customStringArg?: string) {
constructor (typeArg: TKeyValueStore, customStringArg?: string) {
// set kvStoreType
this.type = typeArg
this.identity = customStringArg
this.initFilePath()
// set up the sync Task
this.syncTask = new Task({
buffered: true,
bufferMax: 2,
execDelay: 500,
taskFunction: async () => {
this.dataObject = plugins.smartlodash.merge(
{},
plugins.smartfile.fs.toObjectSync(this.filePath,
this.dataObject
)
await plugins.smartfile.memory.toFs(JSON.stringify(this.dataObject), this.filePath)
},
name: 'syncTask'
})
}
/**
* reads all keyValue pairs at once and returns them
*/
async readAll () {
this.syncTask.trigger()
await this.firstReadDeferred.promise
return this.dataObject
}
/**
* reads a keyValueFile from disk
*/
read() {
async read (keyArg: string) {
let data = this.readAll()
return data[keyArg]
}
/**
* writes a key value file to disk
*/
write() {
async write (keyValueObject) {
plugins.smartlodash.merge(this.dataObject, keyValueObject)
this.syncTask.trigger()
}
/**
* wipes a key value store from disk
*/
wipe() {
async wipe () {
this.dataObject.
}
/**
* updates a value
*/
update() {
async update () {
}
/**
* computes the identity
*/
private initFilePath() {
private initFilePath () {
// determine the right base directory
let baseDir: string
if (this.type === 'custom') {