import { find , flatten , some } from 'lodash' ; import { actions } from './actions' ; import { HostStatus } from '../host' ; import { ForwardingState } from './reducer' ; import { Dispatch , State } from '../types/redux' ; const getStatus = ( state , id ) : HostStatus | null => { const host = find ( state . hosts , { id } ) ; return ! host ? null : host . state . status ; } ; const getForwardings = ( state , id ) : ForwardingState [ ] => { return state . forwardings . filter ( x => x . id === id ) ; } ; const hostExist = ( state , id ) : boolean => some ( state . hosts , { id } ) ; const cancelForwardings = ( prevState , state , dispatch ) => { flatten ( state . hosts . map ( x => x . id ) . filter ( id => getStatus ( prevState , id ) !== 'disconnected' ) . filter ( id => getStatus ( state , id ) === 'disconnected' ) . map ( id => getForwardings ( state , id ) ) ) . forEach ( ( { id , fwdId } ) => { dispatch ( actions . forwardingDisconnected ( id , fwdId ) ) ; } ) ; flatten ( state . hosts . map ( x => x . id ) . filter ( id => getStatus ( prevState , id ) !== 'error' ) . filter ( id => getStatus ( state , id ) === 'error' ) . map ( id => getForwardings ( state , id ) ) ) . forEach ( ( { id , fwdId } ) => { dispatch ( actions . forwardingDisconnected ( id , fwdId ) ) ; } ) ; } ; const cleanupForwardings = ( prevState , state , dispatch ) => { flatten ( state . hosts . map ( x => x . id ) . filter ( id => hostExist ( prevState , id ) ) . filter ( id => ! hostExist ( state , id ) ) . map ( id => getForwardings ( state , id ) ) ) . forEach ( ( { id , fwdId } ) => { dispatch ( actions . forwardingDelete ( id , fwdId ) ) ; } ) ; } ; export const onStateChange = ( prevState , state , dispatch ) => { cancelForwardings ( prevState , state , dispatch ) ; cleanupForwardings ( prevState , state , dispatch ) ; } ;