Skip to content

Commit a7fb0ce

Browse files
committed
Project: Add stopLoading() method
1 parent 8d2458a commit a7fb0ce

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

include/scratchcpp/project.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class LIBSCRATCHCPP_EXPORT Project
2929
Project(const Project &) = delete;
3030

3131
bool load();
32+
void stopLoading();
3233

3334
void start();
3435
void run();

src/project.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include <scratchcpp/project.h>
4+
#include <iostream>
45

56
#include "project_p.h"
7+
#include "internal/projectdownloader.h"
68

79
using namespace libscratchcpp;
810

@@ -27,6 +29,14 @@ bool Project::load()
2729
return impl->load();
2830
}
2931

32+
/*! Cancels project loading if loading in another thread. */
33+
void Project::stopLoading()
34+
{
35+
std::cout << "Aborting project loading..." << std::endl;
36+
impl->stopLoading = true;
37+
impl->downloader->cancel();
38+
}
39+
3040
/*!
3141
* Calls all "when green flag clicked" blocks.
3242
* \note Nothing will happen until run() or frame() is called.

src/project_p.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ bool ProjectPrivate::load()
4747

4848
bool ProjectPrivate::tryLoad(IProjectReader *reader)
4949
{
50+
stopLoading = false;
51+
5052
// Load from URL
5153
ProjectUrl url(fileName);
5254

@@ -57,8 +59,18 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
5759
return false;
5860
}
5961

62+
if (stopLoading) {
63+
loadingAborted();
64+
return false;
65+
}
66+
6067
bool ret = reader->loadData(downloader->json());
6168

69+
if (stopLoading) {
70+
loadingAborted();
71+
return false;
72+
}
73+
6274
if (!ret)
6375
return false;
6476

@@ -91,7 +103,14 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
91103
}
92104

93105
// Download assets
94-
if (!downloader->downloadAssets(assetNames)) {
106+
ret = downloader->downloadAssets(assetNames);
107+
108+
if (stopLoading) {
109+
loadingAborted();
110+
return false;
111+
}
112+
113+
if (!ret) {
95114
std::cerr << "Failed to download the project assets." << std::endl;
96115
return false;
97116
}
@@ -113,7 +132,18 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
113132
return false;
114133
}
115134

135+
if (stopLoading) {
136+
loadingAborted();
137+
return false;
138+
}
139+
116140
bool ret = reader->load();
141+
142+
if (stopLoading) {
143+
loadingAborted();
144+
return false;
145+
}
146+
117147
if (!ret)
118148
return false;
119149
}
@@ -125,9 +155,20 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
125155
engine->setExtensions(reader->extensions());
126156
engine->setUserAgent(reader->userAgent());
127157
engine->compile();
158+
159+
if (stopLoading) {
160+
loadingAborted();
161+
return false;
162+
}
163+
128164
return true;
129165
}
130166

167+
void ProjectPrivate::loadingAborted()
168+
{
169+
std::cout << "Loading aborted." << std::endl;
170+
}
171+
131172
void ProjectPrivate::start()
132173
{
133174
engine->start();

src/project_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct ProjectPrivate
2121

2222
bool load();
2323
bool tryLoad(IProjectReader *reader);
24+
void loadingAborted();
2425

2526
void start();
2627
void run();
@@ -29,6 +30,7 @@ struct ProjectPrivate
2930
sigslot::signal<unsigned int, unsigned int> &downloadProgressChanged();
3031

3132
std::string fileName;
33+
std::atomic<bool> stopLoading = false;
3234
std::shared_ptr<IEngine> engine = nullptr;
3335

3436
static IProjectDownloaderFactory *downloaderFactory;

test/project/project_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,17 @@ TEST(LoadProjectTest, DownloadProgressChanged)
115115
EXPECT_CALL(*downloader, downloadProgressChanged).WillOnce(ReturnRef(signal));
116116
ASSERT_EQ(&p.downloadProgressChanged(), &signal);
117117
}
118+
119+
TEST(LoadProjectTest, AbortDownload)
120+
{
121+
ProjectDownloaderFactoryMock factory;
122+
auto downloader = std::make_shared<ProjectDownloaderMock>();
123+
ProjectPrivate::downloaderFactory = &factory;
124+
125+
EXPECT_CALL(factory, create()).WillOnce(Return(downloader));
126+
Project p;
127+
ProjectPrivate::downloaderFactory = nullptr;
128+
129+
EXPECT_CALL(*downloader, cancel());
130+
p.stopLoading();
131+
}

0 commit comments

Comments
 (0)