@loopback/context 1.8.1 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +58 -0
- package/dist/binding-filter.d.ts +28 -1
- package/dist/binding-filter.js.map +1 -1
- package/dist/binding.d.ts +36 -6
- package/dist/binding.js +65 -38
- package/dist/binding.js.map +1 -1
- package/dist/context-view.d.ts +7 -0
- package/dist/context-view.js +15 -2
- package/dist/context-view.js.map +1 -1
- package/dist/context.d.ts +38 -15
- package/dist/context.js +52 -8
- package/dist/context.js.map +1 -1
- package/dist/inject.d.ts +61 -7
- package/dist/inject.js +93 -46
- package/dist/inject.js.map +1 -1
- package/dist/resolution-session.d.ts +17 -4
- package/dist/resolution-session.js +16 -4
- package/dist/resolution-session.js.map +1 -1
- package/dist/resolver.js +2 -2
- package/dist/resolver.js.map +1 -1
- package/package.json +9 -9
- package/src/binding-filter.ts +29 -1
- package/src/binding.ts +101 -23
- package/src/context-view.ts +20 -3
- package/src/context.ts +85 -29
- package/src/inject.ts +145 -70
- package/src/resolution-session.ts +25 -10
- package/src/resolver.ts +8 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolution-session.js","sourceRoot":"","sources":["../src/resolution-session.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,iCAAiC;AACjC,+CAA+C;AAC/C,gEAAgE;;
|
|
1
|
+
{"version":3,"file":"resolution-session.js","sourceRoot":"","sources":["../src/resolution-session.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,iCAAiC;AACjC,+CAA+C;AAC/C,gEAAgE;;AAEhE,iDAAoD;AACpD,qCAAqC;AAGrC,mDAA2E;AAE3E,MAAM,YAAY,GAAG,WAAW,CAAC,mCAAmC,CAAC,CAAC;AACtE,MAAM,aAAa,GAAG,2BAAgB,CAAC,aAAa,CAAC;AA8BrD;;;GAGG;AACH,SAAS,SAAS,CAChB,OAAsC;IAEtC,OAAO,OAAO,IA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OAAO,EAAE,gBAAgB,EAAC,CAAC;KACpC;IACD,OAAO,gBAAgB,IAAI,EAAE,CAAC;AAChC,CAAC;AARD,kDAQC"}
|
package/dist/resolver.js
CHANGED
|
@@ -36,7 +36,7 @@ nonInjectedArgs) {
|
|
|
36
36
|
debug('Non-injected arguments:', nonInjectedArgs);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
const argsOrPromise = resolveInjectedArguments(ctor, '', ctx, session);
|
|
39
|
+
const argsOrPromise = resolveInjectedArguments(ctor, '', ctx, session, nonInjectedArgs);
|
|
40
40
|
const propertiesOrPromise = resolveInjectedProperties(ctor, ctx, session);
|
|
41
41
|
const inst = value_promise_1.transformValueOrPromise(argsOrPromise, args => {
|
|
42
42
|
/* istanbul ignore if */
|
|
@@ -104,7 +104,7 @@ function resolve(ctx, injection, session) {
|
|
|
104
104
|
session: s,
|
|
105
105
|
// If the `optional` flag is set for the injection, the resolution
|
|
106
106
|
// will return `undefined` instead of throwing an error
|
|
107
|
-
optional: injection.metadata
|
|
107
|
+
optional: injection.metadata.optional,
|
|
108
108
|
});
|
|
109
109
|
}
|
|
110
110
|
}, injection, session);
|
package/dist/resolver.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,iCAAiC;AACjC,+CAA+C;AAC/C,gEAAgE;;AAEhE,iDAAoD;AACpD,iCAAiC;AACjC,qCAAqC;AACrC,uCAAuC;AACvC,qDAAkD;AAGlD,qCAIkB;AAClB,6DAAuD;AACvD,mDAQyB;AAEzB,MAAM,KAAK,GAAG,WAAW,CAAC,2BAA2B,CAAC,CAAC;AACvD,MAAM,aAAa,GAAG,2BAAgB,CAAC,aAAa,CAAC;AAErD;;;;;;;;;;;GAWG;AACH,SAAgB,gBAAgB,CAC9B,IAAoB,EACpB,GAAY,EACZ,OAA2B;AAC3B,kCAAkC;AAClC,eAAuB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E;QACjB,KAAK,CAAC,sCAAsC,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,CAAC;KAC3E;IACD,MAAM,kBAAkB,GAAG,mCAA0B,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IAE7E,OAAO,0BAAU,CAAC,kBAAkB,EAAE,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE;QACrD,IAAI,CAAC,SAAS,CAAC,eAAe,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE;YACpD,MAAM,IAAI,GAAG,aAAa,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;YAC3C,MAAM,IAAI,KAAK,CACb,oCAAoC,IAAI,IAAI;gBAC1C,gBAAgB,CAAC,6CAA6C,CACjE,CAAC;SACH;QAED,OAAO,OAAO,CACZ,GAAG,EACH,SAAS;QACT,4EAA4E;QAC5E,sCAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,CAChC,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AA3BD,8DA2BC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loopback/context",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "LoopBack's container for Inversion of Control",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=8.9"
|
|
@@ -19,20 +19,20 @@
|
|
|
19
19
|
"copyright.owner": "IBM Corp.",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@loopback/metadata": "^1.0
|
|
22
|
+
"@loopback/metadata": "^1.1.0",
|
|
23
23
|
"debug": "^4.0.1",
|
|
24
|
-
"p-event": "^4.
|
|
24
|
+
"p-event": "^4.1.0",
|
|
25
25
|
"uuid": "^3.2.1"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@loopback/build": "^1.
|
|
29
|
-
"@loopback/testlab": "^1.2.
|
|
30
|
-
"@loopback/tslint-config": "^2.0.
|
|
28
|
+
"@loopback/build": "^1.5.0",
|
|
29
|
+
"@loopback/testlab": "^1.2.5",
|
|
30
|
+
"@loopback/tslint-config": "^2.0.4",
|
|
31
31
|
"@types/bluebird": "^3.5.20",
|
|
32
|
-
"@types/debug": "^4.1.
|
|
32
|
+
"@types/debug": "^4.1.4",
|
|
33
33
|
"@types/node": "^10.11.2",
|
|
34
34
|
"@types/uuid": "^3.4.3",
|
|
35
|
-
"bluebird": "^3.5.
|
|
35
|
+
"bluebird": "^3.5.4"
|
|
36
36
|
},
|
|
37
37
|
"keywords": [
|
|
38
38
|
"LoopBack",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"type": "git",
|
|
56
56
|
"url": "https://github.com/strongloop/loopback-next.git"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "fe8ef4641a5d7f200a8744007e9d50d3659353ef"
|
|
59
59
|
}
|
package/src/binding-filter.ts
CHANGED
|
@@ -9,9 +9,37 @@ import {BindingAddress} from './binding-key';
|
|
|
9
9
|
/**
|
|
10
10
|
* A function that filters bindings. It returns `true` to select a given
|
|
11
11
|
* binding.
|
|
12
|
+
*
|
|
13
|
+
* TODO(semver-major): We might change this type in the future to either remove
|
|
14
|
+
* the `<ValueType>` or make it as type guard by asserting the matched binding
|
|
15
|
+
* to be typed with `<ValueType>`.
|
|
16
|
+
*
|
|
17
|
+
* **NOTE**: Originally, we allow filters to be tied with a single value type.
|
|
18
|
+
* This actually does not make much sense - the filter function is typically
|
|
19
|
+
* invoked on all bindings to find those ones matching the given criteria.
|
|
20
|
+
* Filters must be prepared to handle bindings of any value type. We learned
|
|
21
|
+
* about this problem after enabling TypeScript's `strictFunctionTypes` check,
|
|
22
|
+
* but decided to preserve `ValueType` argument for backwards compatibility.
|
|
23
|
+
* The `<ValueType>` represents the value type for matched bindings but it's
|
|
24
|
+
* not used for checking.
|
|
25
|
+
*
|
|
26
|
+
* Ideally, `BindingFilter` should be declared as a type guard as follows:
|
|
27
|
+
* ```ts
|
|
28
|
+
* export type BindingFilterGuard<ValueType = unknown> = (
|
|
29
|
+
* binding: Readonly<Binding<unknown>>,
|
|
30
|
+
* ) => binding is Readonly<Binding<ValueType>>;
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* But TypeScript treats the following types as incompatible and does not accept
|
|
34
|
+
* type 1 for type 2.
|
|
35
|
+
*
|
|
36
|
+
* 1. `(binding: Readonly<Binding<unknown>>) => boolean`
|
|
37
|
+
* 2. `(binding: Readonly<Binding<unknown>>) => binding is Readonly<Binding<ValueType>>`
|
|
38
|
+
*
|
|
12
39
|
*/
|
|
40
|
+
// tslint:disable-next-line:no-unused
|
|
13
41
|
export type BindingFilter<ValueType = unknown> = (
|
|
14
|
-
binding: Readonly<Binding<
|
|
42
|
+
binding: Readonly<Binding<unknown>>,
|
|
15
43
|
) => boolean;
|
|
16
44
|
|
|
17
45
|
/**
|
package/src/binding.ts
CHANGED
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
-
import * as
|
|
6
|
+
import * as debugFactory from 'debug';
|
|
7
7
|
import {BindingAddress, BindingKey} from './binding-key';
|
|
8
8
|
import {Context} from './context';
|
|
9
9
|
import {Provider} from './provider';
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
asResolutionOptions,
|
|
12
|
+
ResolutionOptions,
|
|
13
|
+
ResolutionOptionsOrSession,
|
|
14
|
+
ResolutionSession,
|
|
15
|
+
} from './resolution-session';
|
|
11
16
|
import {instantiateClass} from './resolver';
|
|
12
17
|
import {
|
|
13
18
|
BoundValue,
|
|
@@ -18,7 +23,7 @@ import {
|
|
|
18
23
|
ValueOrPromise,
|
|
19
24
|
} from './value-promise';
|
|
20
25
|
|
|
21
|
-
const debug =
|
|
26
|
+
const debug = debugFactory('loopback:context:binding');
|
|
22
27
|
|
|
23
28
|
/**
|
|
24
29
|
* Scope for binding values
|
|
@@ -113,6 +118,10 @@ export enum BindingType {
|
|
|
113
118
|
* A provider class with `value()` function to get the value
|
|
114
119
|
*/
|
|
115
120
|
PROVIDER = 'Provider',
|
|
121
|
+
/**
|
|
122
|
+
* A alias to another binding key with optional path
|
|
123
|
+
*/
|
|
124
|
+
ALIAS = 'Alias',
|
|
116
125
|
}
|
|
117
126
|
|
|
118
127
|
// tslint:disable-next-line:no-any
|
|
@@ -128,6 +137,11 @@ export type BindingTag = TagMap | string;
|
|
|
128
137
|
*/
|
|
129
138
|
export type BindingTemplate<T = unknown> = (binding: Binding<T>) => void;
|
|
130
139
|
|
|
140
|
+
type ValueGetter<T> = (
|
|
141
|
+
ctx: Context,
|
|
142
|
+
options: ResolutionOptions,
|
|
143
|
+
) => ValueOrPromise<T | undefined>;
|
|
144
|
+
|
|
131
145
|
/**
|
|
132
146
|
* Binding represents an entry in the `Context`. Each binding has a key and a
|
|
133
147
|
* corresponding value getter.
|
|
@@ -161,10 +175,7 @@ export class Binding<T = BoundValue> {
|
|
|
161
175
|
}
|
|
162
176
|
|
|
163
177
|
private _cache: WeakMap<Context, T>;
|
|
164
|
-
private _getValue:
|
|
165
|
-
ctx?: Context,
|
|
166
|
-
session?: ResolutionSession,
|
|
167
|
-
) => ValueOrPromise<T>;
|
|
178
|
+
private _getValue: ValueGetter<T>;
|
|
168
179
|
|
|
169
180
|
private _valueConstructor?: Constructor<T>;
|
|
170
181
|
/**
|
|
@@ -204,6 +215,15 @@ export class Binding<T = BoundValue> {
|
|
|
204
215
|
});
|
|
205
216
|
}
|
|
206
217
|
|
|
218
|
+
/**
|
|
219
|
+
* Clear the cache
|
|
220
|
+
*/
|
|
221
|
+
private _clearCache() {
|
|
222
|
+
if (!this._cache) return;
|
|
223
|
+
// WeakMap does not have a `clear` method
|
|
224
|
+
this._cache = new WeakMap();
|
|
225
|
+
}
|
|
226
|
+
|
|
207
227
|
/**
|
|
208
228
|
* This is an internal function optimized for performance.
|
|
209
229
|
* Users should use `@inject(key)` or `ctx.get(key)` instead.
|
|
@@ -228,7 +248,25 @@ export class Binding<T = BoundValue> {
|
|
|
228
248
|
* @param ctx Context for the resolution
|
|
229
249
|
* @param session Optional session for binding and dependency resolution
|
|
230
250
|
*/
|
|
231
|
-
getValue(ctx: Context, session?: ResolutionSession): ValueOrPromise<T
|
|
251
|
+
getValue(ctx: Context, session?: ResolutionSession): ValueOrPromise<T>;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Returns a value or promise for this binding in the given context. The
|
|
255
|
+
* resolved value can be `undefined` if `optional` is set to `true` in
|
|
256
|
+
* `options`.
|
|
257
|
+
* @param ctx Context for the resolution
|
|
258
|
+
* @param options Optional options for binding and dependency resolution
|
|
259
|
+
*/
|
|
260
|
+
getValue(
|
|
261
|
+
ctx: Context,
|
|
262
|
+
options?: ResolutionOptions,
|
|
263
|
+
): ValueOrPromise<T | undefined>;
|
|
264
|
+
|
|
265
|
+
// Implementation
|
|
266
|
+
getValue(
|
|
267
|
+
ctx: Context,
|
|
268
|
+
optionsOrSession?: ResolutionOptionsOrSession,
|
|
269
|
+
): ValueOrPromise<T | undefined> {
|
|
232
270
|
/* istanbul ignore if */
|
|
233
271
|
if (debug.enabled) {
|
|
234
272
|
debug('Get value for binding %s', this.key);
|
|
@@ -246,11 +284,15 @@ export class Binding<T = BoundValue> {
|
|
|
246
284
|
}
|
|
247
285
|
}
|
|
248
286
|
}
|
|
287
|
+
const options = asResolutionOptions(optionsOrSession);
|
|
249
288
|
if (this._getValue) {
|
|
250
289
|
let result = ResolutionSession.runWithBinding(
|
|
251
|
-
s =>
|
|
290
|
+
s => {
|
|
291
|
+
const optionsWithSession = Object.assign({}, options, {session: s});
|
|
292
|
+
return this._getValue(ctx, optionsWithSession);
|
|
293
|
+
},
|
|
252
294
|
this,
|
|
253
|
-
session,
|
|
295
|
+
options.session,
|
|
254
296
|
);
|
|
255
297
|
return this._cacheValue(ctx, result);
|
|
256
298
|
}
|
|
@@ -317,6 +359,7 @@ export class Binding<T = BoundValue> {
|
|
|
317
359
|
* @param scope Binding scope
|
|
318
360
|
*/
|
|
319
361
|
inScope(scope: BindingScope): this {
|
|
362
|
+
if (this._scope !== scope) this._clearCache();
|
|
320
363
|
this._scope = scope;
|
|
321
364
|
return this;
|
|
322
365
|
}
|
|
@@ -328,11 +371,21 @@ export class Binding<T = BoundValue> {
|
|
|
328
371
|
*/
|
|
329
372
|
applyDefaultScope(scope: BindingScope): this {
|
|
330
373
|
if (!this._scope) {
|
|
331
|
-
this.
|
|
374
|
+
this.inScope(scope);
|
|
332
375
|
}
|
|
333
376
|
return this;
|
|
334
377
|
}
|
|
335
378
|
|
|
379
|
+
/**
|
|
380
|
+
* Set the `_getValue` function
|
|
381
|
+
* @param getValue getValue function
|
|
382
|
+
*/
|
|
383
|
+
private _setValueGetter(getValue: ValueGetter<T>) {
|
|
384
|
+
// Clear the cache
|
|
385
|
+
this._clearCache();
|
|
386
|
+
this._getValue = getValue;
|
|
387
|
+
}
|
|
388
|
+
|
|
336
389
|
/**
|
|
337
390
|
* Bind the key to a constant value. The value must be already available
|
|
338
391
|
* at binding time, it is not allowed to pass a Promise instance.
|
|
@@ -372,7 +425,7 @@ export class Binding<T = BoundValue> {
|
|
|
372
425
|
debug('Bind %s to constant:', this.key, value);
|
|
373
426
|
}
|
|
374
427
|
this._type = BindingType.CONSTANT;
|
|
375
|
-
this.
|
|
428
|
+
this._setValueGetter(() => value);
|
|
376
429
|
return this;
|
|
377
430
|
}
|
|
378
431
|
|
|
@@ -400,7 +453,7 @@ export class Binding<T = BoundValue> {
|
|
|
400
453
|
debug('Bind %s to dynamic value:', this.key, factoryFn);
|
|
401
454
|
}
|
|
402
455
|
this._type = BindingType.DYNAMIC_VALUE;
|
|
403
|
-
this.
|
|
456
|
+
this._setValueGetter(ctx => factoryFn());
|
|
404
457
|
return this;
|
|
405
458
|
}
|
|
406
459
|
|
|
@@ -426,14 +479,14 @@ export class Binding<T = BoundValue> {
|
|
|
426
479
|
debug('Bind %s to provider %s', this.key, providerClass.name);
|
|
427
480
|
}
|
|
428
481
|
this._type = BindingType.PROVIDER;
|
|
429
|
-
this.
|
|
482
|
+
this._setValueGetter((ctx, options) => {
|
|
430
483
|
const providerOrPromise = instantiateClass<Provider<T>>(
|
|
431
484
|
providerClass,
|
|
432
|
-
ctx
|
|
433
|
-
session,
|
|
485
|
+
ctx,
|
|
486
|
+
options.session,
|
|
434
487
|
);
|
|
435
488
|
return transformValueOrPromise(providerOrPromise, p => p.value());
|
|
436
|
-
};
|
|
489
|
+
});
|
|
437
490
|
return this;
|
|
438
491
|
}
|
|
439
492
|
|
|
@@ -450,19 +503,42 @@ export class Binding<T = BoundValue> {
|
|
|
450
503
|
debug('Bind %s to class %s', this.key, ctor.name);
|
|
451
504
|
}
|
|
452
505
|
this._type = BindingType.CLASS;
|
|
453
|
-
this.
|
|
506
|
+
this._setValueGetter((ctx, options) =>
|
|
507
|
+
instantiateClass(ctor, ctx, options.session),
|
|
508
|
+
);
|
|
454
509
|
this._valueConstructor = ctor;
|
|
455
510
|
return this;
|
|
456
511
|
}
|
|
457
512
|
|
|
513
|
+
/**
|
|
514
|
+
* Bind the key to an alias of another binding
|
|
515
|
+
* @param keyWithPath Target binding key with optional path,
|
|
516
|
+
* such as `servers.RestServer.options#apiExplorer`
|
|
517
|
+
*/
|
|
518
|
+
toAlias(keyWithPath: BindingAddress<T>) {
|
|
519
|
+
/* istanbul ignore if */
|
|
520
|
+
if (debug.enabled) {
|
|
521
|
+
debug('Bind %s to alias %s', this.key, keyWithPath);
|
|
522
|
+
}
|
|
523
|
+
this._type = BindingType.ALIAS;
|
|
524
|
+
this._setValueGetter((ctx, optionsOrSession) => {
|
|
525
|
+
const options = asResolutionOptions(optionsOrSession);
|
|
526
|
+
return ctx.getValueOrPromise(keyWithPath, options);
|
|
527
|
+
});
|
|
528
|
+
return this;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Unlock the binding
|
|
533
|
+
*/
|
|
458
534
|
unlock(): this {
|
|
459
535
|
this.isLocked = false;
|
|
460
536
|
return this;
|
|
461
537
|
}
|
|
462
538
|
|
|
463
539
|
/**
|
|
464
|
-
* Apply
|
|
465
|
-
* other attributes as a group.
|
|
540
|
+
* Apply one or more template functions to set up the binding with scope,
|
|
541
|
+
* tags, and other attributes as a group.
|
|
466
542
|
*
|
|
467
543
|
* For example,
|
|
468
544
|
* ```ts
|
|
@@ -472,10 +548,12 @@ export class Binding<T = BoundValue> {
|
|
|
472
548
|
* const serverBinding = new Binding<RestServer>('servers.RestServer1');
|
|
473
549
|
* serverBinding.apply(serverTemplate);
|
|
474
550
|
* ```
|
|
475
|
-
* @param
|
|
551
|
+
* @param templateFns One or more functions to configure the binding
|
|
476
552
|
*/
|
|
477
|
-
apply(
|
|
478
|
-
|
|
553
|
+
apply(...templateFns: BindingTemplate<T>[]): this {
|
|
554
|
+
for (const fn of templateFns) {
|
|
555
|
+
fn(this);
|
|
556
|
+
}
|
|
479
557
|
return this;
|
|
480
558
|
}
|
|
481
559
|
|
package/src/context-view.ts
CHANGED
|
@@ -85,10 +85,11 @@ export class ContextView<T = unknown> extends EventEmitter
|
|
|
85
85
|
/**
|
|
86
86
|
* Find matching bindings and refresh the cache
|
|
87
87
|
*/
|
|
88
|
-
protected findBindings() {
|
|
88
|
+
protected findBindings(): Readonly<Binding<T>>[] {
|
|
89
89
|
debug('Finding matching bindings');
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
const found = this.context.find(this.filter);
|
|
91
|
+
this._cachedBindings = found;
|
|
92
|
+
return found;
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
/**
|
|
@@ -152,3 +153,19 @@ export class ContextView<T = unknown> extends EventEmitter
|
|
|
152
153
|
return () => this.values(session);
|
|
153
154
|
}
|
|
154
155
|
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Create a context view as a getter
|
|
159
|
+
* @param ctx Context object
|
|
160
|
+
* @param bindingFilter A function to match bindings
|
|
161
|
+
* @param session Resolution session
|
|
162
|
+
*/
|
|
163
|
+
export function createViewGetter<T = unknown>(
|
|
164
|
+
ctx: Context,
|
|
165
|
+
bindingFilter: BindingFilter,
|
|
166
|
+
session?: ResolutionSession,
|
|
167
|
+
): Getter<T[]> {
|
|
168
|
+
const view = new ContextView<T>(ctx, bindingFilter);
|
|
169
|
+
view.open();
|
|
170
|
+
return view.asGetter(session);
|
|
171
|
+
}
|
package/src/context.ts
CHANGED
|
@@ -3,14 +3,12 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
-
import * as
|
|
6
|
+
import * as debugFactory from 'debug';
|
|
7
7
|
import {EventEmitter} from 'events';
|
|
8
8
|
import {v1 as uuidv1} from 'uuid';
|
|
9
|
-
import {ValueOrPromise} from '.';
|
|
10
9
|
import {Binding, BindingTag} from './binding';
|
|
11
10
|
import {BindingFilter, filterByKey, filterByTag} from './binding-filter';
|
|
12
11
|
import {BindingAddress, BindingKey} from './binding-key';
|
|
13
|
-
import {ContextView} from './context-view';
|
|
14
12
|
import {
|
|
15
13
|
ContextEventObserver,
|
|
16
14
|
ContextEventType,
|
|
@@ -18,8 +16,19 @@ import {
|
|
|
18
16
|
Notification,
|
|
19
17
|
Subscription,
|
|
20
18
|
} from './context-observer';
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
19
|
+
import {ContextView} from './context-view';
|
|
20
|
+
import {
|
|
21
|
+
asResolutionOptions,
|
|
22
|
+
ResolutionOptions,
|
|
23
|
+
ResolutionOptionsOrSession,
|
|
24
|
+
ResolutionSession,
|
|
25
|
+
} from './resolution-session';
|
|
26
|
+
import {
|
|
27
|
+
BoundValue,
|
|
28
|
+
getDeepProperty,
|
|
29
|
+
isPromiseLike,
|
|
30
|
+
ValueOrPromise,
|
|
31
|
+
} from './value-promise';
|
|
23
32
|
|
|
24
33
|
/**
|
|
25
34
|
* Polyfill Symbol.asyncIterator as required by TypeScript for Node 8.x.
|
|
@@ -29,10 +38,14 @@ if (!Symbol.asyncIterator) {
|
|
|
29
38
|
// tslint:disable-next-line:no-any
|
|
30
39
|
(Symbol as any).asyncIterator = Symbol.for('Symbol.asyncIterator');
|
|
31
40
|
}
|
|
32
|
-
|
|
41
|
+
/**
|
|
42
|
+
* WARNING: This following import must happen after the polyfill. The
|
|
43
|
+
* `auto-import` by an IDE such as VSCode may move the import before the
|
|
44
|
+
* polyfill. It must be then fixed manually.
|
|
45
|
+
*/
|
|
33
46
|
import {iterator, multiple} from 'p-event';
|
|
34
47
|
|
|
35
|
-
const debug =
|
|
48
|
+
const debug = debugFactory('loopback:context');
|
|
36
49
|
|
|
37
50
|
/**
|
|
38
51
|
* Context provides an implementation of Inversion of Control (IoC) container
|
|
@@ -509,7 +522,7 @@ export class Context extends EventEmitter {
|
|
|
509
522
|
find<ValueType = BoundValue>(
|
|
510
523
|
pattern?: string | RegExp | BindingFilter,
|
|
511
524
|
): Readonly<Binding<ValueType>>[] {
|
|
512
|
-
const bindings: Readonly<Binding
|
|
525
|
+
const bindings: Readonly<Binding<ValueType>>[] = [];
|
|
513
526
|
const filter = filterByKey(pattern);
|
|
514
527
|
|
|
515
528
|
for (const b of this.registry.values()) {
|
|
@@ -574,9 +587,14 @@ export class Context extends EventEmitter {
|
|
|
574
587
|
*
|
|
575
588
|
* @param keyWithPath The binding key, optionally suffixed with a path to the
|
|
576
589
|
* (deeply) nested property to retrieve.
|
|
590
|
+
* @param session Optional session for resolution (accepted for backward
|
|
591
|
+
* compatibility)
|
|
577
592
|
* @returns A promise of the bound value.
|
|
578
593
|
*/
|
|
579
|
-
get<ValueType>(
|
|
594
|
+
get<ValueType>(
|
|
595
|
+
keyWithPath: BindingAddress<ValueType>,
|
|
596
|
+
session?: ResolutionSession,
|
|
597
|
+
): Promise<ValueType>;
|
|
580
598
|
|
|
581
599
|
/**
|
|
582
600
|
* Get the value bound to the given key, optionally return a (deep) property
|
|
@@ -594,20 +612,19 @@ export class Context extends EventEmitter {
|
|
|
594
612
|
*
|
|
595
613
|
* @param keyWithPath The binding key, optionally suffixed with a path to the
|
|
596
614
|
* (deeply) nested property to retrieve.
|
|
597
|
-
* @param
|
|
598
|
-
* `ResolutionSession` is accepted for backward compatibility.
|
|
615
|
+
* @param options Options for resolution.
|
|
599
616
|
* @returns A promise of the bound value, or a promise of undefined when
|
|
600
617
|
* the optional binding is not found.
|
|
601
618
|
*/
|
|
602
619
|
get<ValueType>(
|
|
603
620
|
keyWithPath: BindingAddress<ValueType>,
|
|
604
|
-
|
|
621
|
+
options: ResolutionOptions,
|
|
605
622
|
): Promise<ValueType | undefined>;
|
|
606
623
|
|
|
607
624
|
// Implementation
|
|
608
625
|
async get<ValueType>(
|
|
609
626
|
keyWithPath: BindingAddress<ValueType>,
|
|
610
|
-
optionsOrSession?:
|
|
627
|
+
optionsOrSession?: ResolutionOptionsOrSession,
|
|
611
628
|
): Promise<ValueType | undefined> {
|
|
612
629
|
this._debug('Resolving binding: %s', keyWithPath);
|
|
613
630
|
return await this.getValueOrPromise<ValueType | undefined>(
|
|
@@ -636,11 +653,13 @@ export class Context extends EventEmitter {
|
|
|
636
653
|
*
|
|
637
654
|
* @param keyWithPath The binding key, optionally suffixed with a path to the
|
|
638
655
|
* (deeply) nested property to retrieve.
|
|
639
|
-
*
|
|
640
|
-
* `ResolutionSession` is accepted for backward compatibility.
|
|
656
|
+
* @param session Session for resolution (accepted for backward compatibility)
|
|
641
657
|
* @returns A promise of the bound value.
|
|
642
658
|
*/
|
|
643
|
-
getSync<ValueType>(
|
|
659
|
+
getSync<ValueType>(
|
|
660
|
+
keyWithPath: BindingAddress<ValueType>,
|
|
661
|
+
session?: ResolutionSession,
|
|
662
|
+
): ValueType;
|
|
644
663
|
|
|
645
664
|
/**
|
|
646
665
|
* Get the synchronous value bound to the given key, optionally
|
|
@@ -662,19 +681,18 @@ export class Context extends EventEmitter {
|
|
|
662
681
|
*
|
|
663
682
|
* @param keyWithPath The binding key, optionally suffixed with a path to the
|
|
664
683
|
* (deeply) nested property to retrieve.
|
|
665
|
-
*
|
|
666
|
-
* `ResolutionSession` is accepted for backward compatibility.
|
|
684
|
+
* @param options Options for resolution.
|
|
667
685
|
* @returns The bound value, or undefined when an optional binding is not found.
|
|
668
686
|
*/
|
|
669
687
|
getSync<ValueType>(
|
|
670
688
|
keyWithPath: BindingAddress<ValueType>,
|
|
671
|
-
|
|
689
|
+
options?: ResolutionOptions,
|
|
672
690
|
): ValueType | undefined;
|
|
673
691
|
|
|
674
692
|
// Implementation
|
|
675
693
|
getSync<ValueType>(
|
|
676
694
|
keyWithPath: BindingAddress<ValueType>,
|
|
677
|
-
optionsOrSession?:
|
|
695
|
+
optionsOrSession?: ResolutionOptionsOrSession,
|
|
678
696
|
): ValueType | undefined {
|
|
679
697
|
this._debug('Resolving binding synchronously: %s', keyWithPath);
|
|
680
698
|
|
|
@@ -737,6 +755,30 @@ export class Context extends EventEmitter {
|
|
|
737
755
|
);
|
|
738
756
|
}
|
|
739
757
|
|
|
758
|
+
/**
|
|
759
|
+
* Find or create a binding for the given key
|
|
760
|
+
* @param key Binding address
|
|
761
|
+
* @param policy Binding creation policy
|
|
762
|
+
*/
|
|
763
|
+
findOrCreateBinding<T>(
|
|
764
|
+
key: BindingAddress<T>,
|
|
765
|
+
policy?: BindingCreationPolicy,
|
|
766
|
+
) {
|
|
767
|
+
let binding: Binding<T>;
|
|
768
|
+
if (policy === BindingCreationPolicy.ALWAYS_CREATE) {
|
|
769
|
+
binding = this.bind(key);
|
|
770
|
+
} else if (policy === BindingCreationPolicy.NEVER_CREATE) {
|
|
771
|
+
binding = this.getBinding(key);
|
|
772
|
+
} else if (this.isBound(key)) {
|
|
773
|
+
// CREATE_IF_NOT_BOUND - the key is bound
|
|
774
|
+
binding = this.getBinding(key);
|
|
775
|
+
} else {
|
|
776
|
+
// CREATE_IF_NOT_BOUND - the key is not bound
|
|
777
|
+
binding = this.bind(key);
|
|
778
|
+
}
|
|
779
|
+
return binding;
|
|
780
|
+
}
|
|
781
|
+
|
|
740
782
|
/**
|
|
741
783
|
* Get the value bound to the given key.
|
|
742
784
|
*
|
|
@@ -766,22 +808,16 @@ export class Context extends EventEmitter {
|
|
|
766
808
|
*/
|
|
767
809
|
getValueOrPromise<ValueType>(
|
|
768
810
|
keyWithPath: BindingAddress<ValueType>,
|
|
769
|
-
optionsOrSession?:
|
|
811
|
+
optionsOrSession?: ResolutionOptionsOrSession,
|
|
770
812
|
): ValueOrPromise<ValueType | undefined> {
|
|
771
813
|
const {key, propertyPath} = BindingKey.parseKeyWithPath(keyWithPath);
|
|
772
814
|
|
|
773
|
-
|
|
774
|
-
if (optionsOrSession instanceof ResolutionSession) {
|
|
775
|
-
optionsOrSession = {session: optionsOrSession};
|
|
776
|
-
}
|
|
815
|
+
optionsOrSession = asResolutionOptions(optionsOrSession);
|
|
777
816
|
|
|
778
817
|
const binding = this.getBinding<ValueType>(key, optionsOrSession);
|
|
779
818
|
if (binding == null) return undefined;
|
|
780
819
|
|
|
781
|
-
const boundValue = binding.getValue(
|
|
782
|
-
this,
|
|
783
|
-
optionsOrSession && optionsOrSession.session,
|
|
784
|
-
);
|
|
820
|
+
const boundValue = binding.getValue(this, optionsOrSession);
|
|
785
821
|
if (propertyPath === undefined || propertyPath === '') {
|
|
786
822
|
return boundValue;
|
|
787
823
|
}
|
|
@@ -825,3 +861,23 @@ class ContextSubscription implements Subscription {
|
|
|
825
861
|
return this._closed;
|
|
826
862
|
}
|
|
827
863
|
}
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* Policy to control if a binding should be created for the context
|
|
867
|
+
*/
|
|
868
|
+
export enum BindingCreationPolicy {
|
|
869
|
+
/**
|
|
870
|
+
* Always create a binding with the key for the context
|
|
871
|
+
*/
|
|
872
|
+
ALWAYS_CREATE = 'Always',
|
|
873
|
+
/**
|
|
874
|
+
* Never create a binding for the context. If the key is not bound in the
|
|
875
|
+
* context, throw an error.
|
|
876
|
+
*/
|
|
877
|
+
NEVER_CREATE = 'Never',
|
|
878
|
+
/**
|
|
879
|
+
* Create a binding if the key is not bound in the context. Otherwise, return
|
|
880
|
+
* the existing binding.
|
|
881
|
+
*/
|
|
882
|
+
CREATE_IF_NOT_BOUND = 'IfNotBound',
|
|
883
|
+
}
|