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