UNPKG

injection-js

Version:

Dependency Injection library for JavaScript and TypeScript

96 lines 2.8 kB
/** * @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 */ import { stringify } from './facade/lang'; import { resolveForwardRef } from './forward_ref'; /** * A unique object used for retrieving items from the {@link ReflectiveInjector}. * * Keys have: * - a system-wide unique `id`. * - a `token`. * * `Key` is used internally by {@link ReflectiveInjector} 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 ReflectiveInjector} creates keys automatically when * resolving * providers. * @experimental */ var ReflectiveKey = /** @class */ (function () { /** * Private */ function ReflectiveKey(token, id) { this.token = token; this.id = id; if (!token) { throw new Error('Token must be defined!'); } } Object.defineProperty(ReflectiveKey.prototype, "displayName", { /** * Returns a stringified token. */ get: function () { return stringify(this.token); }, enumerable: false, configurable: true }); /** * Retrieves a `Key` for a token. */ ReflectiveKey.get = function (token) { // tslint:disable-next-line:no-use-before-declare return _globalKeyRegistry.get(resolveForwardRef(token)); }; Object.defineProperty(ReflectiveKey, "numberOfKeys", { /** * @returns the number of keys registered in the system. */ get: function () { // tslint:disable-next-line:no-use-before-declare return _globalKeyRegistry.numberOfKeys; }, enumerable: false, configurable: true }); return ReflectiveKey; }()); export { ReflectiveKey }; /** * @internal */ var KeyRegistry = /** @class */ (function () { function KeyRegistry() { this._allKeys = new Map(); } KeyRegistry.prototype.get = function (token) { if (token instanceof ReflectiveKey) return token; if (this._allKeys.has(token)) { return this._allKeys.get(token); } var newKey = new ReflectiveKey(token, ReflectiveKey.numberOfKeys); this._allKeys.set(token, newKey); return newKey; }; Object.defineProperty(KeyRegistry.prototype, "numberOfKeys", { get: function () { return this._allKeys.size; }, enumerable: false, configurable: true }); return KeyRegistry; }()); export { KeyRegistry }; var _globalKeyRegistry = new KeyRegistry(); //# sourceMappingURL=reflective_key.js.map