UNPKG

516 BJavaScriptView Raw
1var fs = require('fs')
2var _ = require('lodash')
3var Writer = require('./writer')
4
5function stringify(obj) {
6 return JSON.stringify(obj, null, 2)
7}
8
9function Store(filename) {
10 if (fs.existsSync(filename)) {
11 this.object = JSON.parse(fs.readFileSync(filename))
12 } else {
13 fs.writeFileSync(filename, '{}')
14 this.object = {}
15 }
16
17 this.writer = new Writer(filename)
18
19 return this
20}
21
22Store.prototype.save = _.throttle(function() {
23 this.writer.write(stringify(this.object))
24}, 10)
25
26module.exports = Store
\No newline at end of file