some refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2014-12-28 20:46:06 +03:00
parent 004d574c14
commit 692b0e7b74

View File

@ -63,20 +63,53 @@ var INDEX_DIR = '.ImageGrid'
// } // }
// //
// XXX return a promise rather than an event emitter.... var guaranteeGlobEvents =
function listIndexes(base){ module.guaranteeGlobEvents =
return glob(base +'/**/'+ INDEX_DIR) function guaranteeGlobEvents(glob, all_matches){
all_matches = all_matches == null ? true : false
var visited = []
return glob
// keep track of visited matches...
.on('match', function(path){
all_matches && visited.push(path)
})
// trigger new handlers...
.on('newListener', function(evt, func){
// trigger the 'end' handler if we have already finished...
if(evt == 'end' && this.found != null){
func.call(this, this.found)
// trigger the 'match' handler for each match already found...
} else if(all_matches && evt == 'match' && visited.length > 0){
visited.forEach(function(path){
func.call(this, path)
})
}
})
} }
// XXX return a promise rather than an event emitter.... // XXX return a promise rather than an event emitter (???)
// XXX glob has a problem: if a match happens fast enough and we are slow
// enough to register a 'match' handler, then that match(s) will get
// missed...
function listIndexes(base){
return guaranteeGlobEvents(glob(base +'/**/'+ INDEX_DIR))
}
// XXX return a promise rather than an event emitter (???)
function listJSON(path, pattern){ function listJSON(path, pattern){
pattern = pattern || '*' pattern = pattern || '*'
return glob(path +'/'+ pattern +'.json') return guaranteeGlobEvents(glob(path +'/'+ pattern +'.json'))
} }
var loadFile = promise.denodeify(fse.readFile) var loadFile = promise.denodeify(fse.readFile)
// XXX handle errors...
function loadJSON(path){ function loadJSON(path){
return loadFile(path).then(JSON.parse) return loadFile(path).then(JSON.parse)
} }
@ -110,6 +143,10 @@ function(path, logger){
// we've got an index... // we've got an index...
if(p.length > 1 && /^\/*$/.test(last)){ if(p.length > 1 && /^\/*$/.test(last)){
listJSON(path) listJSON(path)
// XXX handle errors...
.on('error', function(err){
logger && logger.emit('error', err)
})
.on('end', function(files){ .on('end', function(files){
var res = {} var res = {}
var index = {} var index = {}
@ -152,7 +189,8 @@ function(path, logger){
}) })
// add root files where needed... // add root files where needed...
Object.keys(root).forEach(function(k){ Object.keys(root)
.forEach(function(k){
var n = root[k] var n = root[k]
// no diffs... // no diffs...
@ -185,6 +223,7 @@ function(path, logger){
p = p[1] p = p[1]
// load diff... // load diff...
return loadJSON(p) return loadJSON(p)
// XXX handle errors...
.done(function(json){ .done(function(json){
// merge... // merge...
for(var k in json){ for(var k in json){
@ -217,6 +256,10 @@ function(path, logger){
// XXX handle 'error' event... // XXX handle 'error' event...
listIndexes(path) listIndexes(path)
// XXX handle errors...
.on('error', function(err){
logger && logger.emit('error', err)
})
// collect the found indexes... // collect the found indexes...
.on('match', function(path){ .on('match', function(path){
loadIndex(path, logger) loadIndex(path, logger)