Skip to content

Commit ddb0854

Browse files
committed
refactor: close the opened but unused wsProviders at the first place
1 parent e019ccf commit ddb0854

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

packages/extension-polkagate/src/util/workers/utils/fastestEndpoint.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ export async function fastestEndpoint (endpoints) {
2222

2323
return {
2424
connection,
25+
connectionEndpoint: value,
2526
wsProvider
2627
};
2728
}).filter((i) => !!i);
2829

2930
const api = await Promise.any(connections.map(({ connection }) => connection));
3031

32+
// Find the matching connection that created this API
33+
// @ts-ignore
34+
const notConnectedEndpoint = connections.filter(({ connectionEndpoint }) => connectionEndpoint !== api?._options?.provider?.endpoint);
35+
// @ts-ignore
36+
const connectedEndpoint = connections.find(({ connectionEndpoint }) => connectionEndpoint === api?._options?.provider?.endpoint);
37+
38+
notConnectedEndpoint.forEach(({ wsProvider }) => {
39+
wsProvider.disconnect().catch(() => null);
40+
});
41+
3142
return {
3243
api,
33-
connections
44+
connections: connectedEndpoint ? [connectedEndpoint] : []
3445
};
3546
}

0 commit comments

Comments
 (0)