import { IVtxconfigResetSectionComplete } from '../../vtxconfig/actions/actionTypes' ; import { SagaIterator } from 'redux-saga' ; import { State } from '../../state' ; import { call , put , select } from 'redux-saga/effects' ; import { Dispatch } from 'redux' ; import { VtxpollSetIntervalId } from '../actions/action' ; import { VtxpollEntity } from '../../state/vtxpoll' ; import { ready } from '../../utils/ready' ; import { cloneDeep } from 'lodash' ; let timer = 0 ; let polling = null ; let last_polling_block = null ; const finished_all_calls = ( ) : boolean => { for ( const sec of Object . keys ( polling ) ) { if ( polling [ sec ] === true ) return false ; } return true ; } ; async function loop ( dispatcher , state_getter ) { if ( polling !== null && ! finished_all_calls ( ) ) return ; ++ timer ; const state = cloneDeep < State > ( state_getter ( ) ) ; const block_dependent = last_polling_block !== state . blocks . current_height ; last_polling_block = state . blocks . current_height ; if ( polling === null ) { polling = { } ; for ( const poll of state . vtxpoll . actions ) { polling [ poll . name ] = false ; } } if ( ready ( state ) ) { await Promise . all ( state . vtxpoll . actions . filter ( ( entity ) : boolean => ( timer % entity . interval === 0 && ! polling [ entity . name ] ) ) . map ( async ( entity ) : Promise < void > => { polling [ entity . name ] = true ; await entity . cb ( state , dispatcher , block_dependent ) ; polling [ entity . name ] = false ; } ) ) ; } } export function * VtxconfigResetSectionCompleteSaga ( dispatch , state_getter , action ) { if ( action . section === 'vtxconfig' ) { const state = yield select ( ) ; const clear = state . vtxconfig . web3 === null ; if ( state . vtxpoll . interval_id === undefined && ! clear ) { const interval_id = yield call ( setInterval , loop . bind ( this , dispatch , state_getter ) , state . vtxconfig . poll_timer ) ; yield put ( VtxpollSetIntervalId ( interval_id ) ) ; } if ( state . vtxpoll . interval_id !== undefined && clear ) { clearInterval ( state . vtxpoll . interval_id ) ; yield put ( VtxpollSetIntervalId ( undefined ) ) ; } } }