Skip to content

Commit

Permalink
Update docs about lifecycle hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed May 23, 2024
1 parent bf767d5 commit 5b7790a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
81 changes: 59 additions & 22 deletions docs/guides/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,73 @@ To create a loop, you should decorate an async function with either [`@arc.utils
pip install hikari-arc[cron]
```

```py
# Create a loop out of a function
@arc.utils.interval_loop(seconds=10.0)
async def loopy_loop(value: int) -> None:
print(value)
=== "Gateway"

# Somewhere else:
```py
# Create a loop out of a function
@arc.utils.interval_loop(seconds=10.0)
async def loopy_loop(value: int) -> None:
print(value)

@client.set_startup_hook
async def startup(client: arc.GatewayClient) -> None:
# Start the loop by passing all the parameters it needs
loopy_loop.start(value=10)
```
# Somewhere else:

@client.add_startup_hook
async def startup(client: arc.GatewayClient) -> None:
# Start the loop by passing all the parameters it needs
loopy_loop.start(value=10)
```

=== "REST"

```py
# Create a loop out of a function
@arc.utils.interval_loop(seconds=10.0)
async def loopy_loop(value: int) -> None:
print(value)

# Somewhere else:

@client.add_startup_hook
async def startup(client: arc.RESTClient) -> None:
# Start the loop by passing all the parameters it needs
loopy_loop.start(value=10)
```

If a decorator doesn't suit your needs, you may also use the [`IntervalLoop`][arc.utils.loops.IntervalLoop] and [`CronLoop`][arc.utils.loops.CronLoop] classes directly:

```py
async def loopy_loop(value: int) -> None:
print(value)
=== "Gateway"

```py
async def loopy_loop(value: int) -> None:
print(value)

# Create a loop by passing the function in
loop = arc.utils.IntervalLoop(loopy_loop, seconds=10.0)
# Create a loop by passing the function in
loop = arc.utils.IntervalLoop(loopy_loop, seconds=10.0)

# Somewhere else:
# Somewhere else:

@client.set_startup_hook
async def startup(client: arc.GatewayClient) -> None:
# Start the loop by passing all the parameters it needs
loop.start(value=10)
```
@client.add_startup_hook
async def startup(client: arc.GatewayClient) -> None:
# Start the loop by passing all the parameters it needs
loop.start(value=10)
```

=== "REST"

```py
async def loopy_loop(value: int) -> None:
print(value)

# Create a loop by passing the function in
loop = arc.utils.IntervalLoop(loopy_loop, seconds=10.0)

# Somewhere else:

@client.add_startup_hook
async def startup(client: arc.RESTClient) -> None:
# Start the loop by passing all the parameters it needs
loop.start(value=10)
```

This is identical to using the decorator from above.

Expand Down
10 changes: 5 additions & 5 deletions docs/guides/startup_shutdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ hide:

# Startup & Shutdown

It is possible to execute code when the client has started up or shut down, this can be done via the [`@Client.set_startup_hook`][arc.abc.Client.set_startup_hook] and [`@Client.set_shutdown_hook`][arc.abc.Client.set_shutdown_hook] respectively.
It is possible to execute code when the client has started up or shut down, this can be done via the [`@Client.add_startup_hook`][arc.abc.Client.set_startup_hook] and [`@Client.add_shutdown_hook`][arc.abc.Client.set_shutdown_hook] respectively.

=== "Gateway"

```py
@client.set_startup_hook
@client.add_startup_hook
async def startup_hook(client: arc.GatewayClient) -> None:
print("Client started up!")
```

=== "REST"

```py
@client.set_startup_hook
@client.add_startup_hook
async def startup_hook(client: arc.RESTClient) -> None:
print("Client started up!")
```
Expand All @@ -30,15 +30,15 @@ The **startup hook** is a great place to initialize resources that require an as
=== "Gateway"

```py
@client.set_shutdown_hook
@client.add_shutdown_hook
async def shutdown_hook(client: arc.GatewayClient) -> None:
print("Client shut down!")
```

=== "REST"

```py
@client.set_shutdown_hook
@client.add_shutdown_hook
async def shutdown_hook(client: arc.RESTClient) -> None:
print("Client shut down!")
```
Expand Down

0 comments on commit 5b7790a

Please sign in to comment.