Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit 12bc333

Browse files
committed
fix: mutiple new unidata
1 parent dccfacc commit 12bc333

3 files changed

Lines changed: 44 additions & 18 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@fortawesome/free-solid-svg-icons": "^6.1.1",
2121
"@fortawesome/vue-fontawesome": "^3.0.0-5",
2222
"@walletconnect/web3-provider": "^1.7.8",
23+
"async-lock": "^1.3.1",
2324
"axios": "^0.27.2",
2425
"bind-decorator": "^1.0.11",
2526
"buffer": "^6.0.3",
@@ -43,6 +44,7 @@
4344
"@crxjs/vite-plugin": "^1.0.7",
4445
"@originjs/vite-plugin-commonjs": "^1.0.3",
4546
"@tailwindcss/line-clamp": "^0.4.0",
47+
"@types/async-lock": "^1.1.5",
4648
"@types/chrome": "^0.0.188",
4749
"@types/lodash-es": "^4.17.6",
4850
"@types/markdown-it": "^12.2.3",

pnpm-lock.yaml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/content-script/index.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import '@/common/locationChange';
44
import { connect as w3mConnect } from '@/common/wallet';
55
import Unidata from 'unidata.js';
66
import TwitterHook from './hooks/twitter';
7+
import AsyncLock from 'async-lock';
78

89
import '../css/lib.css';
910

@@ -14,11 +15,14 @@ export interface Hook {
1415

1516
class CrossSyncContentScript {
1617
private unidata: Unidata | undefined;
18+
private lock: AsyncLock;
19+
1720
address: string | undefined;
1821

1922
constructor() {
2023
this.xlog('info', 'CorssSync Content Script is running');
2124
this.initHooks();
25+
this.lock = new AsyncLock();
2226
}
2327

2428
private async initHooks() {
@@ -35,26 +39,28 @@ class CrossSyncContentScript {
3539
}
3640

3741
async getUnidata() {
38-
if (!this.unidata) {
39-
try {
40-
const provider = await w3mConnect(); // Metamask
41-
if (provider) {
42-
this.address = (
43-
await provider.request({
44-
method: 'eth_accounts',
45-
})
46-
)?.[0];
47-
// this.xlog('info', 'Init unidata with address: ', this.address);
48-
this.unidata = new Unidata({
49-
ethereumProvider: provider,
50-
});
51-
} else {
52-
this.xlog('warn', 'No provider');
42+
await this.lock.acquire('getUnidata', async () => {
43+
if (!this.unidata) {
44+
try {
45+
const provider = await w3mConnect(); // Metamask
46+
if (provider) {
47+
this.address = (
48+
await provider.request({
49+
method: 'eth_accounts',
50+
})
51+
)?.[0];
52+
// this.xlog('info', 'Init unidata with address: ', this.address);
53+
this.unidata = new Unidata({
54+
ethereumProvider: provider,
55+
});
56+
} else {
57+
this.xlog('warn', 'No provider');
58+
}
59+
} catch (e: any) {
60+
this.xlog('error', 'Failed to initialize Unidata', e);
5361
}
54-
} catch (e: any) {
55-
this.xlog('error', 'Failed to initialize Unidata', e);
5662
}
57-
}
63+
});
5864
return this.unidata;
5965
}
6066

0 commit comments

Comments
 (0)