{"size": 896, "ext": "js", "max_stars_repo_path": "lib/store-utils.js", "max_stars_repo_name": "mervynrussell/go-build", "max_stars_repo_stars_event_min_datetime": "2019-08-05T23:53:38.000Z", "max_stars_repo_stars_event_max_datetime": "2019-08-05T23:53:38.000Z", "max_issues_repo_path": "lib/store-utils.js", "max_issues_repo_name": "mervynrussell/go-build", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/store-utils.js", "max_forks_repo_name": "mervynrussell/go-build", "max_forks_repo_forks_event_min_datetime": "2019-03-06T23:40:53.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-07T01:13:05.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 19.9111111111, "max_line_length": 51, "alphanum_fraction": 0.5814732143}
'use babel';

import { getDeep } from './utils';

export function serialize (store) {
  const state = store.getState();
  return {
    selectedConfig: state.selectedConfig
  };
}

export function subscribe (store, path, callback) {
  const t = typeof path;
  if (t !== 'string' && t !== 'function') {
    throw new Error('unknown value for "path"');
  }

  let currentValue;

  const check = (state) => {
    if (!path) {
      callback(state);
      return state;
    }
    let newValue;
    if (t === 'string') {
      newValue = getDeep(state, path);
    } else if (t === 'function') {
      newValue = path(state);
    }
    if (newValue !== currentValue) {
      callback(newValue, currentValue);
    }
    return newValue;
  };
  const update = () => {
    const state = store.getState();
    currentValue = check(state);
  };

  update();

  return { dispose: store.subscribe(update) };
}
