@beenotung/tslib
Version:
utils library in Typescript
124 lines • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncStore = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const path = tslib_1.__importStar(require("path"));
const fs_1 = require("./fs");
const store_1 = require("./store");
const uuid_1 = require("./uuid");
Symbol.counter = Symbol.for('counter');
Symbol.dirpath = Symbol.for('dirpath');
Symbol.asyncStoreSetItemResults = Symbol.for('asyncStoreSetItemResults');
class AsyncStore {
constructor(dirpath) {
this[Symbol.counter] = uuid_1.new_counter();
this[Symbol.asyncStoreSetItemResults] = new Map();
if (!fs.existsSync(dirpath)) {
fs.mkdirSync(dirpath);
}
this[Symbol.dirpath] = dirpath;
}
clear() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const fs = yield fs_1.readdir(this[Symbol.dirpath]);
yield Promise.all(fs.map(f => fs_1.unlink(path.join(this[Symbol.dirpath], f))));
});
}
getItem(key) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const b = yield fs_1.readFile(this.keyToPath(key));
return b ? b.toString() : null;
});
}
getObject(key) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return JSON.parse((yield this.getItem(key)));
});
}
key(index) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return fs_1.readdir(this[Symbol.dirpath]).then(fs => {
if (index < fs.length) {
return this.getItem(encodeURIComponent(fs[index]));
}
else {
return null;
}
});
});
}
keys() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return fs_1.readdir(this[Symbol.dirpath]).then(fs => fs.map(f => decodeURIComponent(f)));
});
}
length() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return fs_1.readdir(this[Symbol.dirpath]).then(fs => fs.length);
});
}
removeItem(key) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return fs_1.unlink(this.keyToPath(key));
});
}
setItem(key, value) {
const tasks = this[Symbol.asyncStoreSetItemResults];
if (tasks.has(key)) {
const task = tasks.get(key);
if (task && !task.hasDone) {
task.cancel();
}
}
const filepath = this.keyToPath(key);
const tmpfile = filepath + '.' + Date.now() + '.' + this[Symbol.counter].next();
const status = {
hasCancel: false,
hasDone: false,
};
const p = new Promise((resolve, reject) => {
fs_1.writeFile(tmpfile, value)
.then(() => {
if (status.hasCancel) {
return fs_1.unlink(tmpfile);
}
else {
return fs_1.rename(tmpfile, filepath);
}
})
.then(() => {
status.hasDone = true;
})
.catch(e => {
status.hasDone = true;
return Promise.reject(e);
});
});
const newTask = Object.assign(p, status, {
cancel: () => {
status.hasCancel = true;
},
});
tasks.set(key, newTask);
newTask.then(() => tasks.delete(key));
return newTask;
}
setObject(key, value) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (value === undefined) {
return this.removeItem(key);
}
yield this.setItem(key, JSON.stringify(value));
});
}
keyToPath(key) {
return path.join(this[Symbol.dirpath], encodeURIComponent(key));
}
static create(dirpath) {
const store = new AsyncStore(dirpath);
return store_1.proxyStore(store);
}
}
exports.AsyncStore = AsyncStore;
//# sourceMappingURL=async-store.js.map