##// END OF EJS Templates
Merge pull request #6216 from SylvainCorlay/comm-unregister...
Min RK -
r17480:dc95bffe merge
parent child Browse files
Show More
@@ -56,9 +56,9 b' define(['
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
@@ -88,7 +88,7 b' define(['
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) {
@@ -141,7 +141,6 b' class Widget(LoggingConfigurable):'
141 # Create a comm.
141 # Create a comm.
142 self._comm = Comm(target_name=self._model_name)
142 self._comm = Comm(target_name=self._model_name)
143 self._comm.on_msg(self._handle_msg)
143 self._comm.on_msg(self._handle_msg)
144 self._comm.on_close(self._close)
145 Widget.widgets[self.model_id] = self
144 Widget.widgets[self.model_id] = self
146
145
147 # first update
146 # first update
@@ -158,21 +157,18 b' class Widget(LoggingConfigurable):'
158 #-------------------------------------------------------------------------
157 #-------------------------------------------------------------------------
159 # Methods
158 # Methods
160 #-------------------------------------------------------------------------
159 #-------------------------------------------------------------------------
161 def _close(self):
162 """Private close - cleanup objects, registry entries"""
163 del Widget.widgets[self.model_id]
164 self._comm = None
165
160
166 def close(self):
161 def close(self):
167 """Close method.
162 """Close method.
168
163
169 Closes the widget which closes the underlying comm.
164 Closes the underlying comm.
170 When the comm is closed, all of the widget views are automatically
165 When the comm is closed, all of the widget views are automatically
171 removed from the front-end."""
166 removed from the front-end."""
167 del Widget.widgets[self.model_id]
172 if self._comm is not None:
168 if self._comm is not None:
173 self._comm.close()
169 self._comm.close()
174 self._close()
170 self._comm = None
175
171
176 def send_state(self, key=None):
172 def send_state(self, key=None):
177 """Sends the widget state, or a piece of it, to the front-end.
173 """Sends the widget state, or a piece of it, to the front-end.
178
174
@@ -50,7 +50,6 b' class Comm(LoggingConfigurable):'
50 if target_name:
50 if target_name:
51 kwargs['target_name'] = target_name
51 kwargs['target_name'] = target_name
52 super(Comm, self).__init__(**kwargs)
52 super(Comm, self).__init__(**kwargs)
53 get_ipython().comm_manager.register_comm(self)
54 if self.primary:
53 if self.primary:
55 # I am primary, open my peer.
54 # I am primary, open my peer.
56 self.open(data)
55 self.open(data)
@@ -77,6 +76,8 b' class Comm(LoggingConfigurable):'
77 """Open the frontend-side version of this comm"""
76 """Open the frontend-side version of this comm"""
78 if data is None:
77 if data is None:
79 data = self._open_data
78 data = self._open_data
79 self._closed = False
80 get_ipython().comm_manager.register_comm(self)
80 self._publish_msg('comm_open', data, metadata, target_name=self.target_name)
81 self._publish_msg('comm_open', data, metadata, target_name=self.target_name)
81
82
82 def close(self, data=None, metadata=None):
83 def close(self, data=None, metadata=None):
@@ -87,6 +88,7 b' class Comm(LoggingConfigurable):'
87 if data is None:
88 if data is None:
88 data = self._close_data
89 data = self._close_data
89 self._publish_msg('comm_close', data, metadata)
90 self._publish_msg('comm_close', data, metadata)
91 get_ipython().comm_manager.unregister_comm(self)
90 self._closed = True
92 self._closed = True
91
93
92 def send(self, data=None, metadata=None):
94 def send(self, data=None, metadata=None):
@@ -72,11 +72,10 b' class CommManager(LoggingConfigurable):'
72 self.comms[comm_id] = comm
72 self.comms[comm_id] = comm
73 return comm_id
73 return comm_id
74
74
75 def unregister_comm(self, comm_id):
75 def unregister_comm(self, comm):
76 """Unregister a comm, and close its counterpart"""
76 """Unregister a comm, and close its counterpart"""
77 # unlike get_comm, this should raise a KeyError
77 # unlike get_comm, this should raise a KeyError
78 comm = self.comms.pop(comm_id)
78 comm = self.comms.pop(comm.comm_id)
79 comm.close()
80
79
81 def get_comm(self, comm_id):
80 def get_comm(self, comm_id):
82 """Get a comm with a particular id
81 """Get a comm with a particular id
@@ -110,13 +109,11 b' class CommManager(LoggingConfigurable):'
110 self.log.error("No such comm target registered: %s", target_name)
109 self.log.error("No such comm target registered: %s", target_name)
111 comm.close()
110 comm.close()
112 return
111 return
113 self.register_comm(comm)
114 try:
112 try:
115 f(comm, msg)
113 f(comm, msg)
116 except Exception:
114 except Exception:
117 self.log.error("Exception opening comm with target: %s", target_name, exc_info=True)
115 self.log.error("Exception opening comm with target: %s", target_name, exc_info=True)
118 comm.close()
116 comm.close()
119 self.unregister_comm(comm_id)
120
117
121 def comm_msg(self, stream, ident, msg):
118 def comm_msg(self, stream, ident, msg):
122 """Handler for comm_msg messages"""
119 """Handler for comm_msg messages"""
General Comments 0
You need to be logged in to leave comments. Login now