History

more script caching cleanup

ZappoMan committed 11 years ago

Commit e2d8d821 parent 74d50a97 files · +2 −225Browse files

Showing the changes to libraries/script-engine/src/ScriptCache.cpp (1 of 7 files). Show all

Unified · Split

libraries/script-engine/src/ScriptCache.cpp+1−90
@@ -24,8 +24,6 @@ ScriptCache::ScriptCache(QObject* parent) { · 23 unmodified lines
24 // nothing to do here...24 // nothing to do here...
25}25}
26 26
27// QHash<QUrl, QString> _scriptCache;
28
29QString ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending) {27QString ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending) {
30 QString scriptContents;28 QString scriptContents;
31 if (_scriptCache.contains(url)) {29 if (_scriptCache.contains(url)) {
@@ -52,8 +50,7 @@ void ScriptCache::scriptDownloaded() { · 20 unmodified lines
52 50
53 if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) {51 if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) {
54 _scriptCache[url] = reply->readAll();52 _scriptCache[url] = reply->readAll();
55 qDebug() << "_scriptCache.size:" << _scriptCache.size();53
56
57 foreach(ScriptUser* user, scriptUsers) {54 foreach(ScriptUser* user, scriptUsers) {
58 user->scriptContentsAvailable(url, _scriptCache[url]);55 user->scriptContentsAvailable(url, _scriptCache[url]);
59 }56 }
@@ -66,90 +63,4 @@ void ScriptCache::scriptDownloaded() { · 6 unmodified lines
66 reply->deleteLater();63 reply->deleteLater();
67}64}
68 65
69
70#if 0
71QString ScriptCache::getScript(const QUrl& url) {
72 qDebug() << "Script requested: " << url.toString();
73 QString scriptContents;
74 if (_scriptCache.contains(url)) {
75 qDebug() << "Script found in cache: " << url.toString();
76 scriptContents = _scriptCache[url];
77 } else {
78 qDebug() << "Script not found in cache: " << url.toString();
79
80 if (_blockingScriptsPending.contains(url)) {
81 // if we're already downloading this script, wait for it to complete...
82 qDebug() << "Already downloading script: " << url.toString();
83 while (_blockingScriptsPending.contains(url)) {
84 QEventLoop loop;
85 qDebug() << "about to call loop.processEvents()....";
86 loop.processEvents(QEventLoop::AllEvents, 1);
87 qDebug() << "AFTER loop.processEvents()....";
88 }
89 qDebug() << "Done waiting on downloading script: " << url.toString();
90 scriptContents = _scriptCache[url];
91 } else {
92 _blockingScriptsPending.insert(url);
93 qDebug() << "_blockingScriptsPending: " << _blockingScriptsPending;
94
95 QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
96 QNetworkRequest networkRequest = QNetworkRequest(url);
97 networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
98 QNetworkReply* reply = networkAccessManager.get(networkRequest);
99
100
101 connect(reply, &QNetworkReply::finished, this, &ScriptCache::scriptDownloadedSyncronously);
102 qDebug() << "Downloading script at" << url.toString();
103 //QEventLoop loop;
104 //QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
105 //qDebug() << "starting loop....";
106 //loop.exec();
107 //qDebug() << "after loop....";
108
109 /*
110 if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) {
111 scriptContents = reply->readAll();
112 _scriptCache[url] = scriptContents;
113 qDebug() << "_scriptCache.size:" << _scriptCache.size();
114 } else {
115 qDebug() << "ERROR Loading file:" << url.toString();
116 }
117 delete reply;
118 _blockingScriptsPending.remove(url);
119 qDebug() << "_blockingScriptsPending: " << _blockingScriptsPending;
120 */
121 while (_blockingScriptsPending.contains(url)) {
122 QEventLoop loop;
123 qDebug() << "about to call loop.processEvents()....";
124 loop.processEvents(QEventLoop::AllEvents, 1);
125 qDebug() << "AFTER loop.processEvents()....";
126 }
127 qDebug() << "Done waiting on downloading script: " << url.toString();
128 scriptContents = _scriptCache[url];
129 }
130 }
131 return scriptContents;
132}
133
134void ScriptCache::scriptDownloadedSyncronously() {
135
136 qDebug() << "ScriptCache::scriptDownloadedSyncronously()....";
137
138 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
139 QUrl url = reply->url();
140
141 if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) {
142 _scriptCache[url] = reply->readAll();
143 qDebug() << "_scriptCache.size:" << _scriptCache.size();
144 } else {
145 qDebug() << "ERROR Loading file:" << reply->url().toString();
146 }
147
148 reply->deleteLater();
149
150 _blockingScriptsPending.remove(url);
151 qDebug() << "_blockingScriptsPending: " << _blockingScriptsPending;
152}
153
154#endif
155 66