Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ export async function startWhatsAppConnection(
shouldIgnoreJid: (jid) => isJidGroup(jid),
});

let resolveConnection: (() => void) | null = null;
let rejectConnection: ((err: Error) => void) | null = null;

const connectionReady = new Promise<void>((resolve, reject) => {
resolveConnection = resolve;
rejectConnection = reject;
});

const connectionTimeout = setTimeout(() => {
rejectConnection?.(new Error("WA connection timeout after 20s"));
rejectConnection = null;
resolveConnection = null;
}, 20000);

sock.ev.process(async (events) => {
if (events["connection.update"]) {
const update = events["connection.update"];
Expand All @@ -140,14 +154,20 @@ export async function startWhatsAppConnection(
logger.info("Reconnecting...");
startWhatsAppConnection(logger);
} else {
clearTimeout(connectionTimeout);
rejectConnection?.(new Error("Logged out"));
rejectConnection = null;
resolveConnection = null;
logger.error(
"Connection closed: Logged Out. Please delete auth_info and restart."
);
process.exit(1);
}
} else if (connection === "open") {
logger.info(`Connection opened. WA user: ${sock.user?.name}`);
// console.log("Logged as", sock.user?.name);
clearTimeout(connectionTimeout);
resolveConnection?.();
resolveConnection = null;
Comment on lines +168 to +170

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial | 💤 Low value

Consider nulling rejectConnection for consistency.

On logout (lines 159-160) and timeout (lines 127-128), both resolveConnection and rejectConnection are nulled. Here only resolveConnection is nulled. While functionally harmless (the promise is settled), nulling both would be consistent.

Proposed fix
        clearTimeout(connectionTimeout);
        resolveConnection?.();
        resolveConnection = null;
+       rejectConnection = null;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
clearTimeout(connectionTimeout);
resolveConnection?.();
resolveConnection = null;
clearTimeout(connectionTimeout);
resolveConnection?.();
resolveConnection = null;
rejectConnection = null;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/whatsapp.ts` around lines 168 - 170, The logout/timeout handling nulls
both promise handlers but the success path only nulls resolveConnection; update
the success branch where connectionTimeout is cleared and resolveConnection is
set to null to also set rejectConnection = null for consistency and to avoid
leaving a stale reference to the reject handler (referencing resolveConnection,
rejectConnection, and connectionTimeout).

}
}

Expand Down Expand Up @@ -242,6 +262,7 @@ export async function startWhatsAppConnection(
}
});

await connectionReady;
return sock;
}

Expand Down