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+
2136void 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
3451ReactSourceCode::ReactSourceCode (QObject* parent)
3552 : QObject(parent)
53+ , d_ptr(new ReactSourceCodePrivate)
3654{
3755}
3856
@@ -43,7 +61,8 @@ ReactSourceCode::~ReactSourceCode()
4361
4462void ReactSourceCode::setBridge (ReactBridge* bridge)
4563{
46- m_bridge = bridge;
64+ Q_D (ReactSourceCode);
65+ d->bridge = bridge;
4766}
4867
4968ReactViewManager* ReactSourceCode::viewManager ()
@@ -68,30 +87,58 @@ QVariantMap ReactSourceCode::constantsToExport()
6887
6988QUrl ReactSourceCode::scriptUrl () const
7089{
71- return m_scriptUrl ;
90+ return d_func ()-> scriptUrl ;
7291}
7392
7493void 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
79101QByteArray 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
84120void 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}
0 commit comments