Skip to content

TreeWalker in Bind & better variable names#7288

Merged
dreamofabear merged 6 commits intoampproject:masterfrom
dreamofabear:bind-rename
Feb 2, 2017
Merged

TreeWalker in Bind & better variable names#7288
dreamofabear merged 6 commits intoampproject:masterfrom
dreamofabear:bind-rename

Conversation

@dreamofabear
Copy link
Copy Markdown

Partial for #7230, related to #6199.

  • Replace getElementsByTagName with TreeWalker for DOM scan
  • Rename BindingDef -> BoundPropertyDef and evaluatee -> binding

/to @jridgewell

const elements = [];
let currentElement;
while (currentElement = treeWalker.nextNode()) {
elements.push(currentElement);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the great part about TreeWalker is you don't need to greedily grab every element into an array. Let's pass around the walker instance, and grab #nextNode in the chunktion loops.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done.

* @param {number} end
* @return {boolean}
*/
const scanFromTo = (start, end) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like we could simplify this to take an element, boundElements, and bindings, and extract it out of the closure.

function scanElement(element, boundElements, bindings) {
  // basically, the current scanElement_, with a bit of scanFromTo code
}

function chunktion(idleDeadline) {
  let completed = false;
  if (idleDeadline && !idleDeadline.didTimeout) {
    while (idleDeadline.timeRemaining() > 1) {
      var element = walker.next();
      if (!element) {
        completed = true;
        break;
      }
      scanElement(element, boundElements, bindings);
    }
  } else {
    for (let i = 0; i < 250; i++) {
      var element = walker.next();
      if (!element) {
        completed = true;
        break;
      }
      scanElement(element, boundElements, bindings);
    }
  }
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. Simplified the helper method and extracted the loop.

I kept the advancement of the tree walker in the helper to avoid duplicating the conditional with break above. I also left it as a closure because modifying input params is bad for code readability.

@dreamofabear dreamofabear merged commit df097a9 into ampproject:master Feb 2, 2017
@dreamofabear dreamofabear deleted the bind-rename branch February 2, 2017 20:25
torch2424 pushed a commit to torch2424/amphtml that referenced this pull request Feb 14, 2017
* rename 'bindings' to 'bound properties'

* rename 'evaluatee' to 'binding'

* use treewalker

* minor tweak

* justin's comments

* simplify scan helper func
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
* rename 'bindings' to 'bound properties'

* rename 'evaluatee' to 'binding'

* use treewalker

* minor tweak

* justin's comments

* simplify scan helper func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants