From 914d57336bc8355533d743745989f793ffd4d513 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Wed, 5 Apr 2023 12:15:44 +0200 Subject: [PATCH] chores(medusa): cleanup registrations life time (#3699) --- .changeset/famous-rice-divide.md | 5 +++++ .../development/endpoints/add-middleware.md | 2 +- .../fundamentals/dependency-injection.md | 18 +++++++++--------- .../development/services/create-service.md | 4 ++-- packages/medusa/src/loaders/helpers/plugins.ts | 8 ++++---- packages/medusa/src/loaders/plugins.ts | 14 +++++++------- 6 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 .changeset/famous-rice-divide.md diff --git a/.changeset/famous-rice-divide.md b/.changeset/famous-rice-divide.md new file mode 100644 index 0000000000..9300bb1103 --- /dev/null +++ b/.changeset/famous-rice-divide.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +chores(medusa): cleanup registrations life time diff --git a/docs/content/development/endpoints/add-middleware.md b/docs/content/development/endpoints/add-middleware.md index 65ec72394e..da7ffd6e7b 100644 --- a/docs/content/development/endpoints/add-middleware.md +++ b/docs/content/development/endpoints/add-middleware.md @@ -153,7 +153,7 @@ Notice that you have to wrap your usage of the new resource in a try-catch block If you want to access new registrations in the dependency container within a service, you must set the lifetime of the service either to `Lifetime.SCOPED` or `Lifetime.TRANSIENT`. Services that have a `Lifetime.SINGLETON` lifetime can't access new registrations since they're resolved and cached in the root dependency container beforehand. You can learn more in the [Create Services documentation](../services/create-service.md#service-life-time). -For custom services, no additional action is required as the default lifetime is `Lifetime.TRANSIENT`. However, if you extend a core service, you must change the lifetime since the default lifetime for core services is `Lifetime.SINGLETON`. +For custom services, no additional action is required as the default lifetime is `Lifetime.SCOPED`. However, if you extend a core service, you must change the lifetime since the default lifetime for core services is `Lifetime.SINGLETON`. For example: diff --git a/docs/content/development/fundamentals/dependency-injection.md b/docs/content/development/fundamentals/dependency-injection.md index fba64501e1..5adc648345 100644 --- a/docs/content/development/fundamentals/dependency-injection.md +++ b/docs/content/development/fundamentals/dependency-injection.md @@ -110,7 +110,7 @@ Each service is registered under its camel-case name. For example, the `ProductS -Core services by default have the `SINGLETON` lifetime. However, some have a different lifetime which is indicated in this table. Custom services, including services in plugins, by default have the `TRANSIENT` lifetime, unless defined differently within the custom service. +Core services by default have the `SINGLETON` lifetime. However, some have a different lifetime which is indicated in this table. Custom services, including services in plugins, by default have the `SCOPED` lifetime, unless defined differently within the custom service. @@ -182,7 +182,7 @@ Every payment processor is registered under two names: -By default, it's `TRANSIENT` unless defined differently within the payment processor service. +By default, it's `SINGLETON` unless defined differently within the payment processor service. @@ -205,7 +205,7 @@ An array of all payment processor that extend the `AbstractPaymentService` or `A -`paymentProviders` is `TRANSIENT`, and each item in it is `TRANSIENT`. +`paymentProviders` is `TRANSIENT`, and each item in it is `SINGLETON`. @@ -254,7 +254,7 @@ An array of all fulfillment providers that extend the `FulfillmentService` class -`fulfillmentProviders` is `TRANSIENT`, and each item in it is `TRANSIENT`. +`fulfillmentProviders` is `TRANSIENT`, and each item in it is `SINGLETON`. @@ -303,7 +303,7 @@ An array of all notification providers that extend the `AbstractNotificationServ -`notificationProviders` is `TRANSIENT`, and each item in it is `TRANSIENT`. +`notificationProviders` is `TRANSIENT`, and each item in it is `SINGLETON`. @@ -329,7 +329,7 @@ The file service is registered under two names: -By default, it's `TRANSIENT` unless defined differently within the file service. +By default, it's `SINGLETON` unless defined differently within the file service. @@ -355,7 +355,7 @@ The search service is registered under two names: -By default, it's `TRANSIENT` unless defined differently within the search service. +By default, it's `SINGLETON` unless defined differently within the search service. @@ -404,7 +404,7 @@ An array of every tax provider that extends the `AbstractTaxService` class. -`taxProviders` is `TRANSIENT`, and each item in it is `TRANSIENT`. +`taxProviders` is `TRANSIENT`, and each item in it is `SINGLETON`. @@ -427,7 +427,7 @@ Each Oauth Service is registered under its camel-case name followed by `Oauth`. -By default, it's `TRANSIENT` unless defined differently within the Oauth service. +By default, it's `SINGLETON` unless defined differently within the Oauth service. diff --git a/docs/content/development/services/create-service.md b/docs/content/development/services/create-service.md index e4d8e39dae..bd0ed12ccf 100644 --- a/docs/content/development/services/create-service.md +++ b/docs/content/development/services/create-service.md @@ -38,8 +38,8 @@ As the dependency container in Medusa is built on top of [awilix](https://github There are three lifetime types: -1. `Lifetime.TRANSIENT`: (default for custom services) when used, a new instance of the service is created everytime it is resolved in other resources from the dependency container. -2. `Lifetime.SCOPED`: when used, an instance of the service is created and reused in the scope of the dependency container. So, when the service is resolved in other resources that share that dependency container, the same instance of the service will be returned. +1. `Lifetime.TRANSIENT`: when used, a new instance of the service is created everytime it is resolved in other resources from the dependency container. +2. `Lifetime.SCOPED`: (default for custom services) when used, an instance of the service is created and reused in the scope of the dependency container. So, when the service is resolved in other resources that share that dependency container, the same instance of the service will be returned. 3. `Lifetime.SINGLETON`: (default for core services) when used, the service is always reused, regardless of the scope. An instance of the service is cached in the root container. You can set the lifetime of your service by setting the `LIFE_TIME` static property: diff --git a/packages/medusa/src/loaders/helpers/plugins.ts b/packages/medusa/src/loaders/helpers/plugins.ts index 830e2556e8..16252dd3db 100644 --- a/packages/medusa/src/loaders/helpers/plugins.ts +++ b/packages/medusa/src/loaders/helpers/plugins.ts @@ -28,7 +28,7 @@ export function registerPaymentServiceFromClass( container.registerAdd( "paymentProviders", asFunction((cradle) => new klass(cradle, pluginDetails.options), { - lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, }) ) @@ -36,7 +36,7 @@ export function registerPaymentServiceFromClass( [registrationName]: asFunction( (cradle) => new klass(cradle, pluginDetails.options), { - lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, } ), [`pp_${(klass as unknown as typeof AbstractPaymentService).identifier}`]: @@ -59,7 +59,7 @@ export function registerPaymentProcessorFromClass( container.registerAdd( "paymentProviders", asFunction((cradle) => new klass(cradle, pluginDetails.options), { - lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, }) ) @@ -67,7 +67,7 @@ export function registerPaymentProcessorFromClass( [registrationName]: asFunction( (cradle) => new klass(cradle, pluginDetails.options), { - lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, } ), [`pp_${(klass as unknown as typeof AbstractPaymentProcessor).identifier}`]: diff --git a/packages/medusa/src/loaders/plugins.ts b/packages/medusa/src/loaders/plugins.ts index c367e36da0..791690fe79 100644 --- a/packages/medusa/src/loaders/plugins.ts +++ b/packages/medusa/src/loaders/plugins.ts @@ -385,7 +385,7 @@ export async function registerServices( [`${name}Oauth`]: asFunction( (cradle) => new loaded(cradle, pluginDetails.options), { - lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, } ), }) @@ -394,7 +394,7 @@ export async function registerServices( container.registerAdd( "fulfillmentProviders", asFunction((cradle) => new loaded(cradle, pluginDetails.options), { - lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, }) ) @@ -413,7 +413,7 @@ export async function registerServices( container.registerAdd( "notificationProviders", asFunction((cradle) => new loaded(cradle, pluginDetails.options), { - lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, }) ) @@ -438,7 +438,7 @@ export async function registerServices( [name]: asFunction( (cradle) => new loaded(cradle, pluginDetails.options), { - lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, } ), [`fileService`]: aliasTo(name), @@ -450,7 +450,7 @@ export async function registerServices( [name]: asFunction( (cradle) => new loaded(cradle, pluginDetails.options), { - lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, } ), [`searchService`]: aliasTo(name), @@ -461,7 +461,7 @@ export async function registerServices( container.registerAdd( "taxProviders", asFunction((cradle) => new loaded(cradle, pluginDetails.options), { - lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, }) ) @@ -479,7 +479,7 @@ export async function registerServices( [name]: asFunction( (cradle) => new loaded(cradle, pluginDetails.options), { - lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + lifetime: loaded.LIFE_TIME || Lifetime.SCOPED, } ), })