diff --git a/nayeem.ts b/nayeem.ts new file mode 100644 index 000000000..d41dc534c --- /dev/null +++ b/nayeem.ts @@ -0,0 +1,38 @@ +interface Info { + root: string; + modules: { + specifier: string; + dependencies: { specifier: string }[]; + }[]; +} + +const info = JSON.parse(new TextDecoder().decode(await Deno.readAll(Deno.stdin))) as Info; + +const dependencyMap = new Map( + info.modules.map((module) => [ + module.specifier, + module.dependencies.map(({ specifier }) => new URL(specifier, module.specifier).href), + ]) +); + +const walking = new Set(); +const walked = new Set(); +function walk(module: string) { + walking.add(module); + for (const dependency of dependencyMap.get(module)!) { + if (walking.has(dependency)) { + // Reporting cycle. + const cycleArray = [...walking]; + const cycleIndex = cycleArray.indexOf(dependency); + cycleArray.splice(0, cycleIndex); + cycleArray.push(dependency); + console.log(`🔁 ${cycleArray.join(" \n=> ")}`); + console.log("=========") + } else if (!walked.has(dependency)) { + walk(dependency); + } + } + walking.delete(module); + walked.add(module); +} +walk(info.root); diff --git a/src/types/discordeno/event_handlers.ts b/src/types/discordeno/event_handlers.ts index 8755c37df..57f6eb357 100644 --- a/src/types/discordeno/event_handlers.ts +++ b/src/types/discordeno/event_handlers.ts @@ -1,31 +1,31 @@ -import { DiscordenoChannel } from "../../structures/channel.ts"; -import { DiscordenoGuild } from "../../structures/guild.ts"; -import { DiscordenoMember } from "../../structures/member.ts"; -import { DiscordenoMessage } from "../../structures/message.ts"; -import { DiscordenoRole } from "../../structures/role.ts"; -import { Collection } from "../../util/collection.ts"; -import { PresenceUpdate } from "../activity/presence_update.ts"; -import { StageInstance } from "../channels/stage_instance.ts"; -import { ThreadMember } from "../channels/threads/thread_member.ts"; -import { ThreadMembersUpdate } from "../channels/threads/thread_members_update.ts"; -import { Emoji } from "../emojis/emoji.ts"; -import { GatewayPayload } from "../gateway/gateway_payload.ts"; -import { DiscordGatewayPayload } from "../gateway/gateway_payload.ts"; -import { IntegrationCreateUpdate } from "../integrations/integration_create_update.ts"; -import { IntegrationDelete } from "../integrations/integration_delete.ts"; -import { ApplicationCommandCreateUpdateDelete } from "../interactions/commands/application_command_create_update_delete.ts"; -import { Interaction } from "../interactions/interaction.ts"; -import { InviteCreate } from "../invites/invite_create.ts"; -import { InviteDelete } from "../invites/invite_delete.ts"; -import { MessageReactionAdd } from "../messages/message_reaction_add.ts"; -import { MessageReactionRemove } from "../messages/message_reaction_remove.ts"; -import { MessageReactionRemoveAll } from "../messages/message_reaction_remove_all.ts"; -import { TypingStart } from "../misc/typing_start.ts"; -import { User } from "../users/user.ts"; -import { VoiceServerUpdate } from "../voice/voice_server_update.ts"; -import { VoiceState } from "../voice/voice_state.ts"; -import { DebugArg } from "./debug_arg.ts"; -import { GuildUpdateChange } from "./guild_update_change.ts"; +import type { DiscordenoChannel } from "../../structures/channel.ts"; +import type { DiscordenoGuild } from "../../structures/guild.ts"; +import type { DiscordenoMember } from "../../structures/member.ts"; +import type { DiscordenoMessage } from "../../structures/message.ts"; +import type { DiscordenoRole } from "../../structures/role.ts"; +import type { Collection } from "../../util/collection.ts"; +import type { PresenceUpdate } from "../activity/presence_update.ts"; +import type { StageInstance } from "../channels/stage_instance.ts"; +import type { ThreadMember } from "../channels/threads/thread_member.ts"; +import type { ThreadMembersUpdate } from "../channels/threads/thread_members_update.ts"; +import type { Emoji } from "../emojis/emoji.ts"; +import type { GatewayPayload } from "../gateway/gateway_payload.ts"; +import type { DiscordGatewayPayload } from "../gateway/gateway_payload.ts"; +import type { IntegrationCreateUpdate } from "../integrations/integration_create_update.ts"; +import type { IntegrationDelete } from "../integrations/integration_delete.ts"; +import type { ApplicationCommandCreateUpdateDelete } from "../interactions/commands/application_command_create_update_delete.ts"; +import type { Interaction } from "../interactions/interaction.ts"; +import type { InviteCreate } from "../invites/invite_create.ts"; +import type { InviteDelete } from "../invites/invite_delete.ts"; +import type { MessageReactionAdd } from "../messages/message_reaction_add.ts"; +import type { MessageReactionRemove } from "../messages/message_reaction_remove.ts"; +import type { MessageReactionRemoveAll } from "../messages/message_reaction_remove_all.ts"; +import type { TypingStart } from "../misc/typing_start.ts"; +import type { User } from "../users/user.ts"; +import type { VoiceServerUpdate } from "../voice/voice_server_update.ts"; +import type { VoiceState } from "../voice/voice_state.ts"; +import type { DebugArg } from "./debug_arg.ts"; +import type { GuildUpdateChange } from "./guild_update_change.ts"; export type EventHandlersDefinitions = { /** Sent when a new Slash Command is created, relevant to the current user. */ diff --git a/src/ws/README.md b/src/ws/README.md index 037f1fcb6..0be9e283e 100644 --- a/src/ws/README.md +++ b/src/ws/README.md @@ -174,8 +174,6 @@ export const ws = { log, /** Handles resharding the bot when necessary. */ resharder, - /** Cleanups loading shards that were unable to load. */ - cleanupLoadingShards, }; export interface DiscordenoShard { diff --git a/src/ws/cleanup_loading_shards.ts b/src/ws/cleanup_loading_shards.ts deleted file mode 100644 index e1f61df17..000000000 --- a/src/ws/cleanup_loading_shards.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { ws } from "./ws.ts"; - -/** The handler to clean up shards that identified but never received a READY. */ -export function cleanupLoadingShards() { - if (!ws.loadingShards.size) return; - - ws.log("DEBUG", "Running setTimeout in cleanupLoadingShards function."); - const now = Date.now(); - ws.loadingShards.forEach((loadingShard) => { - ws.log("DEBUG", `Running forEach loop in cleanupLoadingShards function.`); - // Not a minute yet. Max should be few seconds but do a minute to be safe. - if (now < loadingShard.startedAt + 60000) return; - - loadingShard.reject( - `[Identify Failure] Shard ${loadingShard.shardId} has not received READY event in over a minute.` - ); - }); - - if (ws.loadingShards.size) setTimeout(cleanupLoadingShards, 10000); -} diff --git a/src/ws/identify.ts b/src/ws/identify.ts index bc302d71e..31d07dd09 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -54,10 +54,13 @@ export function identify(shardId: number, maxShards: number) { ws.loadingShards.set(shardId, { shardId, resolve, - reject, startedAt: Date.now(), }); - ws.cleanupLoadingShards(); + setTimeout(() => { + reject( + `[Identify Failure] Shard ${shardId} has not received READY event in over a minute.` + ); + }, 600000); }); } diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 10c1188ae..5ed80567e 100644 --- a/src/ws/ws.ts +++ b/src/ws/ws.ts @@ -1,6 +1,5 @@ import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; import { Collection } from "../util/collection.ts"; -import { cleanupLoadingShards } from "./cleanup_loading_shards.ts"; import { closeWS } from "./close_ws.ts"; import { createShard } from "./create_shard.ts"; import { log } from "./events.ts"; @@ -78,7 +77,6 @@ export const ws = { { shardId: number; resolve: (value: unknown) => void; - reject: (reason?: unknown) => void; startedAt: number; } >(), @@ -112,8 +110,6 @@ export const ws = { log, /** Handles resharding the bot when necessary. */ resharder, - /** Cleanups loading shards that were unable to load. */ - cleanupLoadingShards, /** Handles the message events from websocket. */ handleOnMessage, /** Handles processing queue of requests send to this shard. */