Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
websocket stuff separated into messaging.js
azivner91c2f9e7cb5f
diff --git a/public/javascripts/init.js b/public/javascripts/init.jsindex 60d3b0ee01..1cb4b3eb9d 100644--- a/public/javascripts/init.js+++ b/public/javascripts/init.js@@ -139,69 +139,4 @@ function initAjax() {});}-initAjax();--function messageHandler(event) {- console.log(event.data);-- const message = JSON.parse(event.data);-- if (message.type === 'sync') {- lastPingTs = new Date().getTime();- const data = message.data;-- if (data.notes_tree) {- console.log("Reloading tree because of background changes");-- noteTree.reload();- }-- if (data.notes && data.notes.includes(noteEditor.getCurrentNoteId())) {- showMessage('Reloading note because background change');-- noteEditor.reload();- }-- const changesToPushCountEl = $("#changesToPushCount");- changesToPushCountEl.html(message.changesToPushCount);- }-}--let ws = null;--function connectWebSocket() {- // use wss for secure messaging- ws = new WebSocket("ws://" + location.host);- ws.onopen = function (event) {};- ws.onmessage = messageHandler;- ws.onclose = function(){- // Try to reconnect in 5 seconds- setTimeout(() => connectWebSocket(), 5000);- };-}--connectWebSocket();--let lastPingTs = new Date().getTime();-let connectionBrokenNotification = null;--setInterval(async () => {- if (new Date().getTime() - lastPingTs > 5000) {- if (!connectionBrokenNotification) {- connectionBrokenNotification = $.notify({- // options- message: "Lost connection to server"- },{- // settings- type: 'danger',- delay: 100000000 // keep it until we explicitly close it- });- }- }- else if (connectionBrokenNotification) {- await connectionBrokenNotification.close();- connectionBrokenNotification = null;-- showMessage("Re-connected to server");- }-}, 3000);\ No newline at end of file+initAjax();\ No newline at end of filediff --git a/public/javascripts/messaging.js b/public/javascripts/messaging.jsnew file mode 100644index 0000000000..2e2b771670--- /dev/null+++ b/public/javascripts/messaging.js@@ -0,0 +1,68 @@+"use strict";++const messaging = (function() {+ function messageHandler(event) {+ console.log(event.data);++ const message = JSON.parse(event.data);++ if (message.type === 'sync') {+ lastPingTs = new Date().getTime();+ const data = message.data;++ if (data.notes_tree) {+ console.log("Reloading tree because of background changes");++ noteTree.reload();+ }++ if (data.notes && data.notes.includes(noteEditor.getCurrentNoteId())) {+ showMessage('Reloading note because background change');++ noteEditor.reload();+ }++ const changesToPushCountEl = $("#changesToPushCount");+ changesToPushCountEl.html(message.changesToPushCount);+ }+ }++ let ws = null;++ function connectWebSocket() {+ // use wss for secure messaging+ ws = new WebSocket("ws://" + location.host);+ ws.onopen = function (event) {};+ ws.onmessage = messageHandler;+ ws.onclose = function(){+ // Try to reconnect in 5 seconds+ setTimeout(() => connectWebSocket(), 5000);+ };+ }++ connectWebSocket();++ let lastPingTs = new Date().getTime();+ let connectionBrokenNotification = null;++ setInterval(async () => {+ if (new Date().getTime() - lastPingTs > 5000) {+ if (!connectionBrokenNotification) {+ connectionBrokenNotification = $.notify({+ // options+ message: "Lost connection to server"+ },{+ // settings+ type: 'danger',+ delay: 100000000 // keep it until we explicitly close it+ });+ }+ }+ else if (connectionBrokenNotification) {+ await connectionBrokenNotification.close();+ connectionBrokenNotification = null;++ showMessage("Re-connected to server");+ }+ }, 3000);+})();\ No newline at end of filediff --git a/services/notes.js b/services/notes.jsindex 45bac7e7e6..664e227697 100644--- a/services/notes.js+++ b/services/notes.js@@ -137,8 +137,6 @@ async function updateNote(noteId, newNote, ctx) {await encryptNote(newNote, ctx);}- const origNoteDetail = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);-const now = utils.nowTimestamp();const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval'));diff --git a/views/index.ejs b/views/index.ejsindex a29a5167fe..ff03ad98d7 100644--- a/views/index.ejs+++ b/views/index.ejs@@ -327,6 +327,7 @@<script src="javascripts/link.js"></script><script src="javascripts/sync.js"></script>+ <script src="javascripts/messaging.js"></script><script src="javascripts/utils.js"></script></body></html>\ No newline at end of file