plugin_services.md: clarify injecting Coroutine Scope

This commit is contained in:
Yann Cébron 2024-04-03 13:31:18 +02:00
parent e4cfe8aeec
commit 4ec2775bac
2 changed files with 14 additions and 5 deletions

View File

@ -63,8 +63,10 @@ application/project [services](plugin_services.md) provided by a plugin.
### Service Scopes
The **Application Service** and **Project Service** scopes are bound to an application and project [service](plugin_services.md) lifetimes accordingly.
They are children of the [](#intersection-scopes), which means that they are canceled when the application/project is closed or a plugin is unloaded.
<snippet id="serviceScopes">
The **Application Service** and **Project Service** scopes are bound to an application and project [service](plugin_services.md#types) lifetimes accordingly.
They are children of the [](coroutine_scopes.md#intersection-scopes), which means that they are canceled when the application/project is closed or a plugin is unloaded.
The service scope is provided to services via constructor injection.
The following constructor signatures are supported:
@ -75,6 +77,10 @@ The following constructor signatures are supported:
Each service instance receives its own scope instance.
The injected scopes' contexts contain [`Dispatchers.Default`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html) and [`CoroutineName(serviceClass)`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-name/).
See [](launching_coroutines.md#launching-coroutine-from-service-scope) for full samples.
</snippet>
## Using a Correct Scope
### Use Service Scopes

View File

@ -34,9 +34,6 @@ To improve startup performance, avoid any heavy initializations in the construct
Project/Module-level service constructors can have a [`Project`](%gh-ic%/platform/core-api/src/com/intellij/openapi/project/Project.java)/[`Module`](%gh-ic%/platform/core-api/src/com/intellij/openapi/module/Module.java) argument.
When using [](kotlin_coroutines.md), a distinct [Service Scope](coroutine_scopes.md#service-scopes) can be injected as parameter.
See [](launching_coroutines.md#launching-coroutine-from-service-scope) for full samples.
> Using constructor injection of dependency services is deprecated (and not supported in [](#light-services)) for performance reasons.
>
> Other service dependencies must be [acquired only when needed](#retrieving-a-service) in all corresponding methods, e.g., if you need a service to get some data or execute a task, retrieve the service before calling its methods.
@ -46,6 +43,12 @@ See [](launching_coroutines.md#launching-coroutine-from-service-scope) for full
>
{style="warning" title="Do not use Constructor Injection"}
##### Kotlin Coroutines
When using [](kotlin_coroutines.md), a distinct service [scope](coroutine_scopes.md) can be injected as parameter.
<include from="coroutine_scopes.md" element-id="serviceScopes"/>
## Light Services
A service not going to be overridden does not need to be registered in <path>[plugin.xml](plugin_configuration_file.md)</path> (see [](#declaring-a-service)).