backend-plugin-api: add defaultFactory to ServiceRef

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: blam <ben@blam.sh>
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 14:16:04 +02:00
parent 7220eb700e
commit 70343d6813
@@ -28,6 +28,14 @@ export type ServiceRef<T> = {
*/
T: 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, {}>>;
toString(): string;
$$ref: 'service';
@@ -65,12 +73,19 @@ export type AnyServiceFactory = ServiceFactory<
/**
* @public
*/
export function createServiceRef<T>(options: { id: string }): ServiceRef<T> {
export function createServiceRef<T>(options: {
id: string;
defaultFactory?: (
service: ServiceRef<T>,
) => Promise<ServiceFactory<T, T, {}>>;
}): ServiceRef<T> {
const { id, defaultFactory } = options;
return {
id: options.id,
id,
get T(): T {
throw new Error(`tried to read ServiceRef.T of ${this}`);
},
defaultFactory,
toString() {
return `serviceRef{${options.id}}`;
},