Created
August 7, 2024 06:19
-
-
Save juanmaguitar/c25569cb8194fd23fbc554846573f0a7 to your computer and use it in GitHub Desktop.
MIddleware to log actions gutenberg stores - packages/data/src/redux-store/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #region loggerMiddleware | |
const loggerMiddleware = ( optionsLogger ) => ( store ) => ( next ) => { | |
const actions = []; | |
return ( action ) => { | |
const { whatToLog, storeName, storesToLog } = optionsLogger; | |
if ( storesToLog.includes( storeName ) ) { | |
if ( whatToLog.includes( 'action' ) ) { | |
console.log( { storeName, ...action } ); | |
} | |
if ( whatToLog.includes( 'prevState' ) ) { | |
const prevState = store.getState(); | |
console.log( 'Previous state:', prevState ); | |
} | |
// actions.push( action.type ); | |
// console.log( 'Actions:', actions ); | |
} | |
const result = next( action ); | |
if ( storesToLog.includes( storeName ) ) { | |
if ( whatToLog.includes( 'nextState' ) ) { | |
const nextState = store.getState(); | |
console.log( 'Next state:', nextState ); | |
} | |
} | |
return result; | |
}; | |
}; | |
const optionsLogger = { | |
storesToLog: [ 'core/editor' ], | |
storeName: key, | |
whatToLog: [ 'action' ], | |
}; | |
const middlewares = [ | |
createResolversCacheMiddleware( registry, key ), | |
promise, | |
createReduxRoutineMiddleware( normalizedControls ), | |
createThunkMiddleware( thunkArgs ), | |
loggerMiddleware( optionsLogger ), | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment