UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

86 lines 2.84 kB
'use strict';var lang_1 = require('angular2/src/facade/lang'); var exceptions_1 = require('angular2/src/facade/exceptions'); var type_literal_1 = require('./type_literal'); var forward_ref_1 = require('./forward_ref'); var type_literal_2 = require('./type_literal'); exports.TypeLiteral = type_literal_2.TypeLiteral; /** * A unique object used for retrieving items from the {@link Injector}. * * Keys have: * - a system-wide unique `id`. * - a `token`. * * `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 * providers. */ var Key = (function () { /** * Private */ function Key(token, id) { this.token = token; this.id = id; if (lang_1.isBlank(token)) { throw new exceptions_1.BaseException('Token must be defined!'); } } Object.defineProperty(Key.prototype, "displayName", { /** * Returns a stringified token. */ get: function () { return lang_1.stringify(this.token); }, enumerable: true, configurable: true }); /** * Retrieves a `Key` for a token. */ Key.get = function (token) { return _globalKeyRegistry.get(forward_ref_1.resolveForwardRef(token)); }; Object.defineProperty(Key, "numberOfKeys", { /** * @returns the number of keys registered in the system. */ get: function () { return _globalKeyRegistry.numberOfKeys; }, enumerable: true, configurable: true }); return Key; })(); exports.Key = Key; /** * @internal */ var KeyRegistry = (function () { function KeyRegistry() { this._allKeys = new Map(); } KeyRegistry.prototype.get = function (token) { if (token instanceof Key) return token; // TODO: workaround for https://github.com/Microsoft/TypeScript/issues/3123 var theToken = token; if (token instanceof type_literal_1.TypeLiteral) { theToken = token.type; } token = theToken; if (this._allKeys.has(token)) { return this._allKeys.get(token); } var newKey = new Key(token, Key.numberOfKeys); this._allKeys.set(token, newKey); return newKey; }; Object.defineProperty(KeyRegistry.prototype, "numberOfKeys", { get: function () { return this._allKeys.size; }, enumerable: true, configurable: true }); return KeyRegistry; })(); exports.KeyRegistry = KeyRegistry; var _globalKeyRegistry = new KeyRegistry(); //# sourceMappingURL=key.js.map