2017-11-27 12:16:47 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const messaging = require('./messaging');
|
|
|
|
const options = require('./options');
|
2017-12-14 12:03:48 +08:00
|
|
|
const sync_setup = require('./sync_setup');
|
2017-11-27 12:16:47 +08:00
|
|
|
|
2017-12-19 10:58:46 +08:00
|
|
|
let lastSentSyncId;
|
2017-11-27 12:16:47 +08:00
|
|
|
|
2017-12-04 08:18:33 +08:00
|
|
|
async function sendPing() {
|
2017-12-19 10:58:46 +08:00
|
|
|
const syncData = await sql.getResults("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
|
2017-11-27 12:16:47 +08:00
|
|
|
|
|
|
|
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({
|
2017-11-27 12:16:47 +08:00
|
|
|
type: 'sync',
|
2017-12-17 09:48:34 +08:00
|
|
|
data: syncData,
|
2017-12-14 12:03:48 +08:00
|
|
|
changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0
|
2017-11-27 12:16:47 +08:00
|
|
|
});
|
|
|
|
|
2017-12-19 10:58:46 +08:00
|
|
|
if (syncData.length > 0) {
|
|
|
|
lastSentSyncId = syncData[syncData.length - 1].id;
|
2017-11-27 12:16:47 +08:00
|
|
|
}
|
2017-12-04 08:18:33 +08:00
|
|
|
}
|
|
|
|
|
2017-12-19 10:58:46 +08:00
|
|
|
sql.dbReady.then(async () => {
|
|
|
|
lastSentSyncId = await sql.getSingleValue("SELECT MAX(id) FROM sync");
|
|
|
|
|
|
|
|
setInterval(sendPing, 1000);
|
|
|
|
});
|