Skip to content

Commit

Permalink
Merge pull request #544 from mysteriumnetwork/fix-fetch-node-status
Browse files Browse the repository at this point in the history
Fetch node state only when user is authenticated
  • Loading branch information
redmundas authored Nov 11, 2024
2 parents 206105a + 2e201d5 commit 0a01fca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/mobx/Indications.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class IndicationsStore {
status: string = 'pending'
quality: number = 0
loading: boolean = true
interval: ReturnType<typeof setInterval> | undefined = undefined

constructor() {
makeObservable(this, {
Expand All @@ -32,7 +33,9 @@ export class IndicationsStore {
}

setupReactions(): void {
setInterval(() => this.fetchState(), 60 * 1000)
if (!this.interval) {
this.interval = setInterval(() => this.fetchState(), 60 * 1000)
}
this.fetchState()
}

Expand Down
1 change: 0 additions & 1 deletion src/mobx/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class MobXRootStore {
constructor() {
this.sessionsStore = new SessionsStore()
this.indicationsStore = new IndicationsStore()
this.indicationsStore.setupReactions()
this.beneficiaryStore = new BeneficiaryStore()
this.clickBoardingStore = new ClickBoardingStore()
this.menuStore = new MenuStore()
Expand Down
8 changes: 8 additions & 0 deletions src/pages/StateInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import { selectors } from '../redux/selectors'
import { tequila } from '../api/tequila'
import ConnectToSSE from '../sse/server-sent-events'
import listeners from '../redux/listeners'
import { useStores } from '../mobx/store'
import { IdentityRegistrationStatusListener } from './Authenticated/Components/Listeners/IdentityRegistrationStatusListener'

interface Props {
children: ReactNode
}

export const StateInitializer = ({ children }: Props) => {
const { indicationsStore } = useStores()
const dispatch = useAppDispatch()
const actions = {
updateLoadingStore: async (loading: boolean) => dispatch(updateLoadingStore(loading)),
Expand Down Expand Up @@ -56,6 +58,12 @@ export const StateInitializer = ({ children }: Props) => {
}, [loggedIn])

useEffect(() => {
listeners.registerAuthenticatedListener(({ payload }) => {
if (!payload.authenticated) {
return
}
indicationsStore.setupReactions()
})
listeners.registerMinimumRegistrationAmountListener()
}, [])

Expand Down
13 changes: 12 additions & 1 deletion src/redux/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { myst } from '../commons/mysts'
import { addListener, PayloadAction } from '@reduxjs/toolkit'
import { FeesResponse } from 'mysterium-vpn-js'
import { store } from './store'
import { updateFeesStore, updateMinimumRegistrationAmountWeiStore } from './app.slice'
import { updateAuthenticatedStore, updateFeesStore, updateMinimumRegistrationAmountWeiStore } from './app.slice'

const FEE_SPIKE_MULTIPLIER = 1.5

Expand All @@ -26,7 +26,18 @@ const registerMinimumRegistrationAmountListener = () =>
}),
)

const registerAuthenticatedListener = (
callback: (action: PayloadAction<{ authenticated: boolean }>) => Promise<void> | void,
) =>
store.dispatch(
addListener({
actionCreator: updateAuthenticatedStore,
effect: callback,
}),
)

const listeners = {
registerAuthenticatedListener,
registerMinimumRegistrationAmountListener,
}

Expand Down

0 comments on commit 0a01fca

Please sign in to comment.