Skip to content

Commit

Permalink
Merge pull request #55 from underctrl-io/buttonkit-dispose-feature
Browse files Browse the repository at this point in the history
Setup a `dispose` method to the `ButtonKit` component
  • Loading branch information
twlite authored Jan 27, 2024
2 parents d5c5cfd + 535fe9a commit 5071578
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
27 changes: 27 additions & 0 deletions apps/docs/pages/guide/buttonkit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,30 @@ myButton
message.edit({ components: [row] });
});
```

## Dispose button collector

<Callout type="warning">
This feature is currently only available in the [development
version](/guide/installation#development-version).
</Callout>

To dispose the button collector, you can make use of the `dispose` method. By disposing the collector like this, your `onEnd` handler (if any) will be called automatically.

```js copy {15}
myButton
.onClick(
(buttonInteraction) => {
buttonInteraction.reply('You clicked a button!');
},
{ message },
)
.onEnd(() => {
console.log('Button collector ended.');

myButton.setDisabled(true);
message.edit({ components: [row] });
});

myButton.dispose();
```
6 changes: 6 additions & 0 deletions packages/commandkit/src/components/ButtonKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ export class ButtonKit extends ButtonBuilder {
});
}

public dispose() {
this.#destroyCollector();
this.#onEndHandler?.();
return this;
}

#destroyCollector() {
this.#collector?.stop('end');
this.#collector?.removeAllListeners();
Expand Down

0 comments on commit 5071578

Please sign in to comment.