mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-08-03 12:23:51 +00:00
Rudimentary fs.watch usage in filesystemadaptor
Will only watch for changes on wiki files loaded during startup or created by runtime (Does not yet track renames/deletes) (Will not notice new files) May become confused by directory structure changes on some platforms (see node docs on fs.watch for caveats!)
This commit is contained in:
parent
d3d72eff1b
commit
ee236060c7
@ -15,8 +15,30 @@ A sync adaptor module for synchronising with the local filesystem via node.js AP
|
|||||||
// Get a reference to the file system
|
// Get a reference to the file system
|
||||||
var fs = !$tw.browser ? require("fs") : null;
|
var fs = !$tw.browser ? require("fs") : null;
|
||||||
|
|
||||||
|
|
||||||
function FileSystemAdaptor(syncer) {
|
function FileSystemAdaptor(syncer) {
|
||||||
this.syncer = syncer;
|
this.syncer = syncer;
|
||||||
|
this.watchers = {};
|
||||||
|
this.pending = {};
|
||||||
|
|
||||||
|
this.setwatcher = function(filename, title) {
|
||||||
|
return this.watchers[filename] = this.watchers[filename] ||
|
||||||
|
fs.watch(filename, {persistent: false}, function(e) {
|
||||||
|
console.log("Filesystem:", e, filename);
|
||||||
|
if(e === "change") {
|
||||||
|
var tiddlers = $tw.loadTiddlersFromFile(filename).tiddlers;
|
||||||
|
for(var t in tiddlers) {
|
||||||
|
$tw.wiki.tiddlers[tiddlers[t].title] = new $tw.Tiddler(tiddlers[t]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for(var f in $tw.boot.files) {
|
||||||
|
var fileInfo = $tw.boot.files[f];
|
||||||
|
this.setwatcher(fileInfo.filepath, f);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemAdaptor.prototype.getTiddlerInfo = function(tiddler) {
|
FileSystemAdaptor.prototype.getTiddlerInfo = function(tiddler) {
|
||||||
@ -63,6 +85,7 @@ FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
|
|||||||
fileInfo.hasMetaFile = typeInfo.hasMetaFile;
|
fileInfo.hasMetaFile = typeInfo.hasMetaFile;
|
||||||
// Save the newly created fileInfo
|
// Save the newly created fileInfo
|
||||||
$tw.boot.files[title] = fileInfo;
|
$tw.boot.files[title] = fileInfo;
|
||||||
|
self.pending[fileInfo.filepath] = title;
|
||||||
// Pass it to the callback
|
// Pass it to the callback
|
||||||
callback(null,fileInfo);
|
callback(null,fileInfo);
|
||||||
});
|
});
|
||||||
@ -96,8 +119,16 @@ FileSystemAdaptor.prototype.generateTiddlerFilename = function(title,extension,e
|
|||||||
Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
|
Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
|
||||||
*/
|
*/
|
||||||
FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
|
FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
|
||||||
|
var self = this;
|
||||||
this.getTiddlerFileInfo(tiddler,function(err,fileInfo) {
|
this.getTiddlerFileInfo(tiddler,function(err,fileInfo) {
|
||||||
var template, content, encoding;
|
var template, content, encoding;
|
||||||
|
function _finish() {
|
||||||
|
if(self.pending[fileInfo.filepath]) {
|
||||||
|
self.setwatcher(fileInfo.filepath, tiddler.fields.title);
|
||||||
|
delete self.pending[fileInfo.filepath];
|
||||||
|
}
|
||||||
|
callback(null, {}, 0);
|
||||||
|
}
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
@ -117,7 +148,7 @@ FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
console.log("FileSystem: Saved file",fileInfo.filepath);
|
console.log("FileSystem: Saved file",fileInfo.filepath);
|
||||||
callback(null,{},0);
|
_finish();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@ -129,7 +160,7 @@ console.log("FileSystem: Saved file",fileInfo.filepath);
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
console.log("FileSystem: Saved file",fileInfo.filepath);
|
console.log("FileSystem: Saved file",fileInfo.filepath);
|
||||||
callback(null,{},0);
|
_finish();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -155,6 +186,9 @@ FileSystemAdaptor.prototype.deleteTiddler = function(title,callback) {
|
|||||||
// Don't delete the tiddler if it is on the blacklist
|
// Don't delete the tiddler if it is on the blacklist
|
||||||
callback(null);
|
callback(null);
|
||||||
} else {
|
} else {
|
||||||
|
if(this.watchers[fileInfo.filepath]) {
|
||||||
|
this.watchers[fileInfo.filepath].close();
|
||||||
|
}
|
||||||
// Delete the file
|
// Delete the file
|
||||||
fs.unlink(fileInfo.filepath,function(err) {
|
fs.unlink(fileInfo.filepath,function(err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user