mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-06-05 08:04:07 +00:00
Rejigging syncer structuring
The goal is to separate out the saver handling from the syncadaptor handling; it will take a few steps to get there
This commit is contained in:
parent
82860aea33
commit
27f1f82a70
@ -62,8 +62,17 @@ exports.startup = function() {
|
|||||||
document: document
|
document: document
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// Find a working syncadaptor
|
||||||
|
$tw.syncadaptor = undefined;
|
||||||
|
$tw.modules.forEachModuleOfType("syncadaptor",function(title,module) {
|
||||||
|
if(!$tw.syncadaptor && module.adaptorClass) {
|
||||||
|
$tw.syncadaptor = new module.adaptorClass({wiki: $tw.wiki});
|
||||||
|
}
|
||||||
|
});
|
||||||
// Set up the syncer object
|
// Set up the syncer object
|
||||||
$tw.syncer = new $tw.Syncer({wiki: $tw.wiki});
|
if($tw.syncadaptor) {
|
||||||
|
$tw.syncer = new $tw.Syncer({wiki: $tw.wiki, syncadaptor: $tw.syncadaptor});
|
||||||
|
}
|
||||||
// Host-specific startup
|
// Host-specific startup
|
||||||
if($tw.browser) {
|
if($tw.browser) {
|
||||||
// Install the popup manager
|
// Install the popup manager
|
||||||
|
@ -14,20 +14,15 @@ The syncer tracks changes to the store. If a syncadaptor is used then individual
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Instantiate the syncer with the following options:
|
Instantiate the syncer with the following options:
|
||||||
|
syncadaptor: reference to syncadaptor to be used
|
||||||
wiki: wiki to be synced
|
wiki: wiki to be synced
|
||||||
*/
|
*/
|
||||||
function Syncer(options) {
|
function Syncer(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.wiki = options.wiki;
|
this.wiki = options.wiki;
|
||||||
|
this.syncadaptor = options.syncadaptor
|
||||||
// Make a logger
|
// Make a logger
|
||||||
this.logger = new $tw.utils.Logger("syncer" + ($tw.browser ? "-browser" : "") + ($tw.node ? "-server" : ""));
|
this.logger = new $tw.utils.Logger("syncer" + ($tw.browser ? "-browser" : "") + ($tw.node ? "-server" : ""));
|
||||||
// Find a working syncadaptor
|
|
||||||
this.syncadaptor = undefined;
|
|
||||||
$tw.modules.forEachModuleOfType("syncadaptor",function(title,module) {
|
|
||||||
if(!self.syncadaptor && module.adaptorClass) {
|
|
||||||
self.syncadaptor = new module.adaptorClass(self);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Initialise our savers
|
// Initialise our savers
|
||||||
if($tw.browser) {
|
if($tw.browser) {
|
||||||
this.initSavers();
|
this.initSavers();
|
||||||
@ -603,6 +598,8 @@ Syncer.prototype.dispatchTask = function(task,callback) {
|
|||||||
}
|
}
|
||||||
// Invoke the callback
|
// Invoke the callback
|
||||||
callback(null);
|
callback(null);
|
||||||
|
},{
|
||||||
|
tiddlerInfo: self.tiddlerInfo[task.title]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
created: 20130825162100000
|
created: 20130825162100000
|
||||||
modified: 20131129094907624
|
modified: 20140814094907624
|
||||||
tags: dev moduletypes
|
tags: dev moduletypes
|
||||||
title: SyncAdaptorModules
|
title: SyncAdaptorModules
|
||||||
type: text/vnd.tiddlywiki
|
type: text/vnd.tiddlywiki
|
||||||
@ -27,12 +27,16 @@ Nothing should be exported if the adaptor detects that it isn't capable of opera
|
|||||||
|
|
||||||
Adaptor modules must handle the following methods.
|
Adaptor modules must handle the following methods.
|
||||||
|
|
||||||
!! `Constructor(syncer)`
|
!! `Constructor(options)`
|
||||||
|
|
||||||
Initialises a new adaptor instance.
|
Initialises a new adaptor instance.
|
||||||
|
|
||||||
|!Parameter |!Description |
|
|!Parameter |!Description |
|
||||||
|syncer |Syncer object that is using this adaptor |
|
|options |See below |
|
||||||
|
|
||||||
|
Options include:
|
||||||
|
|
||||||
|
* ''options.wiki'': reference to wiki to use with this syncadaptor
|
||||||
|
|
||||||
!! `getTiddlerInfo(tiddler)`
|
!! `getTiddlerInfo(tiddler)`
|
||||||
|
|
||||||
@ -91,10 +95,11 @@ Loads a tiddler from the server.
|
|||||||
|title |Title of tiddler to be retrieved |
|
|title |Title of tiddler to be retrieved |
|
||||||
|callback |Callback function invoked with parameter `err,tiddlerFields` |
|
|callback |Callback function invoked with parameter `err,tiddlerFields` |
|
||||||
|
|
||||||
!! `deleteTiddler(title,callback)`
|
!! `deleteTiddler(title,callback,tiddlerInfo)`
|
||||||
|
|
||||||
Delete a tiddler from the server.
|
Delete a tiddler from the server.
|
||||||
|
|
||||||
|!Parameter |!Description |
|
|!Parameter |!Description |
|
||||||
|title |Title of tiddler to be deleted |
|
|title |Title of tiddler to be deleted |
|
||||||
|callback |Callback function invoked with parameter `err` |
|
|callback |Callback function invoked with parameter `err` |
|
||||||
|
|tiddlerInfo |The tiddlerInfo maintained by the syncer for this tiddler |
|
||||||
|
@ -16,9 +16,9 @@ A sync adaptor module for synchronising with the local filesystem via node.js AP
|
|||||||
var fs = !$tw.browser ? require("fs") : null,
|
var fs = !$tw.browser ? require("fs") : null,
|
||||||
path = !$tw.browser ? require("path") : null;
|
path = !$tw.browser ? require("path") : null;
|
||||||
|
|
||||||
function FileSystemAdaptor(syncer) {
|
function FileSystemAdaptor(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.syncer = syncer;
|
this.wiki = options.wiki;
|
||||||
this.watchers = {};
|
this.watchers = {};
|
||||||
this.pending = {};
|
this.pending = {};
|
||||||
this.logger = new $tw.utils.Logger("FileSystem");
|
this.logger = new $tw.utils.Logger("FileSystem");
|
||||||
@ -31,7 +31,7 @@ function FileSystemAdaptor(syncer) {
|
|||||||
var tiddlers = $tw.loadTiddlersFromFile(filename).tiddlers;
|
var tiddlers = $tw.loadTiddlersFromFile(filename).tiddlers;
|
||||||
for(var t in tiddlers) {
|
for(var t in tiddlers) {
|
||||||
if(tiddlers[t].title) {
|
if(tiddlers[t].title) {
|
||||||
$tw.wiki.addTiddler(tiddlers[t]);
|
self.wiki.addTiddler(tiddlers[t]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,7 +152,7 @@ FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
|
|||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
content = $tw.wiki.renderTiddler("text/plain","$:/core/templates/tiddler-metadata",{variables: {currentTiddler: tiddler.fields.title}});
|
content = self.wiki.renderTiddler("text/plain","$:/core/templates/tiddler-metadata",{variables: {currentTiddler: tiddler.fields.title}});
|
||||||
fs.writeFile(fileInfo.filepath + ".meta",content,{encoding: "utf8"},function (err) {
|
fs.writeFile(fileInfo.filepath + ".meta",content,{encoding: "utf8"},function (err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -164,7 +164,7 @@ FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
|
|||||||
} else {
|
} else {
|
||||||
// Save the tiddler as a self contained templated file
|
// Save the tiddler as a self contained templated file
|
||||||
template = $tw.config.typeTemplates[fileInfo.type];
|
template = $tw.config.typeTemplates[fileInfo.type];
|
||||||
content = $tw.wiki.renderTiddler("text/plain",template,{variables: {currentTiddler: tiddler.fields.title}});
|
content = self.wiki.renderTiddler("text/plain",template,{variables: {currentTiddler: tiddler.fields.title}});
|
||||||
fs.writeFile(fileInfo.filepath,content,{encoding: "utf8"},function (err) {
|
fs.writeFile(fileInfo.filepath,content,{encoding: "utf8"},function (err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -188,7 +188,7 @@ FileSystemAdaptor.prototype.loadTiddler = function(title,callback) {
|
|||||||
/*
|
/*
|
||||||
Delete a tiddler and invoke the callback with (err)
|
Delete a tiddler and invoke the callback with (err)
|
||||||
*/
|
*/
|
||||||
FileSystemAdaptor.prototype.deleteTiddler = function(title,callback) {
|
FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) {
|
||||||
var self = this,
|
var self = this,
|
||||||
fileInfo = $tw.boot.files[title];
|
fileInfo = $tw.boot.files[title];
|
||||||
// Only delete the tiddler if we have writable information for the file
|
// Only delete the tiddler if we have writable information for the file
|
||||||
|
@ -15,15 +15,15 @@ A sync adaptor module for synchronising with TiddlyWeb compatible servers
|
|||||||
var CONFIG_HOST_TIDDLER = "$:/config/tiddlyweb/host",
|
var CONFIG_HOST_TIDDLER = "$:/config/tiddlyweb/host",
|
||||||
DEFAULT_HOST_TIDDLER = "$protocol$//$host$/";
|
DEFAULT_HOST_TIDDLER = "$protocol$//$host$/";
|
||||||
|
|
||||||
function TiddlyWebAdaptor(syncer) {
|
function TiddlyWebAdaptor(options) {
|
||||||
this.syncer = syncer;
|
this.wiki = options.wiki;
|
||||||
this.host = this.getHost();
|
this.host = this.getHost();
|
||||||
this.recipe = undefined;
|
this.recipe = undefined;
|
||||||
this.logger = new $tw.utils.Logger("TiddlyWebAdaptor");
|
this.logger = new $tw.utils.Logger("TiddlyWebAdaptor");
|
||||||
}
|
}
|
||||||
|
|
||||||
TiddlyWebAdaptor.prototype.getHost = function() {
|
TiddlyWebAdaptor.prototype.getHost = function() {
|
||||||
var text = this.syncer.wiki.getTiddlerText(CONFIG_HOST_TIDDLER,DEFAULT_HOST_TIDDLER),
|
var text = this.wiki.getTiddlerText(CONFIG_HOST_TIDDLER,DEFAULT_HOST_TIDDLER),
|
||||||
substitutions = [
|
substitutions = [
|
||||||
{name: "protocol", value: document.location.protocol},
|
{name: "protocol", value: document.location.protocol},
|
||||||
{name: "host", value: document.location.host}
|
{name: "host", value: document.location.host}
|
||||||
@ -46,8 +46,7 @@ Get the current status of the TiddlyWeb connection
|
|||||||
*/
|
*/
|
||||||
TiddlyWebAdaptor.prototype.getStatus = function(callback) {
|
TiddlyWebAdaptor.prototype.getStatus = function(callback) {
|
||||||
// Get status
|
// Get status
|
||||||
var self = this,
|
var self = this;
|
||||||
wiki = self.syncer.wiki;
|
|
||||||
this.logger.log("Getting status");
|
this.logger.log("Getting status");
|
||||||
$tw.utils.httpRequest({
|
$tw.utils.httpRequest({
|
||||||
url: this.host + "status",
|
url: this.host + "status",
|
||||||
@ -198,10 +197,12 @@ TiddlyWebAdaptor.prototype.loadTiddler = function(title,callback) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Delete a tiddler and invoke the callback with (err)
|
Delete a tiddler and invoke the callback with (err)
|
||||||
|
options include:
|
||||||
|
tiddlerInfo: the syncer's tiddlerInfo for this tiddler
|
||||||
*/
|
*/
|
||||||
TiddlyWebAdaptor.prototype.deleteTiddler = function(title,callback) {
|
TiddlyWebAdaptor.prototype.deleteTiddler = function(title,callback,options) {
|
||||||
var self = this,
|
var self = this,
|
||||||
bag = this.syncer.tiddlerInfo[title].adaptorInfo.bag;
|
bag = options.tiddlerInfo.adaptorInfo.bag;
|
||||||
// If we don't have a bag it means that the tiddler hasn't been seen by the server, so we don't need to delete it
|
// If we don't have a bag it means that the tiddler hasn't been seen by the server, so we don't need to delete it
|
||||||
if(!bag) {
|
if(!bag) {
|
||||||
return callback(null);
|
return callback(null);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user