From e309646504a90f72463b852038badb553fd1adc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 30 Jun 2017 14:36:18 +0200 Subject: [PATCH] Prevent unnecessary settings fetch during startup We fetch settings once explicitly after passive login but before completed startup. Without this patch we fetched them again after onUserLoggedIn got fired (if it got fired by the passive login) during startup. That's not necessary because we did the passive login before our initial settings fetch, so IF that already logged us in, our settings fetch already was done with us logged in as well and the onUserLoggedIn later only tells us what we already knew. --- src/octoprint/static/js/app/helpers.js | 16 ++++++++++++++++ src/octoprint/static/js/app/main.js | 1 + .../static/js/app/viewmodels/settings.js | 6 ++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index 01f3146d..a83cd5ed 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -983,6 +983,22 @@ function getOnlyChangedData(data, oldData) { return f(data, oldData); } +function setOnViewModels(allViewModels, key, value) { + setOnViewModelsIf(allViewModels, key, value, undefined); +} + +function setOnViewModelsIf(allViewModels, key, value, condition) { + if (condition === undefined || !_.isFunction(condition)) { + condition = function() { return true; }; + } + + _.each(allViewModels, function(viewModel) { + if (condition(viewModel)) { + viewModel[key] = value; + } + }) +} + function callViewModels(allViewModels, method, callback) { callViewModelsIf(allViewModels, method, undefined, callback); } diff --git a/src/octoprint/static/js/app/main.js b/src/octoprint/static/js/app/main.js index 707ee58f..5960bc7b 100644 --- a/src/octoprint/static/js/app/main.js +++ b/src/octoprint/static/js/app/main.js @@ -613,6 +613,7 @@ $(function() { // startup complete callViewModels(allViewModels, "onStartupComplete"); + setOnViewModels(allViewModels, "_startupComplete", true); // make sure we can track the browser tab visibility OctoPrint.coreui.onBrowserVisibilityChange(function(status) { diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index 38818533..a18bb272 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -884,12 +884,14 @@ $(function() { }; self.onUserLoggedIn = function() { - // we might have other user rights now, refresh + // we might have other user rights now, refresh (but only if startup has fully completed) + if (!self._startupComplete) return; self.requestData(); }; self.onUserLoggedOut = function() { - // we might have other user rights now, refresh + // we might have other user rights now, refresh (but only if startup has fully completed) + if (!self._startupComplete) return; self.requestData(); } }