Skip to content

Instantly share code, notes, and snippets.

@juanmaguitar
Created August 7, 2024 06:19
Show Gist options
  • Save juanmaguitar/c25569cb8194fd23fbc554846573f0a7 to your computer and use it in GitHub Desktop.
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
// #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