##// END OF EJS Templates
Address @jasongrout 's review comments, take 2
Jonathan Frederic -
Show More
@@ -88,38 +88,32 b' define(['
88 /**
88 /**
89 * Displays a view for a particular model.
89 * Displays a view for a particular model.
90 */
90 */
91 var that = this;
91 var cell = this.get_msg_cell(msg.parent_header.msg_id);
92 return new Promise(function(resolve, reject) {
92 if (cell === null) {
93 var cell = that.get_msg_cell(msg.parent_header.msg_id);
93 return Promise.reject(new Error("Could not determine where the display" +
94 if (cell === null) {
94 " message was from. Widget will not be displayed"));
95 reject(new Error("Could not determine where the display" +
95 } else {
96 " message was from. Widget will not be displayed"));
96 return this.display_view_in_cell(cell, model)
97 } else {
97 .catch(utils.reject('View could not be displayed.', true));
98 return that.display_view_in_cell(cell, model)
98 }
99 .catch(function(error) {
100 reject(new utils.WrappedError('View could not be displayed.', error));
101 });
102 }
103 });
104 };
99 };
105
100
106 WidgetManager.prototype.display_view_in_cell = function(cell, model) {
101 WidgetManager.prototype.display_view_in_cell = function(cell, model) {
107 // Displays a view in a cell.
102 // Displays a view in a cell.
108 var that = this;
103 if (cell.display_widget_view) {
109 return new Promise(function(resolve, reject) {
104 var that = this;
110 if (cell.display_widget_view) {
105 return cell.display_widget_view(this.create_view(model, {
111 cell.display_widget_view(that.create_view(model, {cell: cell}))
106 cell: cell,
112 .then(function(view) {
107 // Only set cell_index when view is displayed as directly.
113 that._handle_display_view(view);
108 cell_index: that.notebook.find_cell_index(cell),
114 view.trigger('displayed');
109 })).then(function(view) {
115 resolve(view);
110 that._handle_display_view(view);
116 }, function(error) {
111 view.trigger('displayed');
117 reject(new utils.WrappedError('Could not create or display view', error));
112 resolve(view);
118 });
113 }).catch(utils.reject('Could not create or display view', true));
119 } else {
114 } else {
120 reject(new Error('Cell does not have a `display_widget_view` method'));
115 return Promise.reject(new Error('Cell does not have a `display_widget_view` method'));
121 }
116 }
122 });
123 };
117 };
124
118
125 WidgetManager.prototype._handle_display_view = function (view) {
119 WidgetManager.prototype._handle_display_view = function (view) {
@@ -315,7 +309,7 b' define(['
315 // Dictionary of options with the following contents:
309 // Dictionary of options with the following contents:
316 // only_displayed: (optional) boolean=false
310 // only_displayed: (optional) boolean=false
317 // Only return models with one or more displayed views.
311 // Only return models with one or more displayed views.
318 // not_alive: (optional) boolean=false
312 // not_live: (optional) boolean=false
319 // Include models that have comms with severed connections.
313 // Include models that have comms with severed connections.
320 //
314 //
321 // Returns
315 // Returns
@@ -331,8 +325,8 b' define(['
331 // If the model has one or more views defined for it,
325 // If the model has one or more views defined for it,
332 // consider it displayed.
326 // consider it displayed.
333 var displayed_flag = !(options && options.only_displayed) || Object.keys(model.views).length > 0;
327 var displayed_flag = !(options && options.only_displayed) || Object.keys(model.views).length > 0;
334 var alive_flag = (options && options.not_alive) || model.comm_alive;
328 var live_flag = (options && options.not_live) || model.comm_live;
335 if (displayed_flag && alive_flag) {
329 if (displayed_flag && live_flag) {
336 state[model_id] = {
330 state[model_id] = {
337 model_name: model.name,
331 model_name: model.name,
338 model_module: model.module,
332 model_module: model.module,
@@ -344,13 +338,8 b' define(['
344 for (var id in model.views) {
338 for (var id in model.views) {
345 if (model.views.hasOwnProperty(id)) {
339 if (model.views.hasOwnProperty(id)) {
346 var view = model.views[id];
340 var view = model.views[id];
347 var cell = view.options.cell;
341 if (view.options.cell_index) {
348
342 state[model_id].views.push(view.options.cell_index);
349 // Only store the cell reference if this view is a top level
350 // child of the cell.
351 if (cell.widget_views.indexOf(view) != -1) {
352 var cell_index = that.notebook.find_cell_index(cell);
353 state[model_id].views.push(cell_index);
354 }
343 }
355 }
344 }
356 }
345 }
@@ -358,7 +347,7 b' define(['
358 }
347 }
359 }
348 }
360 return state;
349 return state;
361 });
350 }).catch(utils.reject('Could not get state of widget manager', true));
362 };
351 };
363
352
364 WidgetManager.prototype.set_state = function(state) {
353 WidgetManager.prototype.set_state = function(state) {
@@ -383,7 +372,7 b' define(['
383 kernel.comm_manager.register_comm(new_comm);
372 kernel.comm_manager.register_comm(new_comm);
384
373
385 // Create the model using the recreated comm. When the model is
374 // Create the model using the recreated comm. When the model is
386 // created we don't know yet if the comm is valid so set_comm_alive
375 // created we don't know yet if the comm is valid so set_comm_live
387 // false. Once we receive the first state push from the back-end
376 // false. Once we receive the first state push from the back-end
388 // we know the comm is alive.
377 // we know the comm is alive.
389 var views = kernel.widget_manager.create_model({
378 var views = kernel.widget_manager.create_model({
@@ -392,12 +381,12 b' define(['
392 model_module: state[model_id].model_module})
381 model_module: state[model_id].model_module})
393 .then(function(model) {
382 .then(function(model) {
394
383
395 model.set_comm_alive(false);
384 model.set_comm_live(false);
396 var view_promise = Promise.resolve().then(function() {
385 var view_promise = Promise.resolve().then(function() {
397 return model.set_state(state[model.id].state);
386 return model.set_state(state[model.id].state);
398 }).then(function() {
387 }).then(function() {
399 model.request_state().then(function() {
388 model.request_state().then(function() {
400 model.set_comm_alive(true);
389 model.set_comm_live(true);
401 });
390 });
402
391
403 // Display the views of the model.
392 // Display the views of the model.
@@ -31,6 +31,7 b' define(["widgets/js/manager",'
31 this.state_lock = null;
31 this.state_lock = null;
32 this.id = model_id;
32 this.id = model_id;
33 this.views = {};
33 this.views = {};
34 this._resolve_received_state = {};
34
35
35 if (comm !== undefined) {
36 if (comm !== undefined) {
36 // Remember comm associated with the model.
37 // Remember comm associated with the model.
@@ -42,9 +43,9 b' define(["widgets/js/manager",'
42 comm.on_msg($.proxy(this._handle_comm_msg, this));
43 comm.on_msg($.proxy(this._handle_comm_msg, this));
43
44
44 // Assume the comm is alive.
45 // Assume the comm is alive.
45 this.set_comm_alive(true);
46 this.set_comm_live(true);
46 } else {
47 } else {
47 this.set_comm_alive(false);
48 this.set_comm_live(false);
48 }
49 }
49 return Backbone.Model.apply(this);
50 return Backbone.Model.apply(this);
50 },
51 },
@@ -69,24 +70,24 b' define(["widgets/js/manager",'
69 return;
70 return;
70 }
71 }
71
72
73 var msg_id = this.comm.send({method: 'request_state'}, callbacks || this.widget_manager.callbacks());
74
72 // Promise that is resolved when a state is received
75 // Promise that is resolved when a state is received
73 // from the back-end.
76 // from the back-end.
74 var that = this;
77 var that = this;
75 var received_state = new Promise(function(resolve) {
78 var received_state = new Promise(function(resolve) {
76 that._resolve_received_state = resolve;
79 that._resolve_received_state[msg_id] = resolve;
77 });
80 });
78
79 this.comm.send({method: 'request_state'}, callbacks || this.widget_manager.callbacks());
80 return received_state;
81 return received_state;
81 },
82 },
82
83
83 set_comm_alive: function(alive) {
84 set_comm_live: function(live) {
84 /**
85 /**
85 * Change the comm_alive state of the model.
86 * Change the comm_live state of the model.
86 */
87 */
87 if (this.comm_alive === undefined || this.comm_alive != alive) {
88 if (this.comm_live === undefined || this.comm_live != live) {
88 this.comm_alive = alive;
89 this.comm_live = live;
89 this.trigger(alive ? 'comm_is_live' : 'comm_is_dead', {model: this});
90 this.trigger(live ? 'comm:live' : 'comm:dead', {model: this});
90 }
91 }
91 },
92 },
92
93
@@ -126,9 +127,17 b' define(["widgets/js/manager",'
126 var that = this;
127 var that = this;
127 switch (method) {
128 switch (method) {
128 case 'update':
129 case 'update':
129 this.state_change = this.state_change.then(function() {
130 this.state_change = this.state_change
130 return that.set_state(msg.content.data.state);
131 .then(function() {
131 }).catch(utils.reject("Couldn't process update msg for model id '" + String(that.id) + "'", true));
132 return that.set_state(msg.content.data.state);
133 }).catch(utils.reject("Couldn't process update msg for model id '" + String(that.id) + "'", true))
134 .then(function() {
135 var parent_id = msg.parent_header.msg_id;
136 if (that._resolve_received_state[parent_id] !== undefined) {
137 that._resolve_received_state[parent_id].call();
138 delete that._resolve_received_state[parent_id];
139 }
140 }).catch(utils.reject("Couldn't resolve state request promise.", true));
132 break;
141 break;
133 case 'custom':
142 case 'custom':
134 this.trigger('msg:custom', msg.content.data.content);
143 this.trigger('msg:custom', msg.content.data.content);
@@ -150,11 +159,6 b' define(["widgets/js/manager",'
150 } finally {
159 } finally {
151 that.state_lock = null;
160 that.state_lock = null;
152 }
161 }
153
154 if (that._resolve_received_state !== undefined) {
155 that._resolve_received_state();
156 }
157 return Promise.resolve();
158 }, utils.reject("Couldn't set model state", true));
162 }, utils.reject("Couldn't set model state", true));
159 },
163 },
160
164
General Comments 0
You need to be logged in to leave comments. Login now