<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";

import { DisposableDelegate } from 'phosphor-disposable';

import { 
  IKernel, IKernelFuture, IKernelMessage, IKernelMessageOptions 
} from './ikernel';

import { createKernelMessage } from './kernel';

import * as utils from './utils';


/**
 * A client side Comm interface.
 */
export
interface IComm {
  /**
   * The uuid for the comm channel.
   *
   * Read-only
   */
  commId: string;

  /** 
   * The target name for the comm channel.
   *
   * Read-only
   */
  targetName: string;

  /**
   * The onClose handler.
   */
  onClose: (data?: any) => void;

  /**
   * The onMsg handler.
   */
  onMsg: (data: any) => void;

  /**
   * Open a comm with optional data.
   */
  open(data?: any, metadata?: any): IKernelFuture;

  /**
   * Send a comm message to the kernel.
   */
  send(data: any, metadata?: any, buffers?:(ArrayBuffer | ArrayBufferView)[]): IKernelFuture;

  /**
   * Close the comm.
   */
  close(data?: any, metadata?: any): IKernelFuture;
}


/**
 * Contents of `comm_info` message.
 */
export
interface ICommInfo {
  /**
   * Mapping of comm ids to target names.
   */
  comms: Map<string, string>;
}


/*
 * CommManager for a Kernel.
 *
 * http://ipython.org/ipython-doc/dev/development/messaging.html#custom-messages
 */
export
class CommManager {

  /**
   * Create a CommManager instance.
   */
  constructor(kernel: IKernel) {
    this._kernel = kernel;
    this._kernel.iopubReceived.connect(this._handleKernelMsg, this);
  }

  /**
   * Start a new Comm, sending a "comm_open" message.
   *
   * If commId is given, and a client-side comm with that id exists,
   * that comm is returned.
   */
  startNewComm(targetName: string, data?: any, commId?: string): Promise<IComm> {
    if (commId !== void 0) {
      var promise = this._comms.get(commId);
      if (promise) {
        return promise;
      }
    }
    var comm = new Comm(targetName, commId, this._kernel, () => {
      this._unregisterComm(comm.commId);
    });
    var promise = Promise.resolve(comm as IComm);
    this._comms.set(comm.commId, promise);
    return promise;
  }

  /**
   * Connect to an existing server side comm.  
   *
   * If a client-side comm already exists, it is returned.
   */
  connectToComm(targetName: string, commId: string): Promise<IComm> {
    var promise = this._comms.get(commId);
    if (promise) {
      return promise;
    }
    var comm = new Comm(targetName, commId, this._kernel, () => {
      this._unregisterComm(comm.commId);
    });
    promise = Promise.resolve(comm);
    this._comms.set(commId, promise);
    return promise;
  }

  /**
   * Register the handler for a "comm_open" message on a given targetName.
   */
  registerTarget(targetName: string, cb: (comm: IComm, data: any) => void): void {
    this._targets.set(targetName, cb);
  }

  /**
   * Send a 'comm_info_request', and return the contents of the
   * 'comm_info_reply'.
   */<commit_msg>Consolidate similar functions into connect()<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";

import { DisposableDelegate } from 'phosphor-disposable';

import { 
  IKernel, IKernelFuture, IKernelMessage, IKernelMessageOptions 
} from './ikernel';

import { createKernelMessage } from './kernel';

import * as utils from './utils';


/**
 * A client side Comm interface.
 */
export
interface IComm {
  /**
   * The uuid for the comm channel.
   *
   * Read-only
   */
  commId: string;

  /** 
   * The target name for the comm channel.
   *
   * Read-only
   */
  targetName: string;

  /**
   * The onClose handler.
   */
  onClose: (data?: any) => void;

  /**
   * The onMsg handler.
   */
  onMsg: (data: any) => void;

  /**
   * Open a comm with optional data.
   */
  open(data?: any, metadata?: any): IKernelFuture;

  /**
   * Send a comm message to the kernel.
   */
  send(data: any, metadata?: any, buffers?:(ArrayBuffer | ArrayBufferView)[]): IKernelFuture;

  /**
   * Close the comm.
   */
  close(data?: any, metadata?: any): IKernelFuture;
}


/**
 * Contents of `comm_info` message.
 */
export
interface ICommInfo {
  /**
   * Mapping of comm ids to target names.
   */
  comms: Map<string, string>;
}


/*
 * CommManager for a Kernel.
 *
 * http://ipython.org/ipython-doc/dev/development/messaging.html#custom-messages
 */
export
class CommManager {

  /**
   * Create a CommManager instance.
   */
  constructor(kernel: IKernel) {
    this._kernel = kernel;
    this._kernel.iopubReceived.connect(this._handleKernelMsg, this);
  }

  /**
   * Connect to a comm, or create a new one.
   *
   * If a client-side comm already exists, it is returned.
   */
  connect(targetName: string, commId?: string): Promise<IComm> {
    if (commId === void 0) {
      commId = utils.uuid();
    }
    var promise = this._comms.get(commId);
    if (promise) {
      return promise;
    }
    var comm = new Comm(targetName, commId, this._kernel, () => {
      this._unregisterComm(comm.commId);
    });
    promise = Promise.resolve(comm);
    this._comms.set(commId, promise);
    return promise;
  }

  /**
   * Register the handler for a "comm_open" message on a given targetName.
   */
  registerTarget(targetName: string, cb: (comm: IComm, data: any) => void): void {
    this._targets.set(targetName, cb);
  }

  /**
   * Send a 'comm_info_request', and return the contents of the
   * 'comm_info_reply'.
   */
{'fraction_non_alphanumeric': 0.13821138211382114, 'fraction_numerical': 0.0003613369467028004, 'mean_word_length': 2.791780821917808, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6107070', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1790, 'n_words': 508}