@@ -4,6 +4,7 @@ import '@/common/locationChange';
44import { connect as w3mConnect } from '@/common/wallet' ;
55import Unidata from 'unidata.js' ;
66import TwitterHook from './hooks/twitter' ;
7+ import AsyncLock from 'async-lock' ;
78
89import '../css/lib.css' ;
910
@@ -14,11 +15,14 @@ export interface Hook {
1415
1516class 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