From 5b7790abdf04e107ad26de4a362bce4ec1f8a3f6 Mon Sep 17 00:00:00 2001 From: hypergonial Date: Thu, 23 May 2024 12:23:38 +0200 Subject: [PATCH] Update docs about lifecycle hooks --- docs/guides/loops.md | 81 ++++++++++++++++++++++++--------- docs/guides/startup_shutdown.md | 10 ++-- 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/docs/guides/loops.md b/docs/guides/loops.md index bf262f9..ee6733a 100644 --- a/docs/guides/loops.md +++ b/docs/guides/loops.md @@ -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. diff --git a/docs/guides/startup_shutdown.md b/docs/guides/startup_shutdown.md index e6baf11..a66134a 100644 --- a/docs/guides/startup_shutdown.md +++ b/docs/guides/startup_shutdown.md @@ -7,12 +7,12 @@ 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!") ``` @@ -20,7 +20,7 @@ It is possible to execute code when the client has started up or shut down, this === "REST" ```py - @client.set_startup_hook + @client.add_startup_hook async def startup_hook(client: arc.RESTClient) -> None: print("Client started up!") ``` @@ -30,7 +30,7 @@ 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!") ``` @@ -38,7 +38,7 @@ The **startup hook** is a great place to initialize resources that require an as === "REST" ```py - @client.set_shutdown_hook + @client.add_shutdown_hook async def shutdown_hook(client: arc.RESTClient) -> None: print("Client shut down!") ```