##// END OF EJS Templates
catch errors in comm callbacks
MinRK -
Show More
@@ -1,192 +1,198 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Comm and CommManager bases
9 // Comm and CommManager bases
10 //============================================================================
10 //============================================================================
11 /**
11 /**
12 * Base Comm classes
12 * Base Comm classes
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule comm
15 * @submodule comm
16 */
16 */
17
17
18 var IPython = (function (IPython) {
18 var IPython = (function (IPython) {
19 "use strict";
19 "use strict";
20
20
21 //-----------------------------------------------------------------------
21 //-----------------------------------------------------------------------
22 // CommManager class
22 // CommManager class
23 //-----------------------------------------------------------------------
23 //-----------------------------------------------------------------------
24
24
25 var CommManager = function (kernel) {
25 var CommManager = function (kernel) {
26 this.comms = {};
26 this.comms = {};
27 this.targets = {};
27 this.targets = {};
28 if (kernel !== undefined) {
28 if (kernel !== undefined) {
29 this.init_kernel(kernel);
29 this.init_kernel(kernel);
30 }
30 }
31 };
31 };
32
32
33 CommManager.prototype.init_kernel = function (kernel) {
33 CommManager.prototype.init_kernel = function (kernel) {
34 // connect the kernel, and register message handlers
34 // connect the kernel, and register message handlers
35 this.kernel = kernel;
35 this.kernel = kernel;
36 var msg_types = ['comm_open', 'comm_msg', 'comm_close'];
36 var msg_types = ['comm_open', 'comm_msg', 'comm_close'];
37 for (var i = 0; i < msg_types.length; i++) {
37 for (var i = 0; i < msg_types.length; i++) {
38 var msg_type = msg_types[i];
38 var msg_type = msg_types[i];
39 kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
39 kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
40 }
40 }
41 };
41 };
42
42
43 CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata, comm_id) {
43 CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata, comm_id) {
44 // Create a new Comm, register it, and open its Kernel-side counterpart
44 // Create a new Comm, register it, and open its Kernel-side counterpart
45 // Mimics the auto-registration in `Comm.__init__` in the IPython Comm
45 // Mimics the auto-registration in `Comm.__init__` in the IPython Comm
46 var comm = new Comm(target_name, comm_id);
46 var comm = new Comm(target_name, comm_id);
47 this.register_comm(comm);
47 this.register_comm(comm);
48 comm.open(data, callbacks, metadata);
48 comm.open(data, callbacks, metadata);
49 return comm;
49 return comm;
50 };
50 };
51
51
52 CommManager.prototype.register_target = function (target_name, f) {
52 CommManager.prototype.register_target = function (target_name, f) {
53 // Register a target function for a given target name
53 // Register a target function for a given target name
54 this.targets[target_name] = f;
54 this.targets[target_name] = f;
55 };
55 };
56
56
57 CommManager.prototype.unregister_target = function (target_name, f) {
57 CommManager.prototype.unregister_target = function (target_name, f) {
58 // Unregister a target function for a given target name
58 // Unregister a target function for a given target name
59 delete this.targets[target_name];
59 delete this.targets[target_name];
60 };
60 };
61
61
62 CommManager.prototype.register_comm = function (comm) {
62 CommManager.prototype.register_comm = function (comm) {
63 // Register a comm in the mapping
63 // Register a comm in the mapping
64 this.comms[comm.comm_id] = comm;
64 this.comms[comm.comm_id] = comm;
65 comm.kernel = this.kernel;
65 comm.kernel = this.kernel;
66 return comm.comm_id;
66 return comm.comm_id;
67 };
67 };
68
68
69 CommManager.prototype.unregister_comm = function (comm_id) {
69 CommManager.prototype.unregister_comm = function (comm_id) {
70 // Remove a comm from the mapping
70 // Remove a comm from the mapping
71 delete this.comms[comm_id];
71 delete this.comms[comm_id];
72 };
72 };
73
73
74 // comm message handlers
74 // comm message handlers
75
75
76 CommManager.prototype.comm_open = function (msg) {
76 CommManager.prototype.comm_open = function (msg) {
77 var content = msg.content;
77 var content = msg.content;
78 var f = this.targets[content.target_name];
78 var f = this.targets[content.target_name];
79 if (f === undefined) {
79 if (f === undefined) {
80 console.log("No such target registered: ", content.target_name);
80 console.log("No such target registered: ", content.target_name);
81 console.log("Available targets are: ", this.targets);
81 console.log("Available targets are: ", this.targets);
82 return;
82 return;
83 }
83 }
84 var comm = new Comm(content.target_name, content.comm_id);
84 var comm = new Comm(content.target_name, content.comm_id);
85 this.register_comm(comm);
85 this.register_comm(comm);
86 try {
86 try {
87 f(comm, msg);
87 f(comm, msg);
88 } catch (e) {
88 } catch (e) {
89 console.log("Exception opening new comm:", e, msg);
89 console.log("Exception opening new comm:", e, msg);
90 comm.close();
90 comm.close();
91 this.unregister_comm(comm);
91 this.unregister_comm(comm);
92 }
92 }
93 };
93 };
94
94
95 CommManager.prototype.comm_close = function (msg) {
95 CommManager.prototype.comm_close = function (msg) {
96 var content = msg.content;
96 var content = msg.content;
97 var comm = this.comms[content.comm_id];
97 var comm = this.comms[content.comm_id];
98 if (comm === undefined) {
98 if (comm === undefined) {
99 return;
99 return;
100 }
100 }
101 delete this.comms[content.comm_id];
101 delete this.comms[content.comm_id];
102 try {
102 try {
103 comm.handle_close(msg);
103 comm.handle_close(msg);
104 } catch (e) {
104 } catch (e) {
105 console.log("Exception closing comm: ", e, msg);
105 console.log("Exception closing comm: ", e, msg);
106 }
106 }
107 };
107 };
108
108
109 CommManager.prototype.comm_msg = function (msg) {
109 CommManager.prototype.comm_msg = function (msg) {
110 var content = msg.content;
110 var content = msg.content;
111 var comm = this.comms[content.comm_id];
111 var comm = this.comms[content.comm_id];
112 if (comm === undefined) {
112 if (comm === undefined) {
113 return;
113 return;
114 }
114 }
115 try {
115 try {
116 comm.handle_msg(msg);
116 comm.handle_msg(msg);
117 } catch (e) {
117 } catch (e) {
118 console.log("Exception handling comm msg: ", e, msg);
118 console.log("Exception handling comm msg: ", e, msg);
119 }
119 }
120 };
120 };
121
121
122 //-----------------------------------------------------------------------
122 //-----------------------------------------------------------------------
123 // Comm base class
123 // Comm base class
124 //-----------------------------------------------------------------------
124 //-----------------------------------------------------------------------
125
125
126 var Comm = function (target_name, comm_id) {
126 var Comm = function (target_name, comm_id) {
127 this.target_name = target_name;
127 this.target_name = target_name;
128 this.comm_id = comm_id || IPython.utils.uuid();
128 this.comm_id = comm_id || IPython.utils.uuid();
129 this._msg_callback = this._close_callback = null;
129 this._msg_callback = this._close_callback = null;
130 };
130 };
131
131
132 // methods for sending messages
132 // methods for sending messages
133 Comm.prototype.open = function (data, callbacks, metadata) {
133 Comm.prototype.open = function (data, callbacks, metadata) {
134 var content = {
134 var content = {
135 comm_id : this.comm_id,
135 comm_id : this.comm_id,
136 target_name : this.target_name,
136 target_name : this.target_name,
137 data : data || {},
137 data : data || {},
138 };
138 };
139 return this.kernel.send_shell_message("comm_open", content, callbacks, metadata);
139 return this.kernel.send_shell_message("comm_open", content, callbacks, metadata);
140 };
140 };
141
141
142 Comm.prototype.send = function (data, callbacks, metadata) {
142 Comm.prototype.send = function (data, callbacks, metadata) {
143 var content = {
143 var content = {
144 comm_id : this.comm_id,
144 comm_id : this.comm_id,
145 data : data || {},
145 data : data || {},
146 };
146 };
147 return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata);
147 return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata);
148 };
148 };
149
149
150 Comm.prototype.close = function (data, callbacks, metadata) {
150 Comm.prototype.close = function (data, callbacks, metadata) {
151 var content = {
151 var content = {
152 comm_id : this.comm_id,
152 comm_id : this.comm_id,
153 data : data || {},
153 data : data || {},
154 };
154 };
155 return this.kernel.send_shell_message("comm_close", content, callbacks, metadata);
155 return this.kernel.send_shell_message("comm_close", content, callbacks, metadata);
156 };
156 };
157
157
158 // methods for registering callbacks for incoming messages
158 // methods for registering callbacks for incoming messages
159 Comm.prototype._register_callback = function (key, callback) {
159 Comm.prototype._register_callback = function (key, callback) {
160 this['_' + key + '_callback'] = callback;
160 this['_' + key + '_callback'] = callback;
161 };
161 };
162
162
163 Comm.prototype.on_msg = function (callback) {
163 Comm.prototype.on_msg = function (callback) {
164 this._register_callback('msg', callback);
164 this._register_callback('msg', callback);
165 };
165 };
166
166
167 Comm.prototype.on_close = function (callback) {
167 Comm.prototype.on_close = function (callback) {
168 this._register_callback('close', callback);
168 this._register_callback('close', callback);
169 };
169 };
170
170
171 // methods for handling incoming messages
171 // methods for handling incoming messages
172
172
173 Comm.prototype._maybe_callback = function (key, msg) {
173 Comm.prototype._maybe_callback = function (key, msg) {
174 var callback = this['_' + key + '_callback'];
174 var callback = this['_' + key + '_callback'];
175 if (callback) callback(msg);
175 if (callback) {
176 try {
177 callback(msg);
178 } catch (e) {
179 console.log("Exception in Comm callback", e, msg);
180 }
181 }
176 };
182 };
177
183
178 Comm.prototype.handle_msg = function (msg) {
184 Comm.prototype.handle_msg = function (msg) {
179 this._maybe_callback('msg', msg);
185 this._maybe_callback('msg', msg);
180 };
186 };
181
187
182 Comm.prototype.handle_close = function (msg) {
188 Comm.prototype.handle_close = function (msg) {
183 this._maybe_callback('close', msg);
189 this._maybe_callback('close', msg);
184 };
190 };
185
191
186 IPython.CommManager = CommManager;
192 IPython.CommManager = CommManager;
187 IPython.Comm = Comm;
193 IPython.Comm = Comm;
188
194
189 return IPython;
195 return IPython;
190
196
191 }(IPython));
197 }(IPython));
192
198
General Comments 0
You need to be logged in to leave comments. Login now