1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-11 18:00:26 +00:00

Make some of the sync adaptor methods be optional

This commit is contained in:
Jeremy Ruston 2013-03-24 12:21:01 +00:00
parent 9fb8ef25ed
commit 2870a09dc1

View File

@ -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,7 +89,8 @@ Syncer.prototype.init = function() {
this.wiki.addEventListener("lazyLoad",function(title) {
self.handleLazyLoadEvent(title);
});
// Listen our for login/logout/refresh events
// Listen out for login/logout/refresh events in the browser
if($tw.browser) {
document.addEventListener("tw-login",function(event) {
self.handleLoginEvent(event);
},false);
@ -101,6 +100,7 @@ Syncer.prototype.init = function() {
document.addEventListener("tw-server-refresh",function(event) {
self.handleRefreshEvent(event);
},false);
}
// Get the login status
this.getStatus(function (err,isLoggedIn) {
if(isLoggedIn) {
@ -127,6 +127,11 @@ Syncer.prototype.storeTiddler = function(tiddlerFields) {
Syncer.prototype.getStatus = function(callback) {
var self = this;
// 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);
@ -144,12 +149,16 @@ Syncer.prototype.getStatus = function(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() {
if(this.syncadaptor.getSkinnyTiddlers) {
this.log("Retrieving skinny tiddler list");
var self = this;
if(this.pollTimerId) {
@ -158,7 +167,7 @@ Syncer.prototype.syncFromServer = function() {
}
this.syncadaptor.getSkinnyTiddlers(function(err,tiddlers) {
// Trigger another sync
self.pollTimerId = window.setTimeout(function() {
self.pollTimerId = setTimeout(function() {
self.pollTimerId = null;
self.syncFromServer.call(self);
},self.pollTimerInterval);
@ -191,6 +200,7 @@ Syncer.prototype.syncFromServer = function() {
}
}
});
}
};
/*
@ -248,6 +258,7 @@ Attempt to login to TiddlyWeb.
Syncer.prototype.login = function(username,password,callback) {
this.log("Attempting to login as",username);
var self = this;
if(this.syncadaptor.login) {
this.syncadaptor.login(username,password,function(err) {
if(err) {
return callback(err);
@ -258,6 +269,9 @@ Syncer.prototype.login = function(username,password,callback) {
}
});
});
} else {
callback(null,true);
}
};
/*
@ -266,6 +280,7 @@ Attempt to log out of TiddlyWeb
Syncer.prototype.handleLogoutEvent = function() {
this.log("Attempting to logout");
var self = this;
if(this.syncadaptor.logout) {
this.syncadaptor.logout(function(err) {
if(err) {
self.showError(err);
@ -273,6 +288,7 @@ Syncer.prototype.handleLogoutEvent = function() {
self.getStatus();
}
});
}
};
/*
@ -342,7 +358,7 @@ Trigger a timeout if one isn't already outstanding
Syncer.prototype.triggerTimeout = function() {
var self = this;
if(!this.taskTimerId) {
this.taskTimerId = window.setTimeout(function() {
this.taskTimerId = setTimeout(function() {
self.taskTimerId = null;
self.processTaskQueue.call(self);
},self.taskTimerInterval);