History

update ScriptCache to detect local script modifications

humbletim committed 6 years ago

Commit ae7aa3e1 parent f0127723 files · +27 −8Browse files

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

Unified · Split

libraries/script-engine/src/ScriptCache.cpp+24−7
@@ -85,11 +85,25 @@ void ScriptCache::getScriptContents(const QString& scriptOrURL, contentAvailable · 84 unmodified lines
8585
8686 Lock lock(_containerLock);
8787 if (_scriptCache.contains(url) && !forceDownload) {
88− auto scriptContent = _scriptCache[url];
89− lock.unlock();
90− qCDebug(scriptengine) << "Found script in cache:" << url.fileName();
91− contentAvailable(url.toString(), scriptContent, true, true, STATUS_CACHED);
92− } else {
88+ auto entry = _scriptCache[url];
89+ if (url.isLocalFile() || url.scheme().isEmpty()) {
90+ auto mtime = QFileInfo(url.toLocalFile()).lastModified();
91+ QString localTime = ResourceRequest::toHttpDateString(mtime.toMSecsSinceEpoch());
92+ QString cachedTime = entry["last-modified"].toString();
93+ if (cachedTime != localTime) {
94+ forceDownload = true;
95+ qCDebug(scriptengine) << "Found script in cache, but local file modified; reloading:" << url.fileName()
96+ << "(memory:" << cachedTime << "disk:" << localTime << ")";
97+ }
98+ }
99+ if (!forceDownload) {
100+ lock.unlock();
101+ qCDebug(scriptengine) << "Found script in cache:" << url.fileName();
102+ contentAvailable(url.toString(), entry["data"].toString(), true, true, STATUS_CACHED);
103+ return;
104+ }
105+ }
106+ {
93107 auto& scriptRequest = _activeScriptRequests[url];
94108 bool alreadyWaiting = scriptRequest.scriptUsers.size() > 0;
95109 scriptRequest.scriptUsers.push_back(contentAvailable);
@@ -140,7 +154,10 @@ void ScriptCache::scriptContentAvailable(int maxRetries) { · 44 unmodified lines
140154
141155 _activeScriptRequests.remove(url);
142156
143− _scriptCache[url] = scriptContent = req->getData();
157+ _scriptCache[url] = {
158+ { "data", scriptContent = req->getData() },
159+ { "last-modified", req->property("last-modified") },
160+ };
144161 } else {
145162 auto result = req->getResult();
146163 bool irrecoverable =
@@ -177,7 +194,7 @@ void ScriptCache::scriptContentAvailable(int maxRetries) { · 30 unmodified lines
177194 allCallbacks = scriptRequest.scriptUsers;
178195
179196 if (_scriptCache.contains(url)) {
180− scriptContent = _scriptCache[url];
197+ scriptContent = _scriptCache[url]["data"].toString();
181198 }
182199 _activeScriptRequests.remove(url);
183200 qCWarning(scriptengine) << "Error loading script from URL (" << status <<")";