64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
/**
|
|
* @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 {Type} from '../type';
|
|
import {stringify} from '../util';
|
|
|
|
import {InjectionToken} from './injection_token';
|
|
|
|
const _THROW_IF_NOT_FOUND = new Object();
|
|
export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
|
|
|
|
class _NullInjector implements Injector {
|
|
get(token: any, notFoundValue: any = _THROW_IF_NOT_FOUND): any {
|
|
if (notFoundValue === _THROW_IF_NOT_FOUND) {
|
|
throw new Error(`No provider for ${stringify(token)}!`);
|
|
}
|
|
return notFoundValue;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @whatItDoes Injector interface
|
|
* @howToUse
|
|
* ```
|
|
* const injector: Injector = ...;
|
|
* injector.get(...);
|
|
* ```
|
|
*
|
|
* @description
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
*
|
|
* ### Example
|
|
*
|
|
* {@example core/di/ts/injector_spec.ts region='Injector'}
|
|
*
|
|
* `Injector` returns itself when given `Injector` as a token:
|
|
* {@example core/di/ts/injector_spec.ts region='injectInjector'}
|
|
*
|
|
* @stable
|
|
*/
|
|
export abstract class Injector {
|
|
static THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
|
|
static NULL: Injector = new _NullInjector();
|
|
|
|
/**
|
|
* Retrieves an instance from the injector based on the provided token.
|
|
* If not found:
|
|
* - Throws an error if no `notFoundValue` that is not equal to
|
|
* Injector.THROW_IF_NOT_FOUND is given
|
|
* - Returns the `notFoundValue` otherwise
|
|
*/
|
|
abstract get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T): T;
|
|
/**
|
|
* @deprecated from v4.0.0 use Type<T> or InjectionToken<T>
|
|
* @suppress {duplicate}
|
|
*/
|
|
abstract get(token: any, notFoundValue?: any): any;
|
|
}
|