From 2870a09dc15cf162816c4143b27a41777739984a Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sun, 24 Mar 2013 12:21:01 +0000 Subject: [PATCH] Make some of the sync adaptor methods be optional --- core/modules/syncer.js | 182 ++++++++++++++++++++++------------------- 1 file changed, 99 insertions(+), 83 deletions(-) diff --git a/core/modules/syncer.js b/core/modules/syncer.js index 4269e29b3..e325a2727 100644 --- a/core/modules/syncer.js +++ b/core/modules/syncer.js @@ -81,8 +81,6 @@ Syncer.prototype.init = function() { this.taskInProgress = {}; // Hash of tasks in progress this.taskTimerId = null; // Timer for task dispatch this.pollTimerId = null; // Timer for polling server - // Mark us as not logged in - this.wiki.addTiddler({title: this.titleIsLoggedIn,text: "no"}); // Listen out for changes to tiddlers this.wiki.addEventListener("change",function(changes) { self.syncToServer(changes); @@ -91,16 +89,18 @@ Syncer.prototype.init = function() { this.wiki.addEventListener("lazyLoad",function(title) { self.handleLazyLoadEvent(title); }); - // Listen our for login/logout/refresh events - document.addEventListener("tw-login",function(event) { - self.handleLoginEvent(event); - },false); - document.addEventListener("tw-logout",function(event) { - self.handleLogoutEvent(event); - },false); - document.addEventListener("tw-server-refresh",function(event) { - self.handleRefreshEvent(event); - },false); + // Listen out for login/logout/refresh events in the browser + if($tw.browser) { + document.addEventListener("tw-login",function(event) { + self.handleLoginEvent(event); + },false); + document.addEventListener("tw-logout",function(event) { + self.handleLogoutEvent(event); + },false); + document.addEventListener("tw-server-refresh",function(event) { + self.handleRefreshEvent(event); + },false); + } // Get the login status this.getStatus(function (err,isLoggedIn) { if(isLoggedIn) { @@ -127,70 +127,80 @@ Syncer.prototype.storeTiddler = function(tiddlerFields) { Syncer.prototype.getStatus = function(callback) { var self = this; - this.syncadaptor.getStatus(function(err,isLoggedIn,username) { - if(err) { - self.showError(err); - return; - } - // Set the various status tiddlers - self.wiki.addTiddler({title: self.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"}); - if(isLoggedIn) { - self.wiki.addTiddler({title: self.titleUserName,text: username}); - } else { - self.wiki.deleteTiddler(self.titleUserName); - } - // Invoke the callback - if(callback) { - callback(err,isLoggedIn,username); - } - }); + // Check if the adaptor supports getStatus() + if(this.syncadaptor.getStatus) { + // Mark us as not logged in + this.wiki.addTiddler({title: this.titleIsLoggedIn,text: "no"}); + // Get login status + this.syncadaptor.getStatus(function(err,isLoggedIn,username) { + if(err) { + self.showError(err); + return; + } + // Set the various status tiddlers + self.wiki.addTiddler({title: self.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"}); + if(isLoggedIn) { + self.wiki.addTiddler({title: self.titleUserName,text: username}); + } else { + self.wiki.deleteTiddler(self.titleUserName); + } + // Invoke the callback + if(callback) { + callback(err,isLoggedIn,username); + } + }); + } else { + callback(null,true,"UNAUTHENTICATED"); + } }; /* Synchronise from the server by reading the skinny tiddler list and queuing up loads for any tiddlers that we don't already have up to date */ Syncer.prototype.syncFromServer = function() { - this.log("Retrieving skinny tiddler list"); - var self = this; - if(this.pollTimerId) { - clearTimeout(this.pollTimerId); - this.pollTimerId = null; - } - this.syncadaptor.getSkinnyTiddlers(function(err,tiddlers) { - // Trigger another sync - self.pollTimerId = window.setTimeout(function() { - self.pollTimerId = null; - self.syncFromServer.call(self); - },self.pollTimerInterval); - // Check for errors - if(err) { - self.log("Error retrieving skinny tiddler list:",err); - return; + if(this.syncadaptor.getSkinnyTiddlers) { + this.log("Retrieving skinny tiddler list"); + var self = this; + if(this.pollTimerId) { + clearTimeout(this.pollTimerId); + this.pollTimerId = null; } - // Process each incoming tiddler - for(var t=0; t