From dde43d93f7ffbf545a2c1e39a3f608dc72d72fdc Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 9 Oct 2018 00:56:41 +0300 Subject: [PATCH] notes and minor simplification... Signed-off-by: Alex A. Naanou --- README.md | 4 +++- walk.js | 21 ++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2e6d7c6..52b9e5b 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,9 @@ User provided function, called to process a node. `getter(..)` is passed the cur *`next('queue', state, ...nodes) -> state`* Queue `nodes` for walking (*breadth-first*). The queued nodes will get *walked* after this level of nodes is done, i.e. after the `getter(..)` is called for each node on current level. -*Note that this does not change the state in any way and returns it as-is, this is done for signature compatibility with `next('do', ..)`* +*Note that this does not change the state in any way and returns it as-is, this is done for signature compatibility with `next('do', ..)`.* + +*Note that a common instinct here is to expect to get a promise as a result of `next('queue', ..)`, this is not needed as the `getter(..)` will eventually get all the queued nodes as input anyway and adding promises into the mix would only complicate things.* *`next('do', state, ...nodes) -> state`* diff --git a/walk.js b/walk.js index ac20ba6..c1f5b5f 100644 --- a/walk.js +++ b/walk.js @@ -27,6 +27,10 @@ // NOTE: state can not be a function... // // +// XXX Q: should next('queue', ...) return a promise??? +// ...currently I think no, there is no need to complicate things as +// the getter will eventually get all the queued nodes anyway and +// done(..) will get called when everything processed... // XXX this is essentially a version of .reduce(..), I wonder if it is // feasible to do a version of .map(..), i.e. a mechanism to modify/clone // the input tree... @@ -116,14 +120,17 @@ function(getter, state, ...nodes){ return nodes.length == 0 ? // no new nodes to walk... res - // do the next level... + // do the next level (recursive)... // NOTE: see note below... ( ;) ) - : _step(context, nodes - .map(_get) - .reduce(function(next_nodes, e){ - return e instanceof Array ? - next_nodes.concat(e) - : next_nodes.push(e) }, []), res) + : _step(context, + nodes + // process current nodes and get next level nodes... + .map(_get) + // merge next nodes into a single list... + .reduce(function(next_nodes, e){ + // NOTE: _get(..) can either return an array + // or stop execution... + return next_nodes.concat(e) }, []), res) } // _step(..) wrapper, handle WalkStopException and setup the initial // context...