<commit_before>import { BellhopEventDispatcher } from './BellhopEventDispatcher.js';

/**
 *  Abstract the communication layer between the iframe
 *  and the parent DOM
 *  @class Bellhop
 *  @extends BellhopEventDispatcher
 */
export class Bellhop extends BellhopEventDispatcher {
  /**
   * Creates an instance of Bellhop.
   * @memberof Bellhop
   * @param { string | number } id the id of the Bellhop instance
   */
  constructor(id = (Math.random() * 100) | 0) {
    super();

    /**
     *  The instance ID for bellhop
     *  @property {string} id
     */
    this.id = `BELLHOP:${id}`;
    /**
     *  If we are connected to another instance of the bellhop
     *  @property {Boolean} connected
     *  @readOnly
     *  @default false
     *  @private
     */
    this.connected = false;

    /**
     *  If this instance represents an iframe instance
     *  @property {Boolean} isChild
     *  @private
     *  @default true
     */
    this.isChild = true;

    /**
     *  If we are current trying to connec
     *  @property {Boolean} connecting
     *  @default false
     *  @private
     */
    this.connecting = false;

    /**
     *  If using cross-domain, the domain to post to
     *  @property {string} origin
     *  @private
     *  @default "*"
     */
    this.origin = '*';

    /**
     *  Save any sends to wait until after we're done
     *  @property {Array} _sendLater
     *  @private
     */
    this._sendLater = [];

    /**
     * The iframe element
     * @property {HTMLIFrameElement} iframe
     * @private
     * @readOnly
     */
    this.iframe = null;
  }

  /**
   *  The connection has been established successfully
   *  @event connected
   */

  /**
   *  Connection could not be established
   *  @event failed
   */

  /**
   *  Handle messages in the window
   *  @method receive
   *  @param { MessageEvent } message the post message received from another bellhop instance
   *  @private
   */
  receive(message) {
    // Ignore messages that don't originate from the target
    // we're connected to
    if (this.target !== message.source) {
      return;
    }

    // If this is not the initial connection message
    if (message.data !== 'connected') {
      let data = message.data;
      // Check to see if the data was sent as a stringified json
      if ('string' === typeof data) {
        try {
          data = JSON.parse(data);
        } catch (err) {
          console.error('Bellhop error: ', err);
        }
      }
      if (this.connected && 'object' === typeof data && data.type) {
        this.trigger(data);
      }
      return;
    }
    // Else setup the connection
    this.onConnectionReceived(message.data);
  }
  /**
   * @memberof Bellhop
   * @param {object} message the message received from the other bellhop instance
   * @private
   */
  onConnectionReceived(message) {
    this.connecting = false;
    this.connected = true;

    // If there is a connection event assigned call it
    this.trigger('connected');

    // Be polite and respond to the child that we're ready
    if (!this.isChild) {
      this.target.postMessage(message, this.origin);
    }

    // If we have any sends waiting to send
    // we are now connected and it should be okay
    for (let i = 0, length = this._sendLater.length; i < length; i++) {
      const { type, data } = this._sendLater[i];
      this.send(type, data);
    }
    this._sendLater.length = 0;
  }

  /**
   *  Setup the connection
   *  @method connect<commit_msg>Fix #18 (Problem when using Google closure compiler) and #19 (Cannot send messages from the 'connected' handler)<commit_after>import { BellhopEventDispatcher } from './BellhopEventDispatcher.js';

/**
 *  Abstract the communication layer between the iframe
 *  and the parent DOM
 *  @class Bellhop
 *  @extends BellhopEventDispatcher
 */
export class Bellhop extends BellhopEventDispatcher {
  /**
   * Creates an instance of Bellhop.
   * @memberof Bellhop
   * @param { string | number } id the id of the Bellhop instance
   */
  constructor(id = (Math.random() * 100) | 0) {
    super();

    /**
     *  The instance ID for bellhop
     *  @property {string} id
     */
    this.id = `BELLHOP:${id}`;
    /**
     *  If we are connected to another instance of the bellhop
     *  @property {Boolean} connected
     *  @readOnly
     *  @default false
     *  @private
     */
    this.connected = false;

    /**
     *  If this instance represents an iframe instance
     *  @property {Boolean} isChild
     *  @private
     *  @default true
     */
    this.isChild = true;

    /**
     *  If we are current trying to connec
     *  @property {Boolean} connecting
     *  @default false
     *  @private
     */
    this.connecting = false;

    /**
     *  If using cross-domain, the domain to post to
     *  @property {string} origin
     *  @private
     *  @default "*"
     */
    this.origin = '*';

    /**
     *  Save any sends to wait until after we're done
     *  @property {Array} _sendLater
     *  @private
     */
    this._sendLater = [];

    /**
     * The iframe element
     * @property {HTMLIFrameElement} iframe
     * @private
     * @readOnly
     */
    this.iframe = null;
  }

  /**
   *  The connection has been established successfully
   *  @event connected
   */

  /**
   *  Connection could not be established
   *  @event failed
   */

  /**
   *  Handle messages in the window
   *  @method receive
   *  @param { MessageEvent } message the post message received from another bellhop instance
   *  @private
   */
  receive(message) {
    // Ignore messages that don't originate from the target
    // we're connected to
    if (this.target !== message.source) {
      return;
    }

    // If this is not the initial connection message
    if (message.data !== 'connected') {
      let data = message.data;
      // Check to see if the data was sent as a stringified json
      if ('string' === typeof data) {
        try {
          data = JSON.parse(data);
        } catch (err) {
          console.error('Bellhop error: ', err);
        }
      }
      if (this.connected && 'object' === typeof data && data.type) {
        this.trigger(data);
      }
      return;
    }
    // Else setup the connection
    this.onConnectionReceived(message.data);
  }
  /**
   * @memberof Bellhop
   * @param {object} message the message received from the other bellhop instance
   * @private
   */
  onConnectionReceived(message) {
    this.connecting = false;
    this.connected = true;

    // Be polite and respond to the child that we're ready
    if (!this.isChild) {
      this.target.postMessage(message, this.origin);
    }

    // If we have any sends waiting to send
    // we are now connected and it should be okay
    for (let i = 0; i < this._sendLater.length; i++) {
      const { type, data } = this._sendLater[i];
      this.send(type, data);
    }
    this._sendLater.length = 0;

    // If there is a connection event assigned call it
    this.trigger('connected');
  }

  /**
   *  Setup the connection
   *  @method connect
{'fraction_non_alphanumeric': 0.11217858152020345, 'fraction_numerical': 0.0022605255721955355, 'mean_word_length': 2.1406388642413487, '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': '2704321', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2063, 'n_words': 666}