forked from actualbudget/actual-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-full.js
38 lines (31 loc) · 1.05 KB
/
sync-full.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
let { sequential } = require('./util/async');
let actual = require('@actual-app/api');
let SyncPb = actual.internal.SyncProtoBuf;
// This method must be sequential (TODO: document why, because Actual
// is global etc)
const sync = sequential(async function syncAPI(messages, since, fileId) {
let prefs = await actual.internal.send('load-prefs');
if (prefs == null || prefs.id !== fileId) {
if (prefs != null) {
await actual.internal.send('close-budget');
}
await actual.internal.send('load-budget', { id: fileId });
}
messages = messages.map(envPb => {
let timestamp = envPb.getTimestamp();
let msg = SyncPb.Message.deserializeBinary(envPb.getContent());
return {
timestamp: timestamp,
dataset: msg.getDataset(),
row: msg.getRow(),
column: msg.getColumn(),
value: msg.getValue()
};
});
let newMessages = actual.internal.syncAndReceiveMessages(messages, since);
return {
trie: actual.internal.timestamp.getClock().merkle,
newMessages: newMessages
};
});
module.exports = { sync };