From 70343d6813f75c6f918ac29c92dd10a589a0675b Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 17 Aug 2022 14:16:04 +0200 Subject: [PATCH] backend-plugin-api: add defaultFactory to ServiceRef MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: blam Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .../src/services/system/types.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index d578434170..1c6426ecec 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -28,6 +28,14 @@ export type ServiceRef = { */ T: T; + /** + * The default factory that will be used to create service + * instances if not other factory is provided. + */ + defaultFactory?: ( + service: ServiceRef, + ) => Promise>; + toString(): string; $$ref: 'service'; @@ -65,12 +73,19 @@ export type AnyServiceFactory = ServiceFactory< /** * @public */ -export function createServiceRef(options: { id: string }): ServiceRef { +export function createServiceRef(options: { + id: string; + defaultFactory?: ( + service: ServiceRef, + ) => Promise>; +}): ServiceRef { + 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}}`; },