From 9802586bf7a36b45afb416a3380108a66f8c9a50 Mon Sep 17 00:00:00 2001 From: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com> Date: Fri, 4 Aug 2023 19:35:18 +0300 Subject: [PATCH 1/7] docs: Update services.md --- docs/guide/walkthrough/services.md | 31 +++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/docs/guide/walkthrough/services.md b/docs/guide/walkthrough/services.md index 523556903..cdd610ee2 100644 --- a/docs/guide/walkthrough/services.md +++ b/docs/guide/walkthrough/services.md @@ -48,14 +48,39 @@ export default commandModule({ }) ``` -## Safety +## Safety +- Services cannot be called in other services while makeDependencies is forming. +- You will need to wire dependencies together. + +✅ A good example; +```ts +// index.ts +await makeDependencies(...pass you options here) +``` +```ts +// commands/ping.ts +// This is guaranteed to be defined if configured correctly +import { Service } from '@sern/handler'; +const client = Service('@sern/client'); +``` + +❌ Don't do this +```ts +// index.ts +import { Service, makeDependencies } from '@sern/handler'; +/* DON'T USE SERVICES BEFORE CALLING makeDependencies */ +const logger = Service('@sern/logger'); + +await makeDependencies() +``` + - Services can only be used after sern has made dependencies. - Calling a service before will crash your application. -- Services can be safely used outside of commandModules. +- Services can be safely used outside of commandModules. - Be careful to not cause too many side effects. -## Related api +## Related api - use `Service` for single dependency. - use `Services` for multiple dependencies. From c7a86114767e403d4abbb882053def5c9950d4e3 Mon Sep 17 00:00:00 2001 From: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com> Date: Fri, 4 Aug 2023 19:44:22 +0300 Subject: [PATCH 2/7] =?UTF-8?q?docs:=20=E2=9C=A8=20Add=20some=20magic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/guide/walkthrough/services.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/guide/walkthrough/services.md b/docs/guide/walkthrough/services.md index cdd610ee2..fd3e931fe 100644 --- a/docs/guide/walkthrough/services.md +++ b/docs/guide/walkthrough/services.md @@ -51,9 +51,13 @@ export default commandModule({ ## Safety - Services cannot be called in other services while makeDependencies is forming. - You will need to wire dependencies together. - + +import Tabs from '@theme/Tabs';import TabItem from '@theme/TabItem'; + + + ✅ A good example; -```ts +```ts title="index.ts" showLineNumbers // index.ts await makeDependencies(...pass you options here) ``` @@ -63,16 +67,18 @@ await makeDependencies(...pass you options here) import { Service } from '@sern/handler'; const client = Service('@sern/client'); ``` + + ❌ Don't do this -```ts -// index.ts +```ts title="commands/ping.ts" showLineNumbers import { Service, makeDependencies } from '@sern/handler'; /* DON'T USE SERVICES BEFORE CALLING makeDependencies */ const logger = Service('@sern/logger'); await makeDependencies() ``` + - Services can only be used after sern has made dependencies. - Calling a service before will crash your application. From ef452e678a79085f95bea753fdf42352f9e6aebf Mon Sep 17 00:00:00 2001 From: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com> Date: Fri, 4 Aug 2023 19:50:29 +0300 Subject: [PATCH 3/7] fix: Unterminated JSX tag --- docs/guide/walkthrough/services.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/guide/walkthrough/services.md b/docs/guide/walkthrough/services.md index fd3e931fe..5666cf004 100644 --- a/docs/guide/walkthrough/services.md +++ b/docs/guide/walkthrough/services.md @@ -79,6 +79,7 @@ const logger = Service('@sern/logger'); await makeDependencies() ``` + - Services can only be used after sern has made dependencies. - Calling a service before will crash your application. From 19db611a9b3df837fb67a093a090a0dcbea01e56 Mon Sep 17 00:00:00 2001 From: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com> Date: Fri, 4 Aug 2023 20:01:11 +0300 Subject: [PATCH 4/7] fix: Deploy success --- docs/guide/walkthrough/services.md | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/guide/walkthrough/services.md b/docs/guide/walkthrough/services.md index 5666cf004..fcb38bbc6 100644 --- a/docs/guide/walkthrough/services.md +++ b/docs/guide/walkthrough/services.md @@ -46,40 +46,39 @@ export default commandModule({ } // }) - ``` + ## Safety - Services cannot be called in other services while makeDependencies is forming. - You will need to wire dependencies together. -import Tabs from '@theme/Tabs';import TabItem from '@theme/TabItem'; +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; - -✅ A good example; + + ```ts title="index.ts" showLineNumbers -// index.ts -await makeDependencies(...pass you options here) +await makeDependencies(...pass your options here) ``` -```ts -// commands/ping.ts + +```ts title="commands/ping.ts showLineNumbers // This is guaranteed to be defined if configured correctly import { Service } from '@sern/handler'; const client = Service('@sern/client'); ``` - - -❌ Don't do this -```ts title="commands/ping.ts" showLineNumbers + + - + - Services can only be used after sern has made dependencies. - Calling a service before will crash your application. From b06251f4996326bdf487ebbed4838d4423f30972 Mon Sep 17 00:00:00 2001 From: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com> Date: Fri, 4 Aug 2023 20:06:52 +0300 Subject: [PATCH 5/7] please work --- docs/guide/walkthrough/services.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/guide/walkthrough/services.md b/docs/guide/walkthrough/services.md index fcb38bbc6..e957f789f 100644 --- a/docs/guide/walkthrough/services.md +++ b/docs/guide/walkthrough/services.md @@ -58,11 +58,11 @@ import TabItem from '@theme/TabItem'; -```ts title="index.ts" showLineNumbers +```ts title="index.ts" await makeDependencies(...pass your options here) ``` -```ts title="commands/ping.ts showLineNumbers +```ts title="commands/ping.ts // This is guaranteed to be defined if configured correctly import { Service } from '@sern/handler'; const client = Service('@sern/client'); @@ -71,14 +71,15 @@ const client = Service('@sern/client'); + + - Services can only be used after sern has made dependencies. - Calling a service before will crash your application. From bfc4d6ac8e71b59c8e85c959b69dafe3e877a3c1 Mon Sep 17 00:00:00 2001 From: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com> Date: Fri, 4 Aug 2023 20:14:30 +0300 Subject: [PATCH 6/7] fix: JSX issue --- docs/guide/walkthrough/services.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/guide/walkthrough/services.md b/docs/guide/walkthrough/services.md index e957f789f..2756c2eea 100644 --- a/docs/guide/walkthrough/services.md +++ b/docs/guide/walkthrough/services.md @@ -55,30 +55,32 @@ export default commandModule({ import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; + -```ts title="index.ts" +```ts title="index.ts" showLineNumbers await makeDependencies(...pass your options here) ``` - -```ts title="commands/ping.ts +```ts title="commands/ping.ts" showLineNumbers // This is guaranteed to be defined if configured correctly import { Service } from '@sern/handler'; const client = Service('@sern/client'); ``` - - + +```ts title="index.ts" showLineNumbers import { Service, makeDependencies } from '@sern/handler'; /* DON'T USE SERVICES BEFORE CALLING makeDependencies */ const logger = Service('@sern/logger'); await makeDependencies() ``` + + - Services can only be used after sern has made dependencies. From 0b8de88344c355defb75ae4d8756025fddcc402e Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Fri, 4 Aug 2023 21:08:51 -0500 Subject: [PATCH 7/7] update service guide --- docs/guide/walkthrough/services.md | 69 ++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/docs/guide/walkthrough/services.md b/docs/guide/walkthrough/services.md index 2756c2eea..c72800da3 100644 --- a/docs/guide/walkthrough/services.md +++ b/docs/guide/walkthrough/services.md @@ -2,11 +2,12 @@ sidebar_position: 6 --- +# Services + :::tip This is version 3 api only!! ::: -# Services :::tip TLDR: The direct upgrade to useContainer. if you set up a bot with create-bot, check dependencies.d.ts. @@ -48,14 +49,73 @@ export default commandModule({ }) ``` -## Safety -- Services cannot be called in other services while makeDependencies is forming. -- You will need to wire dependencies together. import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; +## Safety +- Services cannot be called in other services while makeDependencies is forming. + + + + + +Lets pass a logger into our database. +```ts title="index.ts" showLineNumbers +await makeDependencies({ + build: root => root + //Overriding the default logger provided. + .upsert({ '@sern/logger': single(() => new Logger()) }) + + // Wiring our logger into the database. + .add(ctx => { + return { database: single(() => new Database(ctx['sern/logger']))) } + }) +}) +``` + + + + +```ts title="index.ts" showLineNumbers +await makeDependencies({ + build: root => root + //Overriding the default logger provided. + .upsert({ '@sern/logger': single(() => new Logger()) }) + + // Wiring our logger into the database. + // We wire our database incorrectly. Logger should be passed INTO the constructor + .add({ database: single(() => new Database()) }) +}) +``` + +```ts title="index.ts" showLineNumbers +import { Service, makeDependencies } from '@sern/handler'; + +//Calling Service prematurely! +const logger = Service('@sern/logger'); + +class Database { + + constructor() { + this.logger = logger + } +} +``` +This is a code smell anyway. It breaks encapsulation and defeats the purpose of wiring dependencies + + + +- Services can only be used after sern has made dependencies. + - Calling a service before will crash your application. +- Services can be safely used outside of commandModules. + - Be careful to not cause too many side effects. + + + +- You will need to wire dependencies together. + @@ -67,6 +127,7 @@ await makeDependencies(...pass your options here) import { Service } from '@sern/handler'; const client = Service('@sern/client'); ``` +