Skip to content

Commit

Permalink
Merge pull request #210 from xmtp/cv/fix-extra-sign-listeners
Browse files Browse the repository at this point in the history
Fixes extra sign listeners
  • Loading branch information
nplasterer authored Jan 14, 2024
2 parents d5ac040 + 807f491 commit ddae69a
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class Client<ContentTypes> {
conversations: Conversations<ContentTypes>
contacts: Contacts
codecRegistry: { [key: string]: XMTPModule.ContentCodec<unknown> }
private static signSubscription: Subscription | null = null;
private static authSubscription: Subscription | null = null;


/**
* Creates a new instance of the Client class using the provided signer.
Expand Down Expand Up @@ -58,7 +61,7 @@ export class Client<ContentTypes> {
>
>((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
Expand All @@ -80,14 +83,18 @@ export class Client<ContentTypes> {
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 || []))
})
Expand All @@ -102,6 +109,20 @@ export class Client<ContentTypes> {
})
}

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.
*
Expand Down

0 comments on commit ddae69a

Please sign in to comment.