From e37b6976c877bca0f6207eb75ff7e48f8ca9db32 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Wed, 2 Aug 2017 11:53:21 +0100 Subject: [PATCH] Add promise for notebook_loaded.Notebook event --- notebook/static/base/js/promises.js | 8 +------- notebook/static/notebook/js/main.js | 2 ++ notebook/static/notebook/js/promises.js | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 notebook/static/notebook/js/promises.js diff --git a/notebook/static/base/js/promises.js b/notebook/static/base/js/promises.js index 7513c6d15..b8100e4e6 100644 --- a/notebook/static/base/js/promises.js +++ b/notebook/static/base/js/promises.js @@ -1,13 +1,7 @@ // Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. -// Give us an object to bind all events to. This object should be created -// before all other objects so it exists when others register event handlers. -// To register an event handler: -// -// require(['base/js/events'], function (events) { -// events.on("event.Namespace", function () { do_stuff(); }); -// }); +// Define an object to attach promises to for one-time events. define(['base/js/events', 'base/js/namespace'], function(events, Jupyter) { "use strict"; diff --git a/notebook/static/notebook/js/main.js b/notebook/static/notebook/js/main.js index 213bc9c1b..735657e59 100644 --- a/notebook/static/notebook/js/main.js +++ b/notebook/static/notebook/js/main.js @@ -33,6 +33,7 @@ require([ 'auth/js/loginwidget', 'notebook/js/maintoolbar', 'notebook/js/pager', + 'notebook/js/promises', 'notebook/js/quickhelp', 'notebook/js/menubar', 'notebook/js/notificationarea', @@ -57,6 +58,7 @@ require([ loginwidget, maintoolbar, pager, + nb_promises, quickhelp, menubar, notificationarea, diff --git a/notebook/static/notebook/js/promises.js b/notebook/static/notebook/js/promises.js new file mode 100644 index 000000000..9d7ce1df7 --- /dev/null +++ b/notebook/static/notebook/js/promises.js @@ -0,0 +1,22 @@ +// Copyright (c) Jupyter Development Team. +// Distributed under the terms of the Modified BSD License. + +// Define promises for notebook events. + +define(['base/js/events', 'base/js/promises'], function(events, promises) { + "use strict"; + + // Promise to be resolved when the notebook is *initially* loaded. + // The event may fire again if the notebook is reloaded later, but this + // promise only tracks the initial load. + promises.notebook_loaded = new Promise(function(resolve, reject) { + events.one('notebook_loaded.Notebook', function() { + resolve(); + }); + events.one('notebook_load_failed.Notebook', function() { + reject(); + }); + }); + + return promises; +});