<commit_before>import _bind from 'fast.js/function/bind';
import _each from 'fast.js/forEach';
import MethodCall from './MethodCall';


/**
 * Make an RPC calls and track results.
 * Track a DDP connection for canceling active
 * methods calls.
 */
export default class MethodCallManager {
  constructor(connection) {
    this.conn = connection;
    this._methods = {};

    connection.on('status:disconnected', _bind(this._handleDisconnected, this));
    connection.on('message:result', _bind(this._handleMethodResult, this));
    connection.on('message:updated', _bind(this._handleMethodUpdated, this));
  }

  /**
   * Call a Meteor method
   * @param  {String} method
   * @param  {...}    param1, param2, ..
   * @return {MethodCall}
   */
  call(method, ...params) {
    return this.apply(method, params);
  }

  /**
   * Apply a method with given parameters and
   * randomSeed
   * @param  {String} method
   * @param  {Array} params
   * @param  {String} randomSeed
   * @return {MethodCall}
   */
  apply(method, params = [], randomSeed) {
    const call = new MethodCall(method, params, randomSeed, this.conn);
    this._methods[call.id] = call;

    const cleanupCallback = () => {
      if (
        this._methods[call.id] && this._methods[call.id]._result &&
        this._methods[call.id]._updated
      ) {
        delete this._methods[call.id];
      }
    };
    call.result().then(cleanupCallback)
      .updated().then(cleanupCallback);

    return call;
  }

  _handleMethodResult(msg) {
    if (msg.id && this._methods[msg.id]) {
      const { result, error } = msg;
      this._methods[msg.id]._handleResult(error, result);
    }
  }
<commit_msg>fixed unhandled promise rejection on method call<commit_after>import _bind from 'fast.js/function/bind';
import _each from 'fast.js/forEach';
import MethodCall from './MethodCall';


/**
 * Make an RPC calls and track results.
 * Track a DDP connection for canceling active
 * methods calls.
 */
export default class MethodCallManager {
  constructor(connection) {
    this.conn = connection;
    this._methods = {};

    connection.on('status:disconnected', _bind(this._handleDisconnected, this));
    connection.on('message:result', _bind(this._handleMethodResult, this));
    connection.on('message:updated', _bind(this._handleMethodUpdated, this));
  }

  /**
   * Call a Meteor method
   * @param  {String} method
   * @param  {...}    param1, param2, ..
   * @return {MethodCall}
   */
  call(method, ...params) {
    return this.apply(method, params);
  }

  /**
   * Apply a method with given parameters and
   * randomSeed
   * @param  {String} method
   * @param  {Array} params
   * @param  {String} randomSeed
   * @return {MethodCall}
   */
  apply(method, params = [], randomSeed) {
    const call = new MethodCall(method, params, randomSeed, this.conn);
    this._methods[call.id] = call;

    const cleanupCallback = () => {
      if (
        this._methods[call.id] && this._methods[call.id]._result &&
        this._methods[call.id]._updated
      ) {
        delete this._methods[call.id];
      }
    };

    call.then(cleanupCallback, cleanupCallback);
    return call;
  }

  _handleMethodResult(msg) {
    if (msg.id && this._methods[msg.id]) {
      const { result, error } = msg;
      this._methods[msg.id]._handleResult(error, result);
    }
  }

{'fraction_non_alphanumeric': 0.1412327947336924, 'fraction_numerical': 0.0011968880909634949, 'mean_word_length': 3.027710843373494, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7286103', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1031, 'n_words': 236}