From f25a3ce8ca0819a18d639dad407edfac58728ca7 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 28 Dec 2014 23:49:42 +0300 Subject: [PATCH] more refactoring... Signed-off-by: Alex A. Naanou --- ui (gen4)/file.js | 73 ++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/ui (gen4)/file.js b/ui (gen4)/file.js index 323e284b..e2a2e1e7 100755 --- a/ui (gen4)/file.js +++ b/ui (gen4)/file.js @@ -26,6 +26,7 @@ var tasks = require('lib/tasks') var INDEX_DIR = '.ImageGrid' + /*********************************************************************/ // Queue // @@ -34,35 +35,23 @@ var INDEX_DIR = '.ImageGrid' - /*********************************************************************/ -// things we need... -// - load latest by pattern -// - merge -// - load latest base -// - merge diffs later than base -// - find index(s) in subtree -// - load index -// - data version -// - join indexes -// - take care of different base paths in images -// -// -// -// Might also be a nice idea to generic import: -// - get all .ImageGrid/*.json -// - group by ([a-z]*).* — pattern with -// - sort by name, descending -// - split at first non-diff -// - merge diff's in reverse tail to head -// -// ...and output to format: -// { -// : , -// ... -// } -// +// helpers... +// Guarantee that the 'end' and 'match' handlers will always get called +// with all results at least once... +// +// This does two things: +// - every 'end' event handler will get the full result set, regardless +// of when it was set... +// - every 'match' handler will be called for every match found, again +// regardless of whether it was set before or after the time of +// match. +// +// This prevents handlers from missing the event they are waiting for, +// essentially making it similar to how Promise/Deferred handle their +// callbacks. +// var guaranteeGlobEvents = module.guaranteeGlobEvents = function guaranteeGlobEvents(glob, all_matches){ @@ -90,6 +79,12 @@ function guaranteeGlobEvents(glob, all_matches){ } + + +/*********************************************************************/ +// Reader... + + // 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 @@ -121,7 +116,8 @@ function loadJSON(path){ // events emited: // - queued - json file path queued for loading // - loaded - done loading json file path -// - index - done loding index at path +// - index - done loading index at path +// - error - an error occurred... // // NOTE: logger must be an event emitter... // @@ -131,7 +127,7 @@ function loadJSON(path){ // f = m.loadIndex("L:/mnt/hdd15 (photo)/NTFS1/media/img/others") }) // .done(function(d){ console.log(d) }) // XXX need to do better error handling... -// XXX a bit overcomplicated, see if this can be split into more generic +// XXX a bit overcomplicated (???), see if this can be split into more generic // sections... var loadIndex = module.loadIndex = @@ -188,7 +184,7 @@ function(path, logger){ } }) - // add root files where needed... + // add base files back where needed... Object.keys(root) .forEach(function(k){ var n = root[k] @@ -212,6 +208,14 @@ function(path, logger){ var diffs = index[k] var latest = diffs.splice(-1)[0][1] + // NOTE: so far I really do not like how nested and + // unreadable the Promise/Deferred code becomes + // even with a small rise in complexity... + // ...for example, the following code is quite + // simple, but does not look the part. + // + // Maybe it's a style thing... + // load latest... return loadJSON(latest) .then(function(data){ @@ -224,6 +228,10 @@ function(path, logger){ // load diff... return loadJSON(p) // XXX handle errors... + // XXX we should abort loading this index... + .catch(function(err){ + logger && logger.emit('error', err) + }) .done(function(json){ // merge... for(var k in json){ @@ -275,6 +283,11 @@ function(path, logger){ +/*********************************************************************/ +// Writer... + + + /********************************************************************** * vim:set ts=4 sw=4 : */