import { Store , Middleware } from 'redux' ; import { compatiblityCheckSucceeded , compatiblityCheckFailed , } from 'actions/initialization' ; import keys from 'lodash/keys' ; import each from 'lodash/each' ; import { foundMissingFeature } from 'actions/initialization' ; import featureDetails from 'utils/features' ; import { isActionOfType } from 'utils/store' ; const middleware = ( _ ) => ( next ) => async ( action ) => { next ( action ) ; if ( isActionOfType ( action , 'BROWSER_COMPATIBLITY_CHECK_REQUESTED' ) ) { try { const userAgent = action . payload . userAgent ; const Modernizr = require ( 'exports-loader?Modernizr!utils/modernizr.js' ) ; const features = keys ( featureDetails ) ; const total = features . length ; let j = 1 ; const completed = ( feature ) => ( isSupported ) => { j ++ ; if ( ! isSupported && ! featureDetails [ feature ] . optional ) { next ( foundMissingFeature ( { userAgent , feature : { id : feature , optional : featureDetails [ feature ] . optional || false , available : false , } , } ) ) ; } if ( j === total ) { next ( compatiblityCheckSucceeded ( { userAgent } ) ) ; } } ; each ( features , feature => Modernizr . on ( feature , completed ( feature ) ) ) ; } catch ( e ) { console . error ( `Failed to set scale automatically.` , e ) ; next ( compatiblityCheckFailed ( e ) ) ; } } } ; export default middleware ;