From e02a5ec201bc7e12e6b2433659fe832975f3fc24 Mon Sep 17 00:00:00 2001 From: Jonathan Frederic Date: Tue, 18 Nov 2014 19:00:16 -0800 Subject: [PATCH] Promises... --- IPython/html/static/notebook/js/codecell.js | 5 +- IPython/html/static/widgets/js/manager.js | 73 ++++++++++++++------- IPython/html/static/widgets/js/widget.js | 24 ++++--- 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/IPython/html/static/notebook/js/codecell.js b/IPython/html/static/notebook/js/codecell.js index 0e0d9f899..7559641bc 100644 --- a/IPython/html/static/notebook/js/codecell.js +++ b/IPython/html/static/notebook/js/codecell.js @@ -221,7 +221,7 @@ define([ * Display a widget view in the cell. */ CodeCell.prototype.display_widget_view = function(view_promise) { - + // Display a dummy element var dummy = $('
'); this.widget_subarea.append(dummy); @@ -229,8 +229,9 @@ define([ // Display the view. var that = this; return view_promise.then(function(view) { + that.widget_area.show(); dummy.replaceWith(view.$el); - this.widget_views.push(view); + that.widget_views.push(view); return view; }); }; diff --git a/IPython/html/static/widgets/js/manager.js b/IPython/html/static/widgets/js/manager.js index 8d9f24d6d..696c85bb3 100644 --- a/IPython/html/static/widgets/js/manager.js +++ b/IPython/html/static/widgets/js/manager.js @@ -61,26 +61,29 @@ define([ reject(new Error("Could not determine where the display" + " message was from. Widget will not be displayed")); } else { - return that.display_view_in_cell(cell, model); + return that.display_view_in_cell(cell, model) + .catch(function(error) { + reject(new utils.WrappedError('View could not be displayed.', error)); + }); } }); }; WidgetManager.prototype.display_view_in_cell = function(cell, model) { // Displays a view in a cell. + var that = this; return new Promise(function(resolve, reject) { if (cell.display_widget_view) { cell.display_widget_view(that.create_view(model, {cell: cell})) .then(function(view) { - that._handle_display_view(view); view.trigger('displayed'); resolve(view); }, function(error) { - reject(new utils.WrappedError('Could not display view', error)); + reject(new utils.WrappedError('Could not create or display view', error)); }); } else { - reject(new Error('Cell does not have a `display_widget_view` method.')); + reject(new Error('Cell does not have a `display_widget_view` method')); } }); }; @@ -280,7 +283,8 @@ define([ // Only return models with one or more displayed views. // not_alive: (optional) boolean=false // Include models that have comms with severed connections. - return utils.resolve_promise_dict(function(models) { + var that = this; + return utils.resolve_promises_dict(this._models).then(function(models) { var state = {}; for (var model_id in models) { if (models.hasOwnProperty(model_id)) { @@ -291,9 +295,10 @@ define([ var displayed_flag = !(options && options.only_displayed) || Object.keys(model.views).length > 0; var alive_flag = (options && options.not_alive) || model.comm_alive; if (displayed_flag && alive_flag) { - state[model.model_id] = { + state[model_id] = { model_name: model.name, model_module: model.module, + state: model.get_state(), views: [], }; @@ -301,8 +306,8 @@ define([ for (var id in model.views) { if (model.views.hasOwnProperty(id)) { var view = model.views[id]; - var cell_index = this.notebook.find_cell_index(view.options.cell); - state[model.model_id].views.push(cell_index); + var cell_index = that.notebook.find_cell_index(view.options.cell); + state[model_id].views.push(cell_index); } } } @@ -321,40 +326,60 @@ define([ // Get the kernel when it's available. var that = this; return (new Promise(function(resolve, reject) { - if (that.kernel) { - resolve(that.kernel); + if (that.comm_manager && that.comm_manager.kernel) { + resolve(that.comm_manager.kernel); } else { - that.events.on('kernel_created.Session', function(event, data) { + that.events.on('kernel_connected.Session', function(event, data) { resolve(data.kernel); }); } })).then(function(kernel) { - // Recreate all the widget models for the given state. - that.widget_models = []; - for (var i = 0; i < state.length; i++) { + // Recreate all the widget models for the given state and + // display the views. + that.all_views = []; + var model_ids = Object.keys(state); + for (var i = 0; i < model_ids.length; i++) { + var model_id = model_ids[i]; + // Recreate a comm using the widget's model id (model_id == comm_id). - var new_comm = new comm.Comm(kernel.widget_manager.comm_target_name, state[i].model_id); + var new_comm = new comm.Comm(kernel.widget_manager.comm_target_name, model_id); kernel.comm_manager.register_comm(new_comm); // Create the model using the recreated comm. When the model is // created we don't know yet if the comm is valid so set_comm_alive // false. Once we receive the first state push from the back-end // we know the comm is alive. - var model = kernel.widget_manager.create_model({ + var views = kernel.widget_manager.create_model({ comm: new_comm, - model_name: state[i].model_name, - model_module: state[i].model_module}).then(function(model) { - model.set_comm_alive(false); - model.request_state(); - model.received_state.then(function() { + model_name: state[model_id].model_name, + model_module: state[model_id].model_module}) + .then(function(model) { + + model.set_comm_alive(false); + var view_promise = Promise.resolve().then(function() { + return model.set_state(state[model.id].state); + }).then(function() { + model.request_state().then(function() { model.set_comm_alive(true); }); - return model; + + // Display the views of the model. + var views = []; + var model_views = state[model.id].views; + for (var j=0; j