{"size": 2049, "ext": "js", "max_stars_repo_path": "src/lib/Renderer.js", "max_stars_repo_name": "oneut/react-async-router", "max_stars_repo_stars_event_min_datetime": "2017-05-23T02:51:28.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-09T07:17:35.000Z", "max_issues_repo_path": "src/lib/Renderer.js", "max_issues_repo_name": "oneut/react-async-router", "max_issues_repo_issues_event_min_datetime": "2017-05-20T06:15:42.000Z", "max_issues_repo_issues_event_max_datetime": "2018-10-14T04:11:32.000Z", "max_forks_repo_path": "src/lib/Renderer.js", "max_forks_repo_name": "oneut/react-async-router", "max_forks_repo_forks_event_min_datetime": "2018-01-02T20:50:28.000Z", "max_forks_repo_forks_event_max_datetime": "2018-09-05T15:40:42.000Z"}
{"max_stars_count": 29, "max_issues_count": 41, "max_forks_count": 3, "avg_line_length": 19.3301886792, "max_line_length": 79, "alphanum_fraction": 0.6144460713}
export default class Renderer {
  constructor(pathname, component, params, requestCallback, prevRenderer) {
    this.data = {};
    this.component = component;
    this.params = params;
    this.requestCallback = requestCallback;
    this.pathname = pathname;
    this.prevRenderer = prevRenderer;
  }

  getPathname() {
    return this.pathname;
  }

  getComponent() {
    return this.component;
  }

  getParams() {
    return this.params;
  }

  getProps() {
    return {
      pathname: this.pathname,
      params: this.params
    };
  }

  getComponentProps() {
    return Object.assign(this.data, this.getProps());
  }

  getPrevProps() {
    return !!this.prevRenderer ? this.prevRenderer.getProps() : {};
  }

  getPrevComponentProps() {
    if (!!this.prevRenderer) {
      return this.prevRenderer.getComponentProps();
    }

    return {};
  }

  fireInitialPropsWillGet() {
    if (this.isFunction(this.component.initialPropsWillGet)) {
      this.component.initialPropsWillGet(this.getProps(), this.getPrevProps());
    }

    return this;
  }

  fireGetInitialProps() {
    if (!this.isFunction(this.component.getInitialProps)) {
      this.data = {};
      return Promise.resolve(this);
    }

    return Promise.resolve(
      this.component.getInitialProps(this.getProps(), this.getPrevProps())
    ).then((params) => {
      if (!!params) {
        this.data = params;
        return this;
      }
      this.data = {};
      return this;
    });
  }

  getInitialProps() {
    return this.data;
  }

  fireInitialPropsDidGet() {
    if (this.isFunction(this.component.initialPropsDidGet)) {
      this.component.initialPropsDidGet(
        this.getComponentProps(),
        this.getPrevComponentProps()
      );
    }

    return this;
  }

  fireRequestCallback() {
    this.requestCallback();
    return this;
  }

  isFunction(func) {
    return !!func && typeof func === "function";
  }

  setInitialProps(data) {
    if (!data) {
      this.data = {};
      return this;
    }

    this.data = data;
    return this;
  }
}
