backend-next: widen accepted ServiceFactory types to include meta method
Co-authored-by: Fredrik Adelöw <freben@gmail.com> Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -26,7 +26,9 @@ import { stringifyError } from '@backstage/errors';
|
||||
* @internal
|
||||
*/
|
||||
export type InternalServiceRef<T> = ServiceRef<T> & {
|
||||
__defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
|
||||
__defaultFactory?: (
|
||||
service: ServiceRef<T>,
|
||||
) => Promise<ServiceFactory<T> | (() => ServiceFactory<T>)>;
|
||||
};
|
||||
|
||||
export class ServiceRegistry {
|
||||
@@ -40,8 +42,18 @@ export class ServiceRegistry {
|
||||
}
|
||||
>;
|
||||
|
||||
constructor(factories: ServiceFactory<any>[]) {
|
||||
this.#providedFactories = new Map(factories.map(f => [f.service.id, f]));
|
||||
constructor(
|
||||
factories: Array<ServiceFactory<unknown> | (() => ServiceFactory<unknown>)>,
|
||||
) {
|
||||
this.#providedFactories = new Map(
|
||||
factories.map(f => {
|
||||
if (typeof f === 'function') {
|
||||
const cf = f();
|
||||
return [cf.service.id, cf];
|
||||
}
|
||||
return [f.service.id, f];
|
||||
}),
|
||||
);
|
||||
this.#loadedDefaultFactories = new Map();
|
||||
this.#implementations = new Map();
|
||||
}
|
||||
@@ -57,9 +69,11 @@ export class ServiceRegistry {
|
||||
if (!factory) {
|
||||
let loadedFactory = this.#loadedDefaultFactories.get(defaultFactory!);
|
||||
if (!loadedFactory) {
|
||||
loadedFactory = Promise.resolve().then(
|
||||
() => defaultFactory!(ref) as Promise<ServiceFactory>,
|
||||
);
|
||||
loadedFactory = Promise.resolve()
|
||||
.then(() => defaultFactory!(ref))
|
||||
.then(f =>
|
||||
typeof f === 'function' ? f() : f,
|
||||
) as Promise<ServiceFactory>;
|
||||
this.#loadedDefaultFactories.set(defaultFactory!, loadedFactory);
|
||||
}
|
||||
// NOTE: This await is safe as long as #providedFactories is not mutated.
|
||||
|
||||
@@ -43,7 +43,7 @@ export interface BackendRegisterInit {
|
||||
* @public
|
||||
*/
|
||||
export interface CreateSpecializedBackendOptions {
|
||||
services: ServiceFactory[];
|
||||
services: (ServiceFactory | (() => ServiceFactory))[];
|
||||
}
|
||||
|
||||
export type ServiceHolder = {
|
||||
@@ -56,7 +56,9 @@ export type ServiceHolder = {
|
||||
export function createSpecializedBackend(
|
||||
options: CreateSpecializedBackendOptions,
|
||||
): Backend {
|
||||
return new BackstageBackend(options.services);
|
||||
return new BackstageBackend(
|
||||
options.services.map(s => (typeof s === 'function' ? s() : s)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,24 +47,14 @@ export const defaultServiceFactories = [
|
||||
* @public
|
||||
*/
|
||||
export interface CreateBackendOptions {
|
||||
services?: ServiceFactory[];
|
||||
services?: (ServiceFactory | (() => ServiceFactory))[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export function createBackend(options?: CreateBackendOptions): Backend {
|
||||
const services = new Map<string, ServiceFactory>(
|
||||
defaultServiceFactories.map(sf => [sf.service.id, sf as ServiceFactory]),
|
||||
);
|
||||
|
||||
if (options?.services) {
|
||||
for (const sf of options.services) {
|
||||
services.set(sf.service.id, sf);
|
||||
}
|
||||
}
|
||||
|
||||
return createSpecializedBackend({
|
||||
services: Array.from(services.values()),
|
||||
services: [...defaultServiceFactories, ...(options?.services ?? [])],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -41,7 +41,9 @@ export type InternalServiceRef<T> = ServiceRef<T> & {
|
||||
* The default factory that will be used to create service
|
||||
* instances if no other factory is provided.
|
||||
*/
|
||||
__defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
|
||||
__defaultFactory?: (
|
||||
service: ServiceRef<T>,
|
||||
) => Promise<ServiceFactory<T> | (() => ServiceFactory<T>)>;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
@@ -67,7 +69,9 @@ export type ServiceFactory<TService = unknown> = {
|
||||
*/
|
||||
export function createServiceRef<T>(options: {
|
||||
id: string;
|
||||
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
|
||||
defaultFactory?: (
|
||||
service: ServiceRef<T>,
|
||||
) => Promise<ServiceFactory<T> | (() => ServiceFactory<T>)>;
|
||||
}): ServiceRef<T> {
|
||||
const { id, defaultFactory } = options;
|
||||
return {
|
||||
|
||||
@@ -32,6 +32,7 @@ export interface TestBackendOptions<
|
||||
...{
|
||||
[index in keyof TServices]:
|
||||
| ServiceFactory<TServices[index]>
|
||||
| (() => ServiceFactory<TServices[index]>)
|
||||
| [ServiceRef<TServices[index]>, Partial<TServices[index]>];
|
||||
},
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user