fixed indexing issues...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-05-07 18:17:03 +03:00
parent 29b4b4930f
commit 3e264b88c1

View File

@ -842,11 +842,10 @@ var BaseBrowserPrototype = {
// XXX which of the forms should be documented in the signature??? // XXX which of the forms should be documented in the signature???
// NOTE: it does not matter which is used as we manually // NOTE: it does not matter which is used as we manually
// parse arguments... // parse arguments...
// XXX BUG: indexes broken on reverse walking... // XXX passing both index directly and context containing index
// to reproduce: // (context.index) feels excessive...
// dialog_1.walk2((e, i, p) => i, {reverse: false}) //-> correct numbers // ...if this can produce errors we need to simplify...
// dialog_1.walk2((e, i, p) => i, {reverse: 'flat'}) //-> mess // XXX add docs about maintaining context to implement/extend walkers...
// dialog_1.walk2((e, i, p) => i, {reverse: 'tree'}) //-> mess
// XXX can this be simpler??? // XXX can this be simpler???
walk2: function(func, recursion, walkable, options){ walk2: function(func, recursion, walkable, options){
var that = this var that = this
@ -931,6 +930,7 @@ var BaseBrowserPrototype = {
[] []
: list : list
// call .walk2(..) recursively...
var useWalk = function(){ var useWalk = function(){
return list.walk2( return list.walk2(
func, func,
@ -942,24 +942,28 @@ var BaseBrowserPrototype = {
options, options,
context) } context) }
// XXX can we add a simpler default case option where:
// - we call the target method name (given in recursion as string)
// - need a way to form the arguments, i.e. get the
// current state and form the args for the next call...
// - if above not available, call walk()
return (list === false ? return (list === false ?
[] []
: list instanceof Array ? : list instanceof Array ?
// NOTE: this gets the path and i from context... // NOTE: this gets the path and i from context...
next('do', state, ...(reverse ? list.slice().reverse() : list)) next('do', state,
...(reverse ?
list.slice().reverse()
: list))
// user-defined recursion... // user-defined recursion...
: recursion instanceof Function ? : recursion instanceof Function ?
recursion.call(that, list, context.index, p, options, context, func, useWalk) recursion.call(that,
list, context.index, p,
options, context,
func, useWalk)
// method with arg forming... // method with arg forming...
: formArgs instanceof Function : formArgs instanceof Function
&& list[recursion] ? && list[recursion] ?
list[recursion]( list[recursion](
...(formArgs(list, context.index, p, options, context, func) || [])) ...(formArgs(
list, context.index, p,
options, context,
func, useWalk) || []))
: useWalk()) : useWalk())
.run(function(){ .run(function(){
// normalize... // normalize...
@ -967,18 +971,19 @@ var BaseBrowserPrototype = {
this this
: [this] : [this]
// merge recursion results into states... // merge recursion results into states...
if(!(list === false || list instanceof Array)){ // NOTE: since we pass on the context we do
state.splice(state.length, 0, ...nested) // not really need to update the index
context.index += nested.length // here...
} !(list === false || list instanceof Array)
&& state.splice(state.length, 0, ...nested)
return nested return nested
}) })
} }
// prepare context... // prepare context...
var id = node.id || node.value var id = node.id || node.value
// XXX this is all over the place -- revise path handling... path = context.path = context.path || path
path = context.path = this.path = this.path || path
var [inline, p, children] = var [inline, p, children] =
// inline... // inline...
isWalkable(node) ? isWalkable(node) ?
@ -987,7 +992,8 @@ var BaseBrowserPrototype = {
: (!skipNested && isWalkable(node.children)) ? : (!skipNested && isWalkable(node.children)) ?
[false, [false,
// prepare context for nested items... // prepare context for nested items...
context.path = this.path = path.concat([id]), path.push(id)
&& path,
node.children] node.children]
// leaf... // leaf...
: [false, path.concat([id]), undefined] : [false, path.concat([id]), undefined]
@ -1012,7 +1018,7 @@ var BaseBrowserPrototype = {
children children
&& (doNested(), && (doNested(),
// restore path context... // restore path context...
this.path.pop()) context.path.pop())
return state return state
}, },