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