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
|
|
|
|
*/
|
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
import {Type} from '../type';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Configures the {@link Injector} to return an instance of `Type` when `Type' is used
|
|
|
|
* as token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* @Injectable()
|
|
|
|
* class MyService {}
|
|
|
|
*
|
|
|
|
* const provider: TypeProvider = MyService;
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* Create an instance by invoking the `new` operator and supplying additional arguments.
|
|
|
|
* This form is a short form of `TypeProvider`;
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* ### Example
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='TypeProvider'}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* @stable
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
export interface TypeProvider extends Type<any> {}
|
2015-04-15 22:35:38 +00:00
|
|
|
|
2016-08-15 19:37:42 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Configures the {@link Injector} to return a value for a token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* const provider: ValueProvider = {provide: 'someToken', useValue: 'someValue'};
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='ValueProvider'}
|
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface ValueProvider {
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2017-01-03 16:54:46 -08:00
|
|
|
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
provide: any;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* The value to inject.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-06-28 11:35:59 -07:00
|
|
|
useValue: any;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* If true, than injector returns an array of instances. This is useful to allow multiple
|
|
|
|
* providers spread across many files to provide configuration information to a common token.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* ### Example
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
multi?: boolean;
|
|
|
|
}
|
2015-04-15 22:35:38 +00:00
|
|
|
|
2016-08-15 19:37:42 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Configures the {@link Injector} to return an instance of `useClass` for a token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* @Injectable()
|
|
|
|
* class MyService {}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* const provider: ClassProvider = {provide: 'someToken', useClass: MyService};
|
2016-08-15 19:37:42 -07:00
|
|
|
* ```
|
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* ### Example
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='ClassProvider'}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* Note that following two providers are not equal:
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='ClassProviderDifference'}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface ClassProvider {
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2017-01-03 16:54:46 -08:00
|
|
|
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
provide: any;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* Class to instantiate for the `token`.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
useClass: Type<any>;
|
2015-04-09 23:17:05 -07:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* If true, than injector returns an array of instances. This is useful to allow multiple
|
|
|
|
* providers spread across many files to provide configuration information to a common token.
|
2015-09-21 14:19:03 -07:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
multi?: boolean;
|
2015-04-09 23:17:05 -07:00
|
|
|
}
|
|
|
|
|
2015-10-10 22:11:13 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Configures the {@link Injector} to return a value of another `useExisting` token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* const provider: ExistingProvider = {provide: 'someToken', useExisting: 'someOtherToken'};
|
|
|
|
* ```
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2015-12-03 15:49:09 -08:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* ### Example
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='ExistingProvider'}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* @stable
|
2015-10-10 22:11:13 -07:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
export interface ExistingProvider {
|
2015-10-12 11:30:34 -07:00
|
|
|
/**
|
2017-01-03 16:54:46 -08:00
|
|
|
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
|
2015-10-12 11:30:34 -07:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
provide: any;
|
2015-10-12 11:30:34 -07:00
|
|
|
|
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* Existing `token` to return. (equivalent to `injector.get(useExisting)`)
|
2015-10-12 11:30:34 -07:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
useExisting: any;
|
2015-10-12 11:30:34 -07:00
|
|
|
|
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* If true, than injector returns an array of instances. This is useful to allow multiple
|
|
|
|
* providers spread across many files to provide configuration information to a common token.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
|
2015-10-12 11:30:34 -07:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
multi?: boolean;
|
2015-10-10 22:11:13 -07:00
|
|
|
}
|
2015-09-17 13:12:50 -07:00
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Configures the {@link Injector} to return a value by invoking a `useFactory`
|
|
|
|
* function.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* function serviceFactory() { ... }
|
|
|
|
*
|
|
|
|
* const provider: FactoryProvider = {provide: 'someToken', useFactory: serviceFactory, deps: []};
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* ### Example
|
2015-09-03 16:17:23 -07:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='FactoryProvider'}
|
2015-12-03 15:49:09 -08:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* Dependencies can also be marked as optional:
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='FactoryProviderOptionalDeps'}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* @stable
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
export interface FactoryProvider {
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2017-01-03 16:54:46 -08:00
|
|
|
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
provide: any;
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* A function to invoke to create a value for this `token`. The function is invoked with
|
|
|
|
* resolved values of `token`s in the `deps` field.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
useFactory: Function;
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* A list of `token`s which need to be resolved by the injector. The list of values is than
|
|
|
|
* used as arguments to the `useFactory` function.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-24 12:53:28 -07:00
|
|
|
deps?: any[];
|
2015-02-21 15:18:06 +01:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* If true, than injector returns an array of instances. This is useful to allow multiple
|
|
|
|
* providers spread across many files to provide configuration information to a common token.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* ### Example
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
multi?: boolean;
|
2015-04-09 23:17:05 -07:00
|
|
|
}
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-09-02 10:21:28 -07:00
|
|
|
/**
|
2016-09-14 09:43:01 -07:00
|
|
|
* @whatItDoes Describes how the {@link Injector} should be configured.
|
|
|
|
* @howToUse
|
2016-08-15 19:37:42 -07:00
|
|
|
* See {@link TypeProvider}, {@link ValueProvider}, {@link ClassProvider}, {@link ExistingProvider},
|
|
|
|
* {@link FactoryProvider}.
|
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* @stable
|
2015-09-02 10:21:28 -07:00
|
|
|
*/
|
2016-08-15 19:37:42 -07:00
|
|
|
export type Provider =
|
|
|
|
TypeProvider | ValueProvider | ClassProvider | ExistingProvider | FactoryProvider | any[];
|