import pick from '@tinkoff/utils/object/pick' ; import type { Middleware } from '../dispatcher/dispatcher.h' ; import { devTools } from './devTools' ; import { DISPATCH } from './constants' ; interface Options { pickState ? : string [ ] ; } export const middleware = ( { pickState } = { } ) : Middleware => ( api ) => ( next ) => { const getState = pickState && pickState . length ? ( ) => pick ( pickState , api . getState ( ) ) : api . getState ; devTools . subscribe ( ( message ) => { if ( message . type === 'DISPATCH' && message . state ) { switch ( message . payload . type ) { case 'RESET' : case 'COMMIT' : case 'ROLLBACK' : case 'JUMP_TO_STATE' : case 'JUMP_TO_ACTION' : case 'TOGGLE_ACTION' : case 'IMPORT_STATE' : default : } } else if ( message . type === 'ACTION' && message . payload ) { try { const action = new Function ( `return ${ message . payload }` ) ( ) ; api . dispatch ( action ) ; } catch ( e ) { devTools . error ( e . message ) ; } } } ) ; devTools . init ( getState ( ) ) ; return ( event ) => { const result = next ( event ) ; devTools . send ( { type : `${ DISPATCH } ${ event . type }` , event , } , getState ( ) ) ; return result ; } ; } ;