UNPKG

1.9 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Cached = void 0;
4const os_1 = require("os");
5const path_1 = require("path");
6const FileReader_1 = require("yaml-scene/src/elements/File/reader/FileReader");
7const JsonReader_1 = require("yaml-scene/src/elements/File/reader/JsonReader");
8const FileWriter_1 = require("yaml-scene/src/elements/File/writer/FileWriter");
9const JsonWriter_1 = require("yaml-scene/src/elements/File/writer/JsonWriter");
10const VariableManager_1 = require("yaml-scene/src/singleton/VariableManager");
11const FileUtils_1 = require("yaml-scene/src/utils/FileUtils");
12class Cached {
13 tmpFile;
14 reader;
15 writer;
16 data;
17 limit = 50;
18 constructor(uri, history) {
19 const key = VariableManager_1.VariableManager.Instance.vars.$$md5.encrypt(uri);
20 this.tmpFile = (0, path_1.join)((0, os_1.tmpdir)(), key);
21 if (history === 'clean')
22 this.clean();
23 this.writer = new JsonWriter_1.JsonWriter(new FileWriter_1.FileWriter(this.tmpFile));
24 this.reader = new JsonReader_1.JsonReader(new FileReader_1.FileReader(this.tmpFile));
25 this.data = [];
26 }
27 clean() {
28 FileUtils_1.FileUtils.RemoveFilesDirs(this.tmpFile);
29 }
30 async load() {
31 try {
32 const cnt = await this.reader.read();
33 if (cnt) {
34 cnt.forEach(cmd => this.data.push(cmd));
35 }
36 }
37 catch { }
38 }
39 async push(cmd) {
40 const idx = this.data.indexOf(cmd);
41 if (idx !== -1) {
42 this.data.splice(idx, 1);
43 }
44 this.data.splice(0, 0, cmd);
45 if (this.data.length > this.limit) {
46 this.data.splice(this.limit - 10, 10);
47 }
48 await this.save();
49 }
50 async save() {
51 await this.writer.write(this.data);
52 }
53}
54exports.Cached = Cached;
55//# sourceMappingURL=Cached.js.map
\No newline at end of file