-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.ts
92 lines (76 loc) · 2.01 KB
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"use strict";
import * as path from "path";
import { SessionContainer } from "./container";
import { xfs } from "./xfs";
class Cache {
private readonly cachePath: string;
private readonly cacheState: string;
private readonly data: {
[name: string]: {};
};
private readonly collections: Map<string, Collection>;
constructor(cachePath: string, data: any) {
this.cachePath = cachePath;
this.data = data;
this.collections = new Map();
}
getCollections(name: string): Collection {
let collection = this.collections.get(name);
if (!collection) {
const collectionData = this.data[name] || (this.data[name] = {});
collection = new Collection(collectionData);
}
return collection;
}
getCollection(name: string): Collection {
let collection = this.collections.get(name);
if (!collection) {
const collectionData = this.data[name] || (this.data[name] = {});
collection = new Collection(collectionData);
}
return collection;
}
async flush() {
await xfs.writeJsonAtomic(this.data, this.cachePath);
}
}
class Collection {
private readonly collectionData: {
[key: string]: any;
};
constructor(collectionData: {}) {
this.collectionData = collectionData;
}
set(key: string, value: any) {
this.collectionData[key] = value;
}
get(key: string): any {
return this.collectionData[key];
}
delete(key: string) {
delete this.collectionData[key];
}
keys(): string[] {
return Object.keys(this.collectionData);
}
}
const caches = new Map<string, Cache>();
export async function getCache(repoPath: string): Promise<Cache> {
let cache = caches.get(repoPath);
if (!cache) {
cache = await load(repoPath);
caches.set(repoPath, cache);
}
return cache;
}
async function load(repoPath: string): Promise<Cache> {
if (!repoPath.endsWith(".git")) {
repoPath = path.join(repoPath, ".git");
}
const cachePath = path.join(
repoPath,
`codestream-${SessionContainer.instance().session.userId}.cache`
);
const data = await xfs.readJson(cachePath);
return new Cache(cachePath, data || {});
}