Skip to content

Commit

Permalink
feat: allow collectors to incrementally emit
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Oct 23, 2023
1 parent 29818fb commit c1da8a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-dolphin-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"guilded.js": patch
---

feat: add a collect event to collectors
10 changes: 10 additions & 0 deletions packages/guilded.js/lib/structures/collectors/Collector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Collection } from "@discordjs/collection";
import type { MaybePromise } from "../../typings";
import type { Client } from "../Client";
import EventEmitter from "events";
import type TypedEmitter from "typed-emitter";

/**
* A collector is just a wrapper over a set of events for collecting a structure.
Expand All @@ -23,6 +25,8 @@ export abstract class Collector<T extends CollectableStructure> {
/** Bound function for item receiving */
protected boundItemReceiver = this.itemReceived.bind(this);

public emitter = new EventEmitter() as TypedEmitter<CollectorEvents<T>>;

constructor(public readonly client: Client, public options: Partial<CollectorOptions<T>>) {
/** Check if timeLimit is specified */
if (!options.timeLimit) throw new Error("You must specify a time limit in milliseconds for this collector.");
Expand Down Expand Up @@ -64,6 +68,7 @@ export abstract class Collector<T extends CollectableStructure> {
this.isActive = false;
}

this.emitter.emit("collect", entry);
return true;
}

Expand Down Expand Up @@ -137,3 +142,8 @@ export type CollectorOptions<T> = {
/** the max amount of entries allowed to be collected */
max?: number;
};

/** events that collectors can emit */
export type CollectorEvents<T> = {
collect: (item: T) => unknown;
};

0 comments on commit c1da8a6

Please sign in to comment.