Skip to content

Commit

Permalink
Merge pull request #4560 from schmanu/fix/multiple-event-handler
Browse files Browse the repository at this point in the history
fix: handle multiple event handlers for the same event
  • Loading branch information
ganchoradkov authored May 24, 2024
2 parents 022e4d4 + 2faa79b commit 4613df2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/web3wallet/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ export class Engine extends IWeb3WalletEngine {

// ---------- public events ----------------------------------------------- //
public on: IWeb3WalletEngine["on"] = (name, listener) => {
this.setEvent(name, "off");
this.setEvent(name, "on");
return this.client.events.on(name, listener);
};

public once: IWeb3WalletEngine["once"] = (name, listener) => {
this.setEvent(name, "off");
this.setEvent(name, "once");
return this.client.events.once(name, listener);
};
Expand Down
21 changes: 20 additions & 1 deletion packages/web3wallet/test/sign.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { buildApprovedNamespaces, buildAuthObject, getSdkError } from "@walletco
import { toMiliseconds } from "@walletconnect/time";
import { Wallet as CryptoWallet } from "@ethersproject/wallet";

import { expect, describe, it, beforeEach, vi, beforeAll, afterAll, afterEach } from "vitest";
import { expect, describe, it, beforeEach, vi, beforeAll, afterEach, fn } from "vitest";
import { Web3Wallet, IWeb3Wallet } from "../src";
import {
disconnect,
Expand Down Expand Up @@ -464,6 +464,25 @@ describe("Sign Integration", () => {
expect(Object.keys(sessions)[0]).to.be.eq(session.topic);
});

it("should handle multiple session proposal listeners correctly", async () => {
const firstHandler = vi.fn();
const secondHandler = vi.fn();

await Promise.all([
new Promise<void>((resolve) => {
wallet.on("session_proposal", firstHandler);
wallet.on("session_proposal", secondHandler);
wallet.on("session_proposal", () => {
resolve();
});
}),
wallet.pair({ uri: uriString }),
]);

expect(firstHandler.mock.calls).toHaveLength(1);
expect(secondHandler.mock.calls).toHaveLength(1);
});

it("should get pending session proposals", async () => {
// first pair and approve session
await Promise.all([
Expand Down

0 comments on commit 4613df2

Please sign in to comment.