From 60a3abdea921d7ff2a6a01d9a91f76695fc2f24a Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 4 Nov 2017 19:35:39 -0400 Subject: [PATCH] status converted to module --- public/javascripts/status.js | 73 ++++++++++++++++++++---------------- public/javascripts/sync.js | 2 +- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/public/javascripts/status.js b/public/javascripts/status.js index fd8e02edc..bbf32d548 100644 --- a/public/javascripts/status.js +++ b/public/javascripts/status.js @@ -1,40 +1,47 @@ -const treeEl = $("#tree"); +const status = (function() { + const treeEl = $("#tree"); + const $changesToPushCountEl = $("#changesToPushCount"); -async function checkStatus() { - const resp = await $.ajax({ - url: baseApiUrl + 'status', - type: 'POST', - contentType: "application/json", - data: JSON.stringify({ - treeLoadTime: noteTree.getTreeLoadTime(), - currentNoteId: noteEditor.getCurrentNoteId(), - currentNoteDateModified: noteEditor.getCurrentNoteLoadTime() - }), - statusCode: { - 401: () => { - // if the user got logged out then we should display the page - // here we do that by reloading which will force the redirect if the user is really logged out - window.location.reload(true); - }, - 409: () => { - // 409 means we need to migrate database, reload will take care of it - window.location.reload(true); + async function checkStatus() { + const resp = await $.ajax({ + url: baseApiUrl + 'status', + type: 'POST', + contentType: "application/json", + data: JSON.stringify({ + treeLoadTime: noteTree.getTreeLoadTime(), + currentNoteId: noteEditor.getCurrentNoteId(), + currentNoteDateModified: noteEditor.getCurrentNoteLoadTime() + }), + statusCode: { + 401: () => { + // if the user got logged out then we should display the page + // here we do that by reloading which will force the redirect if the user is really logged out + window.location.reload(true); + }, + 409: () => { + // 409 means we need to migrate database, reload will take care of it + window.location.reload(true); + } } + }); + + if (resp.changedTree) { + const treeResp = await noteTree.loadTree(); + + console.log("Reloading tree because of background changes"); + + // this will also reload the note content + await treeEl.fancytree('getTree').reload(treeResp.notes); + + encryption.decryptTreeItems(); } - }); - if (resp.changedTree) { - const treeResp = await noteTree.loadTree(); - - console.log("Reloading tree because of background changes"); - - // this will also reload the note content - await treeEl.fancytree('getTree').reload(treeResp.notes); - - encryption.decryptTreeItems(); + $changesToPushCountEl.html(resp.changesToPushCount); } - $("#changesToPushCount").html(resp.changesToPushCount); -} + setInterval(checkStatus, 5 * 1000); -setInterval(checkStatus, 5 * 1000); \ No newline at end of file + return { + checkStatus + }; +})(); \ No newline at end of file diff --git a/public/javascripts/sync.js b/public/javascripts/sync.js index ebe03fae8..76ecfe93f 100644 --- a/public/javascripts/sync.js +++ b/public/javascripts/sync.js @@ -4,7 +4,7 @@ function syncNow() { type: 'POST', success: result => { if (result.success) { - checkStatus(); + status.checkStatus(); message("Sync triggered."); }