From 955fcdde74aeb86a8cf9b1a815e0e74289d6bb76 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 15 Sep 2020 14:10:43 -0400 Subject: [PATCH 1/4] Add support for legacy promises --- rsconnect_jupyter/static/connect.js | 48 +++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/rsconnect_jupyter/static/connect.js b/rsconnect_jupyter/static/connect.js index bef1a267..a3b2dd54 100644 --- a/rsconnect_jupyter/static/connect.js +++ b/rsconnect_jupyter/static/connect.js @@ -316,6 +316,7 @@ define([ $container.empty(); ContentsManager.list_contents(that.currentPath) .then(function(contents) { + verbose('got contents:', contents); var content = contents.content.sort(function (a, b) { // Directories come first if (a.type === 'directory' ? b.type !== 'directory' : b.type === 'directory') { @@ -1157,18 +1158,45 @@ define([ .then(function() { return ContentsManager.list_contents(notebookDirectory); }) - .then(function (contents) { - verbose('Got contents of directory:', contents); - for(var index in contents.content) { - if (contents.content[index].name === 'requirements.txt') { - verbose('Found requirements.txt:', contents.content[index]); - hasRequirementsTxt = true; - } else if (contents.content[index].name === 'environment.yml') { - // TODO: Enable when ready - // hasEnvironmentYml = true; + .then(function (result) { + function getRequirements(contents) { + verbose('contents:', contents); + for (var index in contents.content) { + if (contents.content[index].name === 'requirements.txt') { + verbose('Found requirements.txt:', contents.content[index]); + hasRequirementsTxt = true; + } else if (contents.content[index].name === 'environment.yml') { + // TODO: Enable when ready + // hasEnvironmentYml = true; + } } } - preparePublishRequirementsTxtDialog(hasRequirementsTxt, hasEnvironmentYml, isCondaEnvironment, environmentOptions); + if (result instanceof window.Promise) { + verbose('Using legacy promise mode.'); + return result.then(getRequirements); + } else { + return getRequirements(result); + } + }) + .then(function(result) { + if (result instanceof window.Promise) { + result.then(function () { + preparePublishRequirementsTxtDialog( + hasRequirementsTxt, + hasEnvironmentYml, + isCondaEnvironment, + environmentOptions + ); + } + ); + } else { + preparePublishRequirementsTxtDialog( + hasRequirementsTxt, + hasEnvironmentYml, + isCondaEnvironment, + environmentOptions + ); + } }); // generate server list From 91b8d5f7dff9b4499053e1c4b205d47f3cf1c982 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 15 Sep 2020 16:05:14 -0400 Subject: [PATCH 2/4] update manifest publishing workflow too --- rsconnect_jupyter/static/connect.js | 42 +++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/rsconnect_jupyter/static/connect.js b/rsconnect_jupyter/static/connect.js index a3b2dd54..4c51f311 100644 --- a/rsconnect_jupyter/static/connect.js +++ b/rsconnect_jupyter/static/connect.js @@ -1963,16 +1963,42 @@ define([ .then(function() { return ContentsManager.list_contents(notebookDirectory); }) - .then(function (contents) { - for(var index in contents.content) { - if (contents.content[index].name === 'requirements.txt') { - hasRequirementsTxt = true; - } else if (contents.content[index].name === 'environment.yml') { - // TODO: Enable when ready - // hasEnvironmentYml = true; + .then(function (result) { + function getRequirements(contents) { + verbose('contents:', contents); + for (var index in contents.content) { + if (contents.content[index].name === 'requirements.txt') { + verbose('Found requirements.txt:', contents.content[index]); + hasRequirementsTxt = true; + } else if (contents.content[index].name === 'environment.yml') { + // TODO: Enable when ready + // hasEnvironmentYml = true; + } } } - prepareManifestRequirementsTxtDialog(hasRequirementsTxt, hasEnvironmentYml, isCondaEnvironment); + + if (result instanceof window.Promise) { + verbose('Using legacy promise mode.'); + return result.then(getRequirements); + } else { + return getRequirements(result); + } + }) + .then(function (result) { +if (result instanceof window.Promise) { + result.then(function () { + prepareManifestRequirementsTxtDialog( + hasRequirementsTxt, + hasEnvironmentYml, + isCondaEnvironment); + } + ); + } else { + prepareManifestRequirementsTxtDialog( + hasRequirementsTxt, + hasEnvironmentYml, + isCondaEnvironment); + } }); var btnCancel = $( From 7f6aa6aa2296f7f29f51acc480c74323f02f2ed6 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Wed, 16 Sep 2020 14:46:03 -0400 Subject: [PATCH 3/4] use legacypromisehandler --- rsconnect_jupyter/static/connect.js | 70 +++++++++++++---------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/rsconnect_jupyter/static/connect.js b/rsconnect_jupyter/static/connect.js index 4c51f311..2e06ff3d 100644 --- a/rsconnect_jupyter/static/connect.js +++ b/rsconnect_jupyter/static/connect.js @@ -19,6 +19,23 @@ define([ console.error.apply(window, arguments); } } + + /** + * legacyPromiseHandler applies a function to a result whether or not + * it's a plain value or a promise. + * @param result {Promise|any} either a promise or a plain value + * @param callback {Function} a function that takes result as a plain value + * @returns {Promise|any} If result was a promise, a promise. If result was a + * plain value, a plain value. + */ + function legacyPromiseHandler(result, callback) { + if (result instanceof window.Promise) { + verbose('Using legacy promise mode.') + return result.then(callback); + } else { + return callback(result); + } + } // this will be filled in by `init()` var notify = null; @@ -1171,32 +1188,18 @@ define([ } } } - if (result instanceof window.Promise) { - verbose('Using legacy promise mode.'); - return result.then(getRequirements); - } else { - return getRequirements(result); - } + return legacyPromiseHandler(result, getRequirements); }) .then(function(result) { - if (result instanceof window.Promise) { - result.then(function () { - preparePublishRequirementsTxtDialog( - hasRequirementsTxt, - hasEnvironmentYml, - isCondaEnvironment, - environmentOptions - ); - } - ); - } else { + function doPrepare() { preparePublishRequirementsTxtDialog( - hasRequirementsTxt, - hasEnvironmentYml, - isCondaEnvironment, - environmentOptions + hasRequirementsTxt, + hasEnvironmentYml, + isCondaEnvironment, + environmentOptions ); } + return legacyPromiseHandler(result, doPrepare); }); // generate server list @@ -1976,29 +1979,16 @@ define([ } } } - - if (result instanceof window.Promise) { - verbose('Using legacy promise mode.'); - return result.then(getRequirements); - } else { - return getRequirements(result); - } + return legacyPromiseHandler(result, getRequirements); }) .then(function (result) { -if (result instanceof window.Promise) { - result.then(function () { - prepareManifestRequirementsTxtDialog( - hasRequirementsTxt, - hasEnvironmentYml, - isCondaEnvironment); - } - ); - } else { + function doPrepare() { prepareManifestRequirementsTxtDialog( - hasRequirementsTxt, - hasEnvironmentYml, - isCondaEnvironment); + hasRequirementsTxt, + hasEnvironmentYml, + isCondaEnvironment); } + return legacyPromiseHandler(result, doPrepare); }); var btnCancel = $( From fe4b09f55e69a9145124883c3cb80a2ed8627d4d Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Wed, 16 Sep 2020 14:59:48 -0400 Subject: [PATCH 4/4] lint --- rsconnect_jupyter/static/connect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsconnect_jupyter/static/connect.js b/rsconnect_jupyter/static/connect.js index 2e06ff3d..87a9de68 100644 --- a/rsconnect_jupyter/static/connect.js +++ b/rsconnect_jupyter/static/connect.js @@ -30,7 +30,7 @@ define([ */ function legacyPromiseHandler(result, callback) { if (result instanceof window.Promise) { - verbose('Using legacy promise mode.') + verbose('Using legacy promise mode.'); return result.then(callback); } else { return callback(result);