Skip to content

Commit

Permalink
Merge pull request #1852 from WalletConnect/feat/provider-subs-clean-up
Browse files Browse the repository at this point in the history
feat: subscriptions clean up
  • Loading branch information
ganchoradkov authored Jan 10, 2023
2 parents 50bd164 + d69e581 commit d263ac0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
16 changes: 9 additions & 7 deletions providers/universal-provider/src/UniversalProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ export class UniversalProvider implements IUniversalProvider {

this.setNamespaces(opts.namespaces);
this.createProviders();
this.cleanupPendingPairings();

await this.cleanupPendingPairings();
return opts.skipPairing === true ? undefined : await this.pair(opts.pairingTopic);
}

Expand Down Expand Up @@ -147,15 +146,18 @@ export class UniversalProvider implements IUniversalProvider {
}
}

public cleanupPendingPairings(): void {
public async cleanupPendingPairings(): Promise<void> {
this.logger.info("Cleaning up inactive pairings...");
const inactivePairings = this.client.pairing.getAll({ active: false });

if (!isValidArray(inactivePairings)) return;

inactivePairings.forEach((pairing) => {
this.client.pairing.delete(pairing.topic, getSdkError("USER_DISCONNECTED"));
});
await Promise.all([
inactivePairings.map((pairing) =>
this.client.pairing.delete(pairing.topic, getSdkError("USER_DISCONNECTED")),
),
inactivePairings.map((pairing) => this.client.core.relayer.unsubscribe(pairing.topic)),
inactivePairings.map((pairing) => this.client.core.expirer.del(pairing.topic)),
]);

this.logger.info(`Inactive pairings cleared: ${inactivePairings.length}`);
}
Expand Down
2 changes: 1 addition & 1 deletion providers/universal-provider/src/types/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ export interface IUniversalProvider extends IEthereumProvider {
pair: (pairingTopic: string | undefined) => Promise<SessionTypes.Struct>;
connect: (opts: ConnectParams) => Promise<SessionTypes.Struct | undefined>;
disconnect: () => void;
cleanupPendingPairings: () => void;
cleanupPendingPairings: () => Promise<void>;
}
22 changes: 9 additions & 13 deletions providers/universal-provider/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,38 +335,34 @@ describe("UniversalProvider", function () {
ethers = new providers.Web3Provider(afterDapp);
const afterAccounts = await ethers.listAccounts();
expect(accounts).to.toMatchObject(afterAccounts);

// delete
await disconnectSocket(afterDapp.client.core);
});
});
describe("pairings", () => {
it("should clean up inactive pairings", async () => {
const PAIRINGS_TO_CREATE = 5;
const dapp = await UniversalProvider.init({
...TEST_PROVIDER_OPTS,
name: "dapp",
});

for (let i = 0; i < PAIRINGS_TO_CREATE; i++) {
const { uri } = await dapp.client.connect({
const { uri } = await provider.client.connect({
requiredNamespaces: TEST_REQUIRED_NAMESPACES,
});

expect(!!uri).to.be.true;
expect(uri).to.be.a("string");
expect(dapp.client.pairing.getAll({ active: false }).length).to.eql(i + 1);
expect(provider.client.pairing.getAll({ active: false }).length).to.eql(i + 1);
}
dapp.cleanupPendingPairings();
expect(dapp.client.pairing.getAll({ active: false }).length).to.eql(0);

// disconnect
await disconnectSocket(dapp.client.core);
await provider.cleanupPendingPairings();
expect(provider.client.pairing.getAll({ active: false }).length).to.eql(0);
});
});
});

describe("validation", () => {
it("should not throw exception when setDefaultChain is called prematurely", async () => {
const provider = await UniversalProvider.init(TEST_PROVIDER_OPTS);
provider.setDefaultChain("eip155:1");
// disconnect
await disconnectSocket(provider.client.core);
});
});
});

0 comments on commit d263ac0

Please sign in to comment.