<commit_before>var Url = require("url");
var Rx = require("rx");
var React = require("react");
var GameStream = require("./GameStream");
var Game = require("./Game");

var SPEEDS = [1, 2, 5, 10, 20, 50, 75, 100, 150, 200];
var DEFAULT_SPEED = 10;

var url = Url.parse(window.location.href, true);
var mount = document.getElementById("game");

function alwaysTrue () { return true; }
function increment (x) { return x + 1; }
function identity (x) { return x; }

function runGame (gameId) {
  // States
  var speed, refreshRate;
  var playing = false;
  var game = null;
  var buffered = 0;

  // Streams
  var gameStream = new Rx.ReplaySubject();
  GameStream(gameId).subscribe(gameStream);
  //var bufferedTurnsStream = gameStream.count();
  var gameInterruptions = new Rx.Subject();
  var refreshRateStream = Rx.Observable.generateWithRelativeTime(0, alwaysTrue, increment, identity, function () { return refreshRate; });

  // Functions
  function render () {
    if (!game) return; // Nothing to render yet
    React.renderComponent(Game({
      game: game,
      refreshRate: refreshRate,
      increaseSpeed: increaseSpeed,
      decreaseSpeed: decreaseSpeed,
      play: play,
      pause: pause,
      jump: jump,
      playing: playing,
      buffered: buffered,
      map: url.query.map
    }), mount);
  }

  function restart (startAtTurn) {
    playing = true;
    gameStream
      .skip(startAtTurn-1)
      .zip(refreshRateStream, identity)
      .bufferWithTime(1000 / 60)
      .takeUntil(gameInterruptions)
      .subscribe(function (g) {
        if (g.length === 0) return; // The buffer is empty
        game = g[g.length-1];
        render();
      }, function (err) {
        console.error(err);
      }, function () {
        playing = false;
        render();
      });
  }
<commit_msg>Polish the stream code thanks to <NAME> feedback :-)<commit_after>var Url = require("url");
var Rx = require("rx");
var React = require("react");
var GameStream = require("./GameStream");
var Game = require("./Game");

var SPEEDS = [1, 2, 5, 10, 20, 50, 75, 100, 150, 200];
var DEFAULT_SPEED = 10;

var url = Url.parse(window.location.href, true);
var mount = document.getElementById("game");

function alwaysTrue () { return true; }
function increment (x) { return x + 1; }
function identity (x) { return x; }

function isNotEmptyArray (array) {
  return array.length > 0;
}

function runGame (gameId) {
  /// States ///
  var speed, refreshRate;
  var playing = false;
  var game = null;
  var buffered = 0;

  /// Streams ///

  // The game stream is cached once forever
  var gameStream = new Rx.ReplaySubject();
  GameStream(gameId).subscribe(gameStream);

  var gameInterruptions = new Rx.Subject();
  var refreshRateStream = Rx.Observable.generateWithRelativeTime(0, alwaysTrue, increment, identity, function () { return refreshRate; });

  // Functions
  function render () {
    if (!game) return; // Nothing to render yet
    React.renderComponent(Game({
      game: game,
      refreshRate: refreshRate,
      increaseSpeed: increaseSpeed,
      decreaseSpeed: decreaseSpeed,
      play: play,
      pause: pause,
      jump: jump,
      playing: playing,
      buffered: buffered,
      map: url.query.map
    }), mount);
  }

  function restart (startAtTurn) {
    playing = true;
    gameStream
      .skip(startAtTurn-1) // Jump at startAtTurn
      .zip(refreshRateStream, identity) // Schedule the game stream to the dynamic refreshRate
      .bufferWithTime(1000 / 60) // Throttle the game states to allow very fast play
      .takeUntil(gameInterruptions) // Stop once there is a game interruption (pause / jump)
      .filter(isNotEmptyArray) // Filter empty buffers
      .subscribe(function (g) {
        game = g[g.length-1]; // The most recent game in the buffer
        render();
      }, function (err) {
        console.error(err);
        playing = false;
        render();
      }, function () {
        playing = false;
        render();
      });
  }

{'fraction_non_alphanumeric': 0.12957393483709273, 'fraction_numerical': 0.017042606516290727, 'mean_word_length': 2.8974609375, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5588156', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1178, 'n_words': 361}