Skip to content

Commit

Permalink
Refactor handling of dispatch events (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonovanDMC authored Jul 20, 2024
1 parent 763041b commit 4322abd
Show file tree
Hide file tree
Showing 7 changed files with 1,143 additions and 1,117 deletions.
4 changes: 4 additions & 0 deletions esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const Collection = (await import("./dist/lib/util/Collection.js")).default.defau
const ComponentInteraction = (await import("./dist/lib/structures/ComponentInteraction.js")).default.default;
const DiscordHTTPError = (await import("./dist/lib/rest/DiscordHTTPError.js")).default.default;
const DiscordRESTError = (await import("./dist/lib/rest/DiscordRESTError.js")).default.default;
const Dispatcher = (await import("./dist/lib/gateway/Dispatcher.js")).default.default;
const DefaultDispatchEvents = (await import("./dist/lib/gateway/events.js")).default;
const Errors = (await import("./dist/lib/util/Errors.js")).default;
const Entitlement = (await import("./dist/lib/structures/Entitlement.js")).default.default;
const ExtendedUser = (await import("./dist/lib/structures/ExtendedUser.js")).default.default;
Expand Down Expand Up @@ -109,6 +111,8 @@ export {
ComponentInteraction,
DiscordHTTPError,
DiscordRESTError,
Dispatcher,
DefaultDispatchEvents,
Errors,
Entitlement,
ExtendedUser,
Expand Down
54 changes: 54 additions & 0 deletions lib/gateway/Dispatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type Shard from "./Shard";
import type ShardManager from "./ShardManager";
import * as DefaultDispatchEvents from "./events";
import type { AnyDispatchPacket } from "../types/gateway-raw";

export type DispatchEvent = AnyDispatchPacket["t"];
export type DispatchEventMap = {
[K in AnyDispatchPacket as K["t"]]: K["d"];
};
export type DispatchFunction<K extends DispatchEvent = DispatchEvent> = (data: DispatchEventMap[K], shard: Shard) => void;
export default class Dispatcher {
private manager!: ShardManager;
events: Map<DispatchEvent, Array<DispatchFunction>> = new Map();
constructor(manager: ShardManager) {
Object.defineProperty(this, "manager", {
value: manager,
writable: false,
enumerable: false,
configurable: false
});

if (this.manager.options.useDefaultDispatchHandlers) {
for (const [event, fn] of Object.entries(DefaultDispatchEvents)) {
this.register(event as DispatchEvent, fn as DispatchFunction);
}
} else {
this.register("READY", DefaultDispatchEvents.READY);
this.register("RESUMED", DefaultDispatchEvents.RESUMED);
}
}

private handle(data: AnyDispatchPacket, shard: Shard): void {
const event = data.t;
if (!this.events.has(event)) return;
const arr = this.events.get(event)!;
for (const fn of arr) fn(data.d, shard);
}

register<K extends DispatchEvent>(event: K, fn: DispatchFunction<K>, replace = false): void {
if (!this.events.has(event)) this.events.set(event, []);
const arr = this.events.get(event)!;
if (replace && arr.length !== 0) arr.splice(0, arr.length);
arr.push(fn as never);
}

unregister<K extends DispatchEvent>(event: K, fn?: DispatchFunction<K>): void {
if (!this.events.has(event)) return;
const arr = this.events.get(event)!;
if (fn) {
const index = arr.indexOf(fn as never);
if (index !== -1) arr.splice(index, 1);
} else arr.splice(0, arr.length);
}
}
Loading

0 comments on commit 4322abd

Please sign in to comment.