swc/crates/swc_bundler/tests/.cache/deno/8ff0ffe6eb419da20775dcf1999218ba4925cb7f.ts
2021-11-09 20:42:49 +09:00

67 lines
1.6 KiB
TypeScript

// Loaded from https://deno.land/x/discordeno@11.0.0-rc.2/src/ws/resume.ts
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { ws } from "./ws.ts";
export async function resume(shardId: number) {
ws.log("RESUMING", { shardId });
// NOW WE HANDLE RESUMING THIS SHARD
// Get the old data for this shard necessary for resuming
const oldShard = ws.shards.get(shardId);
if (oldShard) {
// HOW TO CLOSE OLD SHARD SOCKET!!!
ws.closeWS(oldShard.ws, 3064, "Resuming the shard, closing old shard.");
// STOP OLD HEARTBEAT
clearInterval(oldShard.heartbeat.intervalId);
}
// CREATE A SHARD
const socket = await ws.createShard(shardId);
const sessionId = oldShard?.sessionId || "";
const previousSequenceNumber = oldShard?.previousSequenceNumber || 0;
ws.shards.set(shardId, {
id: shardId,
ws: socket,
resumeInterval: 0,
sessionId: sessionId,
previousSequenceNumber: previousSequenceNumber,
resuming: false,
ready: false,
unavailableGuildIds: new Set(),
lastAvailable: 0,
heartbeat: {
lastSentAt: 0,
lastReceivedAt: 0,
acknowledged: false,
keepAlive: false,
interval: 0,
intervalId: 0,
},
queue: oldShard?.queue || [],
processingQueue: false,
queueStartedAt: Date.now(),
queueCounter: 0,
});
// Resume on open
socket.onopen = () => {
ws.sendShardMessage(
shardId,
{
op: DiscordGatewayOpcodes.Resume,
d: {
token: ws.identifyPayload.token,
session_id: sessionId,
seq: previousSequenceNumber,
},
},
true
);
};
}