backend-plugin-api: simplified ServiceFactory type

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-08-17 18:11:41 +02:00
parent 70343d6813
commit eef91a2558
12 changed files with 70 additions and 108 deletions
@@ -20,6 +20,5 @@ export type {
DepsToDepFactories,
FactoryFunc,
ServiceFactory,
AnyServiceFactory,
} from './types';
export { createServiceRef, createServiceFactory } from './types';
@@ -32,9 +32,7 @@ export type ServiceRef<T> = {
* The default factory that will be used to create service
* instances if not other factory is provided.
*/
defaultFactory?: (
service: ServiceRef<T>,
) => Promise<ServiceFactory<T, T, {}>>;
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
toString(): string;
@@ -53,31 +51,18 @@ export type DepsToDepFactories<T> = {
export type FactoryFunc<Impl> = (pluginId: string) => Promise<Impl>;
/** @public */
export type ServiceFactory<
TApi,
TImpl extends TApi,
TDeps extends { [name in string]: unknown },
> = {
service: ServiceRef<TApi>;
deps: TypesToServiceRef<TDeps>;
factory(deps: DepsToDepFactories<TDeps>): Promise<FactoryFunc<TImpl>>;
export type ServiceFactory<TService = unknown> = {
service: ServiceRef<TService>;
deps: { [key in string]: ServiceRef<unknown> };
factory(deps: { [key in string]: unknown }): Promise<FactoryFunc<TService>>;
};
/** @public */
export type AnyServiceFactory = ServiceFactory<
unknown,
unknown,
{ [key in string]: unknown }
>;
/**
* @public
*/
export function createServiceRef<T>(options: {
id: string;
defaultFactory?: (
service: ServiceRef<T>,
) => Promise<ServiceFactory<T, T, {}>>;
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
}): ServiceRef<T> {
const { id, defaultFactory } = options;
return {
@@ -97,9 +82,13 @@ export function createServiceRef<T>(options: {
* @public
*/
export function createServiceFactory<
Api,
Impl extends Api,
Deps extends { [name in string]: unknown },
>(factory: ServiceFactory<Api, Impl, Deps>): ServiceFactory<Api, Api, {}> {
return factory;
TService,
TImpl extends TService,
TDeps extends { [name in string]: unknown },
>(factory: {
service: ServiceRef<TService>;
deps: TypesToServiceRef<TDeps>;
factory(deps: DepsToDepFactories<TDeps>): Promise<FactoryFunc<TImpl>>;
}): ServiceFactory<TService> {
return factory as ServiceFactory<TService>;
}