mirror of
https://github.com/discordjs/discord.js.git
synced 2024-08-21 15:54:44 +12:00
fix: worker event forwarding
This commit is contained in:
parent
20695b9f7a
commit
d0dd88fa6a
3 changed files with 11 additions and 9 deletions
|
@ -91,7 +91,7 @@ vi.mock('node:worker_threads', async () => {
|
||||||
op: WorkerReceivePayloadOp.Event,
|
op: WorkerReceivePayloadOp.Event,
|
||||||
shardId: message.shardId,
|
shardId: message.shardId,
|
||||||
event: WebSocketShardEvents.Dispatch,
|
event: WebSocketShardEvents.Dispatch,
|
||||||
data: memberChunkData,
|
data: [memberChunkData],
|
||||||
};
|
};
|
||||||
this.emit('message', response);
|
this.emit('message', response);
|
||||||
|
|
||||||
|
@ -204,10 +204,13 @@ test('spawn, connect, send a message, session info, and destroy', async () => {
|
||||||
};
|
};
|
||||||
await manager.send(0, payload);
|
await manager.send(0, payload);
|
||||||
expect(mockSend).toHaveBeenCalledWith(0, payload);
|
expect(mockSend).toHaveBeenCalledWith(0, payload);
|
||||||
expect(managerEmitSpy).toHaveBeenCalledWith(WebSocketShardEvents.Dispatch, {
|
expect(managerEmitSpy).toHaveBeenCalledWith(
|
||||||
|
WebSocketShardEvents.Dispatch,
|
||||||
|
{
|
||||||
...memberChunkData,
|
...memberChunkData,
|
||||||
shardId: 0,
|
},
|
||||||
});
|
0,
|
||||||
|
);
|
||||||
expect(mockRetrieveSessionInfo).toHaveBeenCalledWith(0);
|
expect(mockRetrieveSessionInfo).toHaveBeenCalledWith(0);
|
||||||
expect(mockUpdateSessionInfo).toHaveBeenCalledWith(0, { ...sessionInfo, sequence: sessionInfo.sequence + 1 });
|
expect(mockUpdateSessionInfo).toHaveBeenCalledWith(0, { ...sessionInfo, sequence: sessionInfo.sequence + 1 });
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ export enum WorkerReceivePayloadOp {
|
||||||
|
|
||||||
export type WorkerReceivePayload =
|
export type WorkerReceivePayload =
|
||||||
// Can't seem to get a type-safe union based off of the event, so I'm sadly leaving data as any for now
|
// Can't seem to get a type-safe union based off of the event, so I'm sadly leaving data as any for now
|
||||||
| { data: any; event: WebSocketShardEvents; op: WorkerReceivePayloadOp.Event; shardId: number }
|
| { data: any[]; event: WebSocketShardEvents; op: WorkerReceivePayloadOp.Event; shardId: number }
|
||||||
| { nonce: number; op: WorkerReceivePayloadOp.CancelIdentify }
|
| { nonce: number; op: WorkerReceivePayloadOp.CancelIdentify }
|
||||||
| { nonce: number; op: WorkerReceivePayloadOp.FetchStatusResponse; status: WebSocketShardStatus }
|
| { nonce: number; op: WorkerReceivePayloadOp.FetchStatusResponse; status: WebSocketShardStatus }
|
||||||
| { nonce: number; op: WorkerReceivePayloadOp.RetrieveSessionInfo; shardId: number }
|
| { nonce: number; op: WorkerReceivePayloadOp.RetrieveSessionInfo; shardId: number }
|
||||||
|
|
|
@ -148,12 +148,11 @@ export class WorkerBootstrapper {
|
||||||
for (const shardId of this.data.shardIds) {
|
for (const shardId of this.data.shardIds) {
|
||||||
const shard = new WebSocketShard(new WorkerContextFetchingStrategy(this.data), shardId);
|
const shard = new WebSocketShard(new WorkerContextFetchingStrategy(this.data), shardId);
|
||||||
for (const event of options.forwardEvents ?? Object.values(WebSocketShardEvents)) {
|
for (const event of options.forwardEvents ?? Object.values(WebSocketShardEvents)) {
|
||||||
// @ts-expect-error: Event types incompatible
|
shard.on(event, (...args) => {
|
||||||
shard.on(event, (data) => {
|
|
||||||
const payload: WorkerReceivePayload = {
|
const payload: WorkerReceivePayload = {
|
||||||
op: WorkerReceivePayloadOp.Event,
|
op: WorkerReceivePayloadOp.Event,
|
||||||
event,
|
event,
|
||||||
data,
|
data: args,
|
||||||
shardId,
|
shardId,
|
||||||
};
|
};
|
||||||
parentPort!.postMessage(payload);
|
parentPort!.postMessage(payload);
|
||||||
|
|
Loading…
Reference in a new issue