1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Further refactoring of the new async design

This commit is contained in:
Jeremy Ruston 2011-11-28 15:15:35 +00:00
parent 0005c57669
commit 8bdafd521e
2 changed files with 18 additions and 16 deletions

View File

@ -4,6 +4,7 @@ throttling so that we don't get error EMFILE "Too many open files".
*/ */
var fs = require("fs"), var fs = require("fs"),
path = require("path"),
utils = require("./Utils.js"); utils = require("./Utils.js");
var FileRetriever = exports; var FileRetriever = exports;
@ -17,7 +18,10 @@ var fileRequestQueue = utils.queue(function(task,callback) {
// Retrieve a file given a filepath specifier and a context path. If the filepath isn't an absolute // Retrieve a file given a filepath specifier and a context path. If the filepath isn't an absolute
// filepath or an absolute URL, then it is interpreted relative to the context path, which can also be // filepath or an absolute URL, then it is interpreted relative to the context path, which can also be
// a filepath or a URL. On completion, the callback function is called as callback(err,data) // a filepath or a URL. It returns the final path used to reach the file. On completion, the callback
// function is called as callback(err,data)
FileRetriever.retrieveFile = function(filepath,contextPath,callback) { FileRetriever.retrieveFile = function(filepath,contextPath,callback) {
fileRequestQueue.push({filepath: filepath},callback); var newpath = path.resolve(path.dirname(contextPath),filepath);
fileRequestQueue.push({filepath: newpath},callback);
return newpath;
} }

View File

@ -44,7 +44,7 @@ var Recipe = function(store,filepath,callback) {
this.ingredients = {}; // Hashmap of array of ingredients this.ingredients = {}; // Hashmap of array of ingredients
this.callback = callback; this.callback = callback;
this.fetchCount = 0; this.fetchCount = 0;
this.readRecipe(filepath); // Read the recipe file this.readRecipe(filepath,process.cwd()); // Read the recipe file
} }
// The fetch counter is used to keep track of the number of asynchronous requests outstanding // The fetch counter is used to keep track of the number of asynchronous requests outstanding
@ -60,18 +60,17 @@ Recipe.prototype.decFetchCount = function() {
} }
// Process the contents of a recipe file // Process the contents of a recipe file
Recipe.prototype.readRecipe = function(filepath) { Recipe.prototype.readRecipe = function(filepath,contextPath) {
var dirname = path.dirname(filepath), var me = this;
me = this;
this.incFetchCount(); this.incFetchCount();
retrieveFile(filepath, null, function(err, data) { var actualPath = retrieveFile(filepath, contextPath, function(err, data) {
if (err) throw err; if (err) throw err;
me.processRecipe(data,dirname); me.processRecipe(data,actualPath);
me.decFetchCount(); me.decFetchCount();
}); });
} }
Recipe.prototype.processRecipe = function (data,dirname) { Recipe.prototype.processRecipe = function (data,contextPath) {
var me = this; var me = this;
data.split("\n").forEach(function(line) { data.split("\n").forEach(function(line) {
var p = line.indexOf(":"); var p = line.indexOf(":");
@ -79,14 +78,14 @@ Recipe.prototype.processRecipe = function (data,dirname) {
var marker = line.substr(0, p).trim(), var marker = line.substr(0, p).trim(),
value = line.substr(p+1).trim(); value = line.substr(p+1).trim();
if(marker === "recipe") { if(marker === "recipe") {
me.readRecipe(path.resolve(dirname,value)); me.readRecipe(value,contextPath);
} else { } else {
if(!(marker in me.ingredients)) { if(!(marker in me.ingredients)) {
me.ingredients[marker] = []; me.ingredients[marker] = [];
} }
var ingredientLocation = me.ingredients[marker].length; var ingredientLocation = me.ingredients[marker].length;
me.ingredients[marker][ingredientLocation] = null; me.ingredients[marker][ingredientLocation] = null;
me.readIngredient(dirname,value,function(fields) { me.readIngredient(value,contextPath,function(fields) {
var postProcess = me.readIngredientPostProcess[marker]; var postProcess = me.readIngredientPostProcess[marker];
if(postProcess) if(postProcess)
fields = postProcess(fields); fields = postProcess(fields);
@ -109,9 +108,8 @@ Recipe.prototype.readIngredientPostProcess = {
}; };
// Read an ingredient file and return it as a hashmap of tiddler fields. Also read the .meta file, if present // Read an ingredient file and return it as a hashmap of tiddler fields. Also read the .meta file, if present
Recipe.prototype.readIngredient = function(dirname,filepath,callback) { Recipe.prototype.readIngredient = function(filepath,contextPath,callback) {
var me = this, var me = this,
fullpath = path.resolve(dirname,filepath),
extname = path.extname(filepath), extname = path.extname(filepath),
basename = path.basename(filepath,extname), basename = path.basename(filepath,extname),
fields = { fields = {
@ -119,13 +117,13 @@ Recipe.prototype.readIngredient = function(dirname,filepath,callback) {
}; };
me.incFetchCount(); me.incFetchCount();
// Read the tiddler file // Read the tiddler file
retrieveFile(fullpath,null,function(err,data) { retrieveFile(filepath,contextPath,function(err,data) {
if (err) throw err; if (err) throw err;
fields = tiddlerInput.parseTiddler(data,extname,fields); fields = tiddlerInput.parseTiddler(data,extname,fields);
// Check for the .meta file // Check for the .meta file
var metafile = fullpath + ".meta"; var metafile = filepath + ".meta";
me.incFetchCount(); me.incFetchCount();
retrieveFile(metafile,null,function(err,data) { retrieveFile(metafile,contextPath,function(err,data) {
if(err && err.code !== 'ENOENT') { if(err && err.code !== 'ENOENT') {
throw err; throw err;
} }