-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.js
344 lines (287 loc) · 5.9 KB
/
storage.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"use strict";
const {sep} = require("path");
const AtomFS = require("atom-fs");
const LRUCache = require("lru-cache");
const isWin = "\\" === sep;
const MAX_SIZE = 10000;
const version = 0x007;
let locked = false;
let data = null;
/**
* Cache for session-specific data.
*
* @class
*/
class Storage{
/**
* Initialise session storage.
*
* @param {Object} state - Data serialised from last session
*/
init(state){
locked = false;
data = this.deserialise(state);
this.clean();
}
/**
* Serialise data intended to persist between sessions.
*/
serialise(){
if(!data)
return null;
return {
paths: data.paths.dump(),
version,
};
}
/**
* Deserialise the result of a previous {@link Storage#serialise} call.
*/
deserialise(state){
if(!state || state.version !== version)
return this.createBlankCache();
const paths = new LRUCache({max: MAX_SIZE});
paths.load(state.paths);
return {paths, version};
}
/**
* Purge cache of invalid or irrelevant data.
*/
clean(){
data.paths.forEach((value, path) => {
if(!this.hasData(path) || !this.isProjectRelated(path))
this.deletePath(path);
});
}
/**
* Create a blank cache to store session data.
*
* @return {Object}
*/
createBlankCache(){
return {
paths: new LRUCache({max: MAX_SIZE}),
version,
};
}
/**
* Determine if a currently-open project encloses a path.
*
* @param {String} path
* @return {Boolean}
*/
isProjectRelated(path){
const {rootDirectories} = atom.project;
for(let i = 0, l = rootDirectories.length; i < l; ++i){
const projectPath = rootDirectories[i].path;
if(path === projectPath || 0 === path.indexOf(projectPath + sep))
return true;
if(isWin){
const fixedPath = AtomFS.normalisePath(projectPath);
if(path === fixedPath || 0 === path.indexOf(fixedPath + "/"))
return true;
}
}
return false;
}
/**
* Use path entries when iterating.
*
* @return {Iterator}
*/
[Symbol.iterator](){
const pathData = data.paths;
const pathKeys = pathData.keys();
const pathValues = pathData.values();
const {length} = pathKeys;
let index = 0;
return {
next(){
if(index >= length)
return { done: true };
else{
const path = pathKeys[index];
const value = [path, pathValues[index]];
++index;
return { value };
}
},
};
}
/**
* Create a blank entry for an unlisted path.
*
* Any existing data is blindly overwritten. Use {@link #getPathEntry}
* or {@link #deletePathEntry} to add/delete path-related data.
*
* @param {String} path
* @return {Object}
* @private
*/
addPath(path){
if(locked) return;
const value = {
icon: null,
id: null,
};
data.paths.set(path, value);
return value;
}
/**
* Retrieve the data cached for a path.
*
* A new entry is created if one doesn't exist.
*
* @param {String} path
* @return {Object}
*/
getPathEntry(path){
const entry = data.paths.get(path);
if(entry) return entry;
return locked
? null
: this.addPath(path);
}
/**
* Retrieve the icon data cached for a path.
*
* @param {String} path
* @return {Object}
*/
getPathIcon(path){
const {icon} = this.getPathEntry(path);
if(!icon) return null;
return {
priority: icon[0],
index: icon[1],
iconClass: icon[2],
};
}
/**
* Determine if stored data exists for a given path.
*
* @param {String} path
* @return {Boolean}
*/
hasData(path){
const entry = data.paths.get(path) || null;
return !!(entry && (entry.icon || entry.id));
}
/**
* Determine if icon-related data exists for a given path.
*
* @param {String} path
* @return {Boolean}
*/
hasIcon(path){
const entry = data.paths.get(path);
return !!(entry && entry.icon);
}
/**
* Store icon-related data for a path.
*
* @param {String} path
* @param {Object} iconData
* @param {Number} iconData.priority
* @param {Number} iconData.index
* @param {Array} iconData.iconClass
*/
setPathIcon(path, iconData){
if(!iconData) return;
this.getPathEntry(path).icon = [
iconData.priority,
iconData.index,
iconData.iconClass,
];
}
/**
* Store the ID of a filesystem path.
*
* @param {String} path
* @param {String} id
*/
setPathID(path, id){
if(!id || locked) return;
let entry = this.getPathEntry(path);
// We're holding stale data. Shoot it.
if(entry.id && entry.id !== id){
this.deletePath(path);
entry = this.addPath(path);
}
entry.id = id;
}
/**
* Delete any data being stored for a path.
*
* @param {String} path
*/
deletePath(path){
data.paths.del(path);
}
/**
* Delete a path's cached icon.
*
* @param {String} path
*/
deletePathIcon(path){
delete this.getPathEntry(path).icon;
}
/**
* Block further changes to cached data.
*
* Stops strategies losing cached data when deactivating
* the package, or when closing the project window.
*/
lock(){
locked = true;
}
/**
* Wipe all cached data.
*
* Handler for a user-command. Not used internally.
* @public
*/
reset(){
if(this.locked){
const detail = "This shouldn't have happened. Please restart Atom.";
atom.notifications.addError("Storage locked", {detail});
return;
}
else{
const {size} = this;
data = this.createBlankCache();
atom.project.serialize();
const plural = 1 === size ? "" : "s";
const message = `Cleared ${size} path${plural} from icon cache.`;
atom.notifications.addInfo(message, {dismissable: true});
}
}
/**
* Pointer to the hash which holds all cached data.
*
* @property {Object}
* @readonly
*/
get data(){
return data;
}
/**
* Whether the cache has been locked from modification.
*
* @property {Boolean}
* @readonly
*/
get locked(){
return locked;
}
/**
* Number of paths currently cached.
*
* @property {Number}
* @readonly
*/
get size(){
return data.paths.length;
}
}
module.exports = new Storage();