2015-08-20 14:28:25 -07:00
|
|
|
import {MapWrapper} from 'angular2/src/core/facade/collection';
|
2015-09-10 15:25:36 -07:00
|
|
|
import {stringify, CONST, Type, isBlank} from 'angular2/src/core/facade/lang';
|
|
|
|
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
|
2015-05-08 17:57:03 -07:00
|
|
|
import {TypeLiteral} from './type_literal';
|
2015-05-20 13:46:55 -07:00
|
|
|
import {resolveForwardRef} from './forward_ref';
|
2015-05-08 17:57:03 -07:00
|
|
|
|
|
|
|
export {TypeLiteral} from './type_literal';
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* A unique object used for retrieving items from the {@link Injector}.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-04-17 13:01:07 -07:00
|
|
|
* Keys have:
|
|
|
|
* - a system-wide unique `id`.
|
2015-09-21 13:41:52 -07:00
|
|
|
* - a `token`.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-21 13:41:52 -07:00
|
|
|
* `Key` is used internally by {@link Injector} because its system-wide unique `id` allows the
|
|
|
|
* injector to store created objects in a more efficient way.
|
|
|
|
*
|
|
|
|
* `Key` should not be created directly. {@link Injector} creates keys automatically when resolving
|
|
|
|
* bindings.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2014-09-25 14:04:46 -07:00
|
|
|
export class Key {
|
2015-09-21 13:41:52 -07:00
|
|
|
/**
|
|
|
|
* Private
|
|
|
|
*/
|
2015-06-17 10:12:06 +02:00
|
|
|
constructor(public token: Object, public id: number) {
|
2015-05-13 15:54:46 -07:00
|
|
|
if (isBlank(token)) {
|
|
|
|
throw new BaseException('Token must be defined!');
|
|
|
|
}
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
2015-09-21 13:41:52 -07:00
|
|
|
/**
|
|
|
|
* Returns a stringified token.
|
|
|
|
*/
|
2015-06-17 10:12:06 +02:00
|
|
|
get displayName(): string { return stringify(this.token); }
|
2015-04-21 11:47:53 -07:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* Retrieves a `Key` for a token.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2015-07-07 20:03:00 -07:00
|
|
|
static get(token: Object): Key { return _globalKeyRegistry.get(resolveForwardRef(token)); }
|
2014-10-20 15:17:06 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* @returns the number of keys registered in the system.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
static get numberOfKeys(): number { return _globalKeyRegistry.numberOfKeys; }
|
2014-10-09 12:09:50 -04:00
|
|
|
}
|
2014-11-06 10:55:34 -08:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2014-11-06 10:55:34 -08:00
|
|
|
export class KeyRegistry {
|
2015-06-17 16:21:40 -07:00
|
|
|
private _allKeys: Map<Object, Key> = new Map();
|
2014-11-06 10:55:34 -08:00
|
|
|
|
2015-04-24 15:19:11 -07:00
|
|
|
get(token: Object): Key {
|
2014-11-06 10:55:34 -08:00
|
|
|
if (token instanceof Key) return token;
|
|
|
|
|
2015-05-08 17:57:03 -07:00
|
|
|
// TODO: workaround for https://github.com/Microsoft/TypeScript/issues/3123
|
|
|
|
var theToken = token;
|
|
|
|
if (token instanceof TypeLiteral) {
|
|
|
|
theToken = token.type;
|
|
|
|
}
|
|
|
|
token = theToken;
|
|
|
|
|
2015-06-17 21:42:56 -07:00
|
|
|
if (this._allKeys.has(token)) {
|
2015-06-17 16:21:40 -07:00
|
|
|
return this._allKeys.get(token);
|
2014-11-06 10:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var newKey = new Key(token, Key.numberOfKeys);
|
2015-06-17 16:21:40 -07:00
|
|
|
this._allKeys.set(token, newKey);
|
2014-11-06 10:55:34 -08:00
|
|
|
return newKey;
|
|
|
|
}
|
|
|
|
|
2015-06-17 10:12:06 +02:00
|
|
|
get numberOfKeys(): number { return MapWrapper.size(this._allKeys); }
|
2014-11-06 10:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _globalKeyRegistry = new KeyRegistry();
|