websocket reimplementation of status requests

This commit is contained in:
azivner
2017-11-25 17:43:05 -05:00
parent f433b30089
commit 992238f0b3
6 changed files with 113 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
const sql = require('./sql');
const source_id = require('./source_id');
const utils = require('./utils');
const messaging = require('./messaging');
async function addNoteSync(noteId, sourceId) {
await addEntitySync("notes", noteId, sourceId)
@@ -35,6 +36,41 @@ async function addEntitySync(entityName, entityId, sourceId) {
});
}
let startTime = utils.nowTimestamp();
let sentSyncId = [];
setInterval(async () => {
const syncs = await sql.getResults("SELECT * FROM sync WHERE sync_date >= ?", [startTime]);
startTime = utils.nowTimestamp();
const data = {};
const syncIds = [];
for (const sync of syncs) {
if (sentSyncId.includes(sync.id)) {
continue;
}
if (!data[sync.entity_name]) {
data[sync.entity_name] = [];
}
data[sync.entity_name].push(sync.entity_id);
syncIds.push(sync.id);
}
if (syncIds.length > 0) {
messaging.send({
type: 'sync',
data: data
});
for (const syncId of syncIds) {
sentSyncId.push(syncId);
}
}
}, 1000);
module.exports = {
addNoteSync,
addNoteTreeSync,