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
|
|
|
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
/**
|
|
|
|
* @whatItDoes Configures the {@link Injector} to return a value for a token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* @Injectable(SomeModule, {useValue: 'someValue'})
|
|
|
|
* class SomeClass {}
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='ValueSansProvider'}
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export interface ValueSansProvider {
|
|
|
|
/**
|
|
|
|
* The value to inject.
|
|
|
|
*/
|
|
|
|
useValue: any;
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
2017-07-27 13:49:33 -07:00
|
|
|
* @whatItDoes Configures the {@link Injector} to return a value for a token.
|
2016-09-14 09:43:01 -07:00
|
|
|
* @howToUse
|
|
|
|
* ```
|
2017-07-27 13:49:33 -07:00
|
|
|
* const provider: ValueProvider = {provide: 'someToken', useValue: 'someValue'};
|
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
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2017-07-27 13:49:33 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='ValueProvider'}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* @stable
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
export interface ValueProvider extends ValueSansProvider {
|
2017-07-27 13:49:33 -07:00
|
|
|
/**
|
|
|
|
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
|
|
|
|
*/
|
|
|
|
provide: any;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true, then 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
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
|
|
|
|
*/
|
|
|
|
multi?: boolean;
|
|
|
|
}
|
2015-04-15 22:35:38 +00:00
|
|
|
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
/**
|
|
|
|
* @whatItDoes Configures the {@link Injector} to return an instance of `useClass` for a token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* @Injectable(SomeModule, {useClass: MyService, deps: []})
|
|
|
|
* class MyService {}
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='StaticClassSansProvider'}
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export interface StaticClassSansProvider {
|
|
|
|
/**
|
|
|
|
* An optional class to instantiate for the `token`. (If not provided `provide` is assumed to be a
|
|
|
|
* class to instantiate)
|
|
|
|
*/
|
|
|
|
useClass: Type<any>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of `token`s which need to be resolved by the injector. The list of values is then
|
|
|
|
* used as arguments to the `useClass` constructor.
|
|
|
|
*/
|
|
|
|
deps: any[];
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:37:42 -07:00
|
|
|
/**
|
2017-07-27 13:49:33 -07:00
|
|
|
* @whatItDoes Configures the {@link Injector} to return an instance of `useClass` for a token.
|
2016-09-14 09:43:01 -07:00
|
|
|
* @howToUse
|
|
|
|
* ```
|
2017-07-27 13:49:33 -07:00
|
|
|
* @Injectable()
|
|
|
|
* class MyService {}
|
|
|
|
*
|
|
|
|
* const provider: ClassProvider = {provide: 'someToken', useClass: MyService, deps: []};
|
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
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
2017-07-27 13:49:33 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='StaticClassProvider'}
|
|
|
|
*
|
|
|
|
* Note that following two providers are not equal:
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='StaticClassProviderDifference'}
|
2016-09-14 09:43:01 -07:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* @stable
|
|
|
|
*/
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
export interface StaticClassProvider extends StaticClassSansProvider {
|
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
|
|
|
|
|
|
|
/**
|
2017-02-04 14:41:18 +01:00
|
|
|
* If true, then injector returns an array of instances. This is useful to allow multiple
|
2016-08-15 19:37:42 -07:00
|
|
|
* 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
|
|
|
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
/**
|
|
|
|
* @whatItDoes Configures the {@link Injector} to return an instance of a token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* @Injectable(SomeModule, {deps: []})
|
|
|
|
* class MyService {}
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='ConstructorSansProvider'}
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export interface ConstructorSansProvider {
|
|
|
|
/**
|
|
|
|
* A list of `token`s which need to be resolved by the injector. The list of values is then
|
|
|
|
* used as arguments to the `useClass` constructor.
|
|
|
|
*/
|
|
|
|
deps?: any[];
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:37:42 -07:00
|
|
|
/**
|
2017-07-27 13:49:33 -07:00
|
|
|
* @whatItDoes Configures the {@link Injector} to return an instance of a token.
|
2016-09-14 09:43:01 -07:00
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* @Injectable()
|
|
|
|
* class MyService {}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
2017-07-27 13:49:33 -07:00
|
|
|
* const provider: ClassProvider = {provide: MyClass, deps: []};
|
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
|
|
|
*
|
2017-07-27 13:49:33 -07:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='ConstructorProvider'}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
export interface ConstructorProvider extends ConstructorSansProvider {
|
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
|
|
|
*/
|
2017-07-27 13:49:33 -07:00
|
|
|
provide: Type<any>;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2017-02-04 14:41:18 +01:00
|
|
|
* If true, then injector returns an array of instances. This is useful to allow multiple
|
2016-08-15 19:37:42 -07:00
|
|
|
* 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
|
|
|
}
|
|
|
|
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
/**
|
|
|
|
* @whatItDoes Configures the {@link Injector} to return a value of another `useExisting` token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* @Injectable(SomeModule, {useExisting: 'someOtherToken'})
|
|
|
|
* class SomeClass {}
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='ExistingSansProvider'}
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface ExistingSansProvider {
|
|
|
|
/**
|
|
|
|
* Existing `token` to return. (equivalent to `injector.get(useExisting)`)
|
|
|
|
*/
|
|
|
|
useExisting: any;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
export interface ExistingProvider extends ExistingSansProvider {
|
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
|
|
|
|
|
|
|
/**
|
2017-02-04 14:41:18 +01:00
|
|
|
* If true, then injector returns an array of instances. This is useful to allow multiple
|
2016-08-15 19:37:42 -07:00
|
|
|
* 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() { ... }
|
|
|
|
*
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
* @Injectable(SomeModule, {useFactory: serviceFactory, deps: []})
|
|
|
|
* class SomeClass {}
|
2016-09-14 09:43:01 -07:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
* {@example core/di/ts/provider_spec.ts region='FactorySansProvider'}
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
* @experimental
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
export interface FactorySansProvider {
|
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
|
|
|
/**
|
2017-02-04 14:41:18 +01:00
|
|
|
* A list of `token`s which need to be resolved by the injector. The list of values is then
|
2016-08-15 19:37:42 -07:00
|
|
|
* 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[];
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08: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"}.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='FactoryProvider'}
|
|
|
|
*
|
|
|
|
* Dependencies can also be marked as optional:
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='FactoryProviderOptionalDeps'}
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface FactoryProvider extends FactorySansProvider {
|
|
|
|
/**
|
|
|
|
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
|
|
|
|
*/
|
|
|
|
provide: any;
|
2015-02-21 15:18:06 +01:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2017-02-04 14:41:18 +01:00
|
|
|
* If true, then injector returns an array of instances. This is useful to allow multiple
|
2016-08-15 19:37:42 -07:00
|
|
|
* 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
|
|
|
|
2017-07-27 13:49:33 -07:00
|
|
|
/**
|
|
|
|
* @whatItDoes Describes how the {@link Injector} should be configured in a static way (Without
|
|
|
|
* reflection).
|
|
|
|
* @howToUse
|
|
|
|
* See {@link ValueProvider}, {@link ExistingProvider}, {@link FactoryProvider}.
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export type StaticProvider = ValueProvider | ExistingProvider | StaticClassProvider |
|
|
|
|
ConstructorProvider | FactoryProvider | any[];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* Create an instance by invoking the `new` operator and supplying additional arguments.
|
|
|
|
* This form is a short form of `TypeProvider`;
|
|
|
|
*
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='TypeProvider'}
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface TypeProvider extends Type<any> {}
|
|
|
|
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
/**
|
|
|
|
* @whatItDoes Configures the {@link Injector} to return a value by invoking a `useClass`
|
|
|
|
* function.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* class SomeClassImpl {}
|
|
|
|
*
|
|
|
|
* @Injectable(SomeModule, {useClass: SomeClassImpl})
|
|
|
|
* class SomeClass {}
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='ClassSansProvider'}
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export interface ClassSansProvider {
|
|
|
|
/**
|
|
|
|
* Class to instantiate for the `token`.
|
|
|
|
*/
|
|
|
|
useClass: Type<any>;
|
|
|
|
}
|
|
|
|
|
2017-07-27 13:49:33 -07:00
|
|
|
/**
|
|
|
|
* @whatItDoes Configures the {@link Injector} to return an instance of `useClass` for a token.
|
|
|
|
* @howToUse
|
|
|
|
* ```
|
|
|
|
* @Injectable()
|
|
|
|
* class MyService {}
|
|
|
|
*
|
|
|
|
* const provider: ClassProvider = {provide: 'someToken', useClass: MyService};
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='ClassProvider'}
|
|
|
|
*
|
|
|
|
* Note that following two providers are not equal:
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='ClassProviderDifference'}
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
export interface ClassProvider extends ClassSansProvider {
|
2017-07-27 13:49:33 -07:00
|
|
|
/**
|
|
|
|
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
|
|
|
|
*/
|
|
|
|
provide: any;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true, then 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
|
|
|
|
*
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
|
|
|
|
*/
|
|
|
|
multi?: boolean;
|
|
|
|
}
|
|
|
|
|
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
|
2017-07-27 13:49:33 -07:00
|
|
|
* See {@link TypeProvider}, {@link ClassProvider}, {@link StaticProvider}.
|
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
|
|
|
*
|
|
|
|
* @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[];
|