mirror of
https://github.com/flynx/ImageGrid.git
synced 2025-11-01 03:40:09 +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,26 +91,19 @@ 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
|
|
||||||
|
|
||||||
// XXX to facilitate tracking this needs return an object that both
|
|
||||||
// emits events (EventEmitter) and holds state (promise)...
|
|
||||||
//return new promice(function(resolve, reject){
|
|
||||||
// // XXX
|
|
||||||
//})
|
|
||||||
|
|
||||||
// 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)
|
||||||
@ -145,13 +138,13 @@ function(path, emitter){
|
|||||||
// new keyword...
|
// new keyword...
|
||||||
if(index[k] == null){
|
if(index[k] == null){
|
||||||
index[k] = [[d, n]]
|
index[k] = [[d, n]]
|
||||||
emitter.emit('queued', n)
|
logger && logger.emit('queued', n)
|
||||||
|
|
||||||
// do not add anything past the latest non-diff
|
// do not add anything past the latest non-diff
|
||||||
// for each keyword...
|
// 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([d, n])
|
||||||
emitter.emit('queued', n)
|
logger && logger.emit('queued', n)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -162,12 +155,12 @@ function(path, emitter){
|
|||||||
// no diffs...
|
// no diffs...
|
||||||
if(index[k] == null){
|
if(index[k] == null){
|
||||||
index[k] = [[false, n]]
|
index[k] = [[false, n]]
|
||||||
emitter.emit('queued', n)
|
logger && logger.emit('queued', n)
|
||||||
|
|
||||||
// add root file if no base is found...
|
// add root file if no base is found...
|
||||||
} else if(index[k].slice(-1)[0][0] == true){
|
} else if(index[k].slice(-1)[0][0] == true){
|
||||||
index[k].push([false, n])
|
index[k].push([false, n])
|
||||||
emitter.emit('queued', n)
|
logger && logger.emit('queued', n)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -195,23 +188,23 @@ function(path, emitter){
|
|||||||
data[k] = json[k]
|
data[k] = json[k]
|
||||||
}
|
}
|
||||||
|
|
||||||
emitter.emit('loaded', p)
|
logger && logger.emit('loaded', p)
|
||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
.then(function(){
|
.then(function(){
|
||||||
res[k] = data
|
res[k] = data
|
||||||
|
|
||||||
emitter.emit('loaded', latest)
|
logger && logger.emit('loaded', latest)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
.then(function(){
|
.then(function(){
|
||||||
emitter.emit('index', path, res)
|
logger && logger.emit('index', path, res)
|
||||||
|
|
||||||
// indicate end only if we are not part of a multi-index load...
|
var d = {}
|
||||||
if(end){
|
d[path] = res
|
||||||
emitter.emit('end', {path: res})
|
|
||||||
}
|
resolve(d)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -219,28 +212,26 @@ function(path, emitter){
|
|||||||
} else {
|
} else {
|
||||||
var res = {}
|
var res = {}
|
||||||
|
|
||||||
|
|
||||||
listIndexes(path)
|
listIndexes(path)
|
||||||
.on('end', function(indexes){
|
.on('end', function(indexes){
|
||||||
var i = indexes.length
|
var i = indexes.length
|
||||||
|
|
||||||
|
indexes.forEach(function(path){
|
||||||
|
loadIndex(path, logger)
|
||||||
// collect the found indexes...
|
// collect the found indexes...
|
||||||
emitter.on('index', function(path, obj){
|
.done(function(obj){
|
||||||
i -= 1
|
i -= 1
|
||||||
res[path] = obj
|
res[path] = obj[path]
|
||||||
|
|
||||||
|
// call this when the load is done...
|
||||||
if(i <= 0){
|
if(i <= 0){
|
||||||
// XXX need to call this when the load was done...
|
resolve(res)
|
||||||
emitter.emit('end', res)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
indexes.forEach(function(path){ loadIndex(path, emitter) })
|
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
})
|
||||||
return emitter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user