UNPKG

2.8 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { stringify } from './facade/lang';
9import { resolveForwardRef } from './forward_ref';
10/**
11 * A unique object used for retrieving items from the {@link ReflectiveInjector}.
12 *
13 * Keys have:
14 * - a system-wide unique `id`.
15 * - a `token`.
16 *
17 * `Key` is used internally by {@link ReflectiveInjector} because its system-wide unique `id` allows
18 * the
19 * injector to store created objects in a more efficient way.
20 *
21 * `Key` should not be created directly. {@link ReflectiveInjector} creates keys automatically when
22 * resolving
23 * providers.
24 * @experimental
25 */
26var ReflectiveKey = /** @class */ (function () {
27 /**
28 * Private
29 */
30 function ReflectiveKey(token, id) {
31 this.token = token;
32 this.id = id;
33 if (!token) {
34 throw new Error('Token must be defined!');
35 }
36 }
37 Object.defineProperty(ReflectiveKey.prototype, "displayName", {
38 /**
39 * Returns a stringified token.
40 */
41 get: function () {
42 return stringify(this.token);
43 },
44 enumerable: false,
45 configurable: true
46 });
47 /**
48 * Retrieves a `Key` for a token.
49 */
50 ReflectiveKey.get = function (token) {
51 // tslint:disable-next-line:no-use-before-declare
52 return _globalKeyRegistry.get(resolveForwardRef(token));
53 };
54 Object.defineProperty(ReflectiveKey, "numberOfKeys", {
55 /**
56 * @returns the number of keys registered in the system.
57 */
58 get: function () {
59 // tslint:disable-next-line:no-use-before-declare
60 return _globalKeyRegistry.numberOfKeys;
61 },
62 enumerable: false,
63 configurable: true
64 });
65 return ReflectiveKey;
66}());
67export { ReflectiveKey };
68/**
69 * @internal
70 */
71var KeyRegistry = /** @class */ (function () {
72 function KeyRegistry() {
73 this._allKeys = new Map();
74 }
75 KeyRegistry.prototype.get = function (token) {
76 if (token instanceof ReflectiveKey)
77 return token;
78 if (this._allKeys.has(token)) {
79 return this._allKeys.get(token);
80 }
81 var newKey = new ReflectiveKey(token, ReflectiveKey.numberOfKeys);
82 this._allKeys.set(token, newKey);
83 return newKey;
84 };
85 Object.defineProperty(KeyRegistry.prototype, "numberOfKeys", {
86 get: function () {
87 return this._allKeys.size;
88 },
89 enumerable: false,
90 configurable: true
91 });
92 return KeyRegistry;
93}());
94export { KeyRegistry };
95var _globalKeyRegistry = new KeyRegistry();
96//# sourceMappingURL=reflective_key.js.map
\No newline at end of file