2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
|
2017-01-03 16:54:46 -08:00
|
|
|
import {Type} from '../type';
|
2017-03-02 09:37:01 -08:00
|
|
|
import {stringify} from '../util';
|
2017-01-03 16:54:46 -08:00
|
|
|
|
|
|
|
|
import {InjectionToken} from './injection_token';
|
2015-09-02 10:21:28 -07:00
|
|
|
|
2016-07-30 19:18:14 -07:00
|
|
|
const _THROW_IF_NOT_FOUND = new Object();
|
|
|
|
|
export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
|
2015-07-06 10:38:12 -07:00
|
|
|
|
2016-06-28 09:54:42 -07:00
|
|
|
class _NullInjector implements Injector {
|
|
|
|
|
get(token: any, notFoundValue: any = _THROW_IF_NOT_FOUND): any {
|
|
|
|
|
if (notFoundValue === _THROW_IF_NOT_FOUND) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(`No provider for ${stringify(token)}!`);
|
2016-06-28 09:54:42 -07:00
|
|
|
}
|
|
|
|
|
return notFoundValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-25 15:00:05 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @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'}
|
|
|
|
|
*
|
2016-05-25 15:00:05 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
2016-01-06 14:13:44 -08:00
|
|
|
export abstract class Injector {
|
2016-04-14 12:35:24 -07:00
|
|
|
static THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
|
2016-06-28 09:54:42 -07:00
|
|
|
static NULL: Injector = new _NullInjector();
|
2015-10-10 22:11:13 -07:00
|
|
|
|
2015-06-29 11:15:49 -07:00
|
|
|
/**
|
2015-09-21 14:19:03 -07:00
|
|
|
* Retrieves an instance from the injector based on the provided token.
|
2016-04-14 12:35:24 -07:00
|
|
|
* If not found:
|
2017-04-25 02:15:33 +02:00
|
|
|
* - Throws an error if no `notFoundValue` that is not equal to
|
2016-04-14 12:35:24 -07:00
|
|
|
* Injector.THROW_IF_NOT_FOUND is given
|
|
|
|
|
* - Returns the `notFoundValue` otherwise
|
2015-06-29 11:15:49 -07:00
|
|
|
*/
|
2017-01-25 22:32:32 -08:00
|
|
|
abstract get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T): T;
|
2017-01-03 16:54:46 -08:00
|
|
|
/**
|
2017-03-07 05:08:02 +02:00
|
|
|
* @deprecated from v4.0.0 use Type<T> or InjectionToken<T>
|
2017-01-30 16:25:15 -08:00
|
|
|
* @suppress {duplicate}
|
2017-01-03 16:54:46 -08:00
|
|
|
*/
|
2017-01-25 22:32:32 -08:00
|
|
|
abstract get(token: any, notFoundValue?: any): any;
|
2015-07-22 12:00:35 -07:00
|
|
|
}
|