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