##// END OF EJS Templates
COMM: mirror Python callback API in Javascript
COMM: mirror Python callback API in Javascript

File last commit:

r13198:a84541d1
r13198:a84541d1
Show More
comm.js
173 lines | 5.3 KiB | application/javascript | JavascriptLexer
MinRK
add javascript-side widgets
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
rename widget to comm
r13195 // Comm and CommManager bases
MinRK
add javascript-side widgets
r13189 //============================================================================
/**
MinRK
rename widget to comm
r13195 * Base Comm classes
MinRK
add javascript-side widgets
r13189 * @module IPython
* @namespace IPython
MinRK
rename widget to comm
r13195 * @submodule comm
MinRK
add javascript-side widgets
r13189 */
var IPython = (function (IPython) {
"use strict";
//-----------------------------------------------------------------------
MinRK
rename widget to comm
r13195 // CommManager class
MinRK
add javascript-side widgets
r13189 //-----------------------------------------------------------------------
MinRK
rename widget to comm
r13195 var CommManager = function (kernel) {
this.comms = {};
this.targets = {comm : Comm};
MinRK
add javascript-side widgets
r13189 if (kernel !== undefined) {
this.init_kernel(kernel);
}
};
MinRK
rename widget to comm
r13195 CommManager.prototype.init_kernel = function (kernel) {
MinRK
make js / Python widgets symmetrical...
r13192 // connect the kernel, and register message handlers
MinRK
add javascript-side widgets
r13189 this.kernel = kernel;
MinRK
rename widget to comm
r13195 var msg_types = ['comm_open', 'comm_msg', 'comm_close'];
MinRK
add javascript-side widgets
r13189 for (var i = 0; i < msg_types.length; i++) {
var msg_type = msg_types[i];
MinRK
fix js/Python WidgetManager symmetry...
r13191 kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
MinRK
add javascript-side widgets
r13189 }
};
MinRK
rename widget to comm
r13195 CommManager.prototype.register_target = function (target, constructor) {
// Register a constructor for a given target key
this.targets[target] = constructor;
MinRK
add javascript-side widgets
r13189 };
MinRK
rename widget to comm
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
make js / Python widgets symmetrical...
r13192 };
MinRK
rename widget to comm
r13195 CommManager.prototype.unregister_comm = function (comm_id) {
// Remove a comm from the mapping
delete this.comms[comm_id];
MinRK
make js / Python widgets symmetrical...
r13192 };
MinRK
rename widget to comm
r13195 // comm message handlers
MinRK
make js / Python widgets symmetrical...
r13192
MinRK
rename widget to comm
r13195 CommManager.prototype.comm_open = function (msg) {
MinRK
add javascript-side widgets
r13189 var content = msg.content;
MinRK
rename widget to comm
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
add javascript-side widgets
r13189 return;
}
MinRK
rename widget to comm
r13195 var comm = new Comm(content.comm_id);
this.register_comm(comm);
callback(comm);
MinRK
pass whole message to Comm handlers
r13197 comm.handle_open(msg);
MinRK
add javascript-side widgets
r13189 };
MinRK
rename widget to comm
r13195 CommManager.prototype.comm_close = function (msg) {
MinRK
add javascript-side widgets
r13189 var content = msg.content;
MinRK
rename widget to comm
r13195 var comm = this.comms[content.comm_id];
if (comm === undefined) {
MinRK
add javascript-side widgets
r13189 return;
}
MinRK
rename widget to comm
r13195 delete this.comms[content.comm_id];
MinRK
pass whole message to Comm handlers
r13197 comm.handle_close(msg);
MinRK
add javascript-side widgets
r13189 };
MinRK
rename widget to comm
r13195 CommManager.prototype.comm_msg = function (msg) {
MinRK
add javascript-side widgets
r13189 var content = msg.content;
MinRK
rename widget to comm
r13195 var comm = this.comms[content.comm_id];
if (comm === undefined) {
MinRK
add javascript-side widgets
r13189 return;
}
MinRK
pass whole message to Comm handlers
r13197 comm.handle_msg(msg);
MinRK
add javascript-side widgets
r13189 };
//-----------------------------------------------------------------------
MinRK
rename widget to comm
r13195 // Comm base class
MinRK
add javascript-side widgets
r13189 //-----------------------------------------------------------------------
MinRK
pass whole message to Comm handlers
r13197 var Comm = function (comm_id, target) {
MinRK
rename widget to comm
r13195 this.comm_id = comm_id;
MinRK
pass whole message to Comm handlers
r13197 this.target = target || 'comm';
MinRK
COMM: mirror Python callback API in Javascript
r13198 this._msg_callback = this._open_callback = this._close_callback = null;
MinRK
add javascript-side widgets
r13189 };
MinRK
make js / Python widgets symmetrical...
r13192 // methods for sending messages
MinRK
rename widget to comm
r13195 Comm.prototype.open = function (data) {
MinRK
make js / Python widgets symmetrical...
r13192 var content = {
MinRK
rename widget to comm
r13195 comm_id : this.comm_id,
target : this.target,
MinRK
make js / Python widgets symmetrical...
r13192 data : data || {},
};
MinRK
rename widget to comm
r13195 this.kernel.send_shell_message("comm_open", content);
MinRK
add javascript-side widgets
r13189 };
MinRK
rename widget to comm
r13195 Comm.prototype.send = function (data) {
MinRK
add javascript-side widgets
r13189 var content = {
MinRK
rename widget to comm
r13195 comm_id : this.comm_id,
MinRK
make js / Python widgets symmetrical...
r13192 data : data || {},
MinRK
add javascript-side widgets
r13189 };
MinRK
rename widget to comm
r13195 return this.kernel.send_shell_message("comm_msg", content);
MinRK
add javascript-side widgets
r13189 };
MinRK
rename widget to comm
r13195 Comm.prototype.close = function (data) {
MinRK
add javascript-side widgets
r13189 var content = {
MinRK
rename widget to comm
r13195 comm_id : this.comm_id,
MinRK
make js / Python widgets symmetrical...
r13192 data : data || {},
MinRK
add javascript-side widgets
r13189 };
MinRK
rename widget to comm
r13195 return this.kernel.send_shell_message("comm_close", content);
MinRK
add javascript-side widgets
r13189 };
MinRK
COMM: mirror Python callback API in Javascript
r13198 // 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);
};
MinRK
make js / Python widgets symmetrical...
r13192 // methods for handling incoming messages
MinRK
COMM: mirror Python callback API in Javascript
r13198 Comm.prototype._maybe_callback = function (key, msg) {
var callback = this['_' + key + '_callback'];
if (callback) callback(msg);
};
MinRK
pass whole message to Comm handlers
r13197 Comm.prototype.handle_open = function (msg) {
MinRK
COMM: mirror Python callback API in Javascript
r13198 this._maybe_callback('open', msg);
MinRK
make js / Python widgets symmetrical...
r13192 };
MinRK
pass whole message to Comm handlers
r13197 Comm.prototype.handle_msg = function (msg) {
MinRK
COMM: mirror Python callback API in Javascript
r13198 this._maybe_callback('msg', msg);
MinRK
make js / Python widgets symmetrical...
r13192 };
MinRK
pass whole message to Comm handlers
r13197 Comm.prototype.handle_close = function (msg) {
MinRK
COMM: mirror Python callback API in Javascript
r13198 this._maybe_callback('close', msg);
MinRK
make js / Python widgets symmetrical...
r13192 };
MinRK
rename widget to comm
r13195 IPython.CommManager = CommManager;
IPython.Comm = Comm;
MinRK
add javascript-side widgets
r13189
return IPython;
}(IPython));