Skip to content

refactor(forEach): change to for-of with iterable#919

Closed
PatrickJS wants to merge 1 commit into
angular:masterfrom
PatrickJS:for-of-iterable-refactor
Closed

refactor(forEach): change to for-of with iterable#919
PatrickJS wants to merge 1 commit into
angular:masterfrom
PatrickJS:for-of-iterable-refactor

Conversation

@PatrickJS
Copy link
Copy Markdown
Contributor

Tests are passing. I also used git mv to preserve the git history. I changed references with the name array to iterable
closes #909

@vicb
Copy link
Copy Markdown
Contributor

vicb commented Mar 11, 2015

@gdi2290 the Travis error comes from the benchmarks. infinite_scroll uses ForEach/For and needs to be updated

@PatrickJS
Copy link
Copy Markdown
Contributor Author

@vicb thanks I couldn't make out what the Travis error was. Give me a second

@PatrickJS
Copy link
Copy Markdown
Contributor Author

@vicb Dart doesn't have a for-of so how do we change the directive interface for Dart users to use for-in.

@PatrickJS
Copy link
Copy Markdown
Contributor Author

:/ I'm doing something wrong with my Dart version

dist/dart/angular2/:[hint] The value of the local variable 'item' is not used 
(/home/travis/build/angular/angular/dist/dart/angular2/lib/src/change_detection/pipes/iterable_changes.dart, line 110, col 9)
void iterateListLike(iter, fn(item)) {
  if (iter is List) {
    for (var item = 0; item < iter.length; ++item) {
      fn(iter[item]);
    }
  } else {
    for (var item in iter) {
      fn(item);
    }
  }
}

update: I found one error for iterable_changes in Dart.
update: The problem was due to optimization in array_changes.js (now iterable_changes.js)
previously

  check(collection):boolean {
    this._reset();

    var record:CollectionChangeRecord = this._itHead;
    var mayBeDirty:boolean = false;
    var index:int, item;

    if (ListWrapper.isList(collection)) {
      var list = collection;
      this._length = collection.length;

      for (index = 0; index < this._length; index++) {
        item = list[index];
        if (record === null || !looseIdentical(record.item, item)) {
          record = this._mismatch(record, item, index);
          mayBeDirty = true;
        } else if (mayBeDirty) {
          // TODO(misko): can we limit this to duplicates only?
          record = this._verifyReinsertion(record, item, index);
        }
        record = record._next;
      }
    } else {
      index = 0;
      iterateListLike(collection, (item) => {
        if (record === null || !looseIdentical(record.item, item)) {
          record = this._mismatch(record, item, index);
          mayBeDirty = true;
        } else if (mayBeDirty) {
          // TODO(misko): can we limit this to duplicates only?
          record = this._verifyReinsertion(record, item, index);
        }
        record = record._next;
        index++
      });
      this._length = index;
    }

after refactor

  check(collection):boolean {
    this._reset();

    var record:CollectionChangeRecord = this._itHead;
    var mayBeDirty:boolean = false;
    var index:int, item;

    index = 0;
    iterateListLike(collection, (item) => {
      if (record === null || !looseIdentical(record.item, item)) {
        record = this._mismatch(record, item, index);
        mayBeDirty = true;
      } else if (mayBeDirty) {
        // TODO(misko): can we limit this to duplicates only?
        record = this._verifyReinsertion(record, item, index);
      }
      record = record._next;
      index++
    });
    this._length = index;

the problem was this line sine var item hoist as undefined some people show it explicitly but was more subtle was the combination of multiple variable declaration using , on the same line rather than new line

    var index:int, item;

which is now

    var index:int = 0;

@PatrickJS PatrickJS force-pushed the for-of-iterable-refactor branch 6 times, most recently from 5d4b3c1 to e8bfaff Compare March 13, 2015 18:03
@PatrickJS
Copy link
Copy Markdown
Contributor Author

@mhevery I rebased and fixed the merge conflicts

@PatrickJS
Copy link
Copy Markdown
Contributor Author

is there a better way to rebase on github? I always force push up changes after rebase but that also blows up any commits that were made in the files previously :/

@PatrickJS PatrickJS force-pushed the for-of-iterable-refactor branch from e8bfaff to d0eb5b4 Compare March 13, 2015 18:35
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I also fixed the build in this commit foreach_spec.js#L127

@mhevery
Copy link
Copy Markdown
Contributor

mhevery commented Mar 13, 2015

In general I like this commit. You did make the change detection slower however. Those parts need to be reverted, but the rest can go. Please ping this thread once ready.

@PatrickJS PatrickJS force-pushed the for-of-iterable-refactor branch from d0eb5b4 to ede0c68 Compare March 13, 2015 19:57
@PatrickJS
Copy link
Copy Markdown
Contributor Author

The commit was squash without other changes. I also preserved the git history during renaming. There needs to be Iterator facade next and then a perf PR to avoid polymorphic(or was it megamorphic) code for when it's an iterable

@caitp
Copy link
Copy Markdown
Contributor

caitp commented Mar 13, 2015

there are only a handful of iterables most people are going to use, it's not likely to result in IC misses if the path is only taken when the object is known statically to be iterable (would not necessarily "replace" Array change detection with Iterable change detection, though.)

@PatrickJS PatrickJS force-pushed the for-of-iterable-refactor branch from ede0c68 to 80610c6 Compare March 13, 2015 21:09
@PatrickJS
Copy link
Copy Markdown
Contributor Author

gg yet another rebase and git mv. I have to update the TodoMVC example to use For rather than Foreach
update: I made a helper script so rebase isn't a pain anymore https://github.com/gdi2290/git-mv

@PatrickJS PatrickJS force-pushed the for-of-iterable-refactor branch from 80610c6 to bbf4db1 Compare March 13, 2015 23:22
@PatrickJS
Copy link
Copy Markdown
Contributor Author

@mhevery PR is all green now with correct rebase and renames tracked by git

rename: foreach -> for
rename: array -> iterable
update: DartParseTreeWriter
update: naive_infinite_scroll
update: todo
fix: tests in foreach_spec
@PatrickJS PatrickJS force-pushed the for-of-iterable-refactor branch from bbf4db1 to c0f1d0f Compare March 17, 2015 22:32
@PatrickJS
Copy link
Copy Markdown
Contributor Author

@mhevery I rebase'd Master again so everything is green. If you have any suggestions or changes feel free to ping me again about it

mhevery pushed a commit that referenced this pull request Mar 19, 2015
rename: foreach -> for
rename: array -> iterable
update: DartParseTreeWriter
update: naive_infinite_scroll
update: todo
fix: tests in foreach_spec

Closes #919
@mhevery mhevery added @lgtm action: merge The PR is ready for merge by the caretaker labels Mar 21, 2015
@mhevery
Copy link
Copy Markdown
Contributor

mhevery commented Mar 21, 2015

b61b8d6

@mhevery mhevery closed this Mar 21, 2015
mhevery pushed a commit that referenced this pull request Mar 21, 2015
rename: foreach -> for
rename: array -> iterable
update: DartParseTreeWriter
update: naive_infinite_scroll
update: todo
fix: tests in foreach_spec

Closes #919
@PatrickJS PatrickJS deleted the for-of-iterable-refactor branch May 20, 2015 00:47
@angular-automatic-lock-bot
Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker cla: yes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend *foreach syntax to support ES6 data structures

5 participants