<commit_before>var DDPClient = require('ddp');
var Promise = require('es6-promise').Promise;

module.exports = function makeDDPClientFactory (ddpSetupProvider) {
  "use strict";

  var self = this;
  var ddpClient = null;
  var ddpClientPromise = null;
  var code = null;
  var port = null;
  var host = null;

  return function ddpClientAsPromise () {
    
    return ddpSetupProvider().then(function (setup) {

      if (code === setup.code && port === setup.port && ddpClientPromise) {
        return ddpClientPromise;
      }

      code = setup.code;
      port = setup.port;
      host = setup.host || 'localhost';
      
      ddpClientPromise = new Promise(function (resolve, reject) {
 
        ddpClient && ddpClient.close();

        ddpClient = new DDPClient({
          host : host,
          port : port,
          path : "websocket",
          ssl  : false,
          autoReconnect : true,
          autoReconnectTimer : 500,
          maintainCollections : true,
          ddpVersion : '1'
        });

        //ddpClient.connect(function (err, wasReconnected) {
        //  if (err) {
        //    return reject(err);
        //  }
        //  resolve(ddpClient);
        //});

        var retryCount = 5;

        // XXX we need this because the WebApp.httpServer may start with some delay;
        //     in fact, this should be handled within the app itself
        (function tryConnect() {
          ddpClient.connect(function (err, wasReconnected) {
            console.log('trying to connect');
            if (err) {
              if (retryCount <= 0) {
                reject(typeof err === 'string' ? new Error(err) : err);
              } else {
                setTimeout(tryConnect, 500);
              }
            } else {
              resolve(ddpClient);
            }
          });
        })();

        // TODO: re-enable this feature when we make timeout configurable
        //setTimeout(function () {
        //  reject(new Error('timeout while waiting to establish ddp connection'));
        //}, 2000);

        //-------------------------------------------------------------
        //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\<commit_msg>Make sure to decrease the retryCount<commit_after>var DDPClient = require('ddp');
var Promise = require('es6-promise').Promise;

module.exports = function makeDDPClientFactory (ddpSetupProvider) {
  "use strict";

  var self = this;
  var ddpClient = null;
  var ddpClientPromise = null;
  var code = null;
  var port = null;
  var host = null;

  return function ddpClientAsPromise () {
    
    return ddpSetupProvider().then(function (setup) {

      if (code === setup.code && port === setup.port && ddpClientPromise) {
        return ddpClientPromise;
      }

      code = setup.code;
      port = setup.port;
      host = setup.host || 'localhost';
      
      ddpClientPromise = new Promise(function (resolve, reject) {
 
        ddpClient && ddpClient.close();

        ddpClient = new DDPClient({
          host : host,
          port : port,
          path : "websocket",
          ssl  : false,
          autoReconnect : true,
          autoReconnectTimer : 500,
          maintainCollections : true,
          ddpVersion : '1'
        });

        //ddpClient.connect(function (err, wasReconnected) {
        //  if (err) {
        //    return reject(err);
        //  }
        //  resolve(ddpClient);
        //});

        var retryCount = 5;

        // XXX we need this because the WebApp.httpServer may start with some delay;
        //     in fact, this should be handled within the app itself
        (function tryConnect() {
          ddpClient.connect(function (err, wasReconnected) {
            console.log('trying to connect');
            if (err) {
              if (retryCount <= 0) {
                reject(typeof err === 'string' ? new Error(err) : err);
              } else {
                retryCount -= 1;
                setTimeout(tryConnect, 500);
              }
            } else {
              resolve(ddpClient);
            }
          });
        })();

        // TODO: re-enable this feature when we make timeout configurable
        //setTimeout(function () {
        //  reject(new Error('timeout while waiting to establish ddp connection'));
        //}, 2000);

        //-------------------------------------------------------------
        //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
{'fraction_non_alphanumeric': 0.1628584229390681, 'fraction_numerical': 0.006496415770609319, 'mean_word_length': 1.4888517279821627, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1239006', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1208, 'n_words': 313}