static-injector 2.2.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/import/commonjs/index.js +94 -50
- package/import/es2022/di/index.js +1 -0
- package/import/es2022/di/initializer_token.js +1 -1
- package/import/es2022/di/inject_switch.js +1 -2
- package/import/es2022/di/injection_token.js +13 -6
- package/import/es2022/di/injector.js +3 -3
- package/import/es2022/di/injector_compatibility.js +23 -0
- package/import/es2022/di/injector_token.js +2 -3
- package/import/es2022/di/internal_tokens.js +1 -1
- package/import/es2022/di/r3_injector.js +19 -16
- package/import/es2022/di/scope.js +1 -1
- package/import/es2022/di.js +24 -0
- package/import/es2022/render3/errors_di.js +2 -2
- package/import/es2022/render3/instructions/di.js +7 -0
- package/import/es2022/render3/util/stringify_utils.js +1 -0
- package/import/es2022/util/array_utils.js +7 -0
- package/import/es2022/util/stringify.js +15 -0
- package/import/fesm2022/index.js +88 -44
- package/import/typings/di/create_injector.d.ts +2 -2
- package/import/typings/di/forward_ref.d.ts +1 -1
- package/import/typings/di/index.d.ts +1 -0
- package/import/typings/di/initializer_token.d.ts +1 -1
- package/import/typings/di/injection_token.d.ts +11 -6
- package/import/typings/di/injector.d.ts +3 -3
- package/import/typings/di/injector_compatibility.d.ts +2 -1
- package/import/typings/di/injector_token.d.ts +1 -1
- package/import/typings/di/interface/provider.d.ts +12 -12
- package/import/typings/di/internal_tokens.d.ts +1 -1
- package/import/typings/di/metadata.d.ts +2 -2
- package/import/typings/di/null_injector.d.ts +1 -1
- package/import/typings/di/r3_injector.d.ts +1 -1
- package/import/typings/di.d.ts +24 -0
- package/import/typings/errors.d.ts +11 -3
- package/import/typings/render3/errors_di.d.ts +2 -1
- package/import/typings/render3/instructions/di.d.ts +7 -0
- package/import/typings/render3/util/stringify_utils.d.ts +1 -0
- package/import/typings/util/array_utils.d.ts +7 -0
- package/import/typings/util/empty.d.ts +1 -1
- package/import/typings/util/stringify.d.ts +8 -0
- package/package.json +7 -5
- package/readme.md +2 -2
- package/transform/compiler/src/compiler.d.ts +0 -22
- package/transform/compiler/src/compiler.js +0 -26
- package/transform/compiler/src/core.d.ts +6 -0
- package/transform/compiler/src/core.js +13 -0
- package/transform/compiler/src/injectable_compiler_2.js +2 -2
- package/transform/compiler/src/output/output_ast.d.ts +21 -7
- package/transform/compiler/src/output/output_ast.js +55 -9
- package/transform/compiler/src/render3/partial/api.d.ts +5 -0
- package/transform/compiler/src/render3/r3_factory.js +1 -1
- package/transform/compiler/src/render3/r3_identifiers.js +4 -0
- package/transform/compiler/src/render3/util.js +1 -3
- package/transform/compiler/src/render3/view/util.d.ts +2 -0
- package/transform/compiler/src/render3/view/util.js +10 -2
- package/transform/compiler-cli/src/ngtsc/annotations/common/index.d.ts +0 -7
- package/transform/compiler-cli/src/ngtsc/annotations/common/index.js +0 -7
- package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.d.ts +3 -0
- package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.js +14 -2
- package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.d.ts +3 -2
- package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +3 -1
- package/transform/compiler-cli/src/ngtsc/diagnostics/error.d.ts +2 -2
- package/transform/compiler-cli/src/ngtsc/diagnostics/error_code.js +2 -2
- package/transform/compiler-cli/src/ngtsc/diagnostics/index.d.ts +0 -7
- package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +0 -7
- package/transform/compiler-cli/src/ngtsc/imports/index.d.ts +0 -7
- package/transform/compiler-cli/src/ngtsc/imports/index.js +0 -7
- package/transform/compiler-cli/src/ngtsc/reflection/index.d.ts +0 -7
- package/transform/compiler-cli/src/ngtsc/reflection/index.js +0 -7
- package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +7 -2
- package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +3 -1
- package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +2 -1
- package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +16 -5
- package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.d.ts +4 -3
- package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +25 -9
- package/transform/compiler-cli/src/ngtsc/transform/index.d.ts +0 -7
- package/transform/compiler-cli/src/ngtsc/transform/index.js +0 -7
- package/transform/compiler-cli/src/ngtsc/transform/src/api.d.ts +18 -0
- package/transform/compiler-cli/src/ngtsc/transform/src/api.js +20 -0
- package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +1 -1
- package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +35 -24
- package/transform/compiler-cli/src/ngtsc/translator/index.d.ts +0 -7
- package/transform/compiler-cli/src/ngtsc/translator/index.js +0 -7
- package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.d.ts +9 -1
- package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +21 -3
- package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +13 -3
- package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +1 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +6 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/ts_util.d.ts +12 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/ts_util.js +27 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +1 -0
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +9 -1
- package/transform/index.d.ts +1 -1
- package/transform/injectable-transform.js +2 -2
package/import/commonjs/index.js
CHANGED
|
@@ -163,6 +163,21 @@ function stringify(token) {
|
|
|
163
163
|
const newLineIndex = res.indexOf('\n');
|
|
164
164
|
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
|
165
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Ellipses the string in the middle when longer than the max length
|
|
168
|
+
*
|
|
169
|
+
* @param string
|
|
170
|
+
* @param maxLength of the output string
|
|
171
|
+
* @returns ellipsed string with ... in the middle
|
|
172
|
+
*/
|
|
173
|
+
function truncateMiddle(str, maxLength = 100) {
|
|
174
|
+
if (!str || maxLength < 1 || str.length <= maxLength)
|
|
175
|
+
return str;
|
|
176
|
+
if (maxLength == 1)
|
|
177
|
+
return str.substring(0, 1) + '...';
|
|
178
|
+
const halfLimit = Math.round(maxLength / 2);
|
|
179
|
+
return (str.substring(0, halfLimit) + '...' + str.substring(str.length - halfLimit));
|
|
180
|
+
}
|
|
166
181
|
|
|
167
182
|
/**
|
|
168
183
|
* @license
|
|
@@ -268,8 +283,8 @@ function isForwardRef(fn) {
|
|
|
268
283
|
*/
|
|
269
284
|
/** Throws an error when a token is not found in DI. */
|
|
270
285
|
function throwProviderNotFoundError(token, injectorName) {
|
|
271
|
-
const
|
|
272
|
-
throw new RuntimeError(-201 /* RuntimeErrorCode.PROVIDER_NOT_FOUND */,
|
|
286
|
+
const errorMessage = null;
|
|
287
|
+
throw new RuntimeError(-201 /* RuntimeErrorCode.PROVIDER_NOT_FOUND */, errorMessage);
|
|
273
288
|
}
|
|
274
289
|
|
|
275
290
|
/**
|
|
@@ -451,7 +466,7 @@ function injectRootLimpMode(token, notFoundValue, flags) {
|
|
|
451
466
|
return null;
|
|
452
467
|
if (notFoundValue !== undefined)
|
|
453
468
|
return notFoundValue;
|
|
454
|
-
throwProviderNotFoundError(
|
|
469
|
+
throwProviderNotFoundError(token, 'Injector');
|
|
455
470
|
}
|
|
456
471
|
|
|
457
472
|
/**
|
|
@@ -516,6 +531,27 @@ function ɵɵinject(token, flags = exports.InjectFlags.Default) {
|
|
|
516
531
|
function ɵɵinvalidFactoryDep(index) {
|
|
517
532
|
throw new RuntimeError(202 /* RuntimeErrorCode.INVALID_FACTORY_DEPENDENCY */, null);
|
|
518
533
|
}
|
|
534
|
+
/**
|
|
535
|
+
* @param token A token that represents a static attribute on the host node that should be injected.
|
|
536
|
+
* @returns Value of the attribute if it exists.
|
|
537
|
+
* @throws If called outside of a supported context or the attribute does not exist.
|
|
538
|
+
*
|
|
539
|
+
* @publicApi
|
|
540
|
+
*/
|
|
541
|
+
/**
|
|
542
|
+
* @param token A token that represents a static attribute on the host node that should be injected.
|
|
543
|
+
* @returns Value of the attribute if it exists, otherwise `null`.
|
|
544
|
+
* @throws If called outside of a supported context.
|
|
545
|
+
*
|
|
546
|
+
* @publicApi
|
|
547
|
+
*/
|
|
548
|
+
/**
|
|
549
|
+
* @param token A token that represents a static attribute on the host node that should be injected.
|
|
550
|
+
* @returns Value of the attribute if it exists.
|
|
551
|
+
* @throws If called outside of a supported context or the attribute does not exist.
|
|
552
|
+
*
|
|
553
|
+
* @publicApi
|
|
554
|
+
*/
|
|
519
555
|
/**
|
|
520
556
|
* Injects a token from the currently active injector.
|
|
521
557
|
* `inject` is only supported in an [injection context](/guide/dependency-injection-context). It can
|
|
@@ -582,6 +618,8 @@ function ɵɵinvalidFactoryDep(index) {
|
|
|
582
618
|
* @publicApi
|
|
583
619
|
*/
|
|
584
620
|
function inject(token, flags = exports.InjectFlags.Default) {
|
|
621
|
+
// The `as any` here _shouldn't_ be necessary, but without it JSCompiler
|
|
622
|
+
// throws a disambiguation error due to the multiple signatures.
|
|
585
623
|
return ɵɵinject(token, convertToBitFlags(flags));
|
|
586
624
|
}
|
|
587
625
|
// Converts object-based DI flags (`InjectOptions`) to bit flags (`InjectFlags`).
|
|
@@ -776,17 +814,6 @@ function getFactoryDef(type, throwNotFound) {
|
|
|
776
814
|
return hasFactoryDef ? type[NG_FACTORY_DEF] : null;
|
|
777
815
|
}
|
|
778
816
|
|
|
779
|
-
function deepForEach(input, fn) {
|
|
780
|
-
input.forEach((value) => Array.isArray(value) ? deepForEach(value, fn) : fn(value));
|
|
781
|
-
}
|
|
782
|
-
function newArray(size, value) {
|
|
783
|
-
const list = [];
|
|
784
|
-
for (let i = 0; i < size; i++) {
|
|
785
|
-
list.push(value);
|
|
786
|
-
}
|
|
787
|
-
return list;
|
|
788
|
-
}
|
|
789
|
-
|
|
790
817
|
/**
|
|
791
818
|
* @license
|
|
792
819
|
* Copyright Google LLC All Rights Reserved.
|
|
@@ -823,11 +850,17 @@ if (false) {
|
|
|
823
850
|
* `InjectionToken` is parameterized on `T` which is the type of object which will be returned by
|
|
824
851
|
* the `Injector`. This provides an additional level of type safety.
|
|
825
852
|
*
|
|
826
|
-
*
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
*
|
|
830
|
-
*
|
|
853
|
+
* <div class="alert is-helpful">
|
|
854
|
+
*
|
|
855
|
+
* **Important Note**: Ensure that you use the same instance of the `InjectionToken` in both the
|
|
856
|
+
* provider and the injection call. Creating a new instance of `InjectionToken` in different places,
|
|
857
|
+
* even with the same description, will be treated as different tokens by Angular's DI system,
|
|
858
|
+
* leading to a `NullInjectorError`.
|
|
859
|
+
*
|
|
860
|
+
* </div>
|
|
861
|
+
*
|
|
862
|
+
* <code-example format="typescript" language="typescript" path="injection-token/src/main.ts"
|
|
863
|
+
* region="InjectionToken"></code-example>
|
|
831
864
|
*
|
|
832
865
|
* When creating an `InjectionToken`, you can optionally specify a factory function which returns
|
|
833
866
|
* (possibly by creating) a default value of the parameterized type `T`. This sets up the
|
|
@@ -854,7 +887,6 @@ if (false) {
|
|
|
854
887
|
*
|
|
855
888
|
* {@example core/di/ts/injector_spec.ts region='ShakableInjectionToken'}
|
|
856
889
|
*
|
|
857
|
-
*
|
|
858
890
|
* @publicApi
|
|
859
891
|
*/
|
|
860
892
|
class InjectionToken {
|
|
@@ -872,6 +904,8 @@ class InjectionToken {
|
|
|
872
904
|
this._desc = _desc;
|
|
873
905
|
this.ɵprov = undefined;
|
|
874
906
|
if (typeof options == 'number') {
|
|
907
|
+
// This is a special hack to assign __NG_ELEMENT_ID__ to this instance.
|
|
908
|
+
// See `InjectorMarkers`
|
|
875
909
|
}
|
|
876
910
|
else if (options !== undefined) {
|
|
877
911
|
this.ɵprov = ɵɵdefineInjectable({
|
|
@@ -905,7 +939,7 @@ class InjectionToken {
|
|
|
905
939
|
*
|
|
906
940
|
* @publicApi
|
|
907
941
|
*/
|
|
908
|
-
const ENVIRONMENT_INITIALIZER = new InjectionToken('
|
|
942
|
+
const ENVIRONMENT_INITIALIZER = new InjectionToken('');
|
|
909
943
|
|
|
910
944
|
/**
|
|
911
945
|
* @license
|
|
@@ -922,11 +956,10 @@ const ENVIRONMENT_INITIALIZER = new InjectionToken('ENVIRONMENT_INITIALIZER');
|
|
|
922
956
|
*
|
|
923
957
|
* @publicApi
|
|
924
958
|
*/
|
|
925
|
-
const INJECTOR = new InjectionToken('
|
|
959
|
+
const INJECTOR = new InjectionToken('',
|
|
926
960
|
// Disable tslint because this is const enum which gets inlined not top level prop access.
|
|
927
961
|
// tslint:disable-next-line: no-toplevel-property-access
|
|
928
|
-
-1 /* InjectorMarkers.Injector */
|
|
929
|
-
);
|
|
962
|
+
-1 /* InjectorMarkers.Injector */);
|
|
930
963
|
|
|
931
964
|
/**
|
|
932
965
|
* @license
|
|
@@ -946,7 +979,7 @@ function isEnvironmentProviders(value) {
|
|
|
946
979
|
* Use of this source code is governed by an MIT-style license that can be
|
|
947
980
|
* found in the LICENSE file at https://angular.io/license
|
|
948
981
|
*/
|
|
949
|
-
const INJECTOR_DEF_TYPES = new InjectionToken('
|
|
982
|
+
const INJECTOR_DEF_TYPES = new InjectionToken('');
|
|
950
983
|
|
|
951
984
|
/**
|
|
952
985
|
* @license
|
|
@@ -1082,7 +1115,7 @@ function isClassProvider(value) {
|
|
|
1082
1115
|
* as a root scoped injector when processing requests for unknown tokens which may indicate
|
|
1083
1116
|
* they are provided in the root scope.
|
|
1084
1117
|
*/
|
|
1085
|
-
const INJECTOR_SCOPE = new InjectionToken('
|
|
1118
|
+
const INJECTOR_SCOPE = new InjectionToken('');
|
|
1086
1119
|
|
|
1087
1120
|
/**
|
|
1088
1121
|
* @license
|
|
@@ -1161,7 +1194,7 @@ class R3Injector extends EnvironmentInjector {
|
|
|
1161
1194
|
if (record != null && typeof record.value === 'string') {
|
|
1162
1195
|
this.scopes.add(record.value);
|
|
1163
1196
|
}
|
|
1164
|
-
this.injectorDefTypes = new Set(this.get(INJECTOR_DEF_TYPES
|
|
1197
|
+
this.injectorDefTypes = new Set(this.get(INJECTOR_DEF_TYPES, EMPTY_ARRAY, exports.InjectFlags.Self));
|
|
1165
1198
|
}
|
|
1166
1199
|
/**
|
|
1167
1200
|
* Destroy the injector and release references to every instance or provider associated with it.
|
|
@@ -1235,6 +1268,8 @@ class R3Injector extends EnvironmentInjector {
|
|
|
1235
1268
|
if (def && this.injectableDefInScope(def)) {
|
|
1236
1269
|
// Found an injectable def and it's scoped to this injector. Pretend as if it was here
|
|
1237
1270
|
// all along.
|
|
1271
|
+
if (false) {
|
|
1272
|
+
}
|
|
1238
1273
|
record = makeRecord(injectableDefOrInjectorDefFactory(token), NOT_YET);
|
|
1239
1274
|
}
|
|
1240
1275
|
else {
|
|
@@ -1291,7 +1326,7 @@ class R3Injector extends EnvironmentInjector {
|
|
|
1291
1326
|
if (false) {
|
|
1292
1327
|
}
|
|
1293
1328
|
try {
|
|
1294
|
-
const initializers = this.get(ENVIRONMENT_INITIALIZER
|
|
1329
|
+
const initializers = this.get(ENVIRONMENT_INITIALIZER, EMPTY_ARRAY, exports.InjectFlags.Self);
|
|
1295
1330
|
if (false) {
|
|
1296
1331
|
}
|
|
1297
1332
|
for (const initializer of initializers) {
|
|
@@ -1348,29 +1383,32 @@ class R3Injector extends EnvironmentInjector {
|
|
|
1348
1383
|
multiRecord.multi.push(provider);
|
|
1349
1384
|
}
|
|
1350
1385
|
else {
|
|
1351
|
-
const existing = this.records.get(token);
|
|
1352
1386
|
if (false) {
|
|
1353
1387
|
}
|
|
1354
1388
|
}
|
|
1355
1389
|
this.records.set(token, record);
|
|
1356
1390
|
}
|
|
1357
1391
|
hydrate(token, record) {
|
|
1358
|
-
|
|
1359
|
-
}
|
|
1360
|
-
else if (record.value === NOT_YET) {
|
|
1361
|
-
record.value = CIRCULAR;
|
|
1392
|
+
try {
|
|
1362
1393
|
if (false) {
|
|
1363
1394
|
}
|
|
1364
|
-
else {
|
|
1365
|
-
record.value =
|
|
1395
|
+
else if (record.value === NOT_YET) {
|
|
1396
|
+
record.value = CIRCULAR;
|
|
1397
|
+
if (false) {
|
|
1398
|
+
}
|
|
1399
|
+
else {
|
|
1400
|
+
record.value = record.factory();
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
if (typeof record.value === 'object' &&
|
|
1404
|
+
record.value &&
|
|
1405
|
+
hasOnDestroy(record.value)) {
|
|
1406
|
+
this._ngOnDestroyHooks.add(record.value);
|
|
1366
1407
|
}
|
|
1408
|
+
return record.value;
|
|
1367
1409
|
}
|
|
1368
|
-
|
|
1369
|
-
record.value &&
|
|
1370
|
-
hasOnDestroy(record.value)) {
|
|
1371
|
-
this._ngOnDestroyHooks.add(record.value);
|
|
1410
|
+
finally {
|
|
1372
1411
|
}
|
|
1373
|
-
return record.value;
|
|
1374
1412
|
}
|
|
1375
1413
|
injectableDefInScope(def) {
|
|
1376
1414
|
if (!def.providedIn) {
|
|
@@ -1414,7 +1452,6 @@ function getUndecoratedInjectableFactory(token) {
|
|
|
1414
1452
|
// If the token has parameters then it has dependencies that we cannot resolve implicitly.
|
|
1415
1453
|
const paramLength = token.length;
|
|
1416
1454
|
if (paramLength > 0) {
|
|
1417
|
-
const args = newArray(paramLength, '?');
|
|
1418
1455
|
throw new RuntimeError(204 /* RuntimeErrorCode.INVALID_INJECTION_TOKEN */, null);
|
|
1419
1456
|
}
|
|
1420
1457
|
// The constructor function appears to have no parameters.
|
|
@@ -1550,10 +1587,10 @@ function createInjectorWithoutInjectorInstances(defType, parent = null, addition
|
|
|
1550
1587
|
*/
|
|
1551
1588
|
/**
|
|
1552
1589
|
* Concrete injectors implement this interface. Injectors are configured
|
|
1553
|
-
* with [providers](guide/
|
|
1554
|
-
* dependencies of various types with [injection tokens](guide/
|
|
1590
|
+
* with [providers](guide/dependency-injection-providers) that associate
|
|
1591
|
+
* dependencies of various types with [injection tokens](guide/dependency-injection-providers).
|
|
1555
1592
|
*
|
|
1556
|
-
* @see [
|
|
1593
|
+
* @see [DI Providers](guide/dependency-injection-providers).
|
|
1557
1594
|
* @see {@link StaticProvider}
|
|
1558
1595
|
*
|
|
1559
1596
|
* @usageNotes
|
|
@@ -1597,6 +1634,13 @@ class Injector {
|
|
|
1597
1634
|
static __NG_ELEMENT_ID__ = -1 /* InjectorMarkers.Injector */;
|
|
1598
1635
|
}
|
|
1599
1636
|
|
|
1637
|
+
/**
|
|
1638
|
+
* @license
|
|
1639
|
+
* Copyright Google LLC All Rights Reserved.
|
|
1640
|
+
*
|
|
1641
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
1642
|
+
* found in the LICENSE file at https://angular.io/license
|
|
1643
|
+
*/
|
|
1600
1644
|
/**
|
|
1601
1645
|
* Throws an error indicating that a factory function could not be generated by the compiler for a
|
|
1602
1646
|
* particular class.
|
|
@@ -1692,9 +1736,9 @@ exports.injectInjectorOnly = injectInjectorOnly;
|
|
|
1692
1736
|
exports.isInjectable = isInjectable;
|
|
1693
1737
|
exports.providerToFactory = providerToFactory;
|
|
1694
1738
|
exports.setCurrentInjector = setCurrentInjector;
|
|
1695
|
-
exports
|
|
1696
|
-
exports
|
|
1697
|
-
exports
|
|
1698
|
-
exports
|
|
1699
|
-
exports
|
|
1700
|
-
exports
|
|
1739
|
+
exports.ɵɵdefineInjectable = ɵɵdefineInjectable;
|
|
1740
|
+
exports.ɵɵdefineInjector = ɵɵdefineInjector;
|
|
1741
|
+
exports.ɵɵgetInheritedFactory = ɵɵgetInheritedFactory;
|
|
1742
|
+
exports.ɵɵinject = ɵɵinject;
|
|
1743
|
+
exports.ɵɵinvalidFactory = ɵɵinvalidFactory;
|
|
1744
|
+
exports.ɵɵinvalidFactoryDep = ɵɵinvalidFactoryDep;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* found in the LICENSE file at https://angular.io/license
|
|
7
7
|
*/
|
|
8
8
|
import { throwProviderNotFoundError } from '../render3/errors_di';
|
|
9
|
-
import { stringify } from '../util/stringify';
|
|
10
9
|
import { getInjectableDef } from './interface/defs';
|
|
11
10
|
import { InjectFlags } from './interface/injector';
|
|
12
11
|
/**
|
|
@@ -48,5 +47,5 @@ export function injectRootLimpMode(token, notFoundValue, flags) {
|
|
|
48
47
|
return null;
|
|
49
48
|
if (notFoundValue !== undefined)
|
|
50
49
|
return notFoundValue;
|
|
51
|
-
throwProviderNotFoundError(
|
|
50
|
+
throwProviderNotFoundError(token, 'Injector');
|
|
52
51
|
}
|
|
@@ -16,11 +16,17 @@ import { ɵɵdefineInjectable } from './interface/defs';
|
|
|
16
16
|
* `InjectionToken` is parameterized on `T` which is the type of object which will be returned by
|
|
17
17
|
* the `Injector`. This provides an additional level of type safety.
|
|
18
18
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
19
|
+
* <div class="alert is-helpful">
|
|
20
|
+
*
|
|
21
|
+
* **Important Note**: Ensure that you use the same instance of the `InjectionToken` in both the
|
|
22
|
+
* provider and the injection call. Creating a new instance of `InjectionToken` in different places,
|
|
23
|
+
* even with the same description, will be treated as different tokens by Angular's DI system,
|
|
24
|
+
* leading to a `NullInjectorError`.
|
|
25
|
+
*
|
|
26
|
+
* </div>
|
|
27
|
+
*
|
|
28
|
+
* <code-example format="typescript" language="typescript" path="injection-token/src/main.ts"
|
|
29
|
+
* region="InjectionToken"></code-example>
|
|
24
30
|
*
|
|
25
31
|
* When creating an `InjectionToken`, you can optionally specify a factory function which returns
|
|
26
32
|
* (possibly by creating) a default value of the parameterized type `T`. This sets up the
|
|
@@ -47,7 +53,6 @@ import { ɵɵdefineInjectable } from './interface/defs';
|
|
|
47
53
|
*
|
|
48
54
|
* {@example core/di/ts/injector_spec.ts region='ShakableInjectionToken'}
|
|
49
55
|
*
|
|
50
|
-
*
|
|
51
56
|
* @publicApi
|
|
52
57
|
*/
|
|
53
58
|
export class InjectionToken {
|
|
@@ -65,6 +70,8 @@ export class InjectionToken {
|
|
|
65
70
|
this._desc = _desc;
|
|
66
71
|
this.ɵprov = undefined;
|
|
67
72
|
if (typeof options == 'number') {
|
|
73
|
+
// This is a special hack to assign __NG_ELEMENT_ID__ to this instance.
|
|
74
|
+
// See `InjectorMarkers`
|
|
68
75
|
}
|
|
69
76
|
else if (options !== undefined) {
|
|
70
77
|
this.ɵprov = ɵɵdefineInjectable({
|
|
@@ -12,10 +12,10 @@ import { ɵɵdefineInjectable } from './interface/defs';
|
|
|
12
12
|
import { NullInjector } from './null_injector';
|
|
13
13
|
/**
|
|
14
14
|
* Concrete injectors implement this interface. Injectors are configured
|
|
15
|
-
* with [providers](guide/
|
|
16
|
-
* dependencies of various types with [injection tokens](guide/
|
|
15
|
+
* with [providers](guide/dependency-injection-providers) that associate
|
|
16
|
+
* dependencies of various types with [injection tokens](guide/dependency-injection-providers).
|
|
17
17
|
*
|
|
18
|
-
* @see [
|
|
18
|
+
* @see [DI Providers](guide/dependency-injection-providers).
|
|
19
19
|
* @see {@link StaticProvider}
|
|
20
20
|
*
|
|
21
21
|
* @usageNotes
|
|
@@ -65,6 +65,27 @@ export function ɵɵinject(token, flags = InjectFlags.Default) {
|
|
|
65
65
|
export function ɵɵinvalidFactoryDep(index) {
|
|
66
66
|
throw new RuntimeError(202 /* RuntimeErrorCode.INVALID_FACTORY_DEPENDENCY */, null);
|
|
67
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* @param token A token that represents a static attribute on the host node that should be injected.
|
|
70
|
+
* @returns Value of the attribute if it exists.
|
|
71
|
+
* @throws If called outside of a supported context or the attribute does not exist.
|
|
72
|
+
*
|
|
73
|
+
* @publicApi
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
76
|
+
* @param token A token that represents a static attribute on the host node that should be injected.
|
|
77
|
+
* @returns Value of the attribute if it exists, otherwise `null`.
|
|
78
|
+
* @throws If called outside of a supported context.
|
|
79
|
+
*
|
|
80
|
+
* @publicApi
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* @param token A token that represents a static attribute on the host node that should be injected.
|
|
84
|
+
* @returns Value of the attribute if it exists.
|
|
85
|
+
* @throws If called outside of a supported context or the attribute does not exist.
|
|
86
|
+
*
|
|
87
|
+
* @publicApi
|
|
88
|
+
*/
|
|
68
89
|
/**
|
|
69
90
|
* Injects a token from the currently active injector.
|
|
70
91
|
* `inject` is only supported in an [injection context](/guide/dependency-injection-context). It can
|
|
@@ -131,6 +152,8 @@ export function ɵɵinvalidFactoryDep(index) {
|
|
|
131
152
|
* @publicApi
|
|
132
153
|
*/
|
|
133
154
|
export function inject(token, flags = InjectFlags.Default) {
|
|
155
|
+
// The `as any` here _shouldn't_ be necessary, but without it JSCompiler
|
|
156
|
+
// throws a disambiguation error due to the multiple signatures.
|
|
134
157
|
return ɵɵinject(token, convertToBitFlags(flags));
|
|
135
158
|
}
|
|
136
159
|
// Converts object-based DI flags (`InjectOptions`) to bit flags (`InjectFlags`).
|
|
@@ -14,8 +14,7 @@ import { InjectionToken } from './injection_token';
|
|
|
14
14
|
*
|
|
15
15
|
* @publicApi
|
|
16
16
|
*/
|
|
17
|
-
export const INJECTOR = new InjectionToken('
|
|
17
|
+
export const INJECTOR = new InjectionToken('',
|
|
18
18
|
// Disable tslint because this is const enum which gets inlined not top level prop access.
|
|
19
19
|
// tslint:disable-next-line: no-toplevel-property-access
|
|
20
|
-
-1 /* InjectorMarkers.Injector */
|
|
21
|
-
);
|
|
20
|
+
-1 /* InjectorMarkers.Injector */);
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
import { RuntimeError } from '../errors';
|
|
9
9
|
import { getFactoryDef } from '../render3/definition_factory';
|
|
10
10
|
import { NG_ENV_ID } from '../render3/fields';
|
|
11
|
-
import { newArray } from '../util/array_utils';
|
|
12
11
|
import { EMPTY_ARRAY } from '../util/empty';
|
|
13
12
|
import { stringify } from '../util/stringify';
|
|
14
13
|
import { resolveForwardRef } from './forward_ref';
|
|
@@ -94,7 +93,7 @@ export class R3Injector extends EnvironmentInjector {
|
|
|
94
93
|
if (record != null && typeof record.value === 'string') {
|
|
95
94
|
this.scopes.add(record.value);
|
|
96
95
|
}
|
|
97
|
-
this.injectorDefTypes = new Set(this.get(INJECTOR_DEF_TYPES
|
|
96
|
+
this.injectorDefTypes = new Set(this.get(INJECTOR_DEF_TYPES, EMPTY_ARRAY, InjectFlags.Self));
|
|
98
97
|
}
|
|
99
98
|
/**
|
|
100
99
|
* Destroy the injector and release references to every instance or provider associated with it.
|
|
@@ -168,6 +167,8 @@ export class R3Injector extends EnvironmentInjector {
|
|
|
168
167
|
if (def && this.injectableDefInScope(def)) {
|
|
169
168
|
// Found an injectable def and it's scoped to this injector. Pretend as if it was here
|
|
170
169
|
// all along.
|
|
170
|
+
if (false) {
|
|
171
|
+
}
|
|
171
172
|
record = makeRecord(injectableDefOrInjectorDefFactory(token), NOT_YET);
|
|
172
173
|
}
|
|
173
174
|
else {
|
|
@@ -224,7 +225,7 @@ export class R3Injector extends EnvironmentInjector {
|
|
|
224
225
|
if (false) {
|
|
225
226
|
}
|
|
226
227
|
try {
|
|
227
|
-
const initializers = this.get(ENVIRONMENT_INITIALIZER
|
|
228
|
+
const initializers = this.get(ENVIRONMENT_INITIALIZER, EMPTY_ARRAY, InjectFlags.Self);
|
|
228
229
|
if (false) {
|
|
229
230
|
}
|
|
230
231
|
for (const initializer of initializers) {
|
|
@@ -281,29 +282,32 @@ export class R3Injector extends EnvironmentInjector {
|
|
|
281
282
|
multiRecord.multi.push(provider);
|
|
282
283
|
}
|
|
283
284
|
else {
|
|
284
|
-
const existing = this.records.get(token);
|
|
285
285
|
if (false) {
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
288
|
this.records.set(token, record);
|
|
289
289
|
}
|
|
290
290
|
hydrate(token, record) {
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
else if (record.value === NOT_YET) {
|
|
294
|
-
record.value = CIRCULAR;
|
|
291
|
+
try {
|
|
295
292
|
if (false) {
|
|
296
293
|
}
|
|
297
|
-
else {
|
|
298
|
-
record.value =
|
|
294
|
+
else if (record.value === NOT_YET) {
|
|
295
|
+
record.value = CIRCULAR;
|
|
296
|
+
if (false) {
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
record.value = record.factory();
|
|
300
|
+
}
|
|
299
301
|
}
|
|
302
|
+
if (typeof record.value === 'object' &&
|
|
303
|
+
record.value &&
|
|
304
|
+
hasOnDestroy(record.value)) {
|
|
305
|
+
this._ngOnDestroyHooks.add(record.value);
|
|
306
|
+
}
|
|
307
|
+
return record.value;
|
|
300
308
|
}
|
|
301
|
-
|
|
302
|
-
record.value &&
|
|
303
|
-
hasOnDestroy(record.value)) {
|
|
304
|
-
this._ngOnDestroyHooks.add(record.value);
|
|
309
|
+
finally {
|
|
305
310
|
}
|
|
306
|
-
return record.value;
|
|
307
311
|
}
|
|
308
312
|
injectableDefInScope(def) {
|
|
309
313
|
if (!def.providedIn) {
|
|
@@ -347,7 +351,6 @@ function getUndecoratedInjectableFactory(token) {
|
|
|
347
351
|
// If the token has parameters then it has dependencies that we cannot resolve implicitly.
|
|
348
352
|
const paramLength = token.length;
|
|
349
353
|
if (paramLength > 0) {
|
|
350
|
-
const args = newArray(paramLength, '?');
|
|
351
354
|
throw new RuntimeError(204 /* RuntimeErrorCode.INVALID_INJECTION_TOKEN */, null);
|
|
352
355
|
}
|
|
353
356
|
// The constructor function appears to have no parameters.
|
|
@@ -11,4 +11,4 @@ import { InjectionToken } from './injection_token';
|
|
|
11
11
|
* as a root scoped injector when processing requests for unknown tokens which may indicate
|
|
12
12
|
* they are provided in the root scope.
|
|
13
13
|
*/
|
|
14
|
-
export const INJECTOR_SCOPE = new InjectionToken('
|
|
14
|
+
export const INJECTOR_SCOPE = new InjectionToken('');
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright Google LLC 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
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* This file should not be necessary because node resolution should just default to `./di/index`!
|
|
10
|
+
*
|
|
11
|
+
* However it does not seem to work and it breaks:
|
|
12
|
+
* - //packages/animations/browser/test:test_web_chromium-local
|
|
13
|
+
* - //packages/compiler-cli/test:extract_i18n
|
|
14
|
+
* - //packages/compiler-cli/test:ngc
|
|
15
|
+
* - //packages/compiler-cli/test:perform_watch
|
|
16
|
+
* - //packages/compiler-cli/test/diagnostics:check_types
|
|
17
|
+
* - //packages/compiler-cli/test/transformers:test
|
|
18
|
+
* - //packages/compiler/test:test
|
|
19
|
+
* - //tools/public_api_guard:core_api
|
|
20
|
+
*
|
|
21
|
+
* Remove this file once the above is solved or wait until `ngc` is deleted and then it should be
|
|
22
|
+
* safe to delete this file.
|
|
23
|
+
*/
|
|
24
|
+
export * from './di/index';
|
|
@@ -8,6 +8,6 @@
|
|
|
8
8
|
import { RuntimeError } from '../errors';
|
|
9
9
|
/** Throws an error when a token is not found in DI. */
|
|
10
10
|
export function throwProviderNotFoundError(token, injectorName) {
|
|
11
|
-
const
|
|
12
|
-
throw new RuntimeError(-201 /* RuntimeErrorCode.PROVIDER_NOT_FOUND */,
|
|
11
|
+
const errorMessage = null;
|
|
12
|
+
throw new RuntimeError(-201 /* RuntimeErrorCode.PROVIDER_NOT_FOUND */, errorMessage);
|
|
13
13
|
}
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright Google LLC 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
|
+
*/
|
|
1
8
|
/**
|
|
2
9
|
* Throws an error indicating that a factory function could not be generated by the compiler for a
|
|
3
10
|
* particular class.
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright Google LLC 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
|
+
*/
|
|
1
8
|
export function deepForEach(input, fn) {
|
|
2
9
|
input.forEach((value) => Array.isArray(value) ? deepForEach(value, fn) : fn(value));
|
|
3
10
|
}
|
|
@@ -28,3 +28,18 @@ export function stringify(token) {
|
|
|
28
28
|
const newLineIndex = res.indexOf('\n');
|
|
29
29
|
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
|
30
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Ellipses the string in the middle when longer than the max length
|
|
33
|
+
*
|
|
34
|
+
* @param string
|
|
35
|
+
* @param maxLength of the output string
|
|
36
|
+
* @returns ellipsed string with ... in the middle
|
|
37
|
+
*/
|
|
38
|
+
export function truncateMiddle(str, maxLength = 100) {
|
|
39
|
+
if (!str || maxLength < 1 || str.length <= maxLength)
|
|
40
|
+
return str;
|
|
41
|
+
if (maxLength == 1)
|
|
42
|
+
return str.substring(0, 1) + '...';
|
|
43
|
+
const halfLimit = Math.round(maxLength / 2);
|
|
44
|
+
return (str.substring(0, halfLimit) + '...' + str.substring(str.length - halfLimit));
|
|
45
|
+
}
|