1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-11-07 19:13:00 +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.taskInProgress = {}; // Hash of tasks in progress
this.taskTimerId = null; // Timer for task dispatch this.taskTimerId = null; // Timer for task dispatch
this.pollTimerId = null; // Timer for polling server 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 // Listen out for changes to tiddlers
this.wiki.addEventListener("change",function(changes) { this.wiki.addEventListener("change",function(changes) {
self.syncToServer(changes); self.syncToServer(changes);
@@ -91,16 +89,18 @@ Syncer.prototype.init = function() {
this.wiki.addEventListener("lazyLoad",function(title) { this.wiki.addEventListener("lazyLoad",function(title) {
self.handleLazyLoadEvent(title); self.handleLazyLoadEvent(title);
}); });
// Listen our for login/logout/refresh events // Listen out for login/logout/refresh events in the browser
document.addEventListener("tw-login",function(event) { if($tw.browser) {
self.handleLoginEvent(event); document.addEventListener("tw-login",function(event) {
},false); self.handleLoginEvent(event);
document.addEventListener("tw-logout",function(event) { },false);
self.handleLogoutEvent(event); document.addEventListener("tw-logout",function(event) {
},false); self.handleLogoutEvent(event);
document.addEventListener("tw-server-refresh",function(event) { },false);
self.handleRefreshEvent(event); document.addEventListener("tw-server-refresh",function(event) {
},false); self.handleRefreshEvent(event);
},false);
}
// Get the login status // Get the login status
this.getStatus(function (err,isLoggedIn) { this.getStatus(function (err,isLoggedIn) {
if(isLoggedIn) { if(isLoggedIn) {
@@ -127,70 +127,80 @@ Syncer.prototype.storeTiddler = function(tiddlerFields) {
Syncer.prototype.getStatus = function(callback) { Syncer.prototype.getStatus = function(callback) {
var self = this; var self = this;
this.syncadaptor.getStatus(function(err,isLoggedIn,username) { // Check if the adaptor supports getStatus()
if(err) { if(this.syncadaptor.getStatus) {
self.showError(err); // Mark us as not logged in
return; this.wiki.addTiddler({title: this.titleIsLoggedIn,text: "no"});
} // Get login status
// Set the various status tiddlers this.syncadaptor.getStatus(function(err,isLoggedIn,username) {
self.wiki.addTiddler({title: self.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"}); if(err) {
if(isLoggedIn) { self.showError(err);
self.wiki.addTiddler({title: self.titleUserName,text: username}); return;
} else { }
self.wiki.deleteTiddler(self.titleUserName); // Set the various status tiddlers
} self.wiki.addTiddler({title: self.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"});
// Invoke the callback if(isLoggedIn) {
if(callback) { self.wiki.addTiddler({title: self.titleUserName,text: username});
callback(err,isLoggedIn,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 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() { Syncer.prototype.syncFromServer = function() {
this.log("Retrieving skinny tiddler list"); if(this.syncadaptor.getSkinnyTiddlers) {
var self = this; this.log("Retrieving skinny tiddler list");
if(this.pollTimerId) { var self = this;
clearTimeout(this.pollTimerId); if(this.pollTimerId) {
this.pollTimerId = null; 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;
} }
// Process each incoming tiddler this.syncadaptor.getSkinnyTiddlers(function(err,tiddlers) {
for(var t=0; t<tiddlers.length; t++) { // Trigger another sync
// Get the incoming tiddler fields, and the existing tiddler self.pollTimerId = setTimeout(function() {
var tiddlerFields = tiddlers[t], self.pollTimerId = null;
incomingRevision = tiddlerFields.revision, self.syncFromServer.call(self);
tiddler = self.wiki.getTiddler(tiddlerFields.title), },self.pollTimerInterval);
tiddlerInfo = self.tiddlerInfo[tiddlerFields.title], // Check for errors
currRevision = tiddlerInfo ? tiddlerInfo.revision : null; if(err) {
// Ignore the incoming tiddler if it's the same as the revision we've already got self.log("Error retrieving skinny tiddler list:",err);
if(currRevision !== incomingRevision) { return;
// Do a full load if we've already got a fat version of the tiddler }
if(tiddler && tiddler.fields.text !== undefined) { // Process each incoming tiddler
// Do a full load of this tiddler for(var t=0; t<tiddlers.length; t++) {
self.enqueueSyncTask({ // Get the incoming tiddler fields, and the existing tiddler
type: "load", var tiddlerFields = tiddlers[t],
title: tiddlerFields.title incomingRevision = tiddlerFields.revision,
}); tiddler = self.wiki.getTiddler(tiddlerFields.title),
} else { tiddlerInfo = self.tiddlerInfo[tiddlerFields.title],
// Load the skinny version of the tiddler currRevision = tiddlerInfo ? tiddlerInfo.revision : null;
self.storeTiddler(tiddlerFields); // Ignore the incoming tiddler if it's the same as the revision we've already got
if(currRevision !== incomingRevision) {
// Do a full load if we've already got a fat version of the tiddler
if(tiddler && tiddler.fields.text !== undefined) {
// Do a full load of this tiddler
self.enqueueSyncTask({
type: "load",
title: tiddlerFields.title
});
} else {
// Load the skinny version of the tiddler
self.storeTiddler(tiddlerFields);
}
} }
} }
} });
}); }
}; };
/* /*
@@ -248,16 +258,20 @@ Attempt to login to TiddlyWeb.
Syncer.prototype.login = function(username,password,callback) { Syncer.prototype.login = function(username,password,callback) {
this.log("Attempting to login as",username); this.log("Attempting to login as",username);
var self = this; var self = this;
this.syncadaptor.login(username,password,function(err) { if(this.syncadaptor.login) {
if(err) { this.syncadaptor.login(username,password,function(err) {
return callback(err); if(err) {
} return callback(err);
self.getStatus(function(err,isLoggedIn,username) {
if(callback) {
callback(null,isLoggedIn);
} }
self.getStatus(function(err,isLoggedIn,username) {
if(callback) {
callback(null,isLoggedIn);
}
});
}); });
}); } else {
callback(null,true);
}
}; };
/* /*
@@ -266,13 +280,15 @@ Attempt to log out of TiddlyWeb
Syncer.prototype.handleLogoutEvent = function() { Syncer.prototype.handleLogoutEvent = function() {
this.log("Attempting to logout"); this.log("Attempting to logout");
var self = this; var self = this;
this.syncadaptor.logout(function(err) { if(this.syncadaptor.logout) {
if(err) { this.syncadaptor.logout(function(err) {
self.showError(err); if(err) {
} else { self.showError(err);
self.getStatus(); } else {
} self.getStatus();
}); }
});
}
}; };
/* /*
@@ -342,7 +358,7 @@ Trigger a timeout if one isn't already outstanding
Syncer.prototype.triggerTimeout = function() { Syncer.prototype.triggerTimeout = function() {
var self = this; var self = this;
if(!this.taskTimerId) { if(!this.taskTimerId) {
this.taskTimerId = window.setTimeout(function() { this.taskTimerId = setTimeout(function() {
self.taskTimerId = null; self.taskTimerId = null;
self.processTaskQueue.call(self); self.processTaskQueue.call(self);
},self.taskTimerInterval); },self.taskTimerInterval);