comm.js
194 lines
| 6.2 KiB
| application/javascript
|
JavascriptLexer
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
|
r13189 | |||
|
r17198 | define([ | ||
'base/js/namespace', | ||||
|
r17200 | 'jquery', | ||
|
r17198 | 'base/js/utils', | ||
|
r18898 | 'rsvp', | ||
|
r18897 | ], function(IPython, $, utils, rsvp) { | ||
|
r13189 | "use strict"; | ||
//----------------------------------------------------------------------- | ||||
|
r13195 | // CommManager class | ||
|
r13189 | //----------------------------------------------------------------------- | ||
|
r13195 | var CommManager = function (kernel) { | ||
this.comms = {}; | ||||
|
r13204 | this.targets = {}; | ||
|
r13189 | if (kernel !== undefined) { | ||
this.init_kernel(kernel); | ||||
} | ||||
}; | ||||
|
r13195 | CommManager.prototype.init_kernel = function (kernel) { | ||
|
r13192 | // connect the kernel, and register message handlers | ||
|
r13189 | this.kernel = kernel; | ||
|
r13195 | var msg_types = ['comm_open', 'comm_msg', 'comm_close']; | ||
|
r13189 | for (var i = 0; i < msg_types.length; i++) { | ||
var msg_type = msg_types[i]; | ||||
|
r13191 | kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this)); | ||
|
r13189 | } | ||
}; | ||||
|
r13233 | CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata) { | ||
|
r13229 | // Create a new Comm, register it, and open its Kernel-side counterpart | ||
// Mimics the auto-registration in `Comm.__init__` in the IPython Comm | ||||
|
r13233 | var comm = new Comm(target_name); | ||
|
r13229 | this.register_comm(comm); | ||
comm.open(data, callbacks, metadata); | ||||
return comm; | ||||
}; | ||||
|
r13204 | CommManager.prototype.register_target = function (target_name, f) { | ||
// Register a target function for a given target name | ||||
this.targets[target_name] = f; | ||||
|
r13189 | }; | ||
|
r13226 | CommManager.prototype.unregister_target = function (target_name, f) { | ||
// Unregister a target function for a given target name | ||||
delete this.targets[target_name]; | ||||
}; | ||||
|
r13227 | |||
|
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; | ||||
|
r13192 | }; | ||
|
r17460 | CommManager.prototype.unregister_comm = function (comm) { | ||
|
r13195 | // Remove a comm from the mapping | ||
|
r17460 | delete this.comms[comm.comm_id]; | ||
|
r13192 | }; | ||
|
r13195 | // comm message handlers | ||
|
r13192 | |||
|
r13195 | CommManager.prototype.comm_open = function (msg) { | ||
|
r13189 | var content = msg.content; | ||
|
r18467 | var that = this; | ||
|
r18895 | return utils.load_class(content.target_name, content.target_module, | ||
this.targets).then(function(target) { | ||||
|
r18467 | var comm = new Comm(content.target_name, content.comm_id); | ||
that.register_comm(comm); | ||||
try { | ||||
target(comm, msg); | ||||
} catch (e) { | ||||
comm.close(); | ||||
that.unregister_comm(comm); | ||||
|
r18898 | var wrapped_error = new utils.WrappedError("Exception opening new comm", e); | ||
console.error(wrapped_error); | ||||
return rsvp.Promise.reject(wrapped_error); | ||||
|
r18467 | } | ||
|
r18895 | return comm; | ||
}, utils.reject('Could not open comm', true)); | ||||
|
r13189 | }; | ||
|
r13195 | CommManager.prototype.comm_close = function (msg) { | ||
|
r13189 | var content = msg.content; | ||
|
r13195 | var comm = this.comms[content.comm_id]; | ||
if (comm === undefined) { | ||||
|
r13189 | return; | ||
} | ||||
|
r17460 | this.unregister_comm(comm); | ||
|
r13227 | try { | ||
comm.handle_close(msg); | ||||
} catch (e) { | ||||
|
r14486 | console.log("Exception closing comm: ", e, e.stack, msg); | ||
|
r13227 | } | ||
|
r13189 | }; | ||
|
r13195 | CommManager.prototype.comm_msg = function (msg) { | ||
|
r13189 | var content = msg.content; | ||
|
r13195 | var comm = this.comms[content.comm_id]; | ||
if (comm === undefined) { | ||||
|
r13189 | return; | ||
} | ||||
|
r13227 | try { | ||
comm.handle_msg(msg); | ||||
} catch (e) { | ||||
|
r14486 | console.log("Exception handling comm msg: ", e, e.stack, msg); | ||
|
r13227 | } | ||
|
r13189 | }; | ||
//----------------------------------------------------------------------- | ||||
|
r13195 | // Comm base class | ||
|
r13189 | //----------------------------------------------------------------------- | ||
|
r13229 | var Comm = function (target_name, comm_id) { | ||
|
r13204 | this.target_name = target_name; | ||
|
r17198 | this.comm_id = comm_id || utils.uuid(); | ||
|
r13205 | this._msg_callback = this._close_callback = null; | ||
|
r13189 | }; | ||
|
r13192 | // methods for sending messages | ||
|
r13217 | Comm.prototype.open = function (data, callbacks, metadata) { | ||
|
r13192 | var content = { | ||
|
r13195 | comm_id : this.comm_id, | ||
|
r13204 | target_name : this.target_name, | ||
|
r13192 | data : data || {}, | ||
}; | ||||
|
r13217 | return this.kernel.send_shell_message("comm_open", content, callbacks, metadata); | ||
|
r13189 | }; | ||
|
r18332 | Comm.prototype.send = function (data, callbacks, metadata, buffers) { | ||
|
r13189 | var content = { | ||
|
r13195 | comm_id : this.comm_id, | ||
|
r13192 | data : data || {}, | ||
|
r13189 | }; | ||
|
r18332 | return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata, buffers); | ||
|
r13189 | }; | ||
|
r13217 | Comm.prototype.close = function (data, callbacks, metadata) { | ||
|
r13189 | var content = { | ||
|
r13195 | comm_id : this.comm_id, | ||
|
r13192 | data : data || {}, | ||
|
r13189 | }; | ||
|
r13217 | return this.kernel.send_shell_message("comm_close", content, callbacks, metadata); | ||
|
r13189 | }; | ||
|
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); | ||||
}; | ||||
|
r13202 | |||
|
r13192 | // methods for handling incoming messages | ||
|
r13198 | Comm.prototype._maybe_callback = function (key, msg) { | ||
var callback = this['_' + key + '_callback']; | ||||
|
r13230 | if (callback) { | ||
try { | ||||
callback(msg); | ||||
} catch (e) { | ||||
|
r14486 | console.log("Exception in Comm callback", e, e.stack, msg); | ||
|
r13230 | } | ||
} | ||||
|
r13198 | }; | ||
|
r13197 | Comm.prototype.handle_msg = function (msg) { | ||
|
r13198 | this._maybe_callback('msg', msg); | ||
|
r13192 | }; | ||
|
r13197 | Comm.prototype.handle_close = function (msg) { | ||
|
r13198 | this._maybe_callback('close', msg); | ||
|
r13192 | }; | ||
|
r17198 | // For backwards compatability. | ||
|
r13195 | IPython.CommManager = CommManager; | ||
IPython.Comm = Comm; | ||||
|
r13189 | |||
|
r17198 | return { | ||
'CommManager': CommManager, | ||||
'Comm': Comm | ||||
}; | ||||
}); | ||||