mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-31 07:32:59 +00:00 
			
		
		
		
	Further simplifications to syncer interface
Dropped the idea of exposing separate server connections
This commit is contained in:
		| @@ -29,7 +29,6 @@ exports.startup = function() { | |||||||
| 	// Set up the wiki store | 	// Set up the wiki store | ||||||
| 	$tw.wiki.initParsers(); | 	$tw.wiki.initParsers(); | ||||||
| 	$tw.wiki.initSyncers(); | 	$tw.wiki.initSyncers(); | ||||||
| 	$tw.wiki.initServerConnections(); |  | ||||||
| 	// Set up the command modules | 	// Set up the command modules | ||||||
| 	$tw.Commander.initCommands(); | 	$tw.Commander.initCommands(); | ||||||
| 	// Get the default tiddlers | 	// Get the default tiddlers | ||||||
| @@ -81,7 +80,7 @@ exports.startup = function() { | |||||||
| 				downloadType: "text/plain" | 				downloadType: "text/plain" | ||||||
| 			}); | 			}); | ||||||
| 		},false); | 		},false); | ||||||
| 		// Install the crypto event handler | 		// Install the crypto event handlers | ||||||
| 		document.addEventListener("tw-set-password",function(event) { | 		document.addEventListener("tw-set-password",function(event) { | ||||||
| 			$tw.passwordPrompt.createPrompt({ | 			$tw.passwordPrompt.createPrompt({ | ||||||
| 				serviceName: "Set a new password for this TiddlyWiki", | 				serviceName: "Set a new password for this TiddlyWiki", | ||||||
|   | |||||||
| @@ -599,40 +599,16 @@ exports.initSyncers = function() { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| /* | /* | ||||||
| Initialise server connections | Invoke all the active syncers | ||||||
| */ |  | ||||||
| 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 |  | ||||||
| */ | */ | ||||||
| exports.invokeSyncers = function(method /* ,args */) { | exports.invokeSyncers = function(method /* ,args */) { | ||||||
| 	var args = Array.prototype.slice.call(arguments,1); | 	var args = Array.prototype.slice.call(arguments,1); | ||||||
| 	for(var name in this.syncers) { | 	for(var name in this.syncers) { | ||||||
| 		var syncer = this.syncers[name]; | 		var syncer = this.syncers[name]; | ||||||
|  | 		if(syncer[method]) { | ||||||
| 			syncer[method].apply(syncer,args); | 			syncer[method].apply(syncer,args); | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /* | /* | ||||||
| @@ -648,7 +624,7 @@ exports.getTiddlerText = function(title,defaultText) { | |||||||
| 		// Just return the text if we've got it | 		// Just return the text if we've got it | ||||||
| 		return tiddler.fields.text; | 		return tiddler.fields.text; | ||||||
| 	} else { | 	} 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); | 		this.invokeSyncers("lazyLoad",title,tiddler); | ||||||
| 		// Indicate that the text is being loaded | 		// Indicate that the text is being loaded | ||||||
| 		return null; | 		return null; | ||||||
|   | |||||||
| @@ -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 |  | ||||||
| @@ -17,12 +17,29 @@ Creates a TiddlyWebSyncer object | |||||||
| */ | */ | ||||||
| var TiddlyWebSyncer = function(options) { | var TiddlyWebSyncer = function(options) { | ||||||
| 	this.wiki = options.wiki; | 	this.wiki = options.wiki; | ||||||
| 	this.connection = undefined; |  | ||||||
| 	this.tiddlerInfo = {}; // Hashmap of {revision:,changeCount:} | 	this.tiddlerInfo = {}; // Hashmap of {revision:,changeCount:} | ||||||
| 	// Tasks are {type: "load"/"save", title:, queueTime:, lastModificationTime:} | 	// Tasks are {type: "load"/"save", title:, queueTime:, lastModificationTime:} | ||||||
| 	this.taskQueue = {}; // Hashmap of tasks to be performed | 	this.taskQueue = {}; // Hashmap of tasks to be performed | ||||||
| 	this.taskInProgress = {}; // Hash of tasks in progress | 	this.taskInProgress = {}; // Hash of tasks in progress | ||||||
| 	this.taskTimerId = null; // Sync timer | 	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"; | TiddlyWebSyncer.titleIsLoggedIn = "$:/plugins/tiddlyweb/IsLoggedIn"; | ||||||
| @@ -49,35 +66,6 @@ TiddlyWebSyncer.prototype.log = function(/* arguments */) { | |||||||
| 	$tw.utils.log.apply(null,args); | 	$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 | Lazily load a skinny tiddler if we can | ||||||
| */ | */ | ||||||
| @@ -97,7 +85,7 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) { | |||||||
| 	var self = this; | 	var self = this; | ||||||
| 	this.log("Getting status"); | 	this.log("Getting status"); | ||||||
| 	this.httpRequest({ | 	this.httpRequest({ | ||||||
| 		url: this.connection.host + "status", | 		url: this.host + "status", | ||||||
| 		callback: function(err,data) { | 		callback: function(err,data) { | ||||||
| 			if(err) { | 			if(err) { | ||||||
| 				return callback(err); | 				return callback(err); | ||||||
| @@ -110,10 +98,8 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) { | |||||||
| 			} catch (e) { | 			} catch (e) { | ||||||
| 			} | 			} | ||||||
| 			if(json) { | 			if(json) { | ||||||
| 				// Use the recipe if we don't already have one | 				// Record the recipe | ||||||
| 				if(!self.connection.recipe) { | 				self.recipe = json.space.recipe; | ||||||
| 					self.connection.recipe = json.space.recipe; |  | ||||||
| 				} |  | ||||||
| 				// Check if we're logged in | 				// Check if we're logged in | ||||||
| 				isLoggedIn = json.username !== "GUEST"; | 				isLoggedIn = json.username !== "GUEST"; | ||||||
| 				// Set the various status tiddlers | 				// Set the various status tiddlers | ||||||
| @@ -162,7 +148,7 @@ TiddlyWebSyncer.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, | ||||||
| 		httpRequest = this.httpRequest({ | 		httpRequest = this.httpRequest({ | ||||||
| 			url: this.connection.host + "challenge/tiddlywebplugins.tiddlyspace.cookie_form", | 			url: this.host + "challenge/tiddlywebplugins.tiddlyspace.cookie_form", | ||||||
| 			type: "POST", | 			type: "POST", | ||||||
| 			data: { | 			data: { | ||||||
| 				user: username, | 				user: username, | ||||||
| @@ -194,7 +180,7 @@ TiddlyWebSyncer.prototype.handleLogoutEvent = function(options) { | |||||||
| 	this.log("Attempting to logout"); | 	this.log("Attempting to logout"); | ||||||
| 	var self = this, | 	var self = this, | ||||||
| 		httpRequest = this.httpRequest({ | 		httpRequest = this.httpRequest({ | ||||||
| 		url: this.connection.host + "logout", | 		url: this.host + "logout", | ||||||
| 		type: "POST", | 		type: "POST", | ||||||
| 		data: { | 		data: { | ||||||
| 			csrf_token: this.getCsrfToken(), | 			csrf_token: this.getCsrfToken(), | ||||||
| @@ -218,7 +204,7 @@ TiddlyWebSyncer.prototype.syncFromServer = function() { | |||||||
| 	this.log("Retrieving skinny tiddler list"); | 	this.log("Retrieving skinny tiddler list"); | ||||||
| 	var self = this; | 	var self = this; | ||||||
| 	this.httpRequest({ | 	this.httpRequest({ | ||||||
| 		url: this.connection.host + "recipes/" + this.connection.recipe + "/tiddlers.json", | 		url: this.host + "recipes/" + this.recipe + "/tiddlers.json", | ||||||
| 		callback: function(err,data) { | 		callback: function(err,data) { | ||||||
| 			// Check for errors | 			// Check for errors | ||||||
| 			if(err) { | 			if(err) { | ||||||
| @@ -402,7 +388,7 @@ TiddlyWebSyncer.prototype.dispatchTask = function(task,callback) { | |||||||
| 		var changeCount = this.wiki.getChangeCount(task.title); | 		var changeCount = this.wiki.getChangeCount(task.title); | ||||||
| 		this.log("Dispatching 'save' task:",task.title); | 		this.log("Dispatching 'save' task:",task.title); | ||||||
| 		this.httpRequest({ | 		this.httpRequest({ | ||||||
| 			url: this.connection.host + "recipes/" + this.connection.recipe + "/tiddlers/" + task.title, | 			url: this.host + "recipes/" + this.recipe + "/tiddlers/" + task.title, | ||||||
| 			type: "PUT", | 			type: "PUT", | ||||||
| 			headers: { | 			headers: { | ||||||
| 				"Content-type": "application/json" | 				"Content-type": "application/json" | ||||||
| @@ -424,7 +410,7 @@ TiddlyWebSyncer.prototype.dispatchTask = function(task,callback) { | |||||||
| 		// Load the tiddler | 		// Load the tiddler | ||||||
| 		this.log("Dispatching 'load' task:",task.title); | 		this.log("Dispatching 'load' task:",task.title); | ||||||
| 		this.httpRequest({ | 		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) { | 			callback: function(err,data,request) { | ||||||
| 				if(err) { | 				if(err) { | ||||||
| 					return callback(err); | 					return callback(err); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jeremy Ruston
					Jeremy Ruston