comm.js
150 lines
| 4.5 KiB
| application/javascript
|
JavascriptLexer
MinRK
|
r13189 | //---------------------------------------------------------------------------- | ||
// 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. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
MinRK
|
r13195 | // Comm and CommManager bases | ||
MinRK
|
r13189 | //============================================================================ | ||
/** | ||||
MinRK
|
r13195 | * Base Comm classes | ||
MinRK
|
r13189 | * @module IPython | ||
* @namespace IPython | ||||
MinRK
|
r13195 | * @submodule comm | ||
MinRK
|
r13189 | */ | ||
var IPython = (function (IPython) { | ||||
"use strict"; | ||||
//----------------------------------------------------------------------- | ||||
MinRK
|
r13195 | // CommManager class | ||
MinRK
|
r13189 | //----------------------------------------------------------------------- | ||
MinRK
|
r13195 | var CommManager = function (kernel) { | ||
this.comms = {}; | ||||
this.targets = {comm : Comm}; | ||||
MinRK
|
r13189 | if (kernel !== undefined) { | ||
this.init_kernel(kernel); | ||||
} | ||||
}; | ||||
MinRK
|
r13195 | CommManager.prototype.init_kernel = function (kernel) { | ||
MinRK
|
r13192 | // connect the kernel, and register message handlers | ||
MinRK
|
r13189 | this.kernel = kernel; | ||
MinRK
|
r13195 | var msg_types = ['comm_open', 'comm_msg', 'comm_close']; | ||
MinRK
|
r13189 | for (var i = 0; i < msg_types.length; i++) { | ||
var msg_type = msg_types[i]; | ||||
MinRK
|
r13191 | kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this)); | ||
MinRK
|
r13189 | } | ||
}; | ||||
MinRK
|
r13195 | CommManager.prototype.register_target = function (target, constructor) { | ||
// Register a constructor for a given target key | ||||
this.targets[target] = constructor; | ||||
MinRK
|
r13189 | }; | ||
MinRK
|
r13195 | 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; | ||||
MinRK
|
r13192 | }; | ||
MinRK
|
r13195 | CommManager.prototype.unregister_comm = function (comm_id) { | ||
// Remove a comm from the mapping | ||||
delete this.comms[comm_id]; | ||||
MinRK
|
r13192 | }; | ||
MinRK
|
r13195 | // comm message handlers | ||
MinRK
|
r13192 | |||
MinRK
|
r13195 | CommManager.prototype.comm_open = function (msg) { | ||
MinRK
|
r13189 | var content = msg.content; | ||
MinRK
|
r13195 | var callback = this.targets[content.target]; | ||
if (callback === undefined) { | ||||
console.log("No such target registered: ", content.target); | ||||
console.log("Available targets are: ", this.targets); | ||||
MinRK
|
r13189 | return; | ||
} | ||||
MinRK
|
r13195 | var comm = new Comm(content.comm_id); | ||
this.register_comm(comm); | ||||
callback(comm); | ||||
comm.handle_open(content.data); | ||||
MinRK
|
r13189 | }; | ||
MinRK
|
r13195 | CommManager.prototype.comm_close = function (msg) { | ||
MinRK
|
r13189 | var content = msg.content; | ||
MinRK
|
r13195 | var comm = this.comms[content.comm_id]; | ||
if (comm === undefined) { | ||||
MinRK
|
r13189 | return; | ||
} | ||||
MinRK
|
r13195 | delete this.comms[content.comm_id]; | ||
comm.handle_close(content.data); | ||||
MinRK
|
r13189 | }; | ||
MinRK
|
r13195 | CommManager.prototype.comm_msg = function (msg) { | ||
MinRK
|
r13189 | var content = msg.content; | ||
MinRK
|
r13195 | var comm = this.comms[content.comm_id]; | ||
if (comm === undefined) { | ||||
MinRK
|
r13189 | return; | ||
} | ||||
MinRK
|
r13195 | comm.handle_msg(content.data); | ||
MinRK
|
r13189 | }; | ||
//----------------------------------------------------------------------- | ||||
MinRK
|
r13195 | // Comm base class | ||
MinRK
|
r13189 | //----------------------------------------------------------------------- | ||
MinRK
|
r13195 | var Comm = function (comm_id) { | ||
this.comm_id = comm_id; | ||||
this.target = 'comm'; | ||||
MinRK
|
r13189 | }; | ||
MinRK
|
r13192 | // methods for sending messages | ||
MinRK
|
r13195 | Comm.prototype.open = function (data) { | ||
MinRK
|
r13192 | var content = { | ||
MinRK
|
r13195 | comm_id : this.comm_id, | ||
target : this.target, | ||||
MinRK
|
r13192 | data : data || {}, | ||
}; | ||||
MinRK
|
r13195 | this.kernel.send_shell_message("comm_open", content); | ||
MinRK
|
r13189 | }; | ||
MinRK
|
r13195 | Comm.prototype.send = function (data) { | ||
MinRK
|
r13189 | var content = { | ||
MinRK
|
r13195 | comm_id : this.comm_id, | ||
MinRK
|
r13192 | data : data || {}, | ||
MinRK
|
r13189 | }; | ||
MinRK
|
r13195 | return this.kernel.send_shell_message("comm_msg", content); | ||
MinRK
|
r13189 | }; | ||
MinRK
|
r13195 | Comm.prototype.close = function (data) { | ||
MinRK
|
r13189 | var content = { | ||
MinRK
|
r13195 | comm_id : this.comm_id, | ||
MinRK
|
r13192 | data : data || {}, | ||
MinRK
|
r13189 | }; | ||
MinRK
|
r13195 | return this.kernel.send_shell_message("comm_close", content); | ||
MinRK
|
r13189 | }; | ||
MinRK
|
r13192 | // methods for handling incoming messages | ||
MinRK
|
r13195 | Comm.prototype.handle_open = function (data) { | ||
$([this]).trigger("comm_open", data); | ||||
MinRK
|
r13192 | }; | ||
MinRK
|
r13195 | Comm.prototype.handle_msg = function (data) { | ||
$([this]).trigger("comm_msg", data); | ||||
MinRK
|
r13192 | }; | ||
MinRK
|
r13195 | Comm.prototype.handle_close = function (data) { | ||
$([this]).trigger("comm_close", data); | ||||
MinRK
|
r13192 | }; | ||
MinRK
|
r13195 | IPython.CommManager = CommManager; | ||
IPython.Comm = Comm; | ||||
MinRK
|
r13189 | |||
return IPython; | ||||
}(IPython)); | ||||