actual-server/sync-full.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-03-31 17:19:08 +00:00
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 });
}
2022-05-20 21:37:42 +00:00
messages = messages.map((envPb) => {
2022-03-31 17:19:08 +00:00
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()
};
});
2022-05-20 21:37:42 +00:00
const newMessages = await actual.internal.syncAndReceiveMessages(
messages,
since
);
2022-03-31 17:19:08 +00:00
return {
trie: actual.internal.timestamp.getClock().merkle,
2022-05-20 21:37:42 +00:00
newMessages: newMessages.map((msg) => {
const envelopePb = new SyncPb.MessageEnvelope();
const messagePb = new SyncPb.Message();
messagePb.setDataset(msg.dataset);
messagePb.setRow(msg.row);
messagePb.setColumn(msg.column);
messagePb.setValue(msg.value);
envelopePb.setTimestamp(msg.timestamp);
envelopePb.setContent(messagePb.serializeBinary());
return envelopePb;
})
2022-03-31 17:19:08 +00:00
};
});
module.exports = { sync };