Closed SergeyYuhimovich closed 10 months ago
This has been fixed on the latest version of the library. Can you install the latest version and let us know if you're still encountering this issue?
@romin-halltari I tested today with "@magic-sdk/react-native-bare": "22.3.4",
and unfortunately this issue is still there. Same behavior: magic's async methods executed right after switch from network1 to network2 going well, switch back from network2 to network1 results in infinite await of magic.user.getInfo()
to resolve.
Hi @SergeyYuhimovich! I ran a little POC locally to check if there's any issues when switching between magic instances and rerendering the relayer. I am creating 2 magic instances (one for each network), and I'm calling getInfo
every time I switch to the other instance. This test ran fine for me though, which tells me you might have a race condition in your code. That would also explain the fact that when you set a timeout, the call to getInfo
resolves.
I would also recommend trying to not save the Magic
instance on your app state. Instead, save the relevant info that would help you create a Magic
instance the moment you need to switch to another network.
export default function App() {
const [network1Enabled, setNetwork1Enabled] = useState(true)
// first network magic instance
const magic1 = new Magic('pk_live_...', {
extensions: [
// extension for network 1
],
});
// second network magic instance
const magic2 = new Magic('pk_live..., {
extensions: [
// extension for network 2
],
});
useEffect(() => {
fetchInfo()
}, [network1Enabled]);
const fetchInfo = async () => {
if (network1Enabled) {
alert(JSON.stringify(await magic1.user.getInfo()))
} else {
alert(JSON.stringify(await magic2.user.getInfo()))
}
}
return (
<SafeAreaView>
<Text onPress={() => {
setNetwork1Enabled(!network1Enabled)
}}>Switch enabled magic instance</Text>
{network1Enabled ? <magic1.Relayer /> : <magic2.Relayer/>}
</SafeAreaView>
)
}
โ Prerequisites
magic-sdk
)?๐ Description
We're currently working on the refactoring of our networks switching functionality and we've found that there could be a possible issue with initialization of the Relayer, which leads to magic or web3 async methods neither resolve or reject (stuck indefinitely) if being executed right after Relayer re-render.
๐งฉ Steps to Reproduce
๐ค Expected behaviour
Changing selected network leads to re-render of
<magic.Relayer />
with correct settings, magic and web3 async methods which are executed right after are being resolved.๐ฎ Actual behavior
Async function that generates token for headers being stuck indefinitely either on
magic.user.getInfo()
orweb3.eth.personal.sign()
methods. We was able to "fix" it by adding promise in the start ofgenerateToken()
function that would delay it's execution for couple of seconds. In that case, magic's and web3's methods will work as expected.๐ป Code Sample
App.tsx
MagicRelayer.tsx
client.ts
generateToken.ts
createNetowrkSlice.ts
- a slice in Zustand store where we store our magic instances, network info, and change current network.Home.tsx
๐ Environment
magic-sdk
yarn