Skip to content

Commit

Permalink
fix(lld): reset trustchain sdk when trustchain goes to null
Browse files Browse the repository at this point in the history
more info: live-13410
  • Loading branch information
gre committed Jul 18, 2024
1 parent d431534 commit f123228
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import os from "os";
import { from, lastValueFrom } from "rxjs";
import { useMemo } from "react";
import { useEffect, useMemo, useState } from "react";
import { getEnv } from "@ledgerhq/live-env";
import { getSdk } from "@ledgerhq/trustchain/index";
import { withDevice } from "@ledgerhq/live-common/hw/deviceAccess";
import Transport from "@ledgerhq/hw-transport";
import { trustchainLifecycle } from "@ledgerhq/live-wallet/walletsync/index";
import { useStore } from "react-redux";
import { useSelector, useStore } from "react-redux";
import { walletSelector } from "~/renderer/reducers/wallet";
import { wsStateSelector } from "@ledgerhq/live-wallet/store";
import { trustchainSelector } from "@ledgerhq/trustchain/store";

export function runWithDevice<T>(
deviceId: string | undefined,
Expand Down Expand Up @@ -40,9 +41,21 @@ export function useTrustchainSdk() {
}),
[store],
);

const trustchain = useSelector(trustchainSelector);

// each time Trustchain goes to null, we increment this counter to refresh the SDK that needs to forget its internal state
const [trustchainGoesToNullCounter, setTrustchainGoesToNullCounter] = useState(0);
useEffect(() => {
if (trustchain === null) {
setTrustchainGoesToNullCounter(c => c + 1);
}
}, [trustchain]);

const sdk = useMemo(
() => getSdk(isMockEnv, defaultContext, lifecycle),
[isMockEnv, defaultContext, lifecycle],
// eslint-disable-next-line react-hooks/exhaustive-deps
[isMockEnv, defaultContext, lifecycle, trustchainGoesToNullCounter],
);

return sdk;
Expand Down

0 comments on commit f123228

Please sign in to comment.