wagtail/client/src/utils/performance.js
Thibaud Colas 61b6de2e4e Tidy up new React + API explorer for mobile (fixes #3607) (#3635)
* Remove useless CSS declaration

* Remove commented out styles

* Merge duplicate declarations

* Remove even more commented out code

* Move footer mq to footer declaration

* Remove more useless code

* Stop vendor prefixing for IE below 11

* Remove useless vendor prefixing

* Merge identical declarations

* Fix 1px overflow in content wrapper

* Fix explorer scrolling when open on mobile

* Remove unused import

* Add Redux performance measurements to explorer menu

* Rewrite explorer reducer to avoid unnecessary operations

* Stop changing reducer state on every action regardless of type

* Remove redundant children.isFetching property in nodes reducer

* Remove redundant children.isLoaded property in nodes reducer

* Remove redundant children.isError property in nodes reducer

* Refactor nodes explorer reducer with sub-reducer

* Fix linting issue

* Remove unused class name

* Change default icon className from empty string to null

* Remove old TODO comment

* Hoist icons in ExplorerItem component for better performance

* Add comment

* Add tooling for performance measurement of React components

* Clean up explorer panel component definition

* Make performance measurements opt-in

* Improve alignment of page explorer menu on mobile

* Close explorer on touchend rather than touchstart

* Comment out performance measurement code

* Remove fade transition completely
2017-06-09 00:08:04 +03:00

86 lines
2 KiB
JavaScript

import React, { Component } from 'react';
/* eslint-disable import/no-mutable-exports */
let perfMiddleware;
if (process.env.NODE_ENV !== 'production') {
/**
* Performance middleware for use with a Redux store.
* Will log the time taken by every action across all
* of the reducers of the store.
*/
perfMiddleware = () => {
/* eslint-disable no-console */
// `next` is a function that takes an 'action' and sends it through to the 'reducers'.
const middleware = (next) => (action) => {
let result;
if (!!console.time) {
console.time(action.type);
result = next(action);
console.timeEnd(action.type);
} else {
result = next(action);
}
return result;
};
return middleware;
};
}
let perfComponent;
if (process.env.NODE_ENV !== 'production') {
/**
* Wraps the passed in `Component` in a higher-order component. It can then
* measure the performance of every render of the `Component`.
*
* Can also be used as an ES2016 decorator.
* @param {ReactComponent} Component the component to wrap
* @return {ReactComponent} the wrapped component
* See https://github.com/sheepsteak/react-perf-component
*/
perfComponent = (Target) => {
if (process.env.NODE_ENV === 'production') {
return Target;
}
// eslint-disable-next-line global-require
const ReactPerf = require('react-addons-perf');
class Perf extends Component {
componentDidMount() {
ReactPerf.start();
}
componentDidUpdate() {
ReactPerf.stop();
const measurements = ReactPerf.getLastMeasurements();
ReactPerf.printWasted(measurements);
ReactPerf.start();
}
componentWillUnmount() {
ReactPerf.stop();
}
render() {
return <Target {...this.props} />;
}
}
Perf.displayName = `perf(${Target.displayName || Target.name || 'Component'})`;
Perf.WrappedComponent = Target;
return Perf;
};
}
export {
perfMiddleware,
perfComponent,
};