trilium/services/ping_job.js

30 lines
893 B
JavaScript
Raw Normal View History

const sql = require('./sql');
const messaging = require('./messaging');
const options = require('./options');
const sync_setup = require('./sync_setup');
2017-12-19 10:58:46 +08:00
let lastSentSyncId;
async function sendPing() {
2017-12-19 10:58:46 +08:00
const syncData = await sql.getResults("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
const lastSyncedPush = await options.getOption('last_synced_push');
const changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
2017-11-29 07:33:23 +08:00
messaging.sendMessage({
type: 'sync',
data: syncData,
changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0
});
2017-12-19 10:58:46 +08:00
if (syncData.length > 0) {
lastSentSyncId = syncData[syncData.length - 1].id;
}
}
2017-12-19 10:58:46 +08:00
sql.dbReady.then(async () => {
lastSentSyncId = await sql.getSingleValue("SELECT MAX(id) FROM sync");
setInterval(sendPing, 1000);
});