Further simplifications to syncer interface

Dropped the idea of exposing separate server connections
This commit is contained in:
Jeremy Ruston 2013-03-09 09:54:01 +00:00
parent 3da508b7c4
commit 83a141752c
4 changed files with 32 additions and 83 deletions

View File

@ -29,7 +29,6 @@ exports.startup = function() {
// Set up the wiki store
$tw.wiki.initParsers();
$tw.wiki.initSyncers();
$tw.wiki.initServerConnections();
// Set up the command modules
$tw.Commander.initCommands();
// Get the default tiddlers
@ -81,7 +80,7 @@ exports.startup = function() {
downloadType: "text/plain"
});
},false);
// Install the crypto event handler
// Install the crypto event handlers
document.addEventListener("tw-set-password",function(event) {
$tw.passwordPrompt.createPrompt({
serviceName: "Set a new password for this TiddlyWiki",

View File

@ -599,39 +599,15 @@ exports.initSyncers = function() {
};
/*
Initialise server connections
*/
exports.initServerConnections = function() {
this.serverConnections = {};
var self = this;
$tw.modules.forEachModuleOfType("serverconnection",function(title,module) {
// Get the associated syncer
if(module.syncer) {
var syncer = self.syncers[module.syncer];
if(syncer) {
// Add the connection and save information about it
var connection = syncer.addConnection(module);
if(connection instanceof Error) {
console.log("Error adding connection: " + connection);
} else {
self.serverConnections[title] = {
syncer: syncer,
connection: connection
};
}
}
}
});
};
/*
Invoke all the active server connections
Invoke all the active syncers
*/
exports.invokeSyncers = function(method /* ,args */) {
var args = Array.prototype.slice.call(arguments,1);
for(var name in this.syncers) {
var syncer = this.syncers[name];
syncer[method].apply(syncer,args);
if(syncer[method]) {
syncer[method].apply(syncer,args);
}
}
};
@ -648,7 +624,7 @@ exports.getTiddlerText = function(title,defaultText) {
// Just return the text if we've got it
return tiddler.fields.text;
} else {
// Ask all the server connections to load the tiddler if they can
// Ask all the syncers to load the tiddler if they can
this.invokeSyncers("lazyLoad",title,tiddler);
// Indicate that the text is being loaded
return null;

View File

@ -1,12 +0,0 @@
title: TiddlyWebConnection
type: application/x-tiddler-dictionary
module-type: serverconnection
# Which syncer is used for this connection
syncer: tiddlywebsyncer
# The server host. If not specified, defaults to the host from which the page was loaded
# host: http://tw5tiddlyweb.tiddlyspace.com/
# The recipe to use. If not specified, defaults to the recipe used to serve the page
# recipe: tw5tiddlyweb_public

View File

@ -17,12 +17,29 @@ Creates a TiddlyWebSyncer object
*/
var TiddlyWebSyncer = function(options) {
this.wiki = options.wiki;
this.connection = undefined;
this.tiddlerInfo = {}; // Hashmap of {revision:,changeCount:}
// Tasks are {type: "load"/"save", title:, queueTime:, lastModificationTime:}
this.taskQueue = {}; // Hashmap of tasks to be performed
this.taskInProgress = {}; // Hash of tasks in progress
this.taskTimerId = null; // Sync timer
// Compute the host and recipe
this.host = document.location.protocol + "//" + document.location.host + "/";
this.recipe = undefined; // Filled in by getStatus()
// Mark us as not logged in
this.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: "no"});
// Listen out for changes to tiddlers
this.wiki.addEventListener("",function(changes) {
self.syncToServer(changes);
});
this.log("Initialising with host:",this.host);
// Get the login status
var self = this;
this.getStatus(function (err,isLoggedIn,json) {
if(isLoggedIn) {
// Do a sync
self.syncFromServer();
}
});
};
TiddlyWebSyncer.titleIsLoggedIn = "$:/plugins/tiddlyweb/IsLoggedIn";
@ -49,35 +66,6 @@ TiddlyWebSyncer.prototype.log = function(/* arguments */) {
$tw.utils.log.apply(null,args);
};
TiddlyWebSyncer.prototype.addConnection = function(connection) {
var self = this;
// Check if we've already got a connection
if(this.connection) {
return new Error("TiddlyWebSyncer can only handle a single connection");
}
// If we don't have a host then substitute the host of the page
if(!connection.host) {
connection.host = document.location.protocol + "//" + document.location.host + "/";
}
// Mark us as not logged in
this.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: "no"});
// Save the connection object
this.connection = connection;
// Listen out for changes to tiddlers
this.wiki.addEventListener("",function(changes) {
self.syncToServer(changes);
});
this.log("Adding connection with recipe:",connection.recipe,"host:",connection.host);
// Get the login status
this.getStatus(function (err,isLoggedIn,json) {
if(isLoggedIn) {
// Do a sync
self.syncFromServer();
}
});
return ""; // We only support a single connection
};
/*
Lazily load a skinny tiddler if we can
*/
@ -97,7 +85,7 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) {
var self = this;
this.log("Getting status");
this.httpRequest({
url: this.connection.host + "status",
url: this.host + "status",
callback: function(err,data) {
if(err) {
return callback(err);
@ -110,10 +98,8 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) {
} catch (e) {
}
if(json) {
// Use the recipe if we don't already have one
if(!self.connection.recipe) {
self.connection.recipe = json.space.recipe;
}
// Record the recipe
self.recipe = json.space.recipe;
// Check if we're logged in
isLoggedIn = json.username !== "GUEST";
// Set the various status tiddlers
@ -162,7 +148,7 @@ TiddlyWebSyncer.prototype.login = function(username,password,callback) {
this.log("Attempting to login as",username);
var self = this,
httpRequest = this.httpRequest({
url: this.connection.host + "challenge/tiddlywebplugins.tiddlyspace.cookie_form",
url: this.host + "challenge/tiddlywebplugins.tiddlyspace.cookie_form",
type: "POST",
data: {
user: username,
@ -194,7 +180,7 @@ TiddlyWebSyncer.prototype.handleLogoutEvent = function(options) {
this.log("Attempting to logout");
var self = this,
httpRequest = this.httpRequest({
url: this.connection.host + "logout",
url: this.host + "logout",
type: "POST",
data: {
csrf_token: this.getCsrfToken(),
@ -218,7 +204,7 @@ TiddlyWebSyncer.prototype.syncFromServer = function() {
this.log("Retrieving skinny tiddler list");
var self = this;
this.httpRequest({
url: this.connection.host + "recipes/" + this.connection.recipe + "/tiddlers.json",
url: this.host + "recipes/" + this.recipe + "/tiddlers.json",
callback: function(err,data) {
// Check for errors
if(err) {
@ -402,7 +388,7 @@ TiddlyWebSyncer.prototype.dispatchTask = function(task,callback) {
var changeCount = this.wiki.getChangeCount(task.title);
this.log("Dispatching 'save' task:",task.title);
this.httpRequest({
url: this.connection.host + "recipes/" + this.connection.recipe + "/tiddlers/" + task.title,
url: this.host + "recipes/" + this.recipe + "/tiddlers/" + task.title,
type: "PUT",
headers: {
"Content-type": "application/json"
@ -424,7 +410,7 @@ TiddlyWebSyncer.prototype.dispatchTask = function(task,callback) {
// Load the tiddler
this.log("Dispatching 'load' task:",task.title);
this.httpRequest({
url: this.connection.host + "recipes/" + this.connection.recipe + "/tiddlers/" + task.title,
url: this.host + "recipes/" + this.recipe + "/tiddlers/" + task.title,
callback: function(err,data,request) {
if(err) {
return callback(err);