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