<commit_before>'use strict';

var DocumentSession = require('./DocumentSession');
var DocumentChange = require('./DocumentChange');
var WebSocket = require('../util/WebSocket');
var uuid = require('../util/uuid');

/*
  Session that is connected to a Substance Hub allowing
  collaboration in real-time.

  TODO:
    - error handling strategies
      - e.g. a commit message never gets through to the server
         - how to detect those errors? timeout?
         - how to handle them?
           - resend same commit?
           - just include affected changes with the next regular commit?
*/
function CollabSession(doc, options) {
  CollabSession.super.call(this, doc, options);

  // TODO: this should be retrieved from the server?
  this.sessionId = uuid();

  // TODO: The CollabSession or the doc needs to be aware of a doc id
  // that corresponds to the doc on the server. For now we just
  // store it on the document instance.
  this.doc.id = 'doc-15';

  // TODO: Also we need to somehow store a version number (local version)
  this.doc.version = 1;

  this.messageQueue = options.messageQueue;
  this.nextCommit = null;
  this.ws = new WebSocket(this.messageQueue);

  this.ws.onopen = this._onConnected.bind(this);
  this.ws.onmessage = this._onMessage.bind(this);

  // Try to commit changes to the server every 1s
  setInterval(this.commit.bind(this), 1000);

  // whenever a change of a new collaborator is received
  // we add a record here
  this.collaborators = {};
  this.sessionIdPool = [1,2,3,4,5,6,7,8,9,10];
}

CollabSession.Prototype = function() {

  var _super = Object.getPrototypeOf(this);

  /*
    Send local changes to the world.
  */
  this.commit = function() {
    // If there is something to commit and there is no commit pending
    if (this.nextCommit && !this._committing) {
      // console.log('committing', this.nextCommit);
      this.ws.send(['commit', this._serializeChange(this.nextCommit), this.doc.version]);
      this._pendingCommit = this.nextCommit;
      this.nextCommit = null;
      this._committing = true;
    }
  };

  /*
    We record all local changes into a single change (aka commit) that
  */
  this._recordCommit = function(change) {
    if (!this.nextCommit) {
      this.nextCommit = change;
    } else {
      // Merge new change into nextCommit
      this.nextCommit.ops = this.nextCommit.ops.concat(change.ops);
      this.nextCommit.after = change.after;
    }
  };

  this.afterDocumentChange = function(change, info) {
    _super.afterDocumentChange.apply(this, arguments);

    // Record local chagnes into nextCommit
    if (!info.remote) {
      this._recordCommit(change);<commit_msg>Added CollabSession.start() and stop().<commit_after>'use strict';

var DocumentSession = require('./DocumentSession');
var DocumentChange = require('./DocumentChange');
var WebSocket = require('../util/WebSocket');
var uuid = require('../util/uuid');

/*
  Session that is connected to a Substance Hub allowing
  collaboration in real-time.

  TODO:
    - error handling strategies
      - e.g. a commit message never gets through to the server
         - how to detect those errors? timeout?
         - how to handle them?
           - resend same commit?
           - just include affected changes with the next regular commit?
*/
function CollabSession(doc, options) {
  CollabSession.super.call(this, doc, options);

  // TODO: this should be retrieved from the server?
  this.sessionId = uuid();

  // TODO: The CollabSession or the doc needs to be aware of a doc id
  // that corresponds to the doc on the server. For now we just
  // store it on the document instance.
  this.doc.id = 'doc-15';

  // TODO: Also we need to somehow store a version number (local version)
  this.doc.version = 1;

  this.messageQueue = options.messageQueue;
  this.nextCommit = null;
  this.ws = new WebSocket(this.messageQueue);

  this.ws.onopen = this._onConnected.bind(this);
  this.ws.onmessage = this._onMessage.bind(this);

  // whenever a change of a new collaborator is received
  // we add a record here
  this.collaborators = {};
  this.sessionIdPool = [1,2,3,4,5,6,7,8,9,10];

  this.start();
}

CollabSession.Prototype = function() {

  var _super = Object.getPrototypeOf(this);

  /*
    Send local changes to the world.
  */
  this.commit = function() {
    // If there is something to commit and there is no commit pending
    if (this.nextCommit && !this._committing) {
      // console.log('committing', this.nextCommit);
      this.ws.send(['commit', this._serializeChange(this.nextCommit), this.doc.version]);
      this._pendingCommit = this.nextCommit;
      this.nextCommit = null;
      this._committing = true;
    }
  };

  this.start = function() {
    this._runner = setInterval(this.commit.bind(this), 1000);
  };

  this.stop = function() {
    // Try to commit changes to the server every 1s
    if (this._runner) {
      clearInterval(this._runner);
      this._runner = null;
    }
  };

  /*
    We record all local changes into a single change (aka commit) that
  */
  this._recordCommit = function(change) {
    if (!this.nextCommit) {
      this.nextCommit = change;
    } else {
      // Merge new change into nextCommit
      this.nextCommit.ops = this.nextCommit.ops.concat(change.ops);
      this.nextCommit.after = change.after;
    }
  };

  this.afterDocumentChange = function(change, info) {
    _super.afterDocumentChange.apply(this, arguments);

    // Record local chagnes into nextCommit
    if (!info.remote) {
      this._recordCommit(change);
{'fraction_non_alphanumeric': 0.11648034619545619, 'fraction_numerical': 0.006851785070320952, 'mean_word_length': 2.9906474820143885, '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': '7354155', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1658, 'n_words': 503}