Skip to content

Commit 99cac36

Browse files
author
Justin McPherson
committed
Retry fetch from source url on failure.
- Adds retryCount property to ReactSourceCode.
1 parent 99ec162 commit 99cac36

2 files changed

Lines changed: 67 additions & 13 deletions

File tree

ReactUbuntu/runtime/src/reactsourcecode.cpp

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,46 @@
1111
*
1212
*/
1313

14+
#include <memory>
15+
1416
#include <QNetworkAccessManager>
1517
#include <QNetworkRequest>
1618
#include <QNetworkReply>
19+
#include <QTimer>
1720

1821
#include "reactsourcecode.h"
1922

2023

24+
class ReactSourceCodePrivate
25+
{
26+
public:
27+
ReactBridge* bridge = nullptr;
28+
QUrl scriptUrl;
29+
QByteArray sourceCode;
30+
int retryCount = 4;
31+
int retryAttempts = 0;
32+
int retryTimeout = 250;
33+
};
34+
35+
2136
void ReactSourceCode::getScriptText
2237
(
2338
const ReactModuleInterface::ResponseBlock& success,
2439
const ReactModuleInterface::ErrorBlock& error
2540
)
2641
{
27-
if (!m_sourceCode.isNull())
28-
success(m_bridge, QVariantList{ QVariantMap{ {"text", m_sourceCode}, {"url", m_scriptUrl.toString()} } });
42+
Q_D(ReactSourceCode);
43+
if (!d->sourceCode.isNull())
44+
success(d->bridge, QVariantList{QVariantMap{{"text", d->sourceCode},
45+
{"url", d->scriptUrl.toString()}}});
2946
else
30-
error(m_bridge, QVariantMap{ {"text", "Source code is not available"} });
47+
error(d->bridge, QVariantMap{ {"text", "Source code is not available"} });
3148
}
3249

3350

3451
ReactSourceCode::ReactSourceCode(QObject* parent)
3552
: QObject(parent)
53+
, d_ptr(new ReactSourceCodePrivate)
3654
{
3755
}
3856

@@ -43,7 +61,8 @@ ReactSourceCode::~ReactSourceCode()
4361

4462
void ReactSourceCode::setBridge(ReactBridge* bridge)
4563
{
46-
m_bridge = bridge;
64+
Q_D(ReactSourceCode);
65+
d->bridge = bridge;
4766
}
4867

4968
ReactViewManager* ReactSourceCode::viewManager()
@@ -68,30 +87,58 @@ QVariantMap ReactSourceCode::constantsToExport()
6887

6988
QUrl ReactSourceCode::scriptUrl() const
7089
{
71-
return m_scriptUrl;
90+
return d_func()->scriptUrl;
7291
}
7392

7493
void ReactSourceCode::setScriptUrl(const QUrl& scriptUrl)
7594
{
76-
m_scriptUrl = scriptUrl;
95+
Q_D(ReactSourceCode);
96+
if (d->scriptUrl == scriptUrl)
97+
return;
98+
d->scriptUrl = scriptUrl;
7799
}
78100

79101
QByteArray ReactSourceCode::sourceCode() const
80102
{
81-
return m_sourceCode;
103+
return d_func()->sourceCode;
104+
}
105+
106+
int ReactSourceCode::retryCount() const
107+
{
108+
return d_func()->retryCount;
109+
}
110+
111+
void ReactSourceCode::setRetryCount(int retryCount)
112+
{
113+
Q_D(ReactSourceCode);
114+
if (d->retryCount == retryCount)
115+
return;
116+
d->retryCount = retryCount;
117+
Q_EMIT retryCountChanged();
82118
}
83119

84120
void ReactSourceCode::loadSource(QNetworkAccessManager* nam)
85121
{
86-
QNetworkRequest request(m_scriptUrl);
122+
Q_D(ReactSourceCode);
123+
124+
QNetworkRequest request(d->scriptUrl);
87125
QNetworkReply* reply = nam->get(request);
88126
QObject::connect(reply, &QNetworkReply::finished, [=]() {
89127
reply->deleteLater();
90128
if (reply->error() != QNetworkReply::NoError) {
91-
qCritical() << __PRETTY_FUNCTION__ << ": Error while loading source" << reply->errorString();
129+
if (d->retryAttempts < d->retryCount) {
130+
d->retryAttempts++;
131+
d->retryTimeout *= 2;
132+
QTimer::singleShot(d->retryTimeout, [=] { loadSource(nam); });
133+
} else {
134+
qCritical() << __PRETTY_FUNCTION__ << ": Error while loading source" << reply->errorString();
135+
Q_EMIT loadFailed();
136+
}
92137
return;
93138
}
94-
m_sourceCode = reply->readAll();
139+
d->sourceCode = reply->readAll();
140+
d->retryAttempts = 0;
141+
d->retryTimeout = 250;
95142
Q_EMIT sourceCodeChanged();
96143
});
97144
}

ReactUbuntu/runtime/src/reactsourcecode.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class QNetworkAccessManager;
2424

2525

26+
class ReactSourceCodePrivate;
2627
class ReactSourceCode
2728
: public QObject
2829
, public ReactModuleInterface
@@ -33,11 +34,14 @@ class ReactSourceCode
3334

3435
Q_PROPERTY(QUrl scriptURL READ scriptUrl CONSTANT)
3536
Q_PROPERTY(QByteArray sourceCode READ sourceCode NOTIFY sourceCodeChanged SCRIPTABLE false)
37+
Q_PROPERTY(int retryCount READ retryCount WRITE setRetryCount NOTIFY retryCountChanged SCRIPTABLE false)
3638

3739
Q_INVOKABLE void getScriptText(
3840
const ReactModuleInterface::ResponseBlock& success,
3941
const ReactModuleInterface::ErrorBlock& error);
4042

43+
Q_DECLARE_PRIVATE(ReactSourceCode);
44+
4145
public:
4246
ReactSourceCode(QObject* parent = 0);
4347
~ReactSourceCode();
@@ -56,16 +60,19 @@ class ReactSourceCode
5660

5761
QByteArray sourceCode() const;
5862

63+
int retryCount() const;
64+
void setRetryCount(int retryCount);
65+
5966
void loadSource(QNetworkAccessManager* nam);
6067

6168
Q_SIGNALS:
6269
void scriptUrlChanged();
6370
void sourceCodeChanged();
71+
void retryCountChanged();
72+
void loadFailed();
6473

6574
private:
66-
ReactBridge* m_bridge;
67-
QUrl m_scriptUrl;
68-
QByteArray m_sourceCode;
75+
QScopedPointer<ReactSourceCodePrivate> d_ptr;
6976
};
7077

7178
#endif // REACTSOURCECODE_H

0 commit comments

Comments
 (0)