-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisync.js
45 lines (35 loc) · 1.01 KB
/
isync.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
'use strict';
const
emitter = require('events'),
fs = require('fs');
class instance extends emitter {
constructor (path, period = 10) {
super();
this.path = path;
this.period = period;
const fd = fs.openSync(this.path, fs.constants.O_RDONLY | fs.constants.O_CREAT);
this.data = JSON.parse(fs.readFileSync(fd, 'utf-8') || '{}');
fs.closeSync(fd);
this.link();
}
flush (link) {
fs.writeFile(this.path, JSON.stringify(this.data), null, () => {
this.emit('flush');
if (link) {
this.link();
}
});
}
flushSync () {
fs.writeFileSync(this.path, JSON.stringify(this.data), null);
this.emit('flush');
}
link () {
clearTimeout(this.interval);
this.interval = setTimeout(() => this.flush(true), this.period * 60 * 1000); // DevSkim: reviewed DS172411
}
unlink () {
clearTimeout(this.interval);
}
}
module.exports = instance;