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 | import { throwProviderNotFoundError } from '../render3/errors_di';
|
---|
9 | import { assertNotEqual } from '../util/assert';
|
---|
10 | import { stringify } from '../util/stringify';
|
---|
11 | import { getInjectableDef } from './interface/defs';
|
---|
12 | import { InjectFlags } from './interface/injector';
|
---|
13 | /**
|
---|
14 | * Current implementation of inject.
|
---|
15 | *
|
---|
16 | * By default, it is `injectInjectorOnly`, which makes it `Injector`-only aware. It can be changed
|
---|
17 | * to `directiveInject`, which brings in the `NodeInjector` system of ivy. It is designed this
|
---|
18 | * way for two reasons:
|
---|
19 | * 1. `Injector` should not depend on ivy logic.
|
---|
20 | * 2. To maintain tree shake-ability we don't want to bring in unnecessary code.
|
---|
21 | */
|
---|
22 | let _injectImplementation;
|
---|
23 | export function getInjectImplementation() {
|
---|
24 | return _injectImplementation;
|
---|
25 | }
|
---|
26 | /**
|
---|
27 | * Sets the current inject implementation.
|
---|
28 | */
|
---|
29 | export function setInjectImplementation(impl) {
|
---|
30 | const previous = _injectImplementation;
|
---|
31 | _injectImplementation = impl;
|
---|
32 | return previous;
|
---|
33 | }
|
---|
34 | /**
|
---|
35 | * Injects `root` tokens in limp mode.
|
---|
36 | *
|
---|
37 | * If no injector exists, we can still inject tree-shakable providers which have `providedIn` set to
|
---|
38 | * `"root"`. This is known as the limp mode injection. In such case the value is stored in the
|
---|
39 | * injectable definition.
|
---|
40 | */
|
---|
41 | export function injectRootLimpMode(token, notFoundValue, flags) {
|
---|
42 | const injectableDef = getInjectableDef(token);
|
---|
43 | if (injectableDef && injectableDef.providedIn == 'root') {
|
---|
44 | return injectableDef.value === undefined ? injectableDef.value = injectableDef.factory() :
|
---|
45 | injectableDef.value;
|
---|
46 | }
|
---|
47 | if (flags & InjectFlags.Optional)
|
---|
48 | return null;
|
---|
49 | if (notFoundValue !== undefined)
|
---|
50 | return notFoundValue;
|
---|
51 | throwProviderNotFoundError(stringify(token), 'Injector');
|
---|
52 | }
|
---|
53 | /**
|
---|
54 | * Assert that `_injectImplementation` is not `fn`.
|
---|
55 | *
|
---|
56 | * This is useful, to prevent infinite recursion.
|
---|
57 | *
|
---|
58 | * @param fn Function which it should not equal to
|
---|
59 | */
|
---|
60 | export function assertInjectImplementationNotEqual(fn) {
|
---|
61 | ngDevMode &&
|
---|
62 | assertNotEqual(_injectImplementation, fn, 'Calling ɵɵinject would cause infinite recursion');
|
---|
63 | }
|
---|
64 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5qZWN0X3N3aXRjaC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL2NvcmUvc3JjL2RpL2luamVjdF9zd2l0Y2gudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBRUgsT0FBTyxFQUFDLDBCQUEwQixFQUFDLE1BQU0sc0JBQXNCLENBQUM7QUFDaEUsT0FBTyxFQUFDLGNBQWMsRUFBQyxNQUFNLGdCQUFnQixDQUFDO0FBQzlDLE9BQU8sRUFBQyxTQUFTLEVBQUMsTUFBTSxtQkFBbUIsQ0FBQztBQUU1QyxPQUFPLEVBQUMsZ0JBQWdCLEVBQTBCLE1BQU0sa0JBQWtCLENBQUM7QUFDM0UsT0FBTyxFQUFDLFdBQVcsRUFBQyxNQUFNLHNCQUFzQixDQUFDO0FBSWpEOzs7Ozs7OztHQVFHO0FBQ0gsSUFBSSxxQkFDUyxDQUFDO0FBQ2QsTUFBTSxVQUFVLHVCQUF1QjtJQUNyQyxPQUFPLHFCQUFxQixDQUFDO0FBQy9CLENBQUM7QUFHRDs7R0FFRztBQUNILE1BQU0sVUFBVSx1QkFBdUIsQ0FD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