fix(facade): workaround for lack of Symbol.iterator in es6-shim

Closes #4219
Fixes #4216
This commit is contained in:
Marc Laval 2015-09-17 00:35:59 +02:00
parent e166f6fe95
commit 390aacd442
4 changed files with 37 additions and 6 deletions

View File

@ -1,4 +1,6 @@
import {ListWrapper, MapWrapper} from 'angular2/src/core/facade/collection';
import {getSymbolIterator} from 'angular2/src/core/facade/lang';
/**
* An iterable and observable live list of components in the DOM.
@ -101,7 +103,7 @@ export class QueryList<T> {
map<U>(fn: (item: T) => U): U[] { return this._results.map(fn); }
[Symbol.iterator](): any { return this._results[Symbol.iterator](); }
[getSymbolIterator()](): any { return this._results[getSymbolIterator()](); }
// Internal to the framework.
fireCallbacks(): void {

View File

@ -1,4 +1,11 @@
import {isJsObject, global, isPresent, isBlank, isArray} from 'angular2/src/core/facade/lang';
import {
isJsObject,
global,
isPresent,
isBlank,
isArray,
getSymbolIterator
} from 'angular2/src/core/facade/lang';
export var Map = global.Map;
export var Set = global.Set;
@ -289,8 +296,8 @@ export class ListWrapper {
export function isListLikeIterable(obj: any): boolean {
if (!isJsObject(obj)) return false;
return isArray(obj) ||
(!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v]
Symbol.iterator in obj); // JS Iterable have a Symbol.iterator prop
(!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v]
getSymbolIterator() in obj); // JS Iterable have a Symbol.iterator prop
}
export function iterateListLike(obj: any, fn: Function) {
@ -299,7 +306,7 @@ export function iterateListLike(obj: any, fn: Function) {
fn(obj[i]);
}
} else {
var iterator = obj[Symbol.iterator]();
var iterator = obj[getSymbolIterator()]();
var item;
while (!((item = iterator.next()).done)) {
fn(item.value);

View File

@ -351,3 +351,24 @@ export function setValueOnPath(global: any, path: string, value: any) {
}
obj[parts.shift()] = value;
}
// When Symbol.iterator doesn't exist, retrieves the key used in es6-shim
var _symbolIterator = null;
export function getSymbolIterator(): string | symbol {
if (isBlank(_symbolIterator)) {
if (isPresent(Symbol) && isPresent(Symbol.iterator)) {
_symbolIterator = Symbol.iterator;
} else {
// es6-shim specific logic
var keys = Object.getOwnPropertyNames(Map.prototype);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key !== 'entries' && key !== 'size' &&
Map.prototype[key] === Map.prototype['entries']) {
_symbolIterator = key;
}
}
}
}
return _symbolIterator;
}

View File

@ -1,7 +1,8 @@
import {getSymbolIterator} from 'angular2/src/core/facade/lang';
export class TestIterable {
list: number[];
constructor() { this.list = []; }
[Symbol.iterator]() { return this.list[Symbol.iterator](); }
[getSymbolIterator()]() { return this.list[getSymbolIterator()](); }
}