Welcome to mirror list, hosted at ThFree Co, Russian Federation.

local_recent_db_manager.js « recent « server - github.com/PhieF/CarnetElectron.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e88891c38ed8c0bf2b341d56f005b8bf32df685 (plain)
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
var fs = require('fs');
var getParentFolderFromPath = require('path').dirname;
var lockFile = require('lockfile')
var LocalRecentDBManager = function (path) {
    this.path = path;
    console.logDebug("RecentDBManager with " + path)

}


LocalRecentDBManager.prototype.getFullDB = function (callback, donotparse) {
    console.logDebug("getFullDB")
    fs.readFile(this.path, "utf8", function (err, data) {
        if (data == undefined || data.length == 0)
            data = "{\"data\":[]}";
        callback(err, donotparse ? data : JSON.parse(data)); //working on big object transmitted to ui is a nightmare... so... sending string (far faster)
    });
}

LocalRecentDBManager.prototype.actionArray = function (items, callback) {
    var db = this;
    var time = new Date().getTime();
    db.getFullDB(function (err, data) {
        var fullDB = data;
        for (var i of items) {
            var item = new function () {
                this.time = i.time;
                this.action = i.action;
                this.path = i.path;
                if (i.newPath != undefined)
                    this.newPath = i.newPath
            };
            fullDB["data"].push(item);
        }
        require("mkdirp")(getParentFolderFromPath(db.path)).then(made => {
            // opts is optional, and defaults to {} 

            console.logDebug("writing")
            fs.writeFile(db.path, JSON.stringify(fullDB), function (err) {
                if (callback)
                    callback()
            });

        })
    });
}

LocalRecentDBManager.prototype.move = function (path, newPath, callback) {
    this.actionArray([{
        action: "move",
        time: new Date().getTime(),
        path: path,
        newPath: newPath
    }], callback)
}

LocalRecentDBManager.prototype.action = function (path, action, callback) {
    this.actionArray(path, action, new Date().getTime(), callback);
}
LocalRecentDBManager.prototype.action = function (path, action, time, callback) {
    var db = this;
    console.logDebug("action")
    lockFile.lock('recent.lock', {
        wait: 10000
    }, function (er) {
        db.getFullDB(function (err, data) {
            console.logDebug(data)
            var fullDB = data;
            var item = new function () {
                this.time = time;
                this.action = action;
                this.path = path;
            };

            fullDB["data"].push(item);
            console.logDebug(JSON.stringify(item))
            require("mkdirp")(getParentFolderFromPath(db.path)).then(made => {
                // opts is optional, and defaults to {} 

                console.logDebug("writing")
                fs.writeFile(db.path, JSON.stringify(fullDB), function (err) {
                    if (callback)
                        callback()
                    lockFile.unlock('recent.lock', function (er) {
                        console.logDebug("lock er " + er)
                        // er means that an error happened, and is probably bad. 
                    })
                });

            })
        })
    })
}

// sort on key values
function keysrt(key) {
    return function (a, b) {
        return a[key] > b[key];
    }
}
//returns last time
LocalRecentDBManager.prototype.mergeDB = function (path, callback) {
    console.logDebug("merging with " + path);
    var db = this;
    var hasChanged = false;
    lockFile.lock('recent.lock', {
        wait: 10000
    }, function (er) {
        db.getFullDB(function (err, data) {
            lockFile.unlock('recent.lock', function (er) {
                console.logDebug("lock er " + er)
                // er means that an error happened, and is probably bad. 
            })
            var otherDB = new LocalRecentDBManager(path)
            otherDB.getFullDB(function (err, dataBis) {
                var dataJson = data
                try {
                    var dataBisJson = dataBis
                } catch (e) { //bad :(
                    return
                }
                for (let itemBis of dataBisJson["data"]) {
                    var isIn = false;
                    for (let item of dataJson["data"]) {
                        if (itemBis.time == item.time && itemBis.path == item.path && itemBis.action == item.action) {
                            isIn = true;
                            break;
                        }
                    }
                    if (!isIn) {
                        dataJson["data"].push(itemBis);
                        hasChanged = true;
                    }
                }
                dataJson["data"] = dataJson["data"].sort(keysrt('time'))
                if (hasChanged) {
                    require("mkdirp")(getParentFolderFromPath(db.path)).then(made => {
                        // opts is optional, and defaults to {} 
                        lockFile.lock('recent.lock', {
                            wait: 10000
                        }, function (er) {
                            fs.writeFile(db.path, JSON.stringify(dataJson), function (err) {
                                console.logDebug(err);
                                callback(hasChanged);
                            });
                            lockFile.unlock('recent.lock', function (er) {
                                // er means that an error happened, and is probably bad. 
                            })
                        })
                    })
                } else callback(hasChanged);
            });
        })
    });

}

exports.LocalRecentDBManager = LocalRecentDBManager;