Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/data/src/loading-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Simple loading logging redux middleware.
*
* @type {import('redux').Middleware}
*/

const formatLoadedEntry = ( action ) => {
let formatted = action.selectorName;

if ( action.args?.length ) {
formatted += ': ' + JSON.stringify( action.args );
}

return formatted;
};

const getCurrentTime = () => {
const date = new Date();
return (
date.getHours() +
':' +
date.getMinutes() +
':' +
date.getSeconds() +
'.' +
date.getMilliseconds()
);
};

const actionTypeMap = {
START_RESOLUTION: 'Started loading',
START_RESOLUTIONS: 'Started bulk loading',
FINISH_RESOLUTION: 'Finished loading',
FINISH_RESOLUTIONS: 'Finished bulk loading',
FAIL_RESOLUTION: 'Failed loading',
FAIL_RESOLUTIONS: 'Failed bulk loading',
INVALIDATE_RESOLUTION: 'Canceled loading',
};

const loadingMiddleware = () => ( next ) => ( action ) => {
if ( actionTypeMap.hasOwnProperty( action.type ) ) {
// eslint-disable-next-line no-console
console.log(
getCurrentTime() +
': ' +
actionTypeMap[ action.type ] +
' ' +
formatLoadedEntry( action )
);
}

return next( action );
};

export default loadingMiddleware;
2 changes: 2 additions & 0 deletions packages/data/src/redux-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import createReduxRoutineMiddleware from '@wordpress/redux-routine';
*/
import { builtinControls } from '../controls';
import promise from '../promise-middleware';
import loadingMiddleware from '../loading-middleware';
import createResolversCacheMiddleware from '../resolvers-cache-middleware';
import createThunkMiddleware from './thunk-middleware';
import metadataReducer from './metadata/reducer';
Expand Down Expand Up @@ -242,6 +243,7 @@ function instantiateReduxStore( key, options, registry, thunkArgs ) {
const middlewares = [
createResolversCacheMiddleware( registry, key ),
promise,
loadingMiddleware,
createReduxRoutineMiddleware( normalizedControls ),
createThunkMiddleware( thunkArgs ),
];
Expand Down