Skip to content

Commit

Permalink
Fix switching network and opening a second tab
Browse files Browse the repository at this point in the history
Wait for tabs to sync, and only skipUnlocking if tabs don't sync.

ValidatorList test relied on skipUnlocking being called immediately and
triggering selectNetwork.
  • Loading branch information
lukaw3d committed Nov 3, 2022
1 parent 0646478 commit c125984
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ exports[`<ValidatorList /> list should match snapshot 1`] = `
<span
class="c19"
>
TEST
ROSE
</span>
</span>
</div>
Expand Down Expand Up @@ -808,7 +808,7 @@ exports[`<ValidatorList /> list should match snapshot 1`] = `
<span
class="c19"
>
TEST
ROSE
</span>
</span>
</div>
Expand Down Expand Up @@ -870,7 +870,7 @@ exports[`<ValidatorList /> list should match snapshot 1`] = `
<span
class="c19"
>
TEST
ROSE
</span>
</span>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ describe('<ValidatorList />', () => {
let store: ReturnType<typeof configureAppStore>

beforeEach(() => {
store = configureAppStore()
store = configureAppStore({
network: {
chainContext: '',
epoch: 300,
selectedNetwork: 'mainnet',
ticker: 'ROSE',
minimumStakingAmount: 100,
},
})

jest.mocked(NodeInternal).mockReturnValue({
async stakingAccount(query: any) {
Expand Down
18 changes: 12 additions & 6 deletions src/app/state/network/saga.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as oasis from '@oasisprotocol/client'
import { PayloadAction } from '@reduxjs/toolkit'
import { persistActions } from 'app/state/persist'
import { selectNeedsPassword } from 'app/state/persist/selectors'
import { selectSkipUnlockingOnInit } from 'app/state/persist/selectors'
import { config } from 'config'
import { all, call, put, select, takeLatest } from 'typed-redux-saga'
import { RECEIVE_INIT_STATE } from 'redux-state-sync'
import { all, call, delay, put, race, select, take, takeLatest } from 'typed-redux-saga'
import { backend, backendApi } from 'vendors/backend'

import { networkActions } from '.'
Expand Down Expand Up @@ -59,14 +60,19 @@ export function* networkSaga() {
put(networkActions.selectNetwork(process.env.REACT_APP_LOCALNET ? 'local' : 'mainnet')),
)
yield* takeLatest(persistActions.resetRootState, function* () {
const needsPassword = yield* select(selectNeedsPassword)
if (!needsPassword) {
const skipUnlockOnInit = yield* select(selectSkipUnlockingOnInit)
if (skipUnlockOnInit) {
yield* put(persistActions.skipUnlocking())
}
})

const needsPassword = yield* select(selectNeedsPassword)
if (!needsPassword) {
// Wait for tabs to sync state. >5ms should be enough.
const maybeSynced = yield* race({
tabsSynced: take(RECEIVE_INIT_STATE),
thereAreNoOtherTabs: delay(50),
})
const skipUnlockOnInit = yield* select(selectSkipUnlockingOnInit)
if (!maybeSynced.tabsSynced && skipUnlockOnInit) {
yield* put(persistActions.skipUnlocking())
}
}
2 changes: 1 addition & 1 deletion src/app/state/persist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getInitialState(): PersistState {
// Disable persistence if tabs would override each other.
isPersistenceUnsupported: needsSyncingTabs && !isSyncingTabsSupported,
loading: false,
stringifiedEncryptionKey: localStorage.getItem(STORAGE_FIELD) ? undefined : 'skipped',
stringifiedEncryptionKey: undefined,
enteredWrongPassword: false,
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/app/state/persist/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const selectNeedsPassword = createSelector(
[selectSlice],
state => state.hasPersistedProfiles && !state.stringifiedEncryptionKey,
)
export const selectSkipUnlockingOnInit = createSelector(
[selectSlice],
state => !state.hasPersistedProfiles && !state.stringifiedEncryptionKey,
)
export const selectHasPersistedProfiles = createSelector([selectSlice], state => state.hasPersistedProfiles)
export const selectIsPersistenceUnsupported = createSelector(
[selectSlice],
Expand Down

0 comments on commit c125984

Please sign in to comment.