-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make inversify optional * Remove reflect-metadata imports
- Loading branch information
Showing
104 changed files
with
725 additions
and
596 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { DIIdentifier } from "./internal"; | ||
|
||
export abstract class DIContainer { | ||
abstract registerFactory<T>( | ||
key: DIIdentifier<T>, | ||
factory: (container: DIContainer) => T | ||
): void; | ||
|
||
abstract registerNamedFactory<T>( | ||
key: DIIdentifier<T>, | ||
factory: (container: DIContainer) => T, | ||
name: string | ||
): void; | ||
|
||
abstract registerInstance<T>(key: DIIdentifier<T>, instance: T): void; | ||
|
||
abstract unregister<T>(key: DIIdentifier<T>): void; | ||
|
||
abstract resolve<T>(key: DIIdentifier<T>): T; | ||
|
||
abstract resolveNamed<T>(key: DIIdentifier<T>, name: string): T; | ||
|
||
abstract resolveAll<T>(key: DIIdentifier<T>): T[]; | ||
|
||
abstract createChild(): DIContainer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type Constructor<T> = new (...args: any[]) => T; | ||
export interface Abstract<T> { | ||
prototype: T; | ||
} | ||
|
||
export type DIIdentifier<T> = symbol | Constructor<T> | Abstract<T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
import "reflect-metadata"; | ||
|
||
import { Container, decorate, injectable, METADATA_KEY } from "inversify"; | ||
|
||
import { DIContainer } from "../DIContainer"; | ||
import { Abstract, Constructor, DIIdentifier } from "../internal"; | ||
|
||
export class InversifyWrapper extends DIContainer { | ||
constructor(private _container: Container) { | ||
super(); | ||
} | ||
|
||
public static create(): DIContainer { | ||
return new InversifyWrapper(new Container()); | ||
} | ||
|
||
private needsDecoration<T>( | ||
key: DIIdentifier<T> | ||
): key is Constructor<T> | Abstract<T> { | ||
if (typeof key === "symbol") return false; | ||
return Reflect.hasOwnMetadata(METADATA_KEY.PARAM_TYPES, key); | ||
} | ||
|
||
public override registerFactory<T>( | ||
key: DIIdentifier<T>, | ||
factory: (container: DIContainer) => T | ||
): void { | ||
if (this.needsDecoration(key)) { | ||
decorate(injectable(), key); | ||
} | ||
|
||
this._container | ||
.bind<T>(key) | ||
.toDynamicValue(() => factory(this)) | ||
.inSingletonScope(); | ||
} | ||
|
||
public override registerNamedFactory<T>( | ||
key: DIIdentifier<T>, | ||
factory: (container: DIContainer) => T, | ||
name: string | ||
): void { | ||
if (this.needsDecoration(key)) { | ||
decorate(injectable(), key); | ||
} | ||
|
||
this._container | ||
.bind<T>(key) | ||
.toDynamicValue(() => factory(this)) | ||
.whenTargetNamed(name); | ||
} | ||
|
||
public override registerInstance<T>(key: DIIdentifier<T>, instance: T): void { | ||
this._container.bind<T>(key).toConstantValue(instance); | ||
} | ||
|
||
public override unregister<T>(key: DIIdentifier<T>): void { | ||
this._container.unbind(key); | ||
} | ||
|
||
public override resolve<T>(key: DIIdentifier<T>): T { | ||
return this._container.get<T>(key); | ||
} | ||
|
||
public override resolveNamed<T>(key: DIIdentifier<T>, name: string): T { | ||
return this._container.getNamed<T>(key, name); | ||
} | ||
|
||
public override resolveAll<T>(key: DIIdentifier<T>): T[] { | ||
return this._container.getAll<T>(key); | ||
} | ||
|
||
public override createChild(): DIContainer { | ||
return new InversifyWrapper(this._container.createChild()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
export * from "./InversifyWrapper"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.