From 5ef764830708afc0d52ed448478caad45165798f Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 17 Aug 2022 18:12:26 +0200 Subject: [PATCH] backend-app-api: implement handling of default factories is ServiceRegistry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .../src/wiring/ServiceRegistry.test.ts | 75 ++++++++++++++++++- .../src/wiring/ServiceRegistry.ts | 30 +++++--- 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/packages/backend-app-api/src/wiring/ServiceRegistry.test.ts b/packages/backend-app-api/src/wiring/ServiceRegistry.test.ts index 438a11130f..acdd2a4c41 100644 --- a/packages/backend-app-api/src/wiring/ServiceRegistry.test.ts +++ b/packages/backend-app-api/src/wiring/ServiceRegistry.test.ts @@ -26,7 +26,7 @@ const ref1 = createServiceRef<{ x: number; pluginId: string }>({ const sf1 = createServiceFactory({ service: ref1, deps: {}, - factory: async ({}) => { + factory: async () => { return async pluginId => { return { x: 1, pluginId }; }; @@ -39,7 +39,7 @@ const ref2 = createServiceRef<{ x: number; pluginId: string }>({ const sf2 = createServiceFactory({ service: ref2, deps: {}, - factory: async ({}) => { + factory: async () => { return async pluginId => { return { x: 2, pluginId }; }; @@ -48,13 +48,43 @@ const sf2 = createServiceFactory({ const sf2b = createServiceFactory({ service: ref2, deps: {}, - factory: async ({}) => { + factory: async () => { return async pluginId => { return { x: 22, pluginId }; }; }, }); +const refDefault1 = createServiceRef<{ x: number; pluginId: string }>({ + id: '1', + defaultFactory: async service => + createServiceFactory({ + service, + deps: {}, + factory: async () => async pluginId => ({ x: 10, pluginId }), + }), +}); + +const refDefault2a = createServiceRef<{ x: number; pluginId: string }>({ + id: '2a', + defaultFactory: async service => + createServiceFactory({ + service, + deps: {}, + factory: async () => async pluginId => ({ x: 20, pluginId }), + }), +}); + +const refDefault2b = createServiceRef<{ x: number; pluginId: string }>({ + id: '2b', + defaultFactory: async service => + createServiceFactory({ + service, + deps: {}, + factory: async () => async pluginId => ({ x: 220, pluginId }), + }), +}); + describe('ServiceRegistry', () => { it('should return undefined if there is no factory defined', async () => { const registry = new ServiceRegistry([]); @@ -101,4 +131,43 @@ describe('ServiceRegistry', () => { pluginId: 'catalog', }); }); + + it('should return the defaultFactory from the ref if not provided to the registry', async () => { + const registry = new ServiceRegistry([]); + const factory = registry.get(refDefault1)!; + expect(factory).toEqual(expect.any(Function)); + await expect(factory('catalog')).resolves.toEqual({ + x: 10, + pluginId: 'catalog', + }); + }); + + it('should not return the defaultFactory from the ref if provided to the registry', async () => { + const registry = new ServiceRegistry([sf1]); + const factory = registry.get(refDefault1)!; + expect(factory).toEqual(expect.any(Function)); + await expect(factory('catalog')).resolves.toEqual({ + x: 1, + pluginId: 'catalog', + }); + }); + + it('should handle duplicate defaultFactories by duplicating the implementations', async () => { + const registry = new ServiceRegistry([]); + const factoryA = registry.get(refDefault2a)!; + const factoryB = registry.get(refDefault2b)!; + expect(factoryA).toEqual(expect.any(Function)); + expect(factoryB).toEqual(expect.any(Function)); + await expect(factoryA('catalog')).resolves.toEqual({ + x: 20, + pluginId: 'catalog', + }); + await expect(factoryB('catalog')).resolves.toEqual({ + x: 220, + pluginId: 'catalog', + }); + expect(await factoryA('catalog')).toBe(await factoryA('catalog')); + expect(await factoryB('catalog')).toBe(await factoryB('catalog')); + expect(await factoryA('catalog')).not.toBe(await factoryB('catalog')); + }); }); diff --git a/packages/backend-app-api/src/wiring/ServiceRegistry.ts b/packages/backend-app-api/src/wiring/ServiceRegistry.ts index bdaecf30c6..b471cd67ca 100644 --- a/packages/backend-app-api/src/wiring/ServiceRegistry.ts +++ b/packages/backend-app-api/src/wiring/ServiceRegistry.ts @@ -14,35 +14,47 @@ * limitations under the License. */ import { - AnyServiceFactory, + ServiceFactory, FactoryFunc, ServiceRef, } from '@backstage/backend-plugin-api'; export class ServiceRegistry { - readonly #implementations: Map>; - readonly #factories: Map; + readonly #providedFactories: Map; + readonly #loadedDefaultFactories: Map; + readonly #implementations: Map>; - constructor(factories: AnyServiceFactory[]) { - this.#factories = new Map(factories.map(f => [f.service.id, f])); + constructor(factories: ServiceFactory[]) { + this.#providedFactories = new Map(factories.map(f => [f.service.id, f])); + this.#loadedDefaultFactories = new Map(); this.#implementations = new Map(); } get(ref: ServiceRef): FactoryFunc | undefined { - const factory = this.#factories.get(ref.id); - if (!factory) { + let factory = this.#providedFactories.get(ref.id); + const { defaultFactory } = ref; + if (!factory && !defaultFactory) { return undefined; } return async (pluginId: string): Promise => { - let implementations = this.#implementations.get(ref.id); + if (!factory) { + let loadedFactory = this.#loadedDefaultFactories.get(defaultFactory!); + if (!loadedFactory) { + loadedFactory = (await defaultFactory!(ref)) as ServiceFactory; + this.#loadedDefaultFactories.set(defaultFactory!, loadedFactory); + } + factory = loadedFactory; + } + + let implementations = this.#implementations.get(factory); if (implementations) { if (implementations.has(pluginId)) { return implementations.get(pluginId) as T; } } else { implementations = new Map(); - this.#implementations.set(ref.id, implementations); + this.#implementations.set(factory, implementations); } const factoryDeps = Object.fromEntries(