<commit_before>//----------------------------------------------------------------------------
//  Copyright (C) 2013  The IPython Development Team
//
//  Distributed under the terms of the BSD License.  The full license is in
//  the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------

//============================================================================
// Comm and CommManager bases
//============================================================================
/**
 * Base Comm classes
 * @module IPython
 * @namespace IPython
 * @submodule comm
 */

var IPython = (function (IPython) {
    "use strict";

    //-----------------------------------------------------------------------
    // CommManager class
    //-----------------------------------------------------------------------
    
    var CommManager = function (kernel) {
        this.comms = {};
        this.targets = {comm : Comm};
        if (kernel !== undefined) {
            this.init_kernel(kernel);
        }
    };
    
    CommManager.prototype.init_kernel = function (kernel) {
        // connect the kernel, and register message handlers
        this.kernel = kernel;
        var msg_types = ['comm_open', 'comm_msg', 'comm_close'];
        for (var i = 0; i < msg_types.length; i++) {
            var msg_type = msg_types[i];
            kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
        }
    };
    
    CommManager.prototype.register_target = function (target, constructor) {
        // Register a constructor for a given target key
        this.targets[target] = constructor;
    };
    
    CommManager.prototype.register_comm = function (comm) {
        // Register a comm in the mapping
        this.comms[comm.comm_id] = comm;
        comm.kernel = this.kernel;
        return comm.comm_id;
    };
    
    CommManager.prototype.unregister_comm = function (comm_id) {
        // Remove a comm from the mapping
        delete this.comms[comm_id];
    };
    
    // comm message handlers
    
    CommManager.prototype.comm_open = function (msg) {
        var content = msg.content;
        var callback = this.targets[content.target];
        if (callback === undefined) {
            console.log("No such target registered: ", content.target);
            console.log("Available targets are: ", this.targets);
            return;
        }
        var comm = new Comm(content.comm_id);
        this.register_comm(comm);
        callback(comm);
        comm.handle_open(msg);
    };
    
    CommManager.prototype.comm_close = function (msg) {
        var content = msg.content;
        var comm = this.comms[content.comm_id];
        if (comm === undefined) {
            return;
        }
        delete this.comms[content.comm_id];
        comm.handle_close(msg);
    };
    
    CommManager.prototype.comm_msg = function (msg) {
        var content = msg.content;
        var comm = this.comms[content.comm_id];
        if (comm === undefined) {
            return;
        }
        comm.handle_msg(msg);
    };
    
    //-----------------------------------------------------------------------
    // Comm base class
    //-----------------------------------------------------------------------
    
    var Comm = function (comm_id, target) {
        this.comm_id = comm_id;
        this.target = target || 'comm';
    };
    
    // methods for sending messages
    Comm.prototype.open = function (data) {
        var content = {
            comm_id : this.comm_id,
            target : this.target,
            data : data || {},
        };
        this.kernel.send_shell_message("comm_open", content);
    };
    
    Comm.prototype.send = function (data) {
        var content = {
            comm_id : this.comm_id,
            data : data || {},
        };
        return this.kernel.send_shell_message("comm_msg", content);
    };
    
    Comm.prototype.close = function (data) {
        var content = {
            comm_id : this.comm_id,
            data : data || {},
        };
        return this.kernel.send_shell_message("comm_close", content);
    };
    
    // methods for handling incoming messages
    
    Comm.prototype.handle_open = function (msg) {
        $([this]).trigger("comm_open", msg);
    };
    
    Comm.prototype.handle_msg = function (msg) {
        $([this]).trigger("comm_msg", msg);
    };
    
    Comm.prototype.handle_close = function (msg) {
        $([this]).trigger("comm_close", msg);
    };
    
    IPython.CommManager = CommManager;
    IPython.Comm = Comm;

    return IPython;<commit_msg>COMM: mirror Python callback API in Javascript<commit_after>//----------------------------------------------------------------------------
//  Copyright (C) 2013  The IPython Development Team
//
//  Distributed under the terms of the BSD License.  The full license is in
//  the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------

//============================================================================
// Comm and CommManager bases
//============================================================================
/**
 * Base Comm classes
 * @module IPython
 * @namespace IPython
 * @submodule comm
 */

var IPython = (function (IPython) {
    "use strict";

    //-----------------------------------------------------------------------
    // CommManager class
    //-----------------------------------------------------------------------
    
    var CommManager = function (kernel) {
        this.comms = {};
        this.targets = {comm : Comm};
        if (kernel !== undefined) {
            this.init_kernel(kernel);
        }
    };
    
    CommManager.prototype.init_kernel = function (kernel) {
        // connect the kernel, and register message handlers
        this.kernel = kernel;
        var msg_types = ['comm_open', 'comm_msg', 'comm_close'];
        for (var i = 0; i < msg_types.length; i++) {
            var msg_type = msg_types[i];
            kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
        }
    };
    
    CommManager.prototype.register_target = function (target, constructor) {
        // Register a constructor for a given target key
        this.targets[target] = constructor;
    };
    
    CommManager.prototype.register_comm = function (comm) {
        // Register a comm in the mapping
        this.comms[comm.comm_id] = comm;
        comm.kernel = this.kernel;
        return comm.comm_id;
    };
    
    CommManager.prototype.unregister_comm = function (comm_id) {
        // Remove a comm from the mapping
        delete this.comms[comm_id];
    };
    
    // comm message handlers
    
    CommManager.prototype.comm_open = function (msg) {
        var content = msg.content;
        var callback = this.targets[content.target];
        if (callback === undefined) {
            console.log("No such target registered: ", content.target);
            console.log("Available targets are: ", this.targets);
            return;
        }
        var comm = new Comm(content.comm_id);
        this.register_comm(comm);
        callback(comm);
        comm.handle_open(msg);
    };
    
    CommManager.prototype.comm_close = function (msg) {
        var content = msg.content;
        var comm = this.comms[content.comm_id];
        if (comm === undefined) {
            return;
        }
        delete this.comms[content.comm_id];
        comm.handle_close(msg);
    };
    
    CommManager.prototype.comm_msg = function (msg) {
        var content = msg.content;
        var comm = this.comms[content.comm_id];
        if (comm === undefined) {
            return;
        }
        comm.handle_msg(msg);
    };
    
    //-----------------------------------------------------------------------
    // Comm base class
    //-----------------------------------------------------------------------
    
    var Comm = function (comm_id, target) {
        this.comm_id = comm_id;
        this.target = target || 'comm';
        this._msg_callback = this._open_callback = this._close_callback = null;
    };
    
    // methods for sending messages
    Comm.prototype.open = function (data) {
        var content = {
            comm_id : this.comm_id,
            target : this.target,
            data : data || {},
        };
        this.kernel.send_shell_message("comm_open", content);
    };
    
    Comm.prototype.send = function (data) {
        var content = {
            comm_id : this.comm_id,
            data : data || {},
        };
        return this.kernel.send_shell_message("comm_msg", content);
    };
    
    Comm.prototype.close = function (data) {
        var content = {
            comm_id : this.comm_id,
            data : data || {},
        };
        return this.kernel.send_shell_message("comm_close", content);
    };
    
    // methods for registering callbacks for incoming messages
    Comm.prototype._register_callback = function (key, callback) {
        this['_' + key + '_callback'] = callback;
    };
    
    Comm.prototype.on_open = function (callback) {
        this._register_callback('open', callback);
    };
    
    Comm.prototype.on_msg = function (callback) {
        this._register_callback('msg', callback);
    };
    
    Comm.prototype.on_close = function (callback) {
        this._register_callback('close', callback);
    };

    // methods for handling incoming messages
    
    Comm.prototype._maybe_callback = function (key, msg) {
        var callback = this['_' + key + '_callback'];
        if (callback) callback(msg);
    };
    
    Comm.prototype.handle_open = function (msg) {
        this._maybe_callback('open', msg);
    };
    
    Comm.prototype.handle_msg = function (msg) {
        this._maybe_callback('msg', msg);
    };
    
    Comm.prototype.handle_close = function (msg) {
        this._maybe_callback('close', msg);
    };
    
    IPython.CommManager = CommManager;
    IPython.Comm = Comm;

    return IPython;
{'fraction_non_alphanumeric': 0.22207933524582055, 'fraction_numerical': 0.0009892175289346128, 'mean_word_length': 1.6049987116722495, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '808230', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2583, 'n_words': 601}