1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Added deletion support to HttpSync and LocalFileSync

This commit is contained in:
Jeremy Ruston 2012-04-05 18:25:39 +01:00
parent 0ac55688c4
commit d34f163dbc
3 changed files with 53 additions and 21 deletions

View File

@ -4,7 +4,7 @@ title: js/HttpSync.js
\*/ \*/
(function(){ (function(){
/*jslint node: true */ /*jslint node: true, browser: true */
"use strict"; "use strict";
function HttpSync(store) { function HttpSync(store) {
@ -12,7 +12,8 @@ function HttpSync(store) {
this.changeCounts = {}; this.changeCounts = {};
store.addEventListener("",function(changes) { store.addEventListener("",function(changes) {
for(var title in changes) { for(var title in changes) {
var tiddler = store.getTiddler(title); var x = new XMLHttpRequest(),
tiddler = store.getTiddler(title);
if(tiddler) { if(tiddler) {
var fieldStrings = tiddler.getFieldStrings(), var fieldStrings = tiddler.getFieldStrings(),
fields = {}, fields = {},
@ -21,10 +22,12 @@ function HttpSync(store) {
fields[fieldStrings[t].name] = fieldStrings[t].value; fields[fieldStrings[t].name] = fieldStrings[t].value;
} }
fields.text = tiddler.text; fields.text = tiddler.text;
var x = new XMLHttpRequest();
x.open("PUT",window.location.toString() + encodeURIComponent(title),true); x.open("PUT",window.location.toString() + encodeURIComponent(title),true);
x.setRequestHeader("Content-type", "application/json"); x.setRequestHeader("Content-type", "application/json");
x.send(JSON.stringify(fields)); x.send(JSON.stringify(fields));
} else {
x.open("DELETE",window.location.toString() + encodeURIComponent(title),true);
x.send();
} }
} }
}); });

View File

@ -15,19 +15,26 @@ var retrieveFile = require("./FileRetriever.js").retrieveFile,
util = require("util"), util = require("util"),
async = require("async"); async = require("async");
function LocalFileSync(dirpath,store,callback) { function LocalFileSync(dirpath,store,callback) {
this.dirpath = dirpath; this.dirpath = dirpath;
this.store = store; this.store = store;
this.callback = callback; this.callback = callback;
this.changeCounts = {}; // A hashmap of <tiddlername>: <changeCount> this.tiddlers = {}; // A hashmap of <tiddlername>: {changeCount: <changeCount>, files: [name]}
var self = this; var self = this,
// Set up a queue for loading tiddler files sanitizeFilepath = function(filepath) {
return filepath.replace(/\//mg,"-");
};
// Set up a queue for loading tiddler files, tasks are {filepath:,callback:}
this.loadQueue = async.queue(function(task,callback) { this.loadQueue = async.queue(function(task,callback) {
task.files = [];
retrieveFile(task.filepath,self.dirpath,function(err,data) { retrieveFile(task.filepath,self.dirpath,function(err,data) {
if(err) { if(err) {
callback(err); callback(err);
} else { } else {
// Use the filepath as the default title and src for the tiddler task.files.push(task.filepath);
// Use the filepath as the default title for the tiddler
var fields = { var fields = {
title: data.path title: data.path
}; };
@ -39,6 +46,7 @@ function LocalFileSync(dirpath,store,callback) {
if(err && err.code !== "ENOENT" && err.code !== "404") { if(err && err.code !== "ENOENT" && err.code !== "404") {
callback(err); callback(err);
} else { } else {
task.files.push(metafile);
var fields = tiddlers[0]; var fields = tiddlers[0];
if(!err) { if(!err) {
var text = data.text.split("\n\n")[0]; var text = data.text.split("\n\n")[0];
@ -70,14 +78,17 @@ function LocalFileSync(dirpath,store,callback) {
for(var t=0; t<tiddlers.length; t++) { for(var t=0; t<tiddlers.length; t++) {
var tiddler = tiddlers[t]; var tiddler = tiddlers[t];
self.store.addTiddler(tiddler); self.store.addTiddler(tiddler);
self.changeCounts[tiddler.title] = self.store.getChangeCount(tiddler.title); self.tiddlers[tiddler.title] = {
changeCount: self.store.getChangeCount(tiddler.title),
files: task.files
};
} }
}; };
for(var t=0; t<files.length; t++) { for(var t=0; t<files.length; t++) {
var f = files[t]; var f = files[t];
if(["..",".",".DS_Store"].indexOf(f) === -1 && f.indexOf(".meta") !== f.length-5) { if(["..",".",".DS_Store"].indexOf(f) === -1 && f.indexOf(".meta") !== f.length-5) {
self.loadQueue.push({ self.loadQueue.push({
filepath: path.resolve(self.dirpath,f), filepath: f,
callback: loadCallback callback: loadCallback
}); });
} }
@ -89,7 +100,7 @@ function LocalFileSync(dirpath,store,callback) {
var data = task.data, var data = task.data,
encoding = "utf8"; encoding = "utf8";
if(task.binary) { if(task.binary) {
data = new Buffer(task.data,"base64").toString("binary"), data = new Buffer(task.data,"base64").toString("binary");
encoding = "binary"; encoding = "binary";
} }
fs.writeFile(self.dirpath + "/" + task.name,data,encoding,function(err) { fs.writeFile(self.dirpath + "/" + task.name,data,encoding,function(err) {
@ -98,22 +109,35 @@ function LocalFileSync(dirpath,store,callback) {
},10); },10);
// Install our event listener to listen out for tiddler changes // Install our event listener to listen out for tiddler changes
this.store.addEventListener("",function(changes) { this.store.addEventListener("",function(changes) {
var t;
for(var title in changes) { for(var title in changes) {
// Get the information about the tiddler // Get the information about the tiddler
var tiddler = self.store.getTiddler(title), var tiddler = self.store.getTiddler(title),
changeCount = self.store.getChangeCount(title), changeCount = self.store.getChangeCount(title),
lastChangeCount = self.changeCounts[title], tiddlerInfo = self.tiddlers[title],
files = []; files = [];
// Construct a changecount record if we don't have one if(tiddler) {
if(!lastChangeCount) { // Construct a changecount record if we don't have one
lastChangeCount = 0; if(!tiddlerInfo) {
self.changeCounts[title] = lastChangeCount; tiddlerInfo = {changeCount: 0, files: []};
} self.tiddlers[title] = tiddlerInfo;
// Save the tiddler if the changecount has increased }
if(changeCount > lastChangeCount) { // Save the tiddler if the changecount has increased
files = self.store.serializeTiddlers([tiddler],"application/x-tiddler"); if(changeCount > tiddlerInfo.changeCount) {
for(var t=0; t<files.length; t++) { files = self.store.serializeTiddlers([tiddler],"application/x-tiddler");
self.saveQueue.push(files[t]); for(t=0; t<files.length; t++) {
files[t].name = sanitizeFilepath(files[t].name);
tiddlerInfo.files.push(files[t].name);
self.saveQueue.push(files[t]);
}
}
} else {
// Delete the tiddler file if the tiddler has gone
if(tiddlerInfo) {
for(t=0; t<tiddlerInfo.files.length; t++) {
fs.unlink(self.dirpath + "/" + tiddlerInfo.files[t]);
}
delete self.tiddlers[title];
} }
} }
} }

View File

@ -190,6 +190,11 @@ var commandLineSwitches = {
response.end(); response.end();
}); });
break; break;
case "DELETE":
app.store.deleteTiddler(decodeURIComponent(path.substr(1)));
response.writeHead(204, "OK");
response.end();
break;
case "GET": case "GET":
if(path === "/") { if(path === "/") {
response.writeHead(200, {"Content-Type": "text/html"}); response.writeHead(200, {"Content-Type": "text/html"});