0.1.0 • Published 5 months ago

disposable-discord-client v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Usage

npm i disposable-discord-client
import { DisposableClient } from "disposable-discord-client";

await using client = new DisposableClient(/* ... */);

await client.login();

DisposableClient takes all the same type and value parameters as Client.

In fact, its implementation is small enough to fit here!

import { Client } from "discord.js";

export class DisposableClient<
	Ready extends boolean = boolean,
> extends Client<Ready> {
	async [Symbol.asyncDispose]() {
		await this.destroy();
	}
}

What?

Explicit Resource Management is a TC39 proposal to add explicit syntax for situations where a resource should have some method called upon disposal. It's stage 3 now and supported in TypeScript >=5.2.

The discord.js Client class is a good example of a disposable resource. When a Client instance is no longer needed, its destroy() should generally be called:

try {
	const client = new Client(/* ... */);
	await client.login();
} finally {
	await client.destroy();
}

One gotcha is that if the ( use the client) bit throws, the await client.destroy() won't run - unless you try/catch or try/(catch/)finally). This is a common gotcha in async code.

Contributors

💙 This package was templated with create-typescript-app.

0.1.0

5 months ago