mirror of
https://github.com/flynx/ImageGrid.git
synced 2026-01-04 09:21:08 +00:00
refactored file.loadIndex(..), still not done...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
649c11047f
commit
c5a8e28f33
@ -63,13 +63,13 @@ var INDEX_DIR = '.ImageGrid'
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
|
||||||
// XXX return a promice rather than an event emitter....
|
// XXX return a promise rather than an event emitter....
|
||||||
function listIndexes(base){
|
function listIndexes(base){
|
||||||
return glob(base +'/**/'+ INDEX_DIR)
|
return glob(base +'/**/'+ INDEX_DIR)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// XXX return a promice rather than an event emitter....
|
// 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 glob(path +'/'+ pattern +'.json')
|
||||||
@ -91,156 +91,147 @@ function loadJSON(path){
|
|||||||
// - index <path> <data> - done loding index at path
|
// - index <path> <data> - done loding index at path
|
||||||
// - end <indexes> - done loading all indexes
|
// - end <indexes> - done loading all indexes
|
||||||
//
|
//
|
||||||
// XXX return a promice rather than an event emitter....
|
|
||||||
// XXX test with:
|
// XXX test with:
|
||||||
// requirejs(['file'], function(m){
|
// requirejs(['file'],
|
||||||
// m.loadIndex("L:/mnt/hdd15 (photo)/NTFS1/media/img/others")
|
// function(m){
|
||||||
// .on('index', function(){ console.log('!!!', arguments) }) })
|
// f = m.loadIndex("L:/mnt/hdd15 (photo)/NTFS1/media/img/others") })
|
||||||
|
// .done(function(d){ console.log(d) })
|
||||||
|
// XXX need to do better error handling...
|
||||||
var loadIndex =
|
var loadIndex =
|
||||||
module.loadIndex =
|
module.loadIndex =
|
||||||
function(path, emitter){
|
function(path, logger){
|
||||||
var p = path.split(INDEX_DIR)
|
var p = path.split(INDEX_DIR)
|
||||||
var last = p.slice(-1)[0].trim()
|
var last = p.slice(-1)[0].trim()
|
||||||
|
|
||||||
var end = emitter == null
|
return new promise(function(resolve, reject){
|
||||||
emitter = emitter == null ? new events.EventEmitter() : emitter
|
// we've got an index...
|
||||||
|
if(p.length > 1 && /^\/*$/.test(last)){
|
||||||
|
listJSON(path)
|
||||||
|
.on('end', function(files){
|
||||||
|
var res = {}
|
||||||
|
var index = {}
|
||||||
|
var root = {}
|
||||||
|
|
||||||
// XXX to facilitate tracking this needs return an object that both
|
// group by keyword...
|
||||||
// emits events (EventEmitter) and holds state (promise)...
|
files
|
||||||
//return new promice(function(resolve, reject){
|
.sort()
|
||||||
// // XXX
|
.reverse()
|
||||||
//})
|
.forEach(function(n){
|
||||||
|
var b = pathlib.basename(n)
|
||||||
|
var s = b.split(/[-.]/g).slice(0, -1)
|
||||||
|
|
||||||
// we've got an index...
|
// <keyword>.json / non-diff
|
||||||
if(p.length > 1 && /^\/*$/.test(last)){
|
// NOTE: this is a special case, we add this to
|
||||||
listJSON(path)
|
// a seporate index and then concat it to
|
||||||
.on('end', function(files){
|
// the final list if needed...
|
||||||
var res = {}
|
if(s.length == 1){
|
||||||
var index = {}
|
var k = s[0]
|
||||||
var root = {}
|
root[k] = n
|
||||||
|
return
|
||||||
|
|
||||||
// group by keyword...
|
// <timestamp>-<keyword>[-diff].json / diff / non-diff
|
||||||
files
|
} else {
|
||||||
.sort()
|
var k = s[1]
|
||||||
.reverse()
|
var d = s[2] == 'diff'
|
||||||
.forEach(function(n){
|
}
|
||||||
var b = pathlib.basename(n)
|
|
||||||
var s = b.split(/[-.]/g).slice(0, -1)
|
|
||||||
|
|
||||||
// <keyword>.json / non-diff
|
// new keyword...
|
||||||
// NOTE: this is a special case, we add this to
|
if(index[k] == null){
|
||||||
// a seporate index and then concat it to
|
index[k] = [[d, n]]
|
||||||
// the final list if needed...
|
logger && logger.emit('queued', n)
|
||||||
if(s.length == 1){
|
|
||||||
var k = s[0]
|
|
||||||
root[k] = n
|
|
||||||
return
|
|
||||||
|
|
||||||
// <timestamp>-<keyword>[-diff].json / diff / non-diff
|
// do not add anything past the latest non-diff
|
||||||
} else {
|
// for each keyword...
|
||||||
var k = s[1]
|
} else if(index[k].slice(-1)[0][0] == true){
|
||||||
var d = s[2] == 'diff'
|
index[k].push([d, n])
|
||||||
}
|
logger && logger.emit('queued', n)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// new keyword...
|
// add root files where needed...
|
||||||
|
Object.keys(root).forEach(function(k){
|
||||||
|
var n = root[k]
|
||||||
|
|
||||||
|
// no diffs...
|
||||||
if(index[k] == null){
|
if(index[k] == null){
|
||||||
index[k] = [[d, n]]
|
index[k] = [[false, n]]
|
||||||
emitter.emit('queued', n)
|
logger && logger.emit('queued', n)
|
||||||
|
|
||||||
// do not add anything past the latest non-diff
|
// add root file if no base is found...
|
||||||
// for each keyword...
|
|
||||||
} else if(index[k].slice(-1)[0][0] == true){
|
} else if(index[k].slice(-1)[0][0] == true){
|
||||||
index[k].push([d, n])
|
index[k].push([false, n])
|
||||||
emitter.emit('queued', n)
|
logger && logger.emit('queued', n)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// add root files where needed...
|
// load...
|
||||||
Object.keys(root).forEach(function(k){
|
promise
|
||||||
var n = root[k]
|
.all(Object.keys(index).map(function(k){
|
||||||
|
// get relevant paths...
|
||||||
|
var diffs = index[k]
|
||||||
|
var latest = diffs.splice(-1)[0][1]
|
||||||
|
|
||||||
// no diffs...
|
// load latest...
|
||||||
if(index[k] == null){
|
return loadJSON(latest)
|
||||||
index[k] = [[false, n]]
|
.then(function(data){
|
||||||
emitter.emit('queued', n)
|
// handle diffs...
|
||||||
|
return promise
|
||||||
|
.all(diffs
|
||||||
|
.reverse()
|
||||||
|
.map(function(p){
|
||||||
|
p = p[1]
|
||||||
|
// load diff...
|
||||||
|
return loadJSON(p)
|
||||||
|
.done(function(json){
|
||||||
|
// merge...
|
||||||
|
for(var k in json){
|
||||||
|
data[k] = json[k]
|
||||||
|
}
|
||||||
|
|
||||||
// add root file if no base is found...
|
logger && logger.emit('loaded', p)
|
||||||
} else if(index[k].slice(-1)[0][0] == true){
|
})
|
||||||
index[k].push([false, n])
|
}))
|
||||||
emitter.emit('queued', n)
|
.then(function(){
|
||||||
}
|
res[k] = data
|
||||||
|
|
||||||
|
logger && logger.emit('loaded', latest)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
.then(function(){
|
||||||
|
logger && logger.emit('index', path, res)
|
||||||
|
|
||||||
|
var d = {}
|
||||||
|
d[path] = res
|
||||||
|
|
||||||
|
resolve(d)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// load...
|
// no explicit index given -- find all in sub tree...
|
||||||
promise
|
} else {
|
||||||
.all(Object.keys(index).map(function(k){
|
var res = {}
|
||||||
// get relevant paths...
|
|
||||||
var diffs = index[k]
|
|
||||||
var latest = diffs.splice(-1)[0][1]
|
|
||||||
|
|
||||||
// load latest...
|
listIndexes(path)
|
||||||
return loadJSON(latest)
|
.on('end', function(indexes){
|
||||||
.then(function(data){
|
var i = indexes.length
|
||||||
// handle diffs...
|
|
||||||
return promise
|
|
||||||
.all(diffs
|
|
||||||
.reverse()
|
|
||||||
.map(function(p){
|
|
||||||
p = p[1]
|
|
||||||
// load diff...
|
|
||||||
return loadJSON(p)
|
|
||||||
.done(function(json){
|
|
||||||
// merge...
|
|
||||||
for(var k in json){
|
|
||||||
data[k] = json[k]
|
|
||||||
}
|
|
||||||
|
|
||||||
emitter.emit('loaded', p)
|
indexes.forEach(function(path){
|
||||||
})
|
loadIndex(path, logger)
|
||||||
}))
|
// collect the found indexes...
|
||||||
.then(function(){
|
.done(function(obj){
|
||||||
res[k] = data
|
i -= 1
|
||||||
|
res[path] = obj[path]
|
||||||
|
|
||||||
emitter.emit('loaded', latest)
|
// call this when the load is done...
|
||||||
})
|
if(i <= 0){
|
||||||
|
resolve(res)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}))
|
|
||||||
.then(function(){
|
|
||||||
emitter.emit('index', path, res)
|
|
||||||
|
|
||||||
// indicate end only if we are not part of a multi-index load...
|
|
||||||
if(end){
|
|
||||||
emitter.emit('end', {path: res})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
// no explicit index given -- find all in sub tree...
|
|
||||||
} else {
|
|
||||||
var res = {}
|
|
||||||
|
|
||||||
|
|
||||||
listIndexes(path)
|
|
||||||
.on('end', function(indexes){
|
|
||||||
var i = indexes.length
|
|
||||||
|
|
||||||
// collect the found indexes...
|
|
||||||
emitter.on('index', function(path, obj){
|
|
||||||
i -= 1
|
|
||||||
res[path] = obj
|
|
||||||
|
|
||||||
if(i <= 0){
|
|
||||||
// XXX need to call this when the load was done...
|
|
||||||
emitter.emit('end', res)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
indexes.forEach(function(path){ loadIndex(path, emitter) })
|
})
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return emitter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user