From d1bb7159b814f626ef2e9a43361b2394a5c6439f Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 25 Mar 2024 17:07:36 +0000 Subject: [PATCH] Expose the connection status in the UI --- .../multiwikiclient/multiwikiclientadaptor.js | 31 ++++++++++++------- .../multiwikiclient/sidebarsegment.tid | 5 +++ 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 plugins/tiddlywiki/multiwikiclient/sidebarsegment.tid diff --git a/plugins/tiddlywiki/multiwikiclient/multiwikiclientadaptor.js b/plugins/tiddlywiki/multiwikiclient/multiwikiclientadaptor.js index 8713708cb..4bde1fbb0 100644 --- a/plugins/tiddlywiki/multiwikiclient/multiwikiclientadaptor.js +++ b/plugins/tiddlywiki/multiwikiclient/multiwikiclientadaptor.js @@ -15,12 +15,13 @@ A sync adaptor module for synchronising with MultiWikiServer-compatible servers var CONFIG_HOST_TIDDLER = "$:/config/multiwikiclient/host", DEFAULT_HOST_TIDDLER = "$protocol$//$host$/", BAG_STATE_TIDDLER = "$:/state/multiwikiclient/tiddlers/bag", - REVISION_STATE_TIDDLER = "$:/state/multiwikiclient/tiddlers/revision"; + REVISION_STATE_TIDDLER = "$:/state/multiwikiclient/tiddlers/revision", + CONNECTION_STATE_TIDDLER = "$:/state/multiwikiclient/connection"; -var SERVER_NOT_CONNECTED = 0, - SERVER_CONNECTING_SSE = 1, - SERVER_CONNECTED_SSE = 2, - SERVER_POLLING = 3; +var SERVER_NOT_CONNECTED = "NOT CONNECTED", + SERVER_CONNECTING_SSE = "CONNECTING SSE", + SERVER_CONNECTED_SSE = "CONNECTED SSE", + SERVER_POLLING = "SERVER POLLING"; function MultiWikiClientAdaptor(options) { this.wiki = options.wiki; @@ -31,9 +32,17 @@ function MultiWikiClientAdaptor(options) { this.isLoggedIn = false; this.isReadOnly = false; this.logoutIsAvailable = true; - this.serverUpdatesConnection = SERVER_NOT_CONNECTED; + this.setUpdateConnectionStatus(SERVER_NOT_CONNECTED); } +MultiWikiClientAdaptor.prototype.setUpdateConnectionStatus = function(status) { + this.serverUpdateConnectionStatus = status; + this.wiki.addTiddler({ + title: CONNECTION_STATE_TIDDLER, + text: status + }); +}; + MultiWikiClientAdaptor.prototype.name = "multiwikiclient"; MultiWikiClientAdaptor.prototype.supportsLazyLoading = true; @@ -115,29 +124,29 @@ Get details of changed tiddlers from the server MultiWikiClientAdaptor.prototype.getUpdatedTiddlers = function(syncer,callback) { var self = this; // Do nothing if there's already a connection in progress. - if(this.serverUpdatesConnection !== SERVER_NOT_CONNECTED) { + if(this.serverUpdateConnectionStatus !== SERVER_NOT_CONNECTED) { return callback(null,{ modifications: [], deletions: [] }); } // Try to connect a server stream - this.serverUpdatesConnection = SERVER_CONNECTING_SSE; + this.setUpdateConnectionStatus(SERVER_CONNECTING_SSE); this.connectServerStream({ syncer: syncer, onerror: function(err) { self.logger.log("Error connecting SSE stream",err); // If the stream didn't work, try polling - self.serverUpdatesConnection = SERVER_POLLING; + self.setUpdateConnectionStatus(SERVER_POLLING); self.pollServer({ callback: function(err,changes) { - self.serverUpdatesConnection = SERVER_NOT_CONNECTED; + self.setUpdateConnectionStatus(SERVER_NOT_CONNECTED); callback(null,changes); } }); }, onopen: function() { - self.serverUpdatesConnection = SERVER_CONNECTED_SSE; + self.setUpdateConnectionStatus(SERVER_CONNECTED_SSE); // The syncer is expecting a callback but we don't have any data to send callback(null,{ modifications: [], diff --git a/plugins/tiddlywiki/multiwikiclient/sidebarsegment.tid b/plugins/tiddlywiki/multiwikiclient/sidebarsegment.tid new file mode 100644 index 000000000..88d443aec --- /dev/null +++ b/plugins/tiddlywiki/multiwikiclient/sidebarsegment.tid @@ -0,0 +1,5 @@ +title: $:/plugins/multiwikiclient/SideBarSegment +tags: $:/tags/SideBarSegment +list-before: $:/core/ui/SideBarSegments/page-controls + +MWS Connection Status: {{$:/state/multiwikiclient/connection}}