feat(Change Detector): Add support for collection content watch

This commit is contained in:
Victor Berchet
2014-11-24 18:42:53 +01:00
parent 2d2f44949d
commit bf71b94bde
15 changed files with 609 additions and 206 deletions

View File

@ -51,7 +51,7 @@ class ListWrapper {
static filter(List list, fn) => list.where(fn).toList();
static find(List list, fn) => list.firstWhere(fn, orElse:() => null);
static any(List list, fn) => list.any(fn);
static forEach(list, fn) {
static forEach(list, Function fn) {
list.forEach(fn);
}
static reduce(List list, Function fn, init) {
@ -68,6 +68,15 @@ class ListWrapper {
static void clear(List l) { l.clear(); }
}
bool isListLikeIterable(obj) => obj is Iterable;
void iterateListLike(iter, Function fn) {
assert(iter is Iterable);
for (var item in iter) {
fn (item);
}
}
class SetWrapper {
static Set createFromList(List l) { return new Set.from(l); }
static bool has(Set s, key) { return s.contains(key); }

View File

@ -1,4 +1,4 @@
import {int} from 'facade/lang';
import {int, isJsObject} from 'facade/lang';
export var List = window.Array;
export var Map = window.Map;
@ -25,12 +25,17 @@ export class MapWrapper {
static clear(m) { m.clear(); }
}
// TODO: cannot export StringMap as a type as Dart does not support
// renaming types...
// TODO: cannot export StringMap as a type as Dart does not support renaming types...
/**
* Wraps Javascript Objects
*/
export class StringMapWrapper {
// Note: We are not using Object.create(null) here due to
// performance!
static create():Object { return { }; }
static create():Object {
// Note: We are not using Object.create(null) here due to
// performance!
// http://jsperf.com/ng2-object-create-null
return { };
}
static get(map, key) {
return map.hasOwnProperty(key) ? map[key] : undefined;
}
@ -45,7 +50,9 @@ export class StringMapWrapper {
}
static forEach(map, callback) {
for (var prop in map) {
callback(map[prop], prop);
if (map.hasOwnProperty(prop)) {
callback(map[prop], prop);
}
}
}
}
@ -117,6 +124,19 @@ export class ListWrapper {
}
}
export function isListLikeIterable(obj):boolean {
if (!isJsObject(obj)) return false;
return ListWrapper.isList(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
}
export function iterateListLike(obj, fn:Function) {
for (var item of obj) {
fn(item);
}
}
export class SetWrapper {
static createFromList(lst:List) { return new Set(lst); }
static has(s:Set, key):boolean { return s.has(key); }

View File

@ -161,6 +161,11 @@ dynamic getMapKey(value) {
return value.isNaN ? _NAN_KEY : value;
}
normalizeBlank(obj) {
dynamic normalizeBlank(obj) {
return isBlank(obj) ? null : obj;
}
bool isJsObject(o) {
return false;
}

View File

@ -202,3 +202,7 @@ export function getMapKey(value) {
export function normalizeBlank(obj) {
return isBlank(obj) ? null : obj;
}
export function isJsObject(o):boolean {
return o !== null && (typeof o === "function" || typeof o === "object");
}