<commit_before>var Emitter = require("../common/emitter");


// Preprocess event stream and insert summary info events?
//   - close event if user idle for N seconds
// "Move mouse"
// "Click"
// "Type"
//

/**
 *
 * Events:
 *
 */
var Player = function(env, wd) {
  var me = this;
  me._env = env;
  me._webdriver = wd;
  me._events = [];
  me._event_index = 0;
  me._mouse_last = { x: 0, y: 0 };
  me._start_time = null;
  // we can increment this when things get delayed/paused
  me._delayed_time = 0;

  me._event_key_code_to_webdriver_key_code = {};
  EVENT_CONTROL_KEYS.forEach(function(pair) {
    me._event_key_code_to_webdriver_key_code[pair[0]] = pair[1];
  });

  me._emitter = new Emitter();
  me._before_listeners = [];
  me._after_listeners = [];
};

Player.defaultOptions = function() {
  return {};
};

Player.prototype.events = function(events) {
  var me = this;
  events.forEach(function(e) {
    me._events.push(e);
  });
};

Player.prototype.emitter = function() {
  return this._emitter;
};

Player.prototype.beforeEvent = function(listener) {
  this._before_listeners.push(listener);
};

Player.prototype.afterEvent = function(listener) {
  this._after_listeners.push(listener);
};

Player.prototype.play = function() {
  var me = this;
  me._emitter.emit("play", me._events);
  return this._playNextEvent();
};

Player.prototype.playEvent = function(e) {
  var me = this;

  var promise = webdriver.promise.resolved();
  me._before_listeners.forEach(function(listener) {
    promise = promise.then(function() {
      return listener.call(null, e, me._webdriver);
    });
  });

  if (webdriver.promise.isPromise(promise)) {
    promise = promise.then(function() {
      me._emitter.emit("before_event", e);
      return me._doEvent(e);
    });
  } else {
    me._emitter.emit("before_event", e);
    promise = me._doEvent(e);
  }

  me._after_listeners.reverse().forEach(function(listener) {
    promise = promise.then(function() {
      return listener.call(null, e, me._webdriver);
    });
  });

  promise = promise.then(function() {
    me._emitter.emit("after_event", e);
  });

  return promise;
};

Player.prototype._doEvent = function(e) {
  var me = this;
  var wd = me._webdriver;

  console.log("Cyclops player: playing event", e);

  if (e.type === "config") {
    if (e.width !== undefined && e.height !== undefined) {
      return wd.manage().window().setSize(e.width, e.height);
    }
    return webdriver.promise.resolved();
  } else if (e.type === "start") {
    me._start_time = me._env.now();
    return wd.findElement(document.body).then(function(body) {
      return wd.actions().mouseMove(body, { x: 0, y: 0 }).perform();
    });
  } else if (e.type === "resize") {
    return wd.manage().window().setSize(e.width, e.height);
  } else if (e.type === "mousemove") {
    // Mouse move wants relative, since we control every movement we make
    // absolute.
    // TODO: this is fragile?
    var offset = {
      x: e.x - me._mouse_last.x,
      y: e.y - me._mouse_last.y
    };
    me._mouse_last.x = e.x;
    me._mouse_last.y = e.y;
    return wd.actions().mouseMove(offset).perform();
  } else if (e.type === "mousedown") {
    return wd.actions().mouseDown(e.button).perform();
  } else if (e.type === "mouseup") {
    return wd.actions().mouseUp(e.button).perform();
  } else if (e.type === "keydown" || e.type === "keyup" || e.type === "keypress") {
    return me._keyEventToWebdriverAction(e);
  }

  //xcxc can't synthesize wheel events :(

  //xcxc pass to listener
  return webdriver.promise.resolved();
};

Player.prototype._playNextEvent = function() {
  var me = this;
  var e = me._nextEvent();
  if (e === null) {
    console.log("Cyclops player: no more events, stopping playback");
    return webdriver.promise.resolved();
  }

  var play;
  if (me._start_time !== null) {<commit_msg>cleanup<commit_after>var Emitter = require("../common/emitter");


// Preprocess event stream and insert summary info events?
//   - close event if user idle for N seconds
// "Move mouse"
// "Click"
// "Type"
//

/**
 *
 * Events:
 *
 */
var Player = function(env, wd) {
  var me = this;
  me._env = env;
  me._webdriver = wd;
  me._events = [];
  me._event_index = 0;
  me._mouse_last = { x: 0, y: 0 };
  me._start_time = null;
  // we can increment this when things get delayed/paused
  me._delayed_time = 0;

  me._event_key_code_to_webdriver_key_code = {};
  EVENT_CONTROL_KEYS.forEach(function(pair) {
    me._event_key_code_to_webdriver_key_code[pair[0]] = pair[1];
  });

  me._emitter = new Emitter();
  me._before_listeners = [];
  me._after_listeners = [];
};

Player.defaultOptions = function() {
  return {};
};

Player.prototype.events = function(events) {
  var me = this;
  events.forEach(function(e) {
    me._events.push(e);
  });
};

Player.prototype.emitter = function() {
  return this._emitter;
};

Player.prototype.beforeEvent = function(listener) {
  this._before_listeners.push(listener);
};

Player.prototype.afterEvent = function(listener) {
  this._after_listeners.push(listener);
};

Player.prototype.play = function() {
  var me = this;
  me._emitter.emit("play", me._events);
  return this._playNextEvent();
};

Player.prototype.playEvent = function(e) {
  var me = this;

  var promise = webdriver.promise.resolved();
  me._before_listeners.forEach(function(listener) {
    promise = promise.then(function() {
      return listener.call(null, e, me._webdriver);
    });
  });

  if (webdriver.promise.isPromise(promise)) {
    promise = promise.then(function() {
      me._emitter.emit("before_event", e);
      return me._doEvent(e);
    });
  } else {
    me._emitter.emit("before_event", e);
    promise = me._doEvent(e);
  }

  me._after_listeners.reverse().forEach(function(listener) {
    promise = promise.then(function() {
      return listener.call(null, e, me._webdriver);
    });
  });

  promise = promise.then(function() {
    me._emitter.emit("after_event", e);
  });

  return promise;
};

Player.prototype._doEvent = function(e) {
  var me = this;
  var wd = me._webdriver;

  console.log("Cyclops player: playing event", e);

  if (e.type === "config") {
    if (e.width !== undefined && e.height !== undefined) {
      return wd.manage().window().setSize(e.width, e.height);
    }
    return webdriver.promise.resolved();
  } else if (e.type === "start") {
    me._start_time = me._env.now();
    return wd.findElement(document.body).then(function(body) {
      return wd.actions().mouseMove(body, { x: 0, y: 0 }).perform();
    });
  } else if (e.type === "resize") {
    return wd.manage().window().setSize(e.width, e.height);
  } else if (e.type === "mousemove") {
    // Mouse move wants relative, since we control every movement we make
    // absolute.
    var offset = {
      x: e.x - me._mouse_last.x,
      y: e.y - me._mouse_last.y
    };
    me._mouse_last.x = e.x;
    me._mouse_last.y = e.y;
    return wd.actions().mouseMove(offset).perform();
  } else if (e.type === "mousedown") {
    return wd.actions().mouseDown(e.button).perform();
  } else if (e.type === "mouseup") {
    return wd.actions().mouseUp(e.button).perform();
  } else if (e.type === "keydown" || e.type === "keyup" || e.type === "keypress") {
    return me._keyEventToWebdriverAction(e);
  }

  // TODO: figure out how to simulate scrolling, since we can't synthesize
  // wheel events.  :(

  return webdriver.promise.resolved();
};

Player.prototype._playNextEvent = function() {
  var me = this;
  var e = me._nextEvent();
  if (e === null) {
    console.log("Cyclops player: no more events, stopping playback");
    return webdriver.promise.resolved();
  }

  var play;
  if (me._start_time !== null) {
{'fraction_non_alphanumeric': 0.1698334200937012, 'fraction_numerical': 0.002082248828735034, 'mean_word_length': 3.264705882352941, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2600925', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2613, 'n_words': 548}