diff --git a/packages/behavior/context/index.js b/packages/behavior/context/index.js index c70499e..28571fa 100644 --- a/packages/behavior/context/index.js +++ b/packages/behavior/context/index.js @@ -62,14 +62,17 @@ class Context extends Map { if ('key' !== first.type) { throw new TypeError(`First step in a traversal must be type "key"`); } - return rest.reduce((walk, step, index) => { - return this.takeStep(walk, (walk) => { - return fn(walk, step, index); + let previousNode = null; + return rest.reduce((node, step, index) => { + return this.takeStep(node, (node) => { + const result = fn(node, previousNode, step, index); + previousNode = node; + return result; }); }, this.get(first.key)); } - traverseOneStep(steps, node, step) { + traverseOneStep(steps, previousNode, node, step) { if ('undefined' === typeof node) { const rendered = this.renderStepsUntilNow(steps, step); debug(`"${rendered}" is traversed through but undefined`); @@ -80,17 +83,17 @@ class Context extends Map { return node[step.key]; case 'invoke': const args = step.args.map((arg) => arg.get(this)); - return fastApply(null, node, args); + return fastApply(previousNode, node, args); } } traverseRaw(traversal) { const {steps, value} = traversal; - return this.traverseAndDo(steps, (node, step, index) => { + return this.traverseAndDo(steps, (node, previousNode, step, index) => { const isLastStep = index === steps.length - 2; // Traverse if we're not at the final step with a value. if (!isLastStep || !value) { - return this.traverseOneStep(steps, node, step); + return this.traverseOneStep(steps, previousNode, node, step); } // Try to set the value. switch (step.type) {