import { createLogger } from '../../../logger' ; import type { Action , Middleware } from '../../types' ; const log = createLogger ( __filename ) ; const shouldProcess = ( a ) : boolean => a . meta && ( a . meta . robot || a . meta . robotCommand ) ; export function apiClientMiddleware ( ) { const worker = new Worker ( './worker' , { type : 'module' } ) ; return store => { const { getState , dispatch } = store ; worker . onmessage = function handleWorkerMessage ( event ) { const action = event . data ; if ( action && action . payload ) { const error = action . error === true ? action . payload : action . payload . error ; if ( error ) { log . warn ( 'Error response from robot' , { actionType : action . type } ) ; if ( error . traceback ) log . warn ( error . traceback ) ; } } dispatch ( action ) ; } ; worker . postMessage ( { } ) ; return next => action => { if ( shouldProcess ( action ) ) { worker . postMessage ( { action , state : getState ( ) } ) ; } return next ( action ) ; } ; } ; }