<commit_before>import { assert } from '@ember/debug';
import ObjectProxy from '@ember/object/proxy';
import { bind } from '@ember/runloop';

export default ObjectProxy.extend({
  /*
   * {
   *    url: 'String'
   *    type: 'String'
   *    callback: The function to envoke
   *    context: The context of the function
   *    ref: The actual callback function with is given to socketio
   * }
   */
  listeners: null,

  init() {
    this._super(...arguments);
    this.listeners = [];
  },

  /*
   * This method simply passes the arguments to the socketio on method except it binds the callback function to
   * the run loop.
   */
  on(type, callback, context) {
    assert(
      'The second argument must be a function.',
      typeof callback === 'function'
    );

    const boundCallback = bind(context, callback);
    this.listeners.push({
      url: this.socket.io.uri,
      type,
      callback,
      context,
      ref: boundCallback,
    });
    this.socket.io.on(type, boundCallback);
  },

  off(type, callback) {
    assert(
      'The second argument must be a function.',
      typeof callback === 'function'
    );
    const listeners = this.listeners.filter(
      (listener) =>
        listener.callback === callback &&
        listener.url === this.socket.io.uri &&
        listener.type === type
    );

    if (listeners) {
      listeners.forEach((item) => this.socket.off(type, item.ref));
    }

    this.listeners = this.listeners.filter((l) => listeners.indexOf(l) === -1);
  },

  /*<commit_msg>Fix socket.on (#238)<commit_after>import { assert } from '@ember/debug';
import ObjectProxy from '@ember/object/proxy';
import { bind } from '@ember/runloop';

export default ObjectProxy.extend({
  /*
   * {
   *    url: 'String'
   *    type: 'String'
   *    callback: The function to envoke
   *    context: The context of the function
   *    ref: The actual callback function with is given to socketio
   * }
   */
  listeners: null,

  init() {
    this._super(...arguments);
    this.listeners = [];
  },

  /*
   * This method simply passes the arguments to the socketio on method except it binds the callback function to
   * the run loop.
   */
  on(type, callback, context) {
    assert(
      'The second argument must be a function.',
      typeof callback === 'function'
    );

    const boundCallback = bind(context, callback);
    this.listeners.push({
      url: this.socket.io.uri,
      type,
      callback,
      context,
      ref: boundCallback,
    });
    this.socket.on(type, boundCallback);
  },

  off(type, callback) {
    assert(
      'The second argument must be a function.',
      typeof callback === 'function'
    );
    const listeners = this.listeners.filter(
      (listener) =>
        listener.callback === callback &&
        listener.url === this.socket.io.uri &&
        listener.type === type
    );

    if (listeners) {
      listeners.forEach((item) => this.socket.off(type, item.ref));
    }

    this.listeners = this.listeners.filter((l) => listeners.indexOf(l) === -1);
  },

  /*
{'fraction_non_alphanumeric': 0.13618549967341606, 'fraction_numerical': 0.0016329196603527107, 'mean_word_length': 2.4532130777903043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3721546', 'n_tokens_mistral': 918, 'n_tokens_neox': 901, 'n_words': 254}