##// END OF EJS Templates
Removed comm dependency of widget model and view
Jonathan Frederic -
Show More
@@ -30,22 +30,25
30 // WidgetModel class
30 // WidgetModel class
31 //--------------------------------------------------------------------
31 //--------------------------------------------------------------------
32 var WidgetModel = Backbone.Model.extend({
32 var WidgetModel = Backbone.Model.extend({
33 constructor: function (comm_manager, comm, widget_manager) {
33 constructor: function (widget_manager, widget_id, comm) {
34 this.comm_manager = comm_manager;
35 this.widget_manager = widget_manager;
34 this.widget_manager = widget_manager;
36 this.pending_msgs = 0;
35 this.pending_msgs = 0;
37 this.msg_throttle = 3;
36 this.msg_throttle = 3;
38 this.msg_buffer = null;
37 this.msg_buffer = null;
39 this.views = [];
38 this.views = [];
39 this.id = widget_id;
40 this._custom_msg_callbacks = [];
40 this._custom_msg_callbacks = [];
41
41
42 // Remember comm associated with the model.
42 if (comm !== undefined) {
43 this.comm = comm;
44 comm.model = this;
45
43
46 // Hook comm messages up to model.
44 // Remember comm associated with the model.
47 comm.on_close($.proxy(this._handle_comm_closed, this));
45 this.comm = comm;
48 comm.on_msg($.proxy(this._handle_comm_msg, this));
46 comm.model = this;
47
48 // Hook comm messages up to model.
49 comm.on_close($.proxy(this._handle_comm_closed, this));
50 comm.on_msg($.proxy(this._handle_comm_msg, this));
51 }
49
52
50 return Backbone.Model.apply(this);
53 return Backbone.Model.apply(this);
51 },
54 },
@@ -65,19 +68,21
65
68
66
69
67 send: function (content, cell) {
70 send: function (content, cell) {
68
71 if (this._has_comm()) {
69 // Used the last modified view as the sender of the message. This
72 // Used the last modified view as the sender of the message. This
70 // will insure that any python code triggered by the sent message
73 // will insure that any python code triggered by the sent message
71 // can create and display widgets and output.
74 // can create and display widgets and output.
72 if (cell === undefined) {
75 if (cell === undefined) {
73 if (this.last_modified_view !== undefined &&
76 if (this.last_modified_view !== undefined &&
74 this.last_modified_view.cell !== undefined) {
77 this.last_modified_view.cell !== undefined) {
75 cell = this.last_modified_view.cell;
78 cell = this.last_modified_view.cell;
79 }
76 }
80 }
81 var callbacks = this._make_callbacks(cell);
82 var data = {method: 'custom', custom_content: content};
83
84 this.comm.send(data, callbacks);
77 }
85 }
78 var callbacks = this._make_callbacks(cell);
79 var data = {method: 'custom', custom_content: content};
80 this.comm.send(data, callbacks);
81 },
86 },
82
87
83
88
@@ -124,7 +129,11
124 // Handle when a widget is closed.
129 // Handle when a widget is closed.
125 _handle_comm_closed: function (msg) {
130 _handle_comm_closed: function (msg) {
126 this._execute_views_method('remove');
131 this._execute_views_method('remove');
127 delete this.comm.model; // Delete ref so GC will collect widget model.
132 if (this._has_comm()) {
133 delete this.comm.model; // Delete ref so GC will collect widget model.
134 delete this.comm;
135 }
136 delete this.widget_id; // Delete id from model so widget manager cleans up.
128 },
137 },
129
138
130
139
@@ -158,6 +167,10
158 var class_list = msg.content.data.class_list;
167 var class_list = msg.content.data.class_list;
159 this._execute_views_method(method, selector, class_list);
168 this._execute_views_method(method, selector, class_list);
160 break;
169 break;
170 case 'set_snapshot':
171 var cell = this._get_msg_cell(msg.parent_header.msg_id);
172 cell.metadata.snapshot = msg.content.data.snapshot;
173 break;
161 case 'custom':
174 case 'custom':
162 this._handle_custom_msg(msg.content.data.custom_content);
175 this._handle_custom_msg(msg.content.data.custom_content);
163 break;
176 break;
@@ -185,7 +198,6
185 }
198 }
186 }
199 }
187 }
200 }
188 this.id = this.comm.comm_id;
189 this.save();
201 this.save();
190 } finally {
202 } finally {
191 this.updating = false;
203 this.updating = false;
@@ -195,22 +207,24
195
207
196 _handle_status: function (cell, msg) {
208 _handle_status: function (cell, msg) {
197 //execution_state : ('busy', 'idle', 'starting')
209 //execution_state : ('busy', 'idle', 'starting')
198 if (msg.content.execution_state=='idle') {
210 if (this._has_comm()) {
199
211 if (msg.content.execution_state=='idle') {
200 // Send buffer if this message caused another message to be
212
201 // throttled.
213 // Send buffer if this message caused another message to be
202 if (this.msg_buffer !== null &&
214 // throttled.
203 this.msg_throttle == this.pending_msgs) {
215 if (this.msg_buffer !== null &&
204
216 this.msg_throttle == this.pending_msgs) {
205 var callbacks = this._make_callbacks(cell);
217
206 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
218 var callbacks = this._make_callbacks(cell);
207 this.comm.send(data, callbacks);
219 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
208 this.msg_buffer = null;
220 this.comm.send(data, callbacks);
209 } else {
221 this.msg_buffer = null;
222 } else {
210
223
211 // Only decrease the pending message count if the buffer
224 // Only decrease the pending message count if the buffer
212 // doesn't get flushed (sent).
225 // doesn't get flushed (sent).
213 --this.pending_msgs;
226 --this.pending_msgs;
227 }
214 }
228 }
215 }
229 }
216 },
230 },
@@ -223,44 +237,46
223
237
224 // Only send updated state if the state hasn't been changed
238 // Only send updated state if the state hasn't been changed
225 // during an update.
239 // during an update.
226 if (!this.updating) {
240 if (this._has_comm()) {
227 if (this.pending_msgs >= this.msg_throttle) {
241 if (!this.updating) {
228 // The throttle has been exceeded, buffer the current msg so
242 if (this.pending_msgs >= this.msg_throttle) {
229 // it can be sent once the kernel has finished processing
243 // The throttle has been exceeded, buffer the current msg so
230 // some of the existing messages.
244 // it can be sent once the kernel has finished processing
231 if (method=='patch') {
245 // some of the existing messages.
232 if (this.msg_buffer === null) {
246 if (method=='patch') {
247 if (this.msg_buffer === null) {
248 this.msg_buffer = $.extend({}, model_json); // Copy
249 }
250 for (attr in options.attrs) {
251 this.msg_buffer[attr] = options.attrs[attr];
252 }
253 } else {
233 this.msg_buffer = $.extend({}, model_json); // Copy
254 this.msg_buffer = $.extend({}, model_json); // Copy
234 }
255 }
235 for (attr in options.attrs) {
236 this.msg_buffer[attr] = options.attrs[attr];
237 }
238 } else {
239 this.msg_buffer = $.extend({}, model_json); // Copy
240 }
241
256
242 } else {
257 } else {
243 // We haven't exceeded the throttle, send the message like
258 // We haven't exceeded the throttle, send the message like
244 // normal. If this is a patch operation, just send the
259 // normal. If this is a patch operation, just send the
245 // changes.
260 // changes.
246 var send_json = model_json;
261 var send_json = model_json;
247 if (method =='patch') {
262 if (method =='patch') {
248 send_json = {};
263 send_json = {};
249 for (attr in options.attrs) {
264 for (attr in options.attrs) {
250 send_json[attr] = options.attrs[attr];
265 send_json[attr] = options.attrs[attr];
266 }
251 }
267 }
252 }
253
268
254 var data = {method: 'backbone', sync_method: method, sync_data: send_json};
269 var data = {method: 'backbone', sync_method: method, sync_data: send_json};
255
270
256 var cell = null;
271 var cell = null;
257 if (this.last_modified_view !== undefined && this.last_modified_view !== null) {
272 if (this.last_modified_view !== undefined && this.last_modified_view !== null) {
258 cell = this.last_modified_view.cell;
273 cell = this.last_modified_view.cell;
274 }
275
276 var callbacks = this._make_callbacks(cell);
277 this.comm.send(data, callbacks);
278 this.pending_msgs++;
259 }
279 }
260
261 var callbacks = this._make_callbacks(cell);
262 this.comm.send(data, callbacks);
263 this.pending_msgs++;
264 }
280 }
265 }
281 }
266
282
@@ -301,28 +317,29
301
317
302
318
303 // Create view that represents the model.
319 // Create view that represents the model.
304 _display_view: function (view_name, parent_comm_id, cell) {
320 _display_view: function (view_name, parent_id, cell) {
305 var new_views = [];
321 var new_views = [];
306 var view;
322 var view;
307
323
308 // Try creating and adding the view to it's parent.
324 // Try creating and adding the view to it's parent.
309 var displayed = false;
325 var displayed = false;
310 if (parent_comm_id !== undefined) {
326 if (parent_id !== undefined) {
311 var parent_comm = this.comm_manager.comms[parent_comm_id];
327 var parent_model = this.widget_manager.get_model(parent_id);
312 var parent_model = parent_comm.model;
328 if (parent_model !== null) {
313 var parent_views = parent_model.views;
329 var parent_views = parent_model.views;
314 for (var parent_view_index in parent_views) {
330 for (var parent_view_index in parent_views) {
315 var parent_view = parent_views[parent_view_index];
331 var parent_view = parent_views[parent_view_index];
316 if (parent_view.cell === cell) {
332 if (parent_view.cell === cell) {
317 if (parent_view.display_child !== undefined) {
333 if (parent_view.display_child !== undefined) {
318 view = this._create_view(view_name, cell);
334 view = this._create_view(view_name, cell);
319 if (view !== null) {
335 if (view !== null) {
320 new_views.push(view);
336 new_views.push(view);
321 parent_view.display_child(view);
337 parent_view.display_child(view);
322 displayed = true;
338 displayed = true;
323 this._handle_view_displayed(view);
339 this._handle_view_displayed(view);
324 }
340 }
325 }
341 }
342 }
326 }
343 }
327 }
344 }
328 }
345 }
@@ -377,8 +394,13
377 console.log("Exception in widget model close callback", e, that);
394 console.log("Exception in widget model close callback", e, that);
378 }
395 }
379 }
396 }
380 that.comm.close();
397
381 delete that.comm.model; // Delete ref so GC will collect widget model.
398 if (that._has_comm()) {
399 that.comm.close();
400 delete that.comm.model; // Delete ref so GC will collect widget model.
401 delete that.comm;
402 }
403 delete that.widget_id; // Delete id from model so widget manager cleans up.
382 }
404 }
383 });
405 });
384 return view;
406 return view;
@@ -438,13 +460,15
438 // for the message. get_cell callbacks are registered for
460 // for the message. get_cell callbacks are registered for
439 // widget messages, so this block is actually checking to see if the
461 // widget messages, so this block is actually checking to see if the
440 // message was triggered by a widget.
462 // message was triggered by a widget.
441 var kernel = this.comm_manager.kernel;
463 var kernel = this.widget_manager.get_kernel();
442 var callbacks = kernel.get_callbacks_for_msg(msg_id);
464 if (kernel !== undefined && kernel !== null) {
443 if (callbacks !== undefined &&
465 var callbacks = kernel.get_callbacks_for_msg(msg_id);
444 callbacks.iopub !== undefined &&
466 if (callbacks !== undefined &&
445 callbacks.iopub.get_cell !== undefined) {
467 callbacks.iopub !== undefined &&
446
468 callbacks.iopub.get_cell !== undefined) {
447 return callbacks.iopub.get_cell();
469
470 return callbacks.iopub.get_cell();
471 }
448 }
472 }
449
473
450 // Not triggered by a cell or widget (no get_cell callback
474 // Not triggered by a cell or widget (no get_cell callback
@@ -452,6 +476,12
452 return null;
476 return null;
453 },
477 },
454
478
479
480 // Function that checks if a comm has been attached to this widget
481 // model. Returns True if a valid comm is attached.
482 _has_comm: function() {
483 return this.comm !== undefined && this.comm !== null;
484 },
455 });
485 });
456
486
457
487
@@ -540,6 +570,7
540 this.comm_manager = null;
570 this.comm_manager = null;
541 this.widget_model_types = {};
571 this.widget_model_types = {};
542 this.widget_view_types = {};
572 this.widget_view_types = {};
573 this._model_instances = {};
543
574
544 var that = this;
575 var that = this;
545 Backbone.sync = function (method, model, options, error) {
576 Backbone.sync = function (method, model, options, error) {
@@ -581,8 +612,26
581 return IPython.notebook.get_msg_cell(msg_id);
612 return IPython.notebook.get_msg_cell(msg_id);
582 }
613 }
583 };
614 };
584
615
585
616
617 WidgetManager.prototype.get_model = function (widget_id) {
618 var model = this._model_instances[widget_id];
619 if (model.id == widget_id) {
620 return model;
621 }
622 return null;
623 };
624
625
626 WidgetManager.prototype.get_kernel = function () {
627 if (this.comm_manager === null) {
628 return null;
629 } else {
630 return this.comm_manager.kernel;
631 }
632 };
633
634
586 WidgetManager.prototype.on_create_widget = function (callback) {
635 WidgetManager.prototype.on_create_widget = function (callback) {
587 this._create_widget_callback = callback;
636 this._create_widget_callback = callback;
588 };
637 };
@@ -601,11 +650,11
601
650
602 WidgetManager.prototype._handle_com_open = function (comm, msg) {
651 WidgetManager.prototype._handle_com_open = function (comm, msg) {
603 var widget_type_name = msg.content.target_name;
652 var widget_type_name = msg.content.target_name;
604 var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
653 var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm);
654 this._model_instances[comm.comm_id] = widget_model;
605 this._handle_create_widget(widget_model);
655 this._handle_create_widget(widget_model);
606 };
656 };
607
657
608
609 //--------------------------------------------------------------------
658 //--------------------------------------------------------------------
610 // Init code
659 // Init code
611 //--------------------------------------------------------------------
660 //--------------------------------------------------------------------
General Comments 0
You need to be logged in to leave comments. Login now