* JSONCache for caching JSON data (vs reading + parsing every time)

* Minor changes
This commit is contained in:
Bryan Ashby 2015-07-19 21:49:48 -06:00
parent 306e84b323
commit 89adc83fc6
9 changed files with 149 additions and 52 deletions

View file

@ -458,7 +458,6 @@ function display(options, cb) {
var continous = false;
/*
parser.on('row update', function rowUpdate(row) {
if(row >= nextPauseTermHeight) {
if(!continous && 'termHeight' === options.pause) {
@ -475,7 +474,6 @@ function display(options, cb) {
nextPauseTermHeight += options.client.term.termHeight;
}
});
*/
parser.on('mci', function mciEncountered(mciInfo) {
@ -507,7 +505,7 @@ function display(options, cb) {
mciPosQueue.push(mapKey);
options.client.term.rawWrite(ansi.queryPos());
options.client.term.write(ansi.queryPos(), false);
}
});
@ -542,12 +540,12 @@ function display(options, cb) {
}
if(ansiFont.length > 1) {
options.client.term.rawWrite(ansiFont);
options.client.term.write(ansiFont, false);
}
if(iceColors) {
options.client.term.rawWrite(ansi.blinkToBrightIntensity());
options.client.term.write(ansi.blinkToBrightIntensity(), false);
}
parser.reset(options.art);

View file

@ -124,6 +124,11 @@ function connectEntry(client) {
//
displayBanner(term);
/*
theme.displayThemeArt({client : client, name : 'DM-ENIG2.ANS'}, function onArt() {
});*/
setTimeout(function onTimeout() {
client.gotoMenuModule( { name : Config.firstMenu });
}, 500);

View file

@ -28,22 +28,6 @@ function FTNMailPacket(options) {
var self = this;
self.KLUDGE_PREFIX = '\x01';
/*
this.loadNodeAddresses = function() {
if(Config.networks) {
for(var name in Config.networks) {
if(!Config.networks[name].address) {
continue;
}
this.nodeAddresses[name] = Config.networks[name].address;
}
}
};
this.loadNodeAddresses();
*/
this.getPacketHeaderAddress = function() {
return {
zone : self.packetHeader.destZone,

View file

@ -43,18 +43,6 @@ function getDateFromFtnDateTime(dateTime) {
return (new Date(Date.parse(dateTime))).toISOString();
}
function getFormattedFTNAddress3D(zone, net, node) {
return util.format('%d:%d/%d', zone, net, node);
}
function getFormattedFTNAddress4D(zone, net, node, point) {
return util.format('%d:%d/%d.%d', zone, net, node, point);
}
function getFormattedFTNAddress5D(zone, net, node, point, domain) {
// :TODO:
}
function getFormattedFTNAddress(address, dimensions) {
var addr = util.format('%d:%d', address.zone, address.net);
switch(dimensions) {

65
core/json_cache.js Normal file
View file

@ -0,0 +1,65 @@
/* jslint node: true */
'use strict';
var Config = require('./config.js').config;
var Log = require('./logger.js').log;
var paths = require('path');
var fs = require('fs');
var Gaze = require('gaze').Gaze;
var stripJsonComments = require('strip-json-comments');
var assert = require('assert');
module.exports = exports = new JSONCache();
function JSONCache() {
var self = this;
this.cache = {}; // filePath -> JSON
this.gaze = new Gaze();
this.reCacheJSONFromFile = function(filePath, cb) {
fs.readFile(filePath, { encoding : 'utf-8' }, function fileRead(err, data) {
try {
self.cache[filePath] = JSON.parse(stripJsonComments(data));
cb(null, self.cache[filePath]);
} catch(e) {
cb(e);
}
});
};
this.gaze.on('error', function gazeErr(err) {
});
this.gaze.on('changed', function fileChanged(filePath) {
assert(filePath in self.cache);
Log.info( { filePath : filePath }, 'JSON file changed; recaching');
self.reCacheJSONFromFile(filePath, function reCached(err) {
if(err) {
Log.error( { error : err, filePath : filePath } , 'Error recaching JSON');
}
});
});
}
JSONCache.prototype.getJSON = function(fileName, cb) {
var self = this;
var filePath = paths.join(Config.paths.mods, fileName);
console.log(filePath)
if(filePath in this.cache) {
cb(null, this.cache[filePath]);
} else {
this.reCacheJSONFromFile(filePath, function fileCached(err, json) {
if(!err) {
self.gaze.add(filePath);
}
cb(err, json);
});
}
}

View file

@ -8,6 +8,7 @@ var conf = require('./config.js'); // :TODO: remove me!
var Config = require('./config.js').config;
var asset = require('./asset.js');
var theme = require('./theme.js');
var jsonCache = require('./json_cache.js');
var fs = require('fs');
var paths = require('path');
@ -22,28 +23,13 @@ exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
exports.handleAction = handleAction;
exports.applyThemeCustomization = applyThemeCustomization;
function loadModJSON(fileName, cb) {
// :TODO: really need to cache menu.json and prompt.json only reloading if they change - see chokidar & gaze npms
var filePath = paths.join(Config.paths.mods, fileName);
fs.readFile(filePath, { encoding : 'utf8' }, function jsonData(err, data) {
try {
var json = JSON.parse(stripJsonComments(data));
cb(null, json);
} catch(e) {
cb(e);
}
});
}
function getMenuConfig(name, cb) {
var menuConfig;
async.waterfall(
[
function loadMenuJSON(callback) {
loadModJSON('menu.json', function loaded(err, menuJson) {
jsonCache.getJSON('menu.json', function loaded(err, menuJson) {
callback(err, menuJson);
});
},
@ -57,7 +43,7 @@ function getMenuConfig(name, cb) {
},
function loadPromptJSON(callback) {
if(_.isString(menuConfig.prompt)) {
loadModJSON('prompt.json', function loaded(err, promptJson) {
jsonCache.getJSON('prompt.json', function loaded(err, promptJson) {
callback(err, promptJson);
});
} else {

View file

@ -19,6 +19,7 @@ exports.getRandomTheme = getRandomTheme;
exports.initAvailableThemes = initAvailableThemes;
exports.displayThemeArt = displayThemeArt;
// :TODO: use JSONCache here... may need to fancy it up a bit in order to have events for after re-cache, e.g. to update helpers below:
function loadTheme(themeID, cb) {
var path = paths.join(Config.paths.themes, themeID, 'theme.json');