{"size": 1609, "ext": "js", "max_stars_repo_path": "app/devtools/logLifecycle.js", "max_stars_repo_name": "lelandrichardson/react-native-in-depth", "max_stars_repo_stars_event_min_datetime": "2017-03-30T07:54:11.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-28T01:23:05.000Z", "max_issues_repo_path": "app/devtools/logLifecycle.js", "max_issues_repo_name": "lelandrichardson/react-native-in-depth", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/devtools/logLifecycle.js", "max_forks_repo_name": "lelandrichardson/react-native-in-depth", "max_forks_repo_forks_event_min_datetime": "2017-04-18T18:03:18.000Z", "max_forks_repo_forks_event_max_datetime": "2019-10-01T08:45:35.000Z"}
{"max_stars_count": 83, "max_issues_count": null, "max_forks_count": 6, "avg_line_length": 35.7555555556, "max_line_length": 81, "alphanum_fraction": 0.6699813549}
import performanceNow from '../utils/performanceNow';

function logProtoMethod(proto, name, cname, all, color) {
  const original = proto[name];
  if (original) {
    // eslint-disable-next-line no-param-reassign
    proto[name] = function logger() {
      const start = performanceNow();
      // eslint-disable-next-line prefer-rest-params
      const result = original.apply(this, arguments);
      const time = performanceNow() - start;
      const roundedTime = Math.round(time * 10) / 10;
      if (color !== null) {
        console.log(`%c${cname}:${name} (${roundedTime}ms)`, `color: ${color};`);
      } else {
        console.log(`${cname}:${name} (${roundedTime}ms)`);
      }

      return result;
    };
  } else if (all) {
    // eslint-disable-next-line no-param-reassign
    proto[name] = () => console.log(`${cname}:${name}`);
  }
}

function logLifecycle(Component, all = false, color = null) {
  const name = Component.displayName || Component.name;
  const proto = Component.prototype;
  logProtoMethod(proto, 'componentWillMount', name, all, color);
  logProtoMethod(proto, 'componentDidMount', name, all, color);
  logProtoMethod(proto, 'componentWillUnmount', name, all, color);

  logProtoMethod(proto, 'componentWillReceiveProps', name, all, color);
  logProtoMethod(proto, 'render', name, all, color);

  logProtoMethod(proto, 'componentWillUpdate', name, all, color);
  logProtoMethod(proto, 'componentDidUpdate', name, all, color);

  // only ever log this if it's implemented
  logProtoMethod(proto, 'shouldComponentUpdate', name, false, color);
}

module.exports = logLifecycle;
