Skip to content

Commit

Permalink
fix/session proposal in cold start (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignacio Santise authored Apr 25, 2024
1 parent 2f6c283 commit 88359e5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions wallets/rn_cli_wallet/src/hooks/useInitializeWeb3Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function useInitializeWeb3Wallet() {
SettingsStore.setWallet(eip155Wallets[eip155Addresses[0]]);
await createWeb3Wallet(relayerRegionURL);
setInitialized(true);
SettingsStore.state.initPromiseResolver?.resolve(undefined);
} catch (err: unknown) {
console.log(err);
}
Expand Down
26 changes: 14 additions & 12 deletions wallets/rn_cli_wallet/src/screens/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import * as Sentry from '@sentry/react-native';
import BootSplash from 'react-native-bootsplash';

import {RootStackNavigator} from '../navigators/RootStackNavigator';
import {NotifyClientProvider} from '../provider/NotifyClientProvider';
import useInitializeWeb3Wallet from '@/hooks/useInitializeWeb3Wallet';
// import {useInitializeNotifyClient} from '@/hooks/useInitializeNotifyClient';
import useWalletConnectEventsManager from '@/hooks/useWalletConnectEventsManager';
import {web3wallet} from '@/utils/WalletConnectUtil';
import {RELAYER_EVENTS} from '@walletconnect/core';
import SettingsStore from '@/store/SettingsStore';

if (!__DEV__ && Config.ENV_SENTRY_DSN) {
Sentry.init({
Expand All @@ -28,10 +27,7 @@ const App = () => {
// Step 1 - Initialize wallets and wallet connect client
const initialized = useInitializeWeb3Wallet();

// Step 2 - Initialize Notify Client
// useInitializeNotifyClient();

// Step 3 - Once initialized, set up wallet connect event manager
// Step 2 - Once initialized, set up wallet connect event manager
useWalletConnectEventsManager(initialized);

useEffect(() => {
Expand All @@ -48,14 +44,20 @@ const App = () => {
}
}, [initialized]);

useEffect(() => {
/**
* Empty promise that resolves after web3wallet is initialized
* Usefull for cold starts
*/
SettingsStore.setInitPromise();
}, []);

return (
<NavigationContainer>
<NotifyClientProvider>
<StatusBar
barStyle={scheme === 'light' ? 'dark-content' : 'light-content'}
/>
<RootStackNavigator />
</NotifyClientProvider>
<StatusBar
barStyle={scheme === 'light' ? 'dark-content' : 'light-content'}
/>
<RootStackNavigator />
</NavigationContainer>
);
};
Expand Down
6 changes: 6 additions & 0 deletions wallets/rn_cli_wallet/src/screens/ConnectionsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {CopyURIDialog} from '@/components/CopyURIDialog';
import {ConnectionsStackScreenProps} from '@/utils/TypesUtil';
import ModalStore from '@/store/ModalStore';
import {useInitialURL} from '@/hooks/useInitialUrl';
import SettingsStore from '@/store/SettingsStore';

type Props = ConnectionsStackScreenProps<'ConnectionsScreen'>;

Expand All @@ -28,6 +29,11 @@ export default function ConnectionsScreen({route}: Props) {

async function pair(uri: string) {
ModalStore.open('LoadingModal', {});

/**
* Wait for settings web3wallet to be initialized before calling pair
*/
await SettingsStore.state.initPromise;
await web3wallet.pair({uri});
setCopyDialogVisible(false);
}
Expand Down
11 changes: 11 additions & 0 deletions wallets/rn_cli_wallet/src/store/SettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ interface State {
currentRequestVerifyContext?: Verify.Context;
sessions: SessionTypes.Struct[];
wallet: EIP155Lib | null;
initPromise?: Promise<void>;
initPromiseResolver?: {
resolve: (value: undefined) => void;
reject: (reason?: unknown) => void;
};
}

/**
Expand Down Expand Up @@ -61,6 +66,12 @@ const SettingsStore = {
state.sessions = sessions;
},

setInitPromise() {
state.initPromise = new Promise((resolve, reject) => {
state.initPromiseResolver = {resolve, reject};
});
},

toggleTestNets() {
state.testNets = !state.testNets;
if (state.testNets) {
Expand Down

0 comments on commit 88359e5

Please sign in to comment.