/
binding.ts
396 lines (369 loc) · 11 KB
/
binding.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: @loopback/context
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {Context} from './context';
import {Constructor, instantiateClass} from './resolver';
import {isPromise} from './is-promise';
import {Provider} from './provider';
// tslint:disable-next-line:no-any
export type BoundValue = any;
export type ValueOrPromise<T> = T | Promise<T>;
/**
* Scope for binding values
*/
export enum BindingScope {
/**
* The binding provides a value that is calculated each time. This will be
* the default scope if not set.
*
* For example, with the following context hierarchy:
*
* - app (with a binding 'b1' that produces sequential values 0, 1, ...)
* - req1
* - req2
*
* // get('b1') produces a new value each time for app and its descendants
* app.get('b1') ==> 0
* req1.get('b1') ==> 1
* req2.get('b1') ==> 2
* req2.get('b1') ==> 3
* app.get('b1') ==> 4
*/
TRANSIENT = 'Transient',
/**
* The binding provides a value as a singleton within each local context. The
* value is calculated only once per context and cached for subsequenicial
* uses. Child contexts have their own value and do not share with their
* ancestors.
*
* For example, with the following context hierarchy:
*
* - app (with a binding 'b1' that produces sequential values 0, 1, ...)
* - req1
* - req2
*
* // 0 is the singleton for app afterward
* app.get('b1') ==> 0
*
* // 'b1' is found in app but not in req1, a new value 1 is calculated.
* // 1 is the singleton for req1 afterward
* req1.get('b1') ==> 1
*
* // 'b1' is found in app but not in req2, a new value 2 is calculated.
* // 2 is the singleton for req2 afterward
* req2.get('b1') ==> 2
*/
CONTEXT = 'Context',
/**
* The binding provides a value as a singleton within the context hierarchy
* (the owning context and its descendants). The value is calculated only
* once for the owning context and cached for subsequenicial uses. Child
* contexts share the same value as their ancestors.
*
* For example, with the following context hierarchy:
*
* - app (with a binding 'b1' that produces sequential values 0, 1, ...)
* - req1
* - req2
*
* // 0 is the singleton for app afterward
* app.get('b1') ==> 0
*
* // 'b1' is found in app, reuse it
* req1.get('b1') ==> 0
*
* // 'b1' is found in app, reuse it
* req2.get('b1') ==> 0
*/
SINGLETON = 'Singleton',
}
export enum BindingType {
CONSTANT = 'Constant',
DYNAMIC_VALUE = 'DynamicValue',
CLASS = 'Class',
PROVIDER = 'Provider',
}
// FIXME(bajtos) The binding class should be parameterized by the value
// type stored
export class Binding {
static PROPERTY_SEPARATOR = '#';
/**
* Validate the binding key format. Please note that `#` is reserved.
* @param key Binding key, such as `a, a.b, a:b, a/b
*/
static validateKey(key: string) {
if (!key) throw new Error('Binding key must be provided.');
if (key.includes(Binding.PROPERTY_SEPARATOR)) {
throw new Error(
`Binding key ${key} cannot contain` +
` '${Binding.PROPERTY_SEPARATOR}'.`,
);
}
return key;
}
/**
* Build a binding key from a key and a path
* @param key The key
* @param path The path
*
*/
static buildKeyWithPath(key: string, path: string) {
return `${key}${Binding.PROPERTY_SEPARATOR}${path}`;
}
/**
* Parse a string containing both the binding key and the path to the deeply
* nested property to retrieve.
*
* @param keyWithPath The key with an optional path,
* e.g. "application.instance" or "config#rest.port".
*/
static parseKeyWithPath(keyWithPath: string) {
const index = keyWithPath.indexOf(Binding.PROPERTY_SEPARATOR);
if (index === -1) {
return {key: keyWithPath, path: undefined};
}
return {
key: keyWithPath.substr(0, index).trim(),
path: keyWithPath.substr(index + 1),
};
}
public readonly key: string;
public readonly tags: Set<string> = new Set();
public scope: BindingScope = BindingScope.TRANSIENT;
public type: BindingType;
private _cache: BoundValue;
private _getValue: (ctx?: Context) => BoundValue | Promise<BoundValue>;
// For bindings bound via toClass, this property contains the constructor
// function
public valueConstructor: Constructor<BoundValue>;
constructor(key: string, public isLocked: boolean = false) {
Binding.validateKey(key);
this.key = key;
}
/**
* Cache the resolved value by the binding scope
* @param ctx The current context
* @param result The calculated value for the binding
*/
private _cacheValue(
ctx: Context,
result: BoundValue | Promise<BoundValue>,
): BoundValue | Promise<BoundValue> {
if (isPromise(result)) {
if (this.scope === BindingScope.SINGLETON) {
// Cache the value
result = result.then(val => {
this._cache = val;
return val;
});
} else if (this.scope === BindingScope.CONTEXT) {
// Cache the value
result = result.then(val => {
if (ctx.contains(this.key)) {
// The ctx owns the binding
this._cache = val;
} else {
// Create a binding of the cached value for the current context
ctx.bind(this.key).to(val);
}
return val;
});
}
} else {
if (this.scope === BindingScope.SINGLETON) {
// Cache the value
this._cache = result;
} else if (this.scope === BindingScope.CONTEXT) {
if (ctx.contains(this.key)) {
// The ctx owns the binding
this._cache = result;
} else {
// Create a binding of the cached value for the current context
ctx.bind(this.key).to(result);
}
}
}
return result;
}
/**
* This is an internal function optimized for performance.
* Users should use `@inject(key)` or `ctx.get(key)` instead.
*
* Get the value bound to this key. Depending on `isSync`, this
* function returns either:
* - the bound value
* - a promise of the bound value
*
* Consumers wishing to consume sync values directly should use `isPromise`
* to check the type of the returned value to decide how to handle it.
*
* ```
* const result = binding.getValue(ctx);
* if (isPromise(result)) {
* result.then(doSomething)
* } else {
* doSomething(result);
* }
* ```
*/
getValue(ctx: Context): BoundValue | Promise<BoundValue> {
// First check cached value for non-transient
if (this._cache !== undefined) {
if (this.scope === BindingScope.SINGLETON) {
return this._cache;
} else if (this.scope === BindingScope.CONTEXT) {
if (ctx.contains(this.key)) {
return this._cache;
}
}
}
if (this._getValue) {
const result = this._getValue(ctx);
return this._cacheValue(ctx, result);
}
return Promise.reject(
new Error(`No value was configured for binding ${this.key}.`),
);
}
lock(): this {
this.isLocked = true;
return this;
}
tag(tagName: string | string[]): this {
if (typeof tagName === 'string') {
this.tags.add(tagName);
} else {
tagName.forEach(t => {
this.tags.add(t);
});
}
return this;
}
inScope(scope: BindingScope): this {
this.scope = scope;
return this;
}
/**
* Bind the key to a constant value. The value must be already available
* at binding time, it is not allowed to pass a Promise instance.
*
* @param value The bound value.
*
* @example
*
* ```ts
* ctx.bind('appName').to('CodeHub');
* ```
*/
to(value: BoundValue): this {
if (isPromise(value)) {
// Promises are a construct primarily intended for flow control:
// In an algorithm with steps 1 and 2, we want to wait for the outcome
// of step 1 before starting step 2.
//
// Promises are NOT a tool for storing values that may become available
// in the future, depending on the success or a failure of a background
// async task.
//
// Values stored in bindings are typically accessed only later,
// in a different turn of the event loop or the Promise micro-queue.
// As a result, when a promise is stored via `.to()` and is rejected
// later, then more likely than not, there will be no error (catch)
// handler registered yet, and Node.js will print
// "Unhandled Rejection Warning".
throw new Error(
'Promise instances are not allowed for constant values ' +
'bound via ".to()". Register an async getter function ' +
'via ".toDynamicValue()" instead.',
);
}
this.type = BindingType.CONSTANT;
this._getValue = () => value;
return this;
}
/**
* Bind the key to a computed (dynamic) value.
*
* @param factoryFn The factory function creating the value.
* Both sync and async functions are supported.
*
* @example
*
* ```ts
* // synchronous
* ctx.bind('now').toDynamicValue(() => Date.now());
*
* // asynchronous
* ctx.bind('something').toDynamicValue(
* async () => Promise.delay(10).then(doSomething)
* );
* ```
*/
toDynamicValue(factoryFn: () => BoundValue | Promise<BoundValue>): this {
this.type = BindingType.DYNAMIC_VALUE;
this._getValue = ctx => factoryFn();
return this;
}
/**
* Bind the key to a value computed by a Provider.
*
* * @example
*
* ```ts
* export class DateProvider implements Provider<Date> {
* constructor(@inject('stringDate') private param: String){}
* value(): Date {
* return new Date(param);
* }
* }
* ```
*
* @param provider The value provider to use.
*/
public toProvider<T>(providerClass: Constructor<Provider<T>>): this {
this.type = BindingType.PROVIDER;
this._getValue = ctx => {
const providerOrPromise = instantiateClass<Provider<T>>(
providerClass,
ctx!,
);
if (isPromise(providerOrPromise)) {
return providerOrPromise.then(p => p.value());
} else {
return providerOrPromise.value();
}
};
return this;
}
/**
* Bind the key to an instance of the given class.
*
* @param ctor The class constructor to call. Any constructor
* arguments must be annotated with `@inject` so that
* we can resolve them from the context.
*/
toClass<T>(ctor: Constructor<T>): this {
this.type = BindingType.CLASS;
this._getValue = ctx => instantiateClass(ctor, ctx!);
this.valueConstructor = ctor;
return this;
}
unlock(): this {
this.isLocked = false;
return this;
}
toJSON(): Object {
// tslint:disable-next-line:no-any
const json: {[name: string]: any} = {
key: this.key,
scope: this.scope,
tags: Array.from(this.tags),
isLocked: this.isLocked,
};
if (this.type != null) {
json.type = this.type;
}
return json;
}
}