{"size": 868, "ext": "js", "max_stars_repo_path": "counter-w-redux-clone/src/lib/reducks.js", "max_stars_repo_name": "karlpokus/react-road-trip", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "counter-w-redux-clone/src/lib/reducks.js", "max_issues_repo_name": "karlpokus/react-road-trip", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "counter-w-redux-clone/src/lib/reducks.js", "max_forks_repo_name": "karlpokus/react-road-trip", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.2564102564, "max_line_length": 57, "alphanum_fraction": 0.680875576}
import { isEqual } from 'underscore';

// equivalent to createStore
export default function reducks(reducer, ...middleware) {
  this.reducer = reducer;
  this.states = [];
  this.subscribers = [...middleware];
  // run reducer so we have initial state ready
  this.dispatch({type: '_INITIAL_RUN'});
}

reducks.prototype._runSubscribers = function() {
  if (this.subscribers.length) {
    this.subscribers.forEach(fn => fn(this.getState()));
  }
}

reducks.prototype.getState = function() {
  return this.states[0];
}

reducks.prototype.subscribe = function(fn) {
  this.subscribers.push(fn);
}

reducks.prototype.dispatch = function(action) {
  let prev = this.getState();
  let next = this.reducer(prev, action);

  if (!prev) {
    return this.states.unshift(next);
  }

  if (!isEqual(prev, next)) {
    this.states.unshift(next);
    this._runSubscribers();
  }
}
