Skip to content

Commit 63c325a

Browse files
committed
Refactor of runOnSplashScreen
1 parent ca3fb17 commit 63c325a

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

src/appshell/internal/startupscenario.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QCoreApplication>
2626

2727
#include "async/async.h"
28+
#include "network/networkerrors.h"
2829
#include "translation.h"
2930
#include "log.h"
3031

@@ -94,50 +95,45 @@ muse::async::Promise<muse::Ret> StartupScenario::runOnSplashScreen()
9495
registerAudioPlugins();
9596

9697
if (multiInstancesProvider()->instances().size() != 1) {
97-
const Ret ret = muse::make_ret(Ret::Code::Cancel);
98+
const Ret ret = muse::make_ret(Ret::Code::Ok);
9899
return resolve(ret);
99100
}
100101

101102
// Calculate the total number of expected update checks (TODO: check the
102103
// connection before trying any of this)...
103-
static size_t totalChecksExpected = 0;
104104

105105
const bool canCheckAppUpdate = appUpdateScenario() && appUpdateScenario()->needCheckForUpdate();
106-
if (canCheckAppUpdate) {
107-
++totalChecksExpected;
108-
}
109-
110106
const bool canCheckMuseSoundsUpdate = museSoundsUpdateScenario() && museSoundsUpdateScenario()->needCheckForUpdate();
111-
if (canCheckMuseSoundsUpdate) {
112-
++totalChecksExpected;
113-
}
114-
115107
//! NOTE: A MuseSampler update check also exists but we run it later (see onStartupPageOpened)...
108+
m_totalChecksExpected = size_t(canCheckAppUpdate) + size_t(canCheckMuseSoundsUpdate);
116109

117-
if (totalChecksExpected == 0) {
110+
if (m_totalChecksExpected == 0) {
118111
const Ret ret = muse::make_ret(Ret::Code::Ok);
119112
return resolve(ret);
120113
}
121114

115+
m_totalChecksReceived = 0;
116+
122117
// Resolve once all checks are completed...
123118
const auto onUpdateCheckCompleted = [this, resolve](){
124-
static size_t totalChecksReceived = 0;
125-
IF_ASSERT_FAILED(m_updateCheckInProgress && totalChecksReceived < totalChecksExpected) {
126-
m_updateCheckInProgress = false;
127-
return;
119+
if (!m_updateChecksInProgress) {
120+
return; // Already resolved or timed out...
128121
}
129122

130-
++totalChecksReceived;
123+
++m_totalChecksReceived;
131124

132-
if (totalChecksReceived == totalChecksExpected) {
133-
m_updateCheckInProgress = false;
134-
const Ret ret = muse::make_ret(Ret::Code::Ok);
135-
(void)resolve(ret);
125+
if (m_totalChecksReceived < m_totalChecksExpected) {
126+
return; // Not ready to resolve yet...
136127
}
128+
129+
m_updateChecksInProgress = false;
130+
131+
const Ret ret = muse::make_ret(Ret::Code::Ok);
132+
(void)resolve(ret);
137133
};
138134

139135
// Asynchronously start the checks once we know the total number of expected checks...
140-
m_updateCheckInProgress = true;
136+
m_updateChecksInProgress = true;
141137
async::Async::call(this, [this, onUpdateCheckCompleted, canCheckAppUpdate, canCheckMuseSoundsUpdate]() {
142138
if (canCheckAppUpdate) {
143139
muse::async::Promise<Ret> promise = appUpdateScenario()->checkForUpdate(/*manual*/ false);
@@ -155,11 +151,15 @@ muse::async::Promise<muse::Ret> StartupScenario::runOnSplashScreen()
155151

156152
// Timeout if the checks take too long...
157153
QTimer::singleShot(CHECK_FOR_UPDATES_TIMEOUT, [this, resolve]() {
158-
if (m_updateCheckInProgress) {
159-
LOGE() << "Update checks timed out...";
160-
const Ret ret = muse::make_ret(Ret::Code::Cancel);
161-
(void)resolve(ret);
154+
if (!m_updateChecksInProgress) {
155+
return;
162156
}
157+
158+
m_updateChecksInProgress = false;
159+
160+
LOGE() << "Update checks timed out...";
161+
const Ret ret = network::make_ret(network::Err::Timeout);
162+
(void)resolve(ret);
163163
});
164164

165165
return muse::async::Promise<Ret>::dummy_result();

src/appshell/internal/startupscenario.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ class StartupScenario : public IStartupScenario, public muse::Injectable, public
9191
project::ProjectFile m_startupScoreFile;
9292
bool m_startupCompleted = false;
9393

94-
bool m_updateCheckInProgress = false;
94+
bool m_updateChecksInProgress = false;
95+
size_t m_totalChecksExpected = 0;
96+
size_t m_totalChecksReceived = 0;
9597
};
9698
}
9799

0 commit comments

Comments
 (0)