Skip to content

Commit

Permalink
chore: apply PR feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
gre committed Jul 15, 2024
1 parent bc767e5 commit 89e3500
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Transport from "@ledgerhq/hw-transport";
import { trustchainLifecycle } from "@ledgerhq/live-wallet/walletsync/index";
import { useStore } from "react-redux";
import { walletSelector } from "~/renderer/reducers/wallet";
import { wsStateSelector } from "@ledgerhq/live-wallet/src/store";

export function runWithDevice<T>(
deviceId: string | undefined,
Expand Down Expand Up @@ -35,7 +36,7 @@ export function useTrustchainSdk() {
const lifecycle = useMemo(
() =>
trustchainLifecycle({
getCurrentWSState: () => walletSelector(store.getState()).wsState,
getCurrentWSState: () => wsStateSelector(walletSelector(store.getState())),
}),
[store],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { TrustchainEjected } from "@ledgerhq/trustchain/errors";
import { State } from "~/renderer/reducers";
import { cache as bridgeCache } from "~/renderer/bridge/cache";
import { setAccountNames, walletSyncUpdate } from "@ledgerhq/live-wallet/store";
import { setAccountNames, walletSyncUpdate, wsStateSelector } from "@ledgerhq/live-wallet/store";
import { replaceAccounts } from "~/renderer/actions/accounts";
import noop from "lodash/noop";
import { log } from "@ledgerhq/logs";
Expand All @@ -36,7 +36,7 @@ function localStateSelector(state: State): LocalState {
}

function latestDistantStateSelector(state: State): DistantState | null {
return walletSelector(state).wsState.data;
return wsStateSelector(walletSelector(state)).data;
}

export type WalletSyncUserState = {
Expand All @@ -52,8 +52,8 @@ export function useCloudSyncSDK(): CloudSyncSDK<Schema> {
const dispatch = useDispatch();

const getCurrentVersion = useCallback(() => {
const state = walletSelector(store.getState());
return state.wsState.version;
const wsState = wsStateSelector(walletSelector(store.getState()));
return wsState.version;
}, [store]);

const saveNewUpdate = useCallback(
Expand All @@ -64,7 +64,7 @@ export function useCloudSyncSDK(): CloudSyncSDK<Schema> {
const state = store.getState();
const version = event.version;
const data = event.data;
const wsState = walletSelector(state).wsState;
const wsState = wsStateSelector(walletSelector(state));
const localState = localStateSelector(state);
const ctx = { getAccountBridge, bridgeCache, blacklistedTokenIds: [] };

Expand Down
17 changes: 16 additions & 1 deletion libs/live-wallet/src/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
setAccountName,
setAccountNames,
setAccountStarred,
walletStateExportShouldDiffer,
walletSyncUpdate,
wsStateSelector,
} from "./store";
import { genAccount } from "@ledgerhq/coin-framework/mocks/account";
import type { Account } from "@ledgerhq/types-live";
Expand Down Expand Up @@ -185,7 +187,7 @@ describe("Wallet store", () => {
wsState: { data: {}, version: 42 },
};
const result = handlers.IMPORT_WALLET_SYNC(initialState, importWalletState(exportedState));
expect(result.wsState).toEqual({ data: {}, version: 42 });
expect(wsStateSelector(result)).toEqual({ data: {}, version: 42 });
});

it("can export the wallet state", () => {
Expand All @@ -197,4 +199,17 @@ describe("Wallet store", () => {
wsState: { data: {}, version: 42 },
});
});

it("walletStateExportShouldDiffer", () => {
const exportedState = {
wsState: { data: {}, version: 42 },
};
const result = handlers.IMPORT_WALLET_SYNC(initialState, importWalletState(exportedState));
expect(exportWalletState(result)).toEqual({
wsState: { data: {}, version: 42 },
});
expect(walletStateExportShouldDiffer(initialState, result)).toBe(true);
expect(walletStateExportShouldDiffer(initialState, initialState)).toBe(false);
expect(walletStateExportShouldDiffer(result, result)).toBe(false);
});
});
2 changes: 2 additions & 0 deletions libs/live-wallet/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,5 @@ export const exportWalletState = (state: WalletState): ExportedWalletState => ({
export const walletStateExportShouldDiffer = (a: WalletState, b: WalletState): boolean => {
return a.wsState !== b.wsState;
};

export const wsStateSelector = (state: WalletState): WSState => state.wsState;

0 comments on commit 89e3500

Please sign in to comment.