file.loadIndex(..) now appears to be working, still needs thought...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2014-12-28 03:47:44 +03:00
parent 669edd2180
commit 649c11047f
4 changed files with 74 additions and 17 deletions

View File

@ -4,7 +4,7 @@
* *
**********************************************************************/ **********************************************************************/
var path = require('path') var pathlib = require('path')
var events = require('events') var events = require('events')
var fse = require('fs.extra') var fse = require('fs.extra')
@ -78,7 +78,7 @@ function listJSON(path, pattern){
var loadFile = promise.denodeify(fse.readFile) var loadFile = promise.denodeify(fse.readFile)
function loadJSON(path){ function loadJSON(path){
return readFile(path).then(JSON.parse) return loadFile(path).then(JSON.parse)
} }
@ -92,30 +92,49 @@ function loadJSON(path){
// - end <indexes> - done loading all indexes // - end <indexes> - done loading all indexes
// //
// XXX return a promice rather than an event emitter.... // XXX return a promice rather than an event emitter....
function loadIndex(path, emitter){ // XXX test with:
// requirejs(['file'], function(m){
// m.loadIndex("L:/mnt/hdd15 (photo)/NTFS1/media/img/others")
// .on('index', function(){ console.log('!!!', arguments) }) })
var loadIndex =
module.loadIndex =
function(path, emitter){
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 var end = emitter == null
emitter = emitter == null ? new events.EventEmitter() : emitter 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)
.on('end', function(files){ .on('end', function(files){
var res = {} var res = {}
var index = {} var index = {}
var root = {}
// group by keyword... // group by keyword...
files files
.sort() .sort()
.reverse() .reverse()
.forEach(function(n){ .forEach(function(n){
var s = n.split(/[-.]/g).slice(0, -1) var b = pathlib.basename(n)
var s = b.split(/[-.]/g).slice(0, -1)
// <keyword>.json / non-diff // <keyword>.json / non-diff
// NOTE: this is a special case, we add this to
// a seporate index and then concat it to
// the final list if needed...
if(s.length == 1){ if(s.length == 1){
var k = s[0] var k = s[0]
var d = false root[k] = n
return
// <timestamp>-<keyword>[-diff].json / diff / non-diff // <timestamp>-<keyword>[-diff].json / diff / non-diff
} else { } else {
@ -125,20 +144,33 @@ function loadIndex(path, emitter){
// new keyword... // new keyword...
if(index[k] == null){ if(index[k] == null){
index[k] = [] index[k] = [[d, n]]
emitter.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] == false){ } else if(index[k].slice(-1)[0][0] == true){
index[k].push([d, n]) index[k].push([d, n])
emitter.emit('queued', n) emitter.emit('queued', n)
return
} }
index[k].push([d, n])
emitter.emit('queued', n)
}) })
// add root files where needed...
Object.keys(root).forEach(function(k){
var n = root[k]
// no diffs...
if(index[k] == null){
index[k] = [[false, n]]
emitter.emit('queued', n)
// add root file if no base is found...
} else if(index[k].slice(-1)[0][0] == true){
index[k].push([false, n])
emitter.emit('queued', n)
}
})
// load... // load...
promise promise
.all(Object.keys(index).map(function(k){ .all(Object.keys(index).map(function(k){
@ -155,6 +187,7 @@ function loadIndex(path, emitter){
.reverse() .reverse()
.map(function(p){ .map(function(p){
p = p[1] p = p[1]
// load diff...
return loadJSON(p) return loadJSON(p)
.done(function(json){ .done(function(json){
// merge... // merge...
@ -171,7 +204,7 @@ function loadIndex(path, emitter){
emitter.emit('loaded', latest) emitter.emit('loaded', latest)
}) })
}) })
}) }))
.then(function(){ .then(function(){
emitter.emit('index', path, res) emitter.emit('index', path, res)
@ -186,15 +219,24 @@ function loadIndex(path, emitter){
} else { } else {
var res = {} var res = {}
// collect the found indexes...
emitter.on('index', function(path, obj){ res[path] = obj })
listIndexes(path) listIndexes(path)
.on('end', function(indexes){ .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) }) indexes.forEach(function(path){ loadIndex(path, emitter) })
// XXX need to call this when the load was done...
emitter.emit('end', res)
}) })
} }

View File

@ -17,7 +17,7 @@
}, },
"dependencies": { "dependencies": {
"flickrapi": "^0.3.28", "flickrapi": "^0.3.28",
"fs.extra": "*", "fs.extra": "^1.2.1",
"glob": "^4.0.6", "glob": "^4.0.6",
"promise": "^6.0.1", "promise": "^6.0.1",
"requirejs": "*" "requirejs": "*"

View File

@ -11,6 +11,18 @@ window.nodejs = (typeof(process) === 'object' && process.features.uv)
: null : null
// XXX for some reason requirejs does not fall back to node's require...
if(nodejs){
var requirejs = require('requirejs')
requirejs.config({
nodeRequire: require,
//baseUrl: __dirname,
})
}
define(function(require){ var module = {} define(function(require){ var module = {}
console.log('>>> ui') console.log('>>> ui')
@ -37,6 +49,8 @@ var client = require('client')
var viewer = require('viewer') var viewer = require('viewer')
//var promise = require('promise')
/*********************************************************************/ /*********************************************************************/

View File

@ -17,6 +17,7 @@ var images = require('images')
var ribbons = require('ribbons') var ribbons = require('ribbons')
/*********************************************************************/ /*********************************************************************/
// helpers... // helpers...