refactor(ws): event layout (#10376)

* refactor(ws): event layout

BREAKING CHANGE: All events now emit shard id as its own param

* fix: worker event forwarding

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
DD
2024-07-24 21:40:34 +03:00
committed by GitHub
parent fcd35ea2e7
commit bf6761a44a
7 changed files with 137 additions and 134 deletions

View File

@@ -28,8 +28,8 @@ export class SimpleShardingStrategy implements IShardingStrategy {
const strategy = new SimpleContextFetchingStrategy(this.manager, strategyOptions);
const shard = new WebSocketShard(strategy, shardId);
for (const event of Object.values(WebSocketShardEvents)) {
// @ts-expect-error: Intentional
shard.on(event, (payload) => this.manager.emit(event, { ...payload, shardId }));
// @ts-expect-error Event props can't be resolved properly, but they are correct
shard.on(event, (...args) => this.manager.emit(event, ...args, shardId));
}
this.shards.set(shardId, shard);

View File

@@ -48,7 +48,7 @@ export enum WorkerReceivePayloadOp {
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
| { 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.FetchStatusResponse; status: WebSocketShardStatus }
| { nonce: number; op: WorkerReceivePayloadOp.RetrieveSessionInfo; shardId: number }
@@ -293,7 +293,8 @@ export class WorkerShardingStrategy implements IShardingStrategy {
}
case WorkerReceivePayloadOp.Event: {
this.manager.emit(payload.event, { ...payload.data, shardId: payload.shardId });
// @ts-expect-error Event props can't be resolved properly, but they are correct
this.manager.emit(payload.event, ...payload.data, payload.shardId);
break;
}