-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONStorage.js
93 lines (73 loc) · 3.04 KB
/
JSONStorage.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
/*****************************************************************************
JSONStorage
Storage backend for saving/loading JSON files. Things like groups, the pages,
etc. are stored as JSON objects on disk, and loaded into memory as objects.
When we modify the object we call .invalidate() which sets off a timer
(so we're not constantly writing to disk), and it saved when the timer expires.
You can also call .saveNow() to save the object to disk immediately.
******************************************************************************/
var fs = require('fs');
// Timeout to save: (milliseconds)
var saveTimer = 5 * 60 * 1000;
// Array of opened storage objects - we save this so on quit when we flush everything, we
// have a list of all open objects.
var openedStorages = [];
// Load a JSON file
// - path - the path to a JSON file, it must end in .json
// Returns a wrapper around the JSON file.
exports.load = function(path) {
// some private variables:
var saving = false; // are we currently saving?
var timer = null; // timer for saving (starts when invalidated)
// check if the file exists
if(!fs.existsSync(path)) {
// if it doesn't exist, let's write an empty json object so we have something to load
fs.writeFileSync(path, "{}");
}
var storage = {};
// the actual object we want to load and play with
storage.object = require(path); // load the json object from disk
// save this file to disk now
storage.saveNow = function() {
// stop any timer that may be running
if(timer !== null) {
clearTimeout(timer);
timer = null;
}
// check if there's currently an async saving in progress
if(saving) {
// start another timer
storage.invalidate();
return;
}
saving = true;
// convert the file to JSON format
var json = JSON.stringify(storage.object);
// save it to disk
fs.writeFile(path, json, function(err) {
saving = false; // done writing
if(err)
console.log("Error writing to " + path + ": " + err);
});
};
// invalidate this file - this means it will be saved to disk after a certain amount of time
storage.invalidate = function() {
// check if another timer is running
if(timer !== null) {
// we don't want two timers running simultaneously for the same file - that would defeat
// the whole purpose of timers - to group changes together!
return;
}
// kick off a timer
timer = setTimeout(storage.saveNow, saveTimer);
};
// add this object to our list of open storages
openedStorages.push(storage);
return storage;
};
// Flush all of our storage objects - called when exiting to make sure everything has been flushed.
exports.flush = function() {
// loop through all of our storages
for(var i = 0; i < openedStorages.length; i++)
openedStorages[i].saveNow(); // force to save it
};