From 807f491e73acdfb984128ef4ff928baaf1188006 Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Fri, 12 Jan 2024 19:15:46 -0800 Subject: [PATCH] fix: Removes sign listeners after auth completion --- src/lib/Client.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/lib/Client.ts b/src/lib/Client.ts index 8ea8b96b5..2a88d1cc0 100644 --- a/src/lib/Client.ts +++ b/src/lib/Client.ts @@ -29,6 +29,9 @@ export class Client { conversations: Conversations contacts: Contacts codecRegistry: { [key: string]: XMTPModule.ContentCodec } + private static signSubscription: Subscription | null = null; + private static authSubscription: Subscription | null = null; + /** * Creates a new instance of the Client class using the provided signer. @@ -58,7 +61,7 @@ export class Client { > >((resolve, reject) => { ;(async () => { - XMTPModule.emitter.addListener( + this.signSubscription = XMTPModule.emitter.addListener( 'sign', async (message: { id: string; message: string }) => { const request: { id: string; message: string } = message @@ -80,14 +83,18 @@ export class Client { console.info(errorMessage, e) this.removeSubscription(enableSubscription) this.removeSubscription(createSubscription) + this.removeSignSubscription() + this.removeAuthSubscription() reject(errorMessage) } } ) - XMTPModule.emitter.addListener('authed', async () => { + this.authSubscription = XMTPModule.emitter.addListener('authed', async () => { this.removeSubscription(enableSubscription) this.removeSubscription(createSubscription) + this.removeSignSubscription() + this.removeAuthSubscription() const address = await signer.getAddress() resolve(new Client(address, opts?.codecs || [])) }) @@ -102,6 +109,20 @@ export class Client { }) } + private static removeSignSubscription(): void { + if (this.signSubscription) { + this.signSubscription.remove(); + this.signSubscription = null; + } + } + + private static removeAuthSubscription(): void { + if (this.authSubscription) { + this.authSubscription.remove(); + this.authSubscription = null; + } + } + /** * Creates a new instance of the XMTP Client with a randomly generated address. *