##// END OF EJS Templates
Merge master
Merge master

File last commit:

r17460:4f740782
r17698:0a458d02 merge
Show More
comm.js
190 lines | 6.0 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Almost done!...
r17198 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
MinRK
add javascript-side widgets
r13189
Jonathan Frederic
Almost done!...
r17198 define([
'base/js/namespace',
Jonathan Frederic
MWE,...
r17200 'jquery',
Jonathan Frederic
Almost done!...
r17198 'base/js/utils',
], function(IPython, $, utils) {
MinRK
add javascript-side widgets
r13189 "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 = {};
MinRK
s/target/target_name
r13204 this.targets = {};
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
don't expose comm_id arg via new_comm
r13233 CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata) {
MinRK
Add CommManager.new_comm...
r13229 // Create a new Comm, register it, and open its Kernel-side counterpart
// Mimics the auto-registration in `Comm.__init__` in the IPython Comm
MinRK
don't expose comm_id arg via new_comm
r13233 var comm = new Comm(target_name);
MinRK
Add CommManager.new_comm...
r13229 this.register_comm(comm);
comm.open(data, callbacks, metadata);
return comm;
};
MinRK
s/target/target_name
r13204 CommManager.prototype.register_target = function (target_name, f) {
// Register a target function for a given target name
this.targets[target_name] = f;
MinRK
add javascript-side widgets
r13189 };
MinRK
add unregister_target to CommManagers
r13226 CommManager.prototype.unregister_target = function (target_name, f) {
// Unregister a target function for a given target name
delete this.targets[target_name];
};
MinRK
log exceptions in Comm handlers
r13227
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 };
Sylvain Corlay
bad call and missing call to unregister_comm
r17460 CommManager.prototype.unregister_comm = function (comm) {
MinRK
rename widget to comm
r13195 // Remove a comm from the mapping
Sylvain Corlay
bad call and missing call to unregister_comm
r17460 delete this.comms[comm.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
open is not an event...
r13205 var f = this.targets[content.target_name];
if (f === undefined) {
MinRK
s/target/target_name
r13204 console.log("No such target registered: ", content.target_name);
MinRK
rename widget to comm
r13195 console.log("Available targets are: ", this.targets);
MinRK
add javascript-side widgets
r13189 return;
}
MinRK
Add CommManager.new_comm...
r13229 var comm = new Comm(content.target_name, content.comm_id);
MinRK
rename widget to comm
r13195 this.register_comm(comm);
MinRK
log exceptions in Comm handlers
r13227 try {
f(comm, msg);
} catch (e) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 console.log("Exception opening new comm:", e, e.stack, msg);
MinRK
log exceptions in Comm handlers
r13227 comm.close();
this.unregister_comm(comm);
}
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;
}
Sylvain Corlay
bad call and missing call to unregister_comm
r17460 this.unregister_comm(comm);
MinRK
log exceptions in Comm handlers
r13227 try {
comm.handle_close(msg);
} catch (e) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 console.log("Exception closing comm: ", e, e.stack, msg);
MinRK
log exceptions in Comm handlers
r13227 }
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
log exceptions in Comm handlers
r13227 try {
comm.handle_msg(msg);
} catch (e) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 console.log("Exception handling comm msg: ", e, e.stack, msg);
MinRK
log exceptions in Comm handlers
r13227 }
MinRK
add javascript-side widgets
r13189 };
//-----------------------------------------------------------------------
MinRK
rename widget to comm
r13195 // Comm base class
MinRK
add javascript-side widgets
r13189 //-----------------------------------------------------------------------
MinRK
Add CommManager.new_comm...
r13229 var Comm = function (target_name, comm_id) {
MinRK
s/target/target_name
r13204 this.target_name = target_name;
Jonathan Frederic
Almost done!...
r17198 this.comm_id = comm_id || utils.uuid();
MinRK
open is not an event...
r13205 this._msg_callback = this._close_callback = null;
MinRK
add javascript-side widgets
r13189 };
MinRK
make js / Python widgets symmetrical...
r13192 // methods for sending messages
MinRK
add message metadata to comm and kernel.send_shell_message
r13217 Comm.prototype.open = function (data, callbacks, metadata) {
MinRK
make js / Python widgets symmetrical...
r13192 var content = {
MinRK
rename widget to comm
r13195 comm_id : this.comm_id,
MinRK
s/target/target_name
r13204 target_name : this.target_name,
MinRK
make js / Python widgets symmetrical...
r13192 data : data || {},
};
MinRK
add message metadata to comm and kernel.send_shell_message
r13217 return this.kernel.send_shell_message("comm_open", content, callbacks, metadata);
MinRK
add javascript-side widgets
r13189 };
MinRK
add message metadata to comm and kernel.send_shell_message
r13217 Comm.prototype.send = function (data, callbacks, metadata) {
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
add message metadata to comm and kernel.send_shell_message
r13217 return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata);
MinRK
add javascript-side widgets
r13189 };
MinRK
add message metadata to comm and kernel.send_shell_message
r13217 Comm.prototype.close = function (data, callbacks, metadata) {
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
add message metadata to comm and kernel.send_shell_message
r13217 return this.kernel.send_shell_message("comm_close", content, callbacks, metadata);
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_msg = function (callback) {
this._register_callback('msg', callback);
};
Comm.prototype.on_close = function (callback) {
this._register_callback('close', callback);
};
MinRK
hook up output for comm messages
r13202
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'];
MinRK
catch errors in comm callbacks
r13230 if (callback) {
try {
callback(msg);
} catch (e) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 console.log("Exception in Comm callback", e, e.stack, msg);
MinRK
catch errors in comm callbacks
r13230 }
}
MinRK
COMM: mirror Python callback API in Javascript
r13198 };
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 };
Jonathan Frederic
Almost done!...
r17198 // For backwards compatability.
MinRK
rename widget to comm
r13195 IPython.CommManager = CommManager;
IPython.Comm = Comm;
MinRK
add javascript-side widgets
r13189
Jonathan Frederic
Almost done!...
r17198 return {
'CommManager': CommManager,
'Comm': Comm
};
});