fix: clean up web inbox listeners on close
This commit is contained in:
@@ -115,9 +115,12 @@ export async function monitorWebInbox(options: {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
sock.ev.on("messages.upsert", async (upsert) => {
|
const handleMessagesUpsert = async (upsert: {
|
||||||
|
type?: string;
|
||||||
|
messages?: Array<import("@whiskeysockets/baileys").WAMessage>;
|
||||||
|
}) => {
|
||||||
if (upsert.type !== "notify") return;
|
if (upsert.type !== "notify") return;
|
||||||
for (const msg of upsert.messages) {
|
for (const msg of upsert.messages ?? []) {
|
||||||
const id = msg.key?.id ?? undefined;
|
const id = msg.key?.id ?? undefined;
|
||||||
// De-dupe on message id; Baileys can emit retries.
|
// De-dupe on message id; Baileys can emit retries.
|
||||||
if (id && seen.has(id)) continue;
|
if (id && seen.has(id)) continue;
|
||||||
@@ -295,11 +298,12 @@ export async function monitorWebInbox(options: {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
sock.ev.on("messages.upsert", handleMessagesUpsert);
|
||||||
|
|
||||||
sock.ev.on(
|
const handleConnectionUpdate = (
|
||||||
"connection.update",
|
update: Partial<import("@whiskeysockets/baileys").ConnectionState>,
|
||||||
(update: Partial<import("@whiskeysockets/baileys").ConnectionState>) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
if (update.connection === "close") {
|
if (update.connection === "close") {
|
||||||
const status = getStatusCode(update.lastDisconnect?.error);
|
const status = getStatusCode(update.lastDisconnect?.error);
|
||||||
@@ -320,12 +324,22 @@ export async function monitorWebInbox(options: {
|
|||||||
error: err,
|
error: err,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
);
|
sock.ev.on("connection.update", handleConnectionUpdate);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
close: async () => {
|
close: async () => {
|
||||||
try {
|
try {
|
||||||
|
if (typeof sock.ev.off === "function") {
|
||||||
|
sock.ev.off("messages.upsert", handleMessagesUpsert);
|
||||||
|
sock.ev.off("connection.update", handleConnectionUpdate);
|
||||||
|
} else {
|
||||||
|
sock.ev.removeListener?.("messages.upsert", handleMessagesUpsert);
|
||||||
|
sock.ev.removeListener?.(
|
||||||
|
"connection.update",
|
||||||
|
handleConnectionUpdate,
|
||||||
|
);
|
||||||
|
}
|
||||||
sock.ws?.close();
|
sock.ws?.close();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logVerbose(`Socket close failed: ${String(err)}`);
|
logVerbose(`Socket close failed: ${String(err)}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user