From 219ddcf6a87b2a79b28f2b8e15b91a50082612c2 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 17 Jan 2023 16:03:16 +0100 Subject: [PATCH] docs/backend-system: update service example to match reality 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 --- .../architecture/03-services.md | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/docs/backend-system/architecture/03-services.md b/docs/backend-system/architecture/03-services.md index adedbc6383..27fea27db9 100644 --- a/docs/backend-system/architecture/03-services.md +++ b/docs/backend-system/architecture/03-services.md @@ -36,8 +36,10 @@ export const exampleServiceRef = createServiceRef({ id: 'example', scope: 'plugin', // can be 'root' or 'plugin' - // The defaultFactory is optional to implement but it will be used if no other factory is provided to the backend. - // This is allows for the backend to provide a default implementation of the service without having to wire it beforehand. + // The defaultFactory is optional to implement but it will be used if no + // other factory is provided to the backend. This allows for the backend + // to provide a default implementation of the service without having to wire + // it beforehand. defaultFactory: async service => createServiceFactory({ service, @@ -45,14 +47,21 @@ export const exampleServiceRef = createServiceRef({ logger: coreServices.logger, plugin: coreServices.pluginMetadata, }, - // Logger is available directly in the factory as it's a root scoped service and will be created once per backend instance. - async factory({ logger, plugin }) { - // plugin is available as it's a plugin scoped service and will be created once per plugin. - return async ({ plugin }) => { - // This block will be executed once for every plugin that depends on this service - logger.info('Initializing example service plugin instance'); - return new ExampleImpl({ logger, plugin }); - }; + // This root context method is only available for plugin scoped services. + // It's only called once per backend instance. + // Logger is available at the root context level as it's also a root + // scoped service. + createRootContext({ logger }) { + return new ExampleImplFactory({ logger }); + }, + // Plugin is available as it's a plugin scoped service and will be + // created once per plugin. The logger can be had here too if needed. + // Both this anc the root context can also be async. + factory({ logger, plugin }, rootContext) { + // This block will be executed once for every plugin that depends on + // this service + logger.info('Initializing example service plugin instance'); + return rootContext.forPlugin(plugin.getId()); }, }), });