|
|
|
|
@@ -1,11 +1,11 @@
|
|
|
|
|
import type IDependencyProvider from "./IDependencyProvider";
|
|
|
|
|
import type IDisposable from "../lang/IDisposable";
|
|
|
|
|
import Disposable from "../lang/Disposable";
|
|
|
|
|
import Type from "./Type";
|
|
|
|
|
import type IDependencyManager from "./IDependencyManager";
|
|
|
|
|
import type IDependencyProvider from './IDependencyProvider';
|
|
|
|
|
import type IDisposable from '../lang/IDisposable';
|
|
|
|
|
import Disposable from '../lang/Disposable';
|
|
|
|
|
import Type from './Type';
|
|
|
|
|
import type IDependencyManager from './IDependencyManager';
|
|
|
|
|
|
|
|
|
|
type Constructor<T = unknown> = new (...args: unknown[]) => T;
|
|
|
|
|
type ModuleLoader = (modulePath: string) => Promise<Constructor | { default: Constructor }>;
|
|
|
|
|
type ModuleLoader = (modulePath: string) => Promise<Constructor | {default: Constructor}>;
|
|
|
|
|
|
|
|
|
|
export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
private readonly _moduleLoader: ModuleLoader;
|
|
|
|
|
@@ -16,8 +16,8 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
private readonly _pending: Type[];
|
|
|
|
|
|
|
|
|
|
constructor(moduleLoader: ModuleLoader) {
|
|
|
|
|
if (typeof moduleLoader !== "function") {
|
|
|
|
|
throw new Error("Module loader must be a function");
|
|
|
|
|
if (typeof moduleLoader !== 'function') {
|
|
|
|
|
throw new Error('Module loader must be a function');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._moduleLoader = moduleLoader;
|
|
|
|
|
@@ -30,25 +30,23 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
|
|
|
|
|
public defineDependencies(type: Type, dependencies: Type[]): IDisposable {
|
|
|
|
|
if (!(type instanceof Type)) {
|
|
|
|
|
throw new Error("Type must be a Type");
|
|
|
|
|
throw new Error('Type must be a Type');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(dependencies)) {
|
|
|
|
|
throw new Error("Dependencies must be an array");
|
|
|
|
|
throw new Error('Dependencies must be an array');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const dependency of dependencies) {
|
|
|
|
|
if (!(dependency instanceof Type)) {
|
|
|
|
|
throw new Error("Dependency must be a Type");
|
|
|
|
|
throw new Error('Dependency must be a Type');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const key = type.toURN();
|
|
|
|
|
|
|
|
|
|
if (this._injections.has(key)) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Dependencies already defined for type [${type.toString()}]`,
|
|
|
|
|
);
|
|
|
|
|
throw new Error(`Dependencies already defined for type [${type.toString()}]`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._injections.set(key, dependencies);
|
|
|
|
|
@@ -61,7 +59,7 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
|
|
|
|
|
public getDependencies(type: Type): Type[] | undefined {
|
|
|
|
|
if (!(type instanceof Type)) {
|
|
|
|
|
throw new Error("Type must be an instance of Type");
|
|
|
|
|
throw new Error('Type must be an instance of Type');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._injections.get(type.toURN());
|
|
|
|
|
@@ -69,7 +67,7 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
|
|
|
|
|
public addProvider(provider: IDependencyProvider): IDisposable {
|
|
|
|
|
if (!this.isProvider(provider)) {
|
|
|
|
|
throw new Error("Provider must implement IDependencyProvider");
|
|
|
|
|
throw new Error('Provider must implement IDependencyProvider');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._providers.push(provider);
|
|
|
|
|
@@ -83,7 +81,22 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async resolveProvider(type: Type): Promise<IDependencyProvider> {
|
|
|
|
|
return this.resolve(type);
|
|
|
|
|
if (!(type instanceof Type)) {
|
|
|
|
|
throw new Error('Type must be an instance of Type');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const candidates = this._providers.filter(provider => provider.canProvide(type));
|
|
|
|
|
|
|
|
|
|
if (candidates.length === 0) {
|
|
|
|
|
throw new Error(`No provider for [${type.toString()}]`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (candidates.length > 1) {
|
|
|
|
|
const candidateNames = candidates.map(c => c.toString()).join(', ');
|
|
|
|
|
throw new Error(`Multiple providers for ${type}: ${candidateNames}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return candidates[0] as IDependencyProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async instantiateType(type: Type): Promise<unknown> {
|
|
|
|
|
@@ -95,10 +108,8 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
|
|
|
|
|
// Check for circular dependencies
|
|
|
|
|
if (this._pending.includes(type)) {
|
|
|
|
|
const pendingChain = this._pending.map((t) => t.toString()).join(' -> ');
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Failed to resolve ${type} due to circular dependency: ${pendingChain}`
|
|
|
|
|
);
|
|
|
|
|
const pendingChain = this._pending.map(t => t.toString()).join(' -> ');
|
|
|
|
|
throw new Error(`Failed to resolve ${type} due to circular dependency: ${pendingChain}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._pending.push(type);
|
|
|
|
|
@@ -109,7 +120,7 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
|
|
|
|
|
// Resolve all dependencies
|
|
|
|
|
const resolvedDependencies = await Promise.all(
|
|
|
|
|
dependencies.map(async (dependency) => {
|
|
|
|
|
dependencies.map(async dependency => {
|
|
|
|
|
const provider = await this.resolveProvider(dependency);
|
|
|
|
|
let instance = await provider.provide(dependency);
|
|
|
|
|
|
|
|
|
|
@@ -150,7 +161,7 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
|
|
|
|
|
public addEagerType(type: Type): void {
|
|
|
|
|
if (!(type instanceof Type)) {
|
|
|
|
|
throw new Error("Type must be an instance of Type");
|
|
|
|
|
throw new Error('Type must be an instance of Type');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._eagerTypes.push(type);
|
|
|
|
|
@@ -167,35 +178,14 @@ export default class DependencyManager implements IDependencyManager {
|
|
|
|
|
private isProvider(obj: unknown): obj is IDependencyProvider {
|
|
|
|
|
return (
|
|
|
|
|
obj !== null &&
|
|
|
|
|
typeof obj === "object" &&
|
|
|
|
|
"canProvide" in obj &&
|
|
|
|
|
"provide" in obj &&
|
|
|
|
|
typeof (obj as IDependencyProvider).canProvide === "function" &&
|
|
|
|
|
typeof (obj as IDependencyProvider).provide === "function"
|
|
|
|
|
typeof obj === 'object' &&
|
|
|
|
|
'canProvide' in obj &&
|
|
|
|
|
'provide' in obj &&
|
|
|
|
|
typeof (obj as IDependencyProvider).canProvide === 'function' &&
|
|
|
|
|
typeof (obj as IDependencyProvider).provide === 'function'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private resolve(type: Type): IDependencyProvider {
|
|
|
|
|
if (!(type instanceof Type)) {
|
|
|
|
|
throw new Error("Type must be an instance of Type");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const candidates = this._providers.filter((provider) =>
|
|
|
|
|
provider.canProvide(type),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (candidates.length === 0) {
|
|
|
|
|
throw new Error(`No provider for [${type.toString()}]`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (candidates.length > 1) {
|
|
|
|
|
const candidateNames = candidates.map((c) => c.toString()).join(", ");
|
|
|
|
|
throw new Error(`Multiple providers for ${type}: ${candidateNames}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return candidates[0] as IDependencyProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private injectDependencies(Constructor: Constructor, dependencies: unknown[]): unknown {
|
|
|
|
|
if (dependencies.length === 0) {
|
|
|
|
|
return new Constructor();
|
|
|
|
|
|