implement providerWatcher middleware
This commit is contained in:
parent
8e5af74312
commit
8f14494811
|
@ -2,19 +2,42 @@
|
||||||
import type { Store, AnyAction } from 'redux'
|
import type { Store, AnyAction } from 'redux'
|
||||||
import { type GlobalState } from '~/store/'
|
import { type GlobalState } from '~/store/'
|
||||||
import { ADD_PROVIDER, REMOVE_PROVIDER } from '../actions'
|
import { ADD_PROVIDER, REMOVE_PROVIDER } from '../actions'
|
||||||
|
import { getWeb3, getProviderInfo } from '~/logic/wallets/getWeb3'
|
||||||
|
import { fetchProvider } from '~/logic/wallets/store/actions'
|
||||||
|
|
||||||
const watchedActions = [
|
const watchedActions = [ADD_PROVIDER, REMOVE_PROVIDER]
|
||||||
ADD_PROVIDER,
|
|
||||||
REMOVE_PROVIDER,
|
let watcherInterval = null
|
||||||
]
|
|
||||||
|
|
||||||
const providerWatcherMware = (store: Store<GlobalState>) => (next: Function) => async (action: AnyAction) => {
|
const providerWatcherMware = (store: Store<GlobalState>) => (next: Function) => async (action: AnyAction) => {
|
||||||
const handledAction = next(action)
|
const handledAction = next(action)
|
||||||
|
|
||||||
if (watchedActions.includes(action.type)) {
|
if (watchedActions.includes(action.type)) {
|
||||||
const state: GlobalState = store.getState()
|
|
||||||
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
case ADD_PROVIDER: {
|
||||||
|
const currentProviderProps = action.payload.toJS()
|
||||||
|
|
||||||
|
if (watcherInterval) {
|
||||||
|
clearInterval(watcherInterval)
|
||||||
|
}
|
||||||
|
|
||||||
|
watcherInterval = setInterval(async () => {
|
||||||
|
const web3 = getWeb3()
|
||||||
|
const providerInfo = await getProviderInfo(web3)
|
||||||
|
|
||||||
|
if (
|
||||||
|
currentProviderProps.account !== providerInfo.account
|
||||||
|
|| currentProviderProps.network !== providerInfo.network
|
||||||
|
) {
|
||||||
|
store.dispatch(fetchProvider(web3, () => {}, () => {}))
|
||||||
|
}
|
||||||
|
}, 2000)
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case REMOVE_PROVIDER:
|
||||||
|
clearInterval(watcherInterval)
|
||||||
|
break
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import {
|
||||||
import thunk from 'redux-thunk'
|
import thunk from 'redux-thunk'
|
||||||
import safe, { SAFE_REDUCER_ID, type SafeReducerState as SafeState } from '~/routes/safe/store/reducer/safe'
|
import safe, { SAFE_REDUCER_ID, type SafeReducerState as SafeState } from '~/routes/safe/store/reducer/safe'
|
||||||
import safeStorage from '~/routes/safe/store/middleware/safeStorage'
|
import safeStorage from '~/routes/safe/store/middleware/safeStorage'
|
||||||
|
import providerWatcher from '~/logic/wallets/store/middlewares/providerWatcher'
|
||||||
import transactions, {
|
import transactions, {
|
||||||
type State as TransactionsState,
|
type State as TransactionsState,
|
||||||
TRANSACTIONS_REDUCER_ID,
|
TRANSACTIONS_REDUCER_ID,
|
||||||
|
@ -22,7 +23,9 @@ export const history = createBrowserHistory()
|
||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
|
||||||
const finalCreateStore = composeEnhancers(applyMiddleware(thunk, routerMiddleware(history), safeStorage))
|
const finalCreateStore = composeEnhancers(
|
||||||
|
applyMiddleware(thunk, routerMiddleware(history), safeStorage, providerWatcher),
|
||||||
|
)
|
||||||
|
|
||||||
export type GlobalState = {
|
export type GlobalState = {
|
||||||
providers: ProviderState,
|
providers: ProviderState,
|
||||||
|
@ -45,8 +48,4 @@ const reducers: Reducer<GlobalState> = combineReducers({
|
||||||
|
|
||||||
export const store: Store<GlobalState> = createStore(reducers, finalCreateStore)
|
export const store: Store<GlobalState> = createStore(reducers, finalCreateStore)
|
||||||
|
|
||||||
export const aNewStore = (localState?: Object): Store<GlobalState> => createStore(
|
export const aNewStore = (localState?: Object): Store<GlobalState> => createStore(reducers, localState, finalCreateStore)
|
||||||
reducers,
|
|
||||||
localState,
|
|
||||||
finalCreateStore,
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue