2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2017-03-02 09:37:01 -08:00
|
|
|
import {stringify} from '../util';
|
2015-05-20 13:46:55 -07:00
|
|
|
import {resolveForwardRef} from './forward_ref';
|
2015-05-08 17:57:03 -07:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2016-04-14 12:35:24 -07:00
|
|
|
* A unique object used for retrieving items from the {@link ReflectiveInjector}.
|
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
|
|
|
*
|
2016-04-14 12:35:24 -07:00
|
|
|
* `Key` is used internally by {@link ReflectiveInjector} because its system-wide unique `id` allows
|
|
|
|
|
* the
|
2015-09-21 13:41:52 -07:00
|
|
|
* injector to store created objects in a more efficient way.
|
|
|
|
|
*
|
2016-04-14 12:35:24 -07:00
|
|
|
* `Key` should not be created directly. {@link ReflectiveInjector} creates keys automatically when
|
|
|
|
|
* resolving
|
2015-10-10 22:11:13 -07:00
|
|
|
* providers.
|
2017-07-27 13:49:33 -07:00
|
|
|
* @deprecated No replacement
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-04-14 12:35:24 -07:00
|
|
|
export class ReflectiveKey {
|
2017-09-28 13:36:44 -07:00
|
|
|
public readonly displayName: string;
|
2015-09-21 13:41:52 -07:00
|
|
|
/**
|
|
|
|
|
* Private
|
|
|
|
|
*/
|
2015-06-17 10:12:06 +02:00
|
|
|
constructor(public token: Object, public id: number) {
|
2016-09-30 09:26:53 -07:00
|
|
|
if (!token) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error('Token must be defined!');
|
2015-05-13 15:54:46 -07:00
|
|
|
}
|
2017-09-08 18:06:33 -07:00
|
|
|
this.displayName = stringify(this.token);
|
2014-09-30 14:56:33 -04: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
|
|
|
*/
|
2016-04-14 12:35:24 -07:00
|
|
|
static get(token: Object): ReflectiveKey {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
export class KeyRegistry {
|
2016-04-14 12:35:24 -07:00
|
|
|
private _allKeys = new Map<Object, ReflectiveKey>();
|
2014-11-06 10:55:34 -08:00
|
|
|
|
2016-04-14 12:35:24 -07:00
|
|
|
get(token: Object): ReflectiveKey {
|
|
|
|
|
if (token instanceof ReflectiveKey) return token;
|
2014-11-06 10:55:34 -08:00
|
|
|
|
2015-06-17 21:42:56 -07:00
|
|
|
if (this._allKeys.has(token)) {
|
2017-03-29 09:34:45 -07:00
|
|
|
return this._allKeys.get(token) !;
|
2014-11-06 10:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const newKey = new ReflectiveKey(token, ReflectiveKey.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-10-08 16:01:18 -07:00
|
|
|
get numberOfKeys(): number { return this._allKeys.size; }
|
2014-11-06 10:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const _globalKeyRegistry = new KeyRegistry();
|