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-08-15 19:37:42 -07:00
|
|
|
* Configures the {@link Injector} to return an instance of `Type` when `Type' is used as token.
|
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-08-15 19:37:42 -07:00
|
|
|
* ### Example
|
2015-04-15 22:35:38 +00:00
|
|
|
* ```javascript
|
2016-08-15 19:37:42 -07:00
|
|
|
* @Injectable()
|
|
|
|
* class Greeting {
|
|
|
|
* text: 'Hello';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @Injectable()
|
|
|
|
* class MyClass {
|
|
|
|
* greeting:string;
|
|
|
|
* constructor(greeting: Greeting) {
|
|
|
|
* this.greeting = greeting.text;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* Greeting, // Shorthand for { provide: Greeting, useClass: Greeting }
|
|
|
|
* MyClass // Shorthand for { provide: MyClass, useClass: MyClass }
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* const myClass: MyClass = injector.get(MyClass);
|
|
|
|
* expect(myClass.greeting).toEqual('Hello');
|
2015-04-15 22:35:38 +00:00
|
|
|
* ```
|
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
|
|
|
/**
|
|
|
|
* Configures the {@link Injector} to return a value for a token.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* {provide: String, useValue: 'Hello'}
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(String)).toEqual('Hello');
|
|
|
|
* ```
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface ValueProvider {
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* An injection token. (Typically an instance of `Type` or `OpaqueToken`, 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
|
|
|
|
* ```javascript
|
|
|
|
* var locale = new OpaqueToken('local');
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* { provide: locale, useValue: 'en' },
|
|
|
|
* { provide: locale, useValue: 'sk' },
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* const locales: string[] = injector.get(locale);
|
|
|
|
* expect(locales).toEqual(['en', 'sk']);
|
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
|
|
|
/**
|
|
|
|
* Configures the {@link Injector} to return an instance of `useClass` for a token.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
* ```javascript
|
|
|
|
* abstract class Shape {
|
|
|
|
* name: string;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class Square extends Shape {
|
|
|
|
* name = 'square';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* {provide: Shape, useClass: Square}
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* const shape: Shape = injector.get(Shape);
|
|
|
|
* expect(shape.name).toEqual('square');
|
|
|
|
* expect(shape instanceof Square).toBe(true);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Note that following is not equal:
|
|
|
|
* ```javascript
|
|
|
|
* class Greeting {
|
|
|
|
* salutation = 'Hello';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class FormalGreeting extends Greeting {
|
|
|
|
* salutation = 'Greetings';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* FormalGreeting,
|
|
|
|
* {provide: Greeting, useClass: FormalGreeting}
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* // The injector returns different instances.
|
|
|
|
* // See: {provide: ?, useExisting: ?} if you want the same instance.
|
|
|
|
* expect(injector.get(FormalGreeting)).not.toBe(injector.get(Greeting));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface ClassProvider {
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* An injection token. (Typically an instance of `Type` or `OpaqueToken`, 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
|
|
|
* ```javascript
|
|
|
|
* abstract class Locale {
|
|
|
|
* name: string;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* @Injectable()
|
|
|
|
* class EnLocale extends Locale {
|
|
|
|
* name: 'en';
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* @Injectable()
|
|
|
|
* class SkLocale extends Locale {
|
|
|
|
* name: 'sk';
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* { provide: Locale, useValue: EnLocale, multi: true },
|
|
|
|
* { provide: Locale, useValue: SkLocale, multi: true },
|
2015-09-02 10:21:28 -07:00
|
|
|
* ]);
|
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* const locales: Locale[] = injector.get(Locale);
|
|
|
|
* const localeNames: string[] = locals.map((l) => l.name);
|
|
|
|
* expect(localeNames).toEqual(['en', 'sk']);
|
2015-09-02 10:21:28 -07:00
|
|
|
* ```
|
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-08-15 19:37:42 -07:00
|
|
|
* Configures the {@link Injector} to return a value of another `useExisting` token.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
* ```javascript
|
|
|
|
* class Greeting {
|
|
|
|
* salutation = 'Hello';
|
|
|
|
* }
|
2015-12-03 15:49:09 -08:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* class FormalGreeting extends Greeting {
|
|
|
|
* salutation = 'Greetings';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* FormalGreeting,
|
|
|
|
* {provide: Greeting, useExisting: FormalGreeting}
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(Greeting).name).toEqual('Hello');
|
|
|
|
* expect(injector.get(FormalGreeting).name).toEqual('Hello');
|
|
|
|
* expect(injector.get(Salutation).name).toBe(injector.get(Greeting));
|
|
|
|
* ```
|
|
|
|
* @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
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* An injection token. (Typically an instance of `Type` or `OpaqueToken`, 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
|
|
|
|
* ```javascript
|
|
|
|
* abstract class Locale {
|
|
|
|
* name: string;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* @Injectable()
|
|
|
|
* class EnLocale extends Locale {
|
|
|
|
* name: 'en';
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* @Injectable()
|
|
|
|
* class SkLocale extends Locale {
|
|
|
|
* name: 'sk';
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* EnLocale,
|
|
|
|
* SkLocale
|
|
|
|
* { provide: Locale, useExisting: EnLocale, multi: true },
|
|
|
|
* { provide: Locale, useExisting: SkLocale, multi: true },
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* const locales: Locale[] = injector.get(Locale);
|
|
|
|
* const localeNames: string[] = locals.map((l) => l.name);
|
|
|
|
* expect(localeNames).toEqual(['en', 'sk']);
|
|
|
|
* ```
|
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-08-15 19:37:42 -07:00
|
|
|
* Configures the {@link Injector} to return a value by invoking a `useFactory` function.
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* ### Example
|
|
|
|
* ```javascript
|
|
|
|
* const HASH = new OpaqueToken('hash');
|
2015-09-03 16:17:23 -07:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* {provide: Location, useValue: window.location},
|
|
|
|
* {provide: HASH, useFactory: (location: Location) => location.hash, deps: [Location]}
|
|
|
|
* ]);
|
2015-12-03 15:49:09 -08:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
*
|
|
|
|
* // Assume location is: http://angular.io/#someLocation
|
|
|
|
* expect(injector.get(HASH)).toEqual('someLocation');
|
|
|
|
* ``
|
|
|
|
* @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
|
|
|
/**
|
2016-08-15 19:37:42 -07:00
|
|
|
* An injection token. (Typically an instance of `Type` or `OpaqueToken`, 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
|
|
|
|
* ```javascript
|
|
|
|
* class Locale {
|
|
|
|
* constructor(public name: string) {}
|
|
|
|
* };
|
|
|
|
* const PRIMARY = new OpequeToken('primary');
|
|
|
|
* const SECONDARY = new OpequeToken('secondary');
|
|
|
|
*
|
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* { provide: PRIMARY: useValue: 'en'},
|
|
|
|
* { provide: SECONDARY: useValue: 'sk'},
|
|
|
|
* { provide: Locale, useFactory: (n) => new Locale(n), deps: [PRIMARY], multi: true},
|
|
|
|
* { provide: Locale, useFactory: (n) => new Locale(n), deps: [SECONDARY], multi: true},
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* const locales: Locale[] = injector.get(Locale);
|
|
|
|
* const localeNames: string[] = locals.map((l) => l.name);
|
|
|
|
* expect(localeNames).toEqual(['en', 'sk']);
|
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-08-15 19:37:42 -07:00
|
|
|
* Describes how the {@link Injector} should be configured.
|
|
|
|
*
|
|
|
|
* See {@link TypeProvider}, {@link ValueProvider}, {@link ClassProvider}, {@link ExistingProvider},
|
|
|
|
* {@link FactoryProvider}.
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* class Greeting {
|
|
|
|
* salutation = 'Hello';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class FormalGreeting extends Greeting {
|
|
|
|
* salutation = 'Greetings';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* abstract class Operation {
|
|
|
|
* apply(a,b): any;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class AddOperation extends Operation {
|
|
|
|
* apply(a,b) { return a+b; }
|
|
|
|
* }
|
2015-09-02 10:21:28 -07:00
|
|
|
*
|
2016-04-14 12:35:24 -07:00
|
|
|
*
|
2016-08-15 19:37:42 -07:00
|
|
|
* const injector = Injector.resolveAndCreate([
|
|
|
|
* FormalGreeting,
|
|
|
|
* {provide: String, useValue: 'Hello World!'},
|
|
|
|
* {provide: Greeting, useExisting: FormalGreeting},
|
|
|
|
* {provide: Operation, useClass: AddOperation},
|
|
|
|
* {provide: Number, useFactory: (op) =>op.apply(1,2), deps: [Operation] }
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(FormalGreeting).name).toEqual('Greetings');
|
|
|
|
* expect(injector.get(String).name).toEqual('Hello World!');
|
|
|
|
* expect(injector.get(Greeting).name).toBe(injector.get(FormalGreeting));
|
|
|
|
* expect(injector.get(Number).toEqual(3);
|
|
|
|
* ```
|
|
|
|
* @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[];
|