UNPKG

899 BTypeScriptView Raw
1import { ValueOrPromise } from './value-promise';
2/**
3 * Providers allow developers to compute injected values dynamically,
4 * with any dependencies required by the value getter injected automatically
5 * from the Context.
6 *
7 * @example
8 *
9 * ```ts
10 * export class DateProvider implements Provider<Date> {
11 * constructor(@inject('stringDate') private param: String){}
12 * value(): Date {
13 * return new Date(param);
14 * }
15 * }
16 *
17 * ctx.bind('stringDate').to('2017-01-01')
18 * ctx.bind('provider_key').toProvider(DateProvider);
19 *
20 * const value = ctx.getAsync('provider_key');
21 * // value is a Date instance
22 * ```
23 */
24export interface Provider<T> {
25 /**
26 * @returns The value to inject to dependents.
27 * This method can return a promise too, in which case the IoC framework
28 * will resolve this promise to obtain the value to inject.
29 */
30 value(): ValueOrPromise<T>;
31}