From 1028c8d1929324f00868ab8b9706437ca2e7f8bc Mon Sep 17 00:00:00 2001 From: Min RK Date: Mon, 12 Jan 2015 12:55:42 -0800 Subject: [PATCH] use promises to wait for kernelspecs on notebook load should avoid race condition that causes attempt to load kernelspec prior to loading the kernelspec list --- IPython/html/static/notebook/js/kernelselector.js | 8 ++++++++ IPython/html/static/notebook/js/notebook.js | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/IPython/html/static/notebook/js/kernelselector.js b/IPython/html/static/notebook/js/kernelselector.js index 1d5e9fa97..9be8936f2 100644 --- a/IPython/html/static/notebook/js/kernelselector.js +++ b/IPython/html/static/notebook/js/kernelselector.js @@ -9,6 +9,7 @@ define([ "use strict"; var KernelSelector = function(selector, notebook) { + var that = this; this.selector = selector; this.notebook = notebook; this.notebook.set_kernelselector(this); @@ -22,6 +23,11 @@ define([ this.bind_events(); // Make the object globally available for user convenience & inspection IPython.kernelselector = this; + this._finish_load = null; + this.loaded = new Promise(function(resolve, reject) { + that._finish_load = resolve; + }); + Object.seal(this); }; @@ -67,6 +73,8 @@ define([ .text(ks.spec.display_name)); new_notebook_submenu.append(ks_submenu_entry); } + // trigger loaded promise + this._finish_load(); }; KernelSelector.prototype._spec_changed = function (event, ks) { diff --git a/IPython/html/static/notebook/js/notebook.js b/IPython/html/static/notebook/js/notebook.js index e6ca53371..0cbb8c81f 100644 --- a/IPython/html/static/notebook/js/notebook.js +++ b/IPython/html/static/notebook/js/notebook.js @@ -2077,11 +2077,16 @@ define([ * @param {string} notebook_path - A notebook to load */ Notebook.prototype.load_notebook = function (notebook_path) { + var that = this; this.notebook_path = notebook_path; this.notebook_name = utils.url_path_split(this.notebook_path)[1]; this.events.trigger('notebook_loading.Notebook'); this.contents.get(notebook_path, {type: 'notebook'}).then( - $.proxy(this.load_notebook_success, this), + function (data) { + that.kernel_selector.loaded.then( + that.load_notebook_success(data) + ); + }, $.proxy(this.load_notebook_error, this) ); };