-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): allow easily supporting redis gateway
- Loading branch information
Showing
10 changed files
with
126 additions
and
172 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
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,50 @@ | ||
import type { PubSubRedisBroker } from '@discordjs/brokers'; | ||
import type { ManagerShardEventsMap, WebSocketShardEvents } from '@discordjs/ws'; | ||
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter'; | ||
import type { GatewaySendPayload, GatewayDispatchEvents } from 'discord-api-types/v10'; | ||
import type { DiscordEvents } from '../client.js'; | ||
import type { Gateway } from './Gateway.js'; | ||
|
||
interface BrokerIntrinsicProps { | ||
shardId: number; | ||
} | ||
|
||
interface Events extends DiscordEvents { | ||
gateway_send: GatewaySendPayload; | ||
} | ||
|
||
export type RedisBrokerDiscordEvents = { | ||
[K in keyof Events]: BrokerIntrinsicProps & { payload: Events[K] }; | ||
}; | ||
|
||
export class RedisGateway | ||
extends AsyncEventEmitter<{ dispatch: ManagerShardEventsMap[WebSocketShardEvents.Dispatch] }> | ||
implements Gateway | ||
{ | ||
public constructor( | ||
private readonly broker: PubSubRedisBroker<RedisBrokerDiscordEvents>, | ||
private readonly shardCount: number, | ||
) { | ||
super(); | ||
} | ||
|
||
public getShardCount(): number { | ||
return this.shardCount; | ||
} | ||
|
||
public async send(shardId: number, payload: GatewaySendPayload): Promise<void> { | ||
await this.broker.publish('gateway_send', { payload, shardId }); | ||
} | ||
|
||
public async init(group: string, events: GatewayDispatchEvents[]) { | ||
for (const event of events) { | ||
this.broker.on(event, ({ data: { payload, shardId }, ack }) => { | ||
// @ts-expect-error - Union shenanigans | ||
this.emit('dispatch', { shardId, data: payload }); | ||
void ack(); | ||
}); | ||
} | ||
|
||
await this.broker.subscribe(group, events); | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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