Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (90 loc) · 3.75 KB
/
Copy pathindex.js
File metadata and controls
104 lines (90 loc) · 3.75 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
/* @flow weak */
'use strict'
class SourceCache {
constructor(api) {
this.api = api;
this.hashCache = new Map();
this.uriCache = new Map();
this.debug = true
}
/**
* Handle the change of a file
*
*/
isCached(meta) {
if (this.debug) console.log("SourceCache.checkIfShouldOpen("+meta.uri+")")
if (this.debug) console.log("\n\nSourceCache.checkIfShouldOpen: "+JSON.stringify(meta, null, 2));
const hasKnownHash = this.hashCache.has(meta.hash);
const hasknownUri = this.uriCache.has(meta.uri);
const knownHash = this.hashCache.get(meta.hash);
const knownUri = this.uriCache.get(meta.uri);
// file wasn't moved and didn't change
if (hasKnownHash && hasknownUri) {
if (this.debug) console.log("SourceCache.checkIfShouldOpen: uri didn't change, hash didn't change");
knownHash.date = meta.date; // update
knownUri.date = meta.date; // normally this isn't necessary as our meta object reference is the same
} else if (hasKnownHash && !hasknownUri) {
if (this.debug) console.log("SourceCache.checkIfShouldOpen: uri changed, hash didn't change");
knownHash.date = meta.date; // update
knownHash.uri.add(meta.uri); // update
this.uriCache.set(meta.hash, knownHash); // overwrite
} else if (!hasKnownHash && hasknownUri) {
if (this.debug) console.log("SourceCache.checkIfShouldOpen: uri didn't change, hash changed");
// note: this is a major event so we have to cut the remove to the uri
// and create a brand new object for the new uri
const oldHash = knownUri.hash;
const oldObj = this.hashCache.get(oldHash);
if (oldObj) {
if (this.debug) console.log("SourceCache.checkIfShouldOpen: delete old uri for old hash "+oldHash);
oldObj.uri.delete(meta.uri);
}
const changedObj = {
uri: new Set(meta.uri),
hash: meta.hash,
date: meta.date
}
if (this.debug) console.log("SourceCache.checkIfShouldOpen: created new uri reference");
this.hashCache.set(meta.hash, changedObj); // new
this.uriCache.set(meta.uri, changedObj); // overwrite
return true;
// this.api.open(meta); // call and forget
} else if (!hasKnownHash && !hasknownUri) {
if (this.debug) console.log("SourceCache.checkIfShouldOpen: uri is new, hash is new");
const newObj = {
uri: new Set(meta.uri),
hash: meta.hash,
date: meta.date
}
if (this.debug) console.log("SourceCache.checkIfShouldOpen: created new uri reference");
this.hashCache.set(meta.hash, newObj); // new
this.uriCache.set(meta.uri, newObj); // new
// this.api.open(meta); // call and forget
return true;
}
return false;
}
onDelete(meta) {
if (this.debug) console.log("SourceCache.onDelete: deleting "+meta.uri);
const hasknownUri = this.uriCache.has(meta.uri);
const knownUri = this.uriCache.get(meta.uri);
const hasKnownHash = this.hashCache.has(hasknownUri ? knownUri.hash : '');
const knownHash = this.hashCache.get(hasknownUri ? knownUri.hash : '');
// file wasn't moved and didn't change
if (hasknownUri) {
if (this.debug) console.log("SourceCache.onDelete: known uri");
knownUri.uri.delete(meta.uri);
knownUri.delete(meta.uri);
this.api.delete({ uri: meta.uri, hash: knownHash }); // call and forget
} else if (!hasknownUri) {
if (this.debug) console.log("SourceCache.onDelete: unknown uri");
} else {
const obj = uriCache.delete(uri);
if (obj){
this.api.delete({ uri: uri, hash: knownHash }); // call and forget
} else {
if (this.debug) console.log("SourceCache.onDelete: nothing to delete, file was not in cache");
}
}
}
}
module.exports = SourceCache