mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
improve startup time .-.
This commit is contained in:
@@ -5,6 +5,7 @@ import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.
|
||||
import type { Guild } from "../../types/guilds/guild.ts";
|
||||
import { snowflakeToBigint } from "../../util/bigint.ts";
|
||||
import { ws } from "../../ws/ws.ts";
|
||||
import { guildAvailable } from "../misc/READY.ts";
|
||||
|
||||
export async function handleGuildCreate(data: DiscordGatewayPayload, shardId: number) {
|
||||
const payload = data.d as Guild;
|
||||
@@ -18,8 +19,7 @@ export async function handleGuildCreate(data: DiscordGatewayPayload, shardId: nu
|
||||
|
||||
if (shard?.unavailableGuildIds.has(guild.id)) {
|
||||
await cacheHandlers.delete("unavailableGuilds", guild.id);
|
||||
shard.unavailableGuildIds.delete(guild.id);
|
||||
shard.lastAvailable = Date.now();
|
||||
guildAvailable(shard, guild.id);
|
||||
|
||||
return eventHandlers.guildAvailable?.(guild);
|
||||
}
|
||||
|
||||
+16
-22
@@ -23,33 +23,27 @@ export function handleReady(data: DiscordGatewayPayload, shardId: number) {
|
||||
shard.ready = false;
|
||||
// All guilds are unavailable at first
|
||||
shard.unavailableGuildIds = new Set(payload.guilds.map((g) => snowflakeToBigint(g.id)));
|
||||
// Set the last available to now
|
||||
shard.lastAvailable = Date.now();
|
||||
|
||||
// Start ready check in 2 seconds
|
||||
setTimeout(() => {
|
||||
eventHandlers.debug?.("loop", `1. Running setTimeout in READY file.`);
|
||||
checkReady(payload, shard);
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
/** This function checks if the shard is fully loaded */
|
||||
function checkReady(payload: Ready, shard: DiscordenoShard) {
|
||||
// Check if all guilds were loaded
|
||||
if (!shard.unavailableGuildIds.size) return loaded(shard);
|
||||
|
||||
// If the last GUILD_CREATE was received 5 seconds ago, the remaining guilds are most likely not available
|
||||
if (shard.lastAvailable + 5000 < Date.now()) {
|
||||
// Falied to load check
|
||||
shard.failedToLoadTimeoutId = setTimeout(() => {
|
||||
eventHandlers.shardFailedToLoad?.(shard.id, shard.unavailableGuildIds);
|
||||
// Force execute the loaded function to prevent infinite loop
|
||||
return loaded(shard);
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
// Not all guilds were loaded but 5 seconds haven't passed so check again
|
||||
setTimeout(() => {
|
||||
eventHandlers.debug?.("loop", `2. Running setTimeout in READY file.`);
|
||||
checkReady(payload, shard);
|
||||
}, 2000);
|
||||
export function guildAvailable(shard: DiscordenoShard, guildId: bigint) {
|
||||
if (!shard.failedToLoadTimeoutId) return;
|
||||
|
||||
clearTimeout(shard.failedToLoadTimeoutId);
|
||||
shard.unavailableGuildIds.delete(guildId);
|
||||
if (!shard.unavailableGuildIds.size) return loaded(shard);
|
||||
|
||||
shard.failedToLoadTimeoutId = setTimeout(() => {
|
||||
eventHandlers.shardFailedToLoad?.(shard.id, shard.unavailableGuildIds);
|
||||
// Force execute the loaded function to prevent infinite loop
|
||||
return loaded(shard);
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
function loaded(shard: DiscordenoShard) {
|
||||
|
||||
@@ -24,7 +24,6 @@ export function identify(shardId: number, maxShards: number) {
|
||||
resuming: false,
|
||||
ready: false,
|
||||
unavailableGuildIds: new Set(),
|
||||
lastAvailable: 0,
|
||||
heartbeat: {
|
||||
lastSentAt: 0,
|
||||
lastReceivedAt: 0,
|
||||
|
||||
@@ -30,7 +30,6 @@ export function resume(shardId: number) {
|
||||
resuming: false,
|
||||
ready: false,
|
||||
unavailableGuildIds: new Set(),
|
||||
lastAvailable: 0,
|
||||
heartbeat: {
|
||||
lastSentAt: 0,
|
||||
lastReceivedAt: 0,
|
||||
|
||||
+1
-2
@@ -139,8 +139,7 @@ export interface DiscordenoShard {
|
||||
ready: boolean;
|
||||
/** The list of guild ids that are currently unavailable due to an outage. */
|
||||
unavailableGuildIds: Set<bigint>;
|
||||
/** Last time when a GUILD_CREATE event has been received for an unavailable guild. This is used to prevent infinite loops in the READY event handler. */
|
||||
lastAvailable: number;
|
||||
failedToLoadTimeoutId?: number;
|
||||
heartbeat: {
|
||||
/** The exact timestamp the last heartbeat was sent. */
|
||||
lastSentAt: number;
|
||||
|
||||
Reference in New Issue
Block a user