-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.js
60 lines (52 loc) · 1.32 KB
/
listen.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
var OrgFile = require('org-lite')
, path = require('path')
, oid = require('bson').ObjectID
, debug = require('debug')('nm:listen');
function genId() {
return new oid().toHexString();
}
function dumpDir(basedir) {
var objects = new OrgFile(basedir, true, {genId: genId, time: new Date()});
if (objects.dirty) {
objects.save();
}
return objects;
}
module.exports = function listen(socket) {
var db;
function getDb() {
var dir = process.env.NOTABLEMIND_HOME || path.join(process.env.HOME, '.notablemind');
debug('dump', dir);
db = dumpDir(dir);
}
socket.on('dump', function () {
if (!db) getDb();
socket.emit('dump', db.chunks.children);
});
// path, note
socket.on('change', function (data) {
if (!db) getDb();
debug('change', data);
db.modify(data.note.properties.id, data.note);
db.save();
});
socket.on('move', function (data) {
debug('move', data);
if (!db) getDb();
db.move(data.id, data.oldpid, data.pid, data.index);
db.save();
});
// id, pid
socket.on('delete', function (data) {
if (!db) getDb();
debug('delete', data);
db.remove(data.id, data.pid);
db.save();
});
socket.on('create', function (data) {
if (!db) getDb();
debug('create', data);
db.add(data.note, data.pid, data.index);
db.save();
});
};