{"size": 1014, "ext": "js", "max_stars_repo_path": "streams/scroll-tracker-stream.js", "max_stars_repo_name": "stefhatcher/morlock.js", "max_stars_repo_stars_event_min_datetime": "2015-01-28T22:28:41.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-08T12:40:24.000Z", "max_issues_repo_path": "streams/scroll-tracker-stream.js", "max_issues_repo_name": "stefhatcher/morlock.js", "max_issues_repo_issues_event_min_datetime": "2015-02-05T23:36:42.000Z", "max_issues_repo_issues_event_max_datetime": "2016-02-20T00:41:17.000Z", "max_forks_repo_path": "streams/scroll-tracker-stream.js", "max_forks_repo_name": "stefhatcher/morlock.js", "max_forks_repo_forks_event_min_datetime": "2015-04-06T20:44:56.000Z", "max_forks_repo_forks_event_max_datetime": "2016-11-24T19:22:57.000Z"}
{"max_stars_count": 33, "max_issues_count": 7, "max_forks_count": 5, "avg_line_length": 31.6875, "max_line_length": 88, "alphanum_fraction": 0.7021696252}
var Stream = require('../core/stream');
var ScrollStream = require('../streams/scroll-stream');

/**
 * Create a new Stream containing events which fire when a position has
 * been scrolled past.
 * @param {number} targetScrollY The position we are tracking.
 * @return {Stream} The resulting stream.
 */
function create(targetScrollY) {
  var scrollPositionStream = ScrollStream.create();
  var overTheLineStream = Stream.create();
  var pastScrollY = false;
  var firstRun = true;

  Stream.onValue(scrollPositionStream, function onScrollTrackPosition_(currentScrollY) {
    if ((firstRun || pastScrollY) && (currentScrollY < targetScrollY)) {
      pastScrollY = false;
      Stream.emit(overTheLineStream, ['before', targetScrollY]);
    } else if ((firstRun || !pastScrollY) && (currentScrollY >= targetScrollY)) {
      pastScrollY = true;
      Stream.emit(overTheLineStream, ['after', targetScrollY]);
    }

    firstRun = false;
  });

  return overTheLineStream;
}

module.exports = { create: create };
