-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor handling of dispatch events (#180)
- Loading branch information
1 parent
763041b
commit 4322abd
Showing
7 changed files
with
1,143 additions
and
1,117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.