bugfix...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-09-30 00:29:28 +03:00
parent 95b089c317
commit 7df2a6b4d9
2 changed files with 43 additions and 26 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "generic-walk", "name": "generic-walk",
"version": "1.0.0", "version": "1.1.0",
"description": "An extensible tree walk(..) framework...", "description": "An extensible tree walk(..) framework...",
"main": "walk.js", "main": "walk.js",
"scripts": { "scripts": {

67
walk.js
View File

@ -46,7 +46,7 @@ function(getter, state, ...nodes){
// NOTE: this can leak out but we only care about it's identity thus // NOTE: this can leak out but we only care about it's identity thus
// no damage is likely to be done... // no damage is likely to be done...
var WalkStopException var WalkStopException
// This is used to hold the result when stop(..) is called, until we // this is used to hold the result when stop(..) is called, until we
// catch WalkStopException and return it from the walker... // catch WalkStopException and return it from the walker...
var stop_res var stop_res
@ -55,7 +55,7 @@ function(getter, state, ...nodes){
// construct a comfortable env for the user and handle the // construct a comfortable env for the user and handle the
// results... // results...
var _get = function(node){ var _get = function(node){
var next = [] var next_nodes = []
// stop walking... // stop walking...
// stop() // stop()
@ -63,32 +63,49 @@ function(getter, state, ...nodes){
// //
// NOTE: 'throw' is used to stop all handling, including // NOTE: 'throw' is used to stop all handling, including
// the rest of the current level... // the rest of the current level...
var stop = function(r){ var stop = function(state){
stop_res = r stop_res = state
WalkStopException = new Error('WALK_STOP_EVENT') WalkStopException = new Error('WALK_STOP_EVENT')
throw WalkStopException throw WalkStopException
} }
res = getter.call(context, // handle more nodes...
res, //
node, // Qeueue nodes for processing (breadth-first)...
function(action, state, ...nodes){ // next('queue', state, ...nodes)
// queue nodes (breadth-first)... // -> state
if(action == 'queue'){ // NOTE: this returns state as-is, this is done to
next = nodes // preserve signature compatibility with
// next('do', ..)...
//
// Process nodes (depth-first)...
// next('do', state, ...nodes)
// -> state
//
// Stop processing and return from walker...
// next('stop')
// next('stop', state)
//
var next = function(action, state, ...nodes){
// queue nodes (breadth-first)...
if(action == 'queue'){
next_nodes = nodes
// process nodes (depth-first)... // process nodes (depth-first)...
} else if(action == 'do'){ } else if(action == 'do'){
state = _step(context, nodes, state) state = _step(context, nodes, state)
// stop processing... // stop processing...
} else if(action == 'stop'){ } else if(action == 'stop'){
stop(state) stop(state)
} }
return state return state
}, }
stop)
return next // call the getter...
res = getter.call(context, res, node, next, stop)
return next_nodes
} }
return nodes.length == 0 ? return nodes.length == 0 ?
@ -98,10 +115,10 @@ function(getter, state, ...nodes){
// NOTE: see note below... ( ;) ) // NOTE: see note below... ( ;) )
: _step(context, nodes : _step(context, nodes
.map(_get) .map(_get)
.reduce(function(next, e){ .reduce(function(next_nodes, e){
return e instanceof Array ? return e instanceof Array ?
next.concat(e) next_nodes.concat(e)
: next.push(e) }, []), res) : next_nodes.push(e) }, []), res)
} }
// _step(..) wrapper, handle WalkStopException and setup the initial // _step(..) wrapper, handle WalkStopException and setup the initial
// context... // context...