From b7ceb86a0706116b5566f002d863976344ab6d30 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 30 Aug 2023 17:48:33 +0200 Subject: [PATCH] backend-app-api: minor ServiceRegistry.create refactor Signed-off-by: Patrik Oldsberg --- .../src/wiring/ServiceRegistry.ts | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/backend-app-api/src/wiring/ServiceRegistry.ts b/packages/backend-app-api/src/wiring/ServiceRegistry.ts index c89916974b..d009a319ba 100644 --- a/packages/backend-app-api/src/wiring/ServiceRegistry.ts +++ b/packages/backend-app-api/src/wiring/ServiceRegistry.ts @@ -58,9 +58,9 @@ const pluginMetadataServiceFactory = createServiceFactory( ); export class ServiceRegistry implements EnumerableServiceHolder { - static create(factories: Array): EnumerableServiceHolder { + static create(factories: Array): ServiceRegistry { const registry = new ServiceRegistry(factories); - registry.checkForCircularDeps(); + registry.#checkForCircularDeps(); return registry; } @@ -80,7 +80,6 @@ export class ServiceRegistry implements EnumerableServiceHolder { InternalServiceFactory, Promise >(); - readonly #dependencyGraph: DependencyGraph; private constructor(factories: Array) { this.#providedFactories = new Map( @@ -88,15 +87,6 @@ export class ServiceRegistry implements EnumerableServiceHolder { ); this.#loadedDefaultFactories = new Map(); this.#implementations = new Map(); - this.#dependencyGraph = DependencyGraph.fromIterable( - Array.from(this.#providedFactories).map( - ([serviceId, serviceFactory]) => ({ - value: serviceId, - provides: [serviceId], - consumes: Object.values(serviceFactory.deps).map(d => d.id), - }), - ), - ); } #resolveFactory( @@ -163,10 +153,17 @@ export class ServiceRegistry implements EnumerableServiceHolder { } } - checkForCircularDeps(): void { - const circularDependencies = Array.from( - this.#dependencyGraph.detectCircularDependencies(), + #checkForCircularDeps(): void { + const graph = DependencyGraph.fromIterable( + Array.from(this.#providedFactories).map( + ([serviceId, serviceFactory]) => ({ + value: serviceId, + provides: [serviceId], + consumes: Object.values(serviceFactory.deps).map(d => d.id), + }), + ), ); + const circularDependencies = Array.from(graph.detectCircularDependencies()); if (circularDependencies.length) { const cycles = circularDependencies