Skip to content

Commit

Permalink
Merge pull request #39 from sern-handler/Murtatrxx-patch-1
Browse files Browse the repository at this point in the history
refactor: Update services.md
  • Loading branch information
jacoobes authored Aug 5, 2023
2 parents d59674e + 0b8de88 commit 71f100d
Showing 1 changed file with 99 additions and 4 deletions.
103 changes: 99 additions & 4 deletions docs/guide/walkthrough/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -46,16 +47,110 @@ export default commandModule({
}
//
})
```


import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';


## Safety
- Services cannot be called in other services while makeDependencies is forming.

<Tabs>

<TabItem value="good" label="A good example">

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']))) }
})
})
```
</TabItem>

<TabItem value="bad" label="Don't do this">

```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
}
}
```
## Safety
This is a code smell anyway. It breaks encapsulation and defeats the purpose of wiring dependencies
</TabItem>
</Tabs>

- 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.

<Tabs>
<TabItem value="good" label="A good example">

```ts title="index.ts" showLineNumbers
await makeDependencies(...pass your options here)
```
```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');
```

</TabItem>

<TabItem value="bad" label="Don't do this">

```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()
```

</TabItem>

</Tabs>

- 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.

Expand Down

0 comments on commit 71f100d

Please sign in to comment.