Skip to content

Commit

Permalink
refactor(graph): Encounter.branchInto(nodes) instead of .next(nodes)
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jan 5, 2019
1 parent 99cf82c commit 571d7de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/graph/matching/encounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ export default class Encounter {
* For every outgoing node:
* - Run the match method, checking if it matches
*/
next(nodes, score, consumedTokens, data) {
if(! Array.isArray(nodes)) {
// If the first argument is not a list of nodes assume outgoing
return this.next(this.outgoing, nodes, score, consumedTokens);
}

next(score, consumedTokens, data) {
let nextIndex = this.currentIndex + (consumedTokens || 0);
const nextScore = this.currentScore + (score || 0);

Expand Down Expand Up @@ -152,6 +147,7 @@ export default class Encounter {
}
};

const nodes = this.outgoing;
let promise = Promise.resolve();
for(let i=0; i<nodes.length; i++) {
promise = promise.then(branchInto(nodes[i]));
Expand All @@ -174,11 +170,23 @@ export default class Encounter {
&& this.supportsFuzzy
&& nextIndex !== this.tokens.length - 1
) {
return this.next(nodes, (score || 0), (consumedTokens || 0) + 1, data);
return this.next((score || 0), (consumedTokens || 0) + 1, data);
}
});
}

/**
* Branch into and evaluate the expression against the given nodes.
*
* @param {array} nodes
*/
branchInto(nodes) {
const outgoing = this.outgoing;
this.outgoing = nodes;
return this.next()
.then(() => this.outgoing = outgoing);
}

branchWithOnMatch(newOnMatch, func) {
const onMatch = this.onMatch;
const currentDataDepth = this.currentDataDepth;
Expand Down
2 changes: 1 addition & 1 deletion src/graph/sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default class SubNode extends Node {
encounter.supportsFuzzy = this.supportsFuzzy;
encounter.skipPunctuation = this.skipPunctuation;

return encounter.next(this.roots);
return encounter.branchInto(this.roots);
}).then(() => {
// Switch back to previous supported values
encounter.supportsPartial = supportsPartial;
Expand Down

0 comments on commit 571d7de

Please sign in to comment.