70 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-04-15 22:35:38 +00:00
import {MapWrapper} from 'angular2/src/facade/collection';
//import {int} from 'angular2/src/facade/lang';
2015-04-15 22:35:38 +00:00
// TODO: uncoment `int` once https://github.com/angular/angular/issues/1414 is fixed
/**
2015-04-17 03:29:05 -07:00
* A unique object used for retrieving items from the [Injector].
2015-04-15 22:35:38 +00:00
*
* [Key]s have:
2015-04-17 03:29:05 -07:00
* - a system-wide unique [id].
* - a [token], usually the [Type] of the instance.
2015-04-15 22:35:38 +00:00
*
2015-04-17 03:29:05 -07:00
* [Key]s are used internally in [Injector]s because their system-wide unique [id]s allow the
* injector to index in arrays rather than looking up items in maps.
2015-04-15 22:35:38 +00:00
*
* @exportedAs angular2/di
*/
export class Key {
token;
2015-04-15 22:35:38 +00:00
id/* :int */;
metadata:any;
2015-04-15 22:35:38 +00:00
constructor(token, id/* :int */) {
this.token = token;
this.id = id;
2014-10-20 15:17:06 -04:00
this.metadata = null;
}
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* Retrieves a [Key] for a token.
2015-04-15 22:35:38 +00:00
*/
static get(token):Key {
return _globalKeyRegistry.get(token);
2014-10-20 15:17:06 -04:00
}
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* @returns the number of [Key]s registered in the system.
2015-04-15 22:35:38 +00:00
*/
static get numberOfKeys()/* :int */ {
return _globalKeyRegistry.numberOfKeys;
}
2014-10-09 12:09:50 -04:00
}
2015-04-15 22:35:38 +00:00
/**
* @private
*/
export class KeyRegistry {
_allKeys:Map;
constructor() {
this._allKeys = MapWrapper.create();
}
get(token):Key {
if (token instanceof Key) return token;
if (MapWrapper.contains(this._allKeys, token)) {
return MapWrapper.get(this._allKeys, token);
}
var newKey = new Key(token, Key.numberOfKeys);
MapWrapper.set(this._allKeys, token, newKey);
return newKey;
}
2015-04-15 22:35:38 +00:00
get numberOfKeys()/* :int */ {
return MapWrapper.size(this._allKeys);
}
}
var _globalKeyRegistry = new KeyRegistry();