refactor(collection): use Map instead of Object

This commit is contained in:
vsavkin 2014-10-18 17:50:55 -04:00
parent 1f4caa8773
commit 2a4b63b614
4 changed files with 11 additions and 12 deletions

View File

@ -45,7 +45,7 @@ export class Injector {
_createListOfBindings(flattenBindings):List { _createListOfBindings(flattenBindings):List {
var bindings = ListWrapper.createFixedSize(Key.numberOfKeys() + 1); var bindings = ListWrapper.createFixedSize(Key.numberOfKeys() + 1);
MapWrapper.forEach(flattenBindings, (keyId, v) => bindings[keyId] = v); MapWrapper.forEach(flattenBindings, (v, keyId) => bindings[keyId] = v);
return bindings; return bindings;
} }

View File

@ -1,7 +1,7 @@
import {MapWrapper} from 'facade/collection'; import {MapWrapper, Map} from 'facade/collection';
import {FIELD, int, isPresent} from 'facade/lang'; import {FIELD, int, isPresent} from 'facade/lang';
var _allKeys = {}; var _allKeys = MapWrapper.create();
var _id:int = 0; var _id:int = 0;
export class Key { export class Key {
@ -15,8 +15,9 @@ export class Key {
static get(token) { static get(token) {
if (token instanceof Key) return token; if (token instanceof Key) return token;
var obj = MapWrapper.get(_allKeys, token); if (MapWrapper.contains(_allKeys, token)) {
if (isPresent(obj)) return obj; return MapWrapper.get(_allKeys, token);
}
var newKey = new Key(token, ++_id); var newKey = new Key(token, ++_id);
MapWrapper.set(_allKeys, token, newKey); MapWrapper.set(_allKeys, token, newKey);

View File

@ -9,7 +9,7 @@ class MapWrapper {
static void set(m, k, v){ m[k] = v; } static void set(m, k, v){ m[k] = v; }
static contains(m, k) => m.containsKey(k); static contains(m, k) => m.containsKey(k);
static forEach(m, fn) { static forEach(m, fn) {
m.forEach(fn); m.forEach((k,v) => fn(v,k));
} }
} }

View File

@ -4,13 +4,11 @@ export var Set = window.Set;
export class MapWrapper { export class MapWrapper {
static create():Map { return new Map(); } static create():Map { return new Map(); }
static get(m, k) { return m[k]; } static get(m, k) { return m.get(k); }
static set(m, k, v) { m[k] = v; } static set(m, k, v) { m.set(k,v); }
static contains(m, k) { return m[k] != undefined; } static contains(m, k) { return m.has(k); }
static forEach(m, fn) { static forEach(m, fn) {
for(var k in m) { m.forEach(fn);
fn(k, m[k]);
}
} }
} }