Skip to content

Commit 3b1cbbe

Browse files
committed
refactor(whatsapp): extract attached socket session
1 parent 703826c commit 3b1cbbe

6 files changed

Lines changed: 556 additions & 439 deletions

‎extensions/whatsapp/src/inbound/group-metadata-cache.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ function createOwner(params: {
4141
reconnectCache?: WhatsAppGroupMetadataCache;
4242
baileysCache?: WhatsAppBaileysGroupMetadataCache;
4343
}) {
44+
const emitter = params.sock.ev as unknown as EventEmitter;
4445
return createWhatsAppGroupMetadataCacheOwner({
4546
sock: params.sock,
4647
getCurrentSock: () => params.sock,
4748
resolveInboundJid: async (jid) => jid?.replace("@s.whatsapp.net", "") ?? null,
4849
reconnectCache: params.reconnectCache,
4950
baileysCache: params.baileysCache,
51+
listen: (event, listener) => {
52+
emitter.on(event, listener);
53+
return () => emitter.off(event, listener);
54+
},
5055
logVerbose: vi.fn(),
5156
logHydrationWarning: vi.fn(),
5257
});

‎extensions/whatsapp/src/inbound/group-metadata-cache.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
rememberWhatsAppBaileysCacheEntry,
99
type WhatsAppBaileysGroupMetadataCache,
1010
} from "./baileys-cache.js";
11-
import { attachEmitterListener } from "./lifecycle.js";
1211
import {
1312
addWhatsAppOutboundMentionsToContent,
1413
mayContainWhatsAppOutboundMention,
@@ -38,6 +37,7 @@ type GroupMetadataCacheOwnerParams = {
3837
resolveInboundJid: (jid: string | null | undefined) => Promise<string | null>;
3938
reconnectCache?: WhatsAppGroupMetadataCache;
4039
baileysCache?: WhatsAppBaileysGroupMetadataCache;
40+
listen: (event: string, listener: (...args: unknown[]) => void) => () => void;
4141
logVerbose: (message: string) => void;
4242
logHydrationWarning: (error: string) => void;
4343
};
@@ -219,13 +219,8 @@ export function createWhatsAppGroupMetadataCacheOwner(params: GroupMetadataCache
219219
return;
220220
}
221221
started = true;
222-
const emitter = params.sock.ev as unknown as {
223-
on: (event: string, listener: (...args: unknown[]) => void) => void;
224-
off?: (event: string, listener: (...args: unknown[]) => void) => void;
225-
removeListener?: (event: string, listener: (...args: unknown[]) => void) => void;
226-
};
227222
const listen = (event: string, listener: (...args: unknown[]) => void) => {
228-
detachListeners.push(attachEmitterListener(emitter, event, listener));
223+
detachListeners.push(params.listen(event, listener));
229224
};
230225

231226
listen("groups.upsert", ((groups: GroupMetadata[]) => {

0 commit comments

Comments
 (0)