Skip to content

Commit

Permalink
Add inContext options for kernel resolution to support child containers
Browse files Browse the repository at this point in the history
  • Loading branch information
hisorange committed Dec 3, 2023
1 parent 81909c5 commit 7880162
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"lodash.debounce": "^4.0.8",
"node-schedule": "^2.1.0",
"p-timeout": "^6.0.0",
"pino": "^8.14.1"
"pino": "^8.16.2"
}
}
}
3 changes: 3 additions & 0 deletions src/decorator/inject.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type InjectDecorator = (
| undefined,
) => void;

/**
* Inject a dependency by the name or binding key
*/
export const Inject: InjectDecorator = function (
bindingSelector: BindingSelector | Constructor<object>,
metadata?: InjectionMetadata,
Expand Down
35 changes: 24 additions & 11 deletions src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,24 +380,30 @@ export class Kernel implements IKernel {
/**
* @inheritdoc
*/
replace(key: BindingAddress | Constructor<object>, value: any): void {
replace(
key: BindingAddress | Constructor<object>,
value: any,
inContext?: IContext,
): void {
// Binding key can be a class and we use the class's name to resolve it.
if (typeof key === 'function') {
if (key?.name) {
key = key.name;
}
}

if (!inContext) inContext = this.context;

key = key as BindingAddress;

if (!this.context.contains(key)) {
if (!inContext.contains(key)) {
throw new Exception(`Binding [${key}] is not registered`);
}

const binding = this.context.getBinding(key);
const binding = inContext.getBinding(key);

// Clear the cached resolution.
binding.refresh(this.context);
binding.refresh(inContext);

switch (binding.type) {
case BindingType.CONSTANT:
Expand All @@ -420,7 +426,10 @@ export class Kernel implements IKernel {
/**
* @inheritdoc
*/
async get<T>(key: BindingAddress<T> | Constructor<object>): Promise<T> {
async get<T>(
key: BindingAddress<T> | Constructor<object>,
inContext?: IContext,
): Promise<T> {
// Binding key can be a class and we use the class's name to resolve it.
if (typeof key === 'function') {
if (key?.name) {
Expand All @@ -430,16 +439,20 @@ export class Kernel implements IKernel {

key = key as BindingAddress;

return this.context.get<T>(key);
return (inContext ?? this.context).get<T>(key);
}

/**
* @inheritdoc
*/
create<T>(concrete: Constructor<T>, params?: any[]): ValueOrPromise<T> {
create<T>(
concrete: Constructor<T>,
params?: any[],
inContext?: IContext,
): ValueOrPromise<T> {
return instantiateClass(
concrete as Constructor<object>,
this.context,
inContext ?? this.context,
undefined,
params,
) as ValueOrPromise<T>;
Expand All @@ -448,11 +461,11 @@ export class Kernel implements IKernel {
/**
* @inheritdoc
*/
panic(summary: string, exitCode: ExitCode, context?: unknown): void {
panic(summary: string, exitCode: ExitCode, errorContext?: unknown): void {
console.error(chalk.red('Kernel panicked with message:'), summary);

if (context) {
console.error(chalk.red('Captured error context:'), context);
if (errorContext) {
console.error(chalk.red('Captured error context:'), errorContext);
}

// Kill the process.
Expand Down
2 changes: 1 addition & 1 deletion src/types/logger.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from 'pino';
import type { Logger } from 'pino';

// This interface is here to avoid import collision with the @Logger decorator.
export type ILogger = Logger;
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2220,10 +2220,10 @@ picomatch@^2.0.4, picomatch@^2.2.3, picomatch@^2.3.1:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==

pino-abstract-transport@v1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/pino-abstract-transport/-/pino-abstract-transport-1.0.0.tgz#cc0d6955fffcadb91b7b49ef220a6cc111d48bb3"
integrity sha512-c7vo5OpW4wIS42hUVcT5REsL8ZljsUfBjqV/e2sFxmFEFZiq1XLUp5EYLtuDH6PEHq9W1egWqRbnLUP5FuZmOA==
pino-abstract-transport@v1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pino-abstract-transport/-/pino-abstract-transport-1.1.0.tgz#083d98f966262164504afb989bccd05f665937a8"
integrity sha512-lsleG3/2a/JIWUtf9Q5gUNErBqwIu1tUKTT3dUzaf5DySw9ra1wcqKjJjLX1VTY64Wk1eEOYsVGSaGfCK85ekA==
dependencies:
readable-stream "^4.0.0"
split2 "^4.0.0"
Expand All @@ -2233,21 +2233,21 @@ pino-std-serializers@^6.0.0:
resolved "https://registry.yarnpkg.com/pino-std-serializers/-/pino-std-serializers-6.2.2.tgz#d9a9b5f2b9a402486a5fc4db0a737570a860aab3"
integrity sha512-cHjPPsE+vhj/tnhCy/wiMh3M3z3h/j15zHQX+S9GkTBgqJuTuJzYJ4gUyACLhDaJ7kk9ba9iRDmbH2tJU03OiA==

pino@^8.14.1:
version "8.14.1"
resolved "https://registry.yarnpkg.com/pino/-/pino-8.14.1.tgz#bb38dcda8b500dd90c1193b6c9171eb777a47ac8"
integrity sha512-8LYNv7BKWXSfS+k6oEc6occy5La+q2sPwU3q2ljTX5AZk7v+5kND2o5W794FyRaqha6DJajmkNRsWtPpFyMUdw==
pino@^8.16.2:
version "8.16.2"
resolved "https://registry.yarnpkg.com/pino/-/pino-8.16.2.tgz#7a906f2d9a8c5b4c57412c9ca95d6820bd2090cd"
integrity sha512-2advCDGVEvkKu9TTVSa/kWW7Z3htI/sBKEZpqiHk6ive0i/7f5b1rsU8jn0aimxqfnSz5bj/nOYkwhBUn5xxvg==
dependencies:
atomic-sleep "^1.0.0"
fast-redact "^3.1.1"
on-exit-leak-free "^2.1.0"
pino-abstract-transport v1.0.0
pino-abstract-transport v1.1.0
pino-std-serializers "^6.0.0"
process-warning "^2.0.0"
quick-format-unescaped "^4.0.3"
real-require "^0.2.0"
safe-stable-stringify "^2.3.1"
sonic-boom "^3.1.0"
sonic-boom "^3.7.0"
thread-stream "^2.0.0"

pirates@^4.0.4:
Expand Down Expand Up @@ -2412,10 +2412,10 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==

sonic-boom@^3.1.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-3.3.0.tgz#cffab6dafee3b2bcb88d08d589394198bee1838c"
integrity sha512-LYxp34KlZ1a2Jb8ZQgFCK3niIHzibdwtwNUWKg0qQRzsDoJ3Gfgkf8KdBTFU3SkejDEIlWwnSnpVdOZIhFMl/g==
sonic-boom@^3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-3.7.0.tgz#b4b7b8049a912986f4a92c51d4660b721b11f2f2"
integrity sha512-IudtNvSqA/ObjN97tfgNmOKyDOs4dNcg4cUUsHDebqsgb8wGBBwb31LIgShNO8fye0dFI52X1+tFoKKI6Rq1Gg==
dependencies:
atomic-sleep "^1.0.0"

Expand Down

0 comments on commit 7880162

Please sign in to comment.