History

remove debug prints; CR feedback

humbletim committed 8 years ago

Commit 8d2e0411 parent 8c7a8f02 files · +70 −85Browse files

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

Unified · Split

libraries/script-engine/src/AssetScriptingInterface.cpp+69−70
@@ -42,12 +42,12 @@ void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { · 41 unmodified lines
4242 QByteArray dataByteArray = data.toUtf8();
4343 auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray);
4444
45− Promise deferred = makePromise(__FUNCTION__)
46− ->ready([this, handler](QString error, QVariantMap result) {
47− auto url = result.value("url").toString();
48− auto hash = result.value("hash").toString();
49− jsCallback(handler, url, hash);
50− });
45+ Promise deferred = makePromise(__FUNCTION__);
46+ deferred->ready([this, handler](QString error, QVariantMap result) {
47+ auto url = result.value("url").toString();
48+ auto hash = result.value("hash").toString();
49+ jsCallback(handler, url, hash);
50+ });
5151
5252 connect(upload, &AssetUpload::finished, upload, [this, deferred](AssetUpload* upload, const QString& hash) {
5353 // we are now on the "Resource Manager" thread (and "hash" being a *reference* makes it unsafe to use directly)
@@ -64,10 +64,10 @@ void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { · 10 unmodified lines
6464 void AssetScriptingInterface::setMapping(QString path, QString hash, QScriptValue callback) {
6565 auto handler = makeScopedHandlerObject(thisObject(), callback);
6666 auto setMappingRequest = assetClient()->createSetMappingRequest(path, hash);
67− Promise deferred = makePromise(__FUNCTION__)
68− ->ready([=](QString error, QVariantMap result) {
69− jsCallback(handler, error, result);
70− });
67+ Promise deferred = makePromise(__FUNCTION__);
68+ deferred->ready([=](QString error, QVariantMap result) {
69+ jsCallback(handler, error, result);
70+ });
7171
7272 connect(setMappingRequest, &SetMappingRequest::finished, setMappingRequest, [this, deferred](SetMappingRequest* request) {
7373 Q_ASSERT(QThread::currentThread() == request->thread());
@@ -96,11 +96,11 @@ void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callb · 22 unmodified lines
9696 auto assetClient = DependencyManager::get<AssetClient>();
9797 auto assetRequest = assetClient->createRequest(hash);
9898
99− Promise deferred = makePromise(__FUNCTION__)
100− ->ready([=](QString error, QVariantMap result) {
101− // FIXME: to remain backwards-compatible the signature here is "callback(data, n/a)"
102− jsCallback(handler, result.value("data").toString(), { { "errorMessage", error } });
103− });
99+ Promise deferred = makePromise(__FUNCTION__);
100+ deferred->ready([=](QString error, QVariantMap result) {
101+ // FIXME: to remain backwards-compatible the signature here is "callback(data, n/a)"
102+ jsCallback(handler, result.value("data").toString(), { { "errorMessage", error } });
103+ });
104104
105105 connect(assetRequest, &AssetRequest::finished, assetRequest, [this, deferred](AssetRequest* request) {
106106 Q_ASSERT(QThread::currentThread() == request->thread());
@@ -127,10 +127,10 @@ void AssetScriptingInterface::setBakingEnabled(QString path, bool enabled, QScri · 20 unmodified lines
127127 auto handler = makeScopedHandlerObject(thisObject(), callback);
128128 auto setBakingEnabledRequest = DependencyManager::get<AssetClient>()->createSetBakingEnabledRequest({ path }, enabled);
129129
130− Promise deferred = makePromise(__FUNCTION__)
131− ->ready([=](QString error, QVariantMap result) {
132− jsCallback(handler, error, result);
133− });
130+ Promise deferred = makePromise(__FUNCTION__);
131+ deferred->ready([=](QString error, QVariantMap result) {
132+ jsCallback(handler, error, result);
133+ });
134134
135135 connect(setBakingEnabledRequest, &SetBakingEnabledRequest::finished, setBakingEnabledRequest, [this, deferred](SetBakingEnabledRequest* request) {
136136 Q_ASSERT(QThread::currentThread() == request->thread());
@@ -160,14 +160,13 @@ void AssetScriptingInterface::getMapping(QString asset, QScriptValue callback) { · 23 unmodified lines
160160 JS_ASSERT(AssetUtils::isValidFilePath(path), "invalid ATP file path: " + asset + "(path:"+path+")");
161161 JS_ASSERT(callback.isFunction(), "expected second parameter to be a callback function");
162162 qDebug() << ">>getMapping//getAssetInfo" << path;
163− getAssetInfo(path)->ready([this, handler](QString error, QVariantMap result) {
163+ Promise promise = getAssetInfo(path);
164+ promise->ready([this, handler](QString error, QVariantMap result) {
164165 qDebug() << "//getMapping//getAssetInfo" << error << result.keys();
165166 jsCallback(handler, error, result.value("hash").toString());
166167 });
167168 }
168169
169−/////////////////////////// new APIS ////////////////////////////////////
170−
171170 bool AssetScriptingInterface::jsAssert(bool condition, const QString& error) {
172171 if (condition) {
173172 return true;
@@ -187,10 +186,6 @@ void AssetScriptingInterface::jsCallback(const QScriptValue& handler, · 13 unmodified lines
187186 JS_ASSERT(handler.isObject() && handler.property("callback").isFunction(),
188187 QString("jsCallback -- .callback is not a function (%1)")
189188 .arg(handler.property("callback").toVariant().typeName()));
190−#if 1 || DEGUG_JSCALLBACK
191− QScriptValue debug = result;
192− debug.setProperty("toString", handler.engine()->evaluate("1,function() { return JSON.stringify(this, 0, 2); }"));
193−#endif
194189 ::callScopedHandlerObject(handler, errorValue, result);
195190 }
196191
@@ -228,8 +223,8 @@ void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, · 31 unmodified lines
228223 JS_ASSERT(RESPONSE_TYPES.contains(responseType),
229224 QString("Invalid responseType: '%1' (expected: %2)").arg(responseType).arg(RESPONSE_TYPES.join(" | ")));
230225
231− Promise resolved = makePromise("resolved"),
232− loaded = makePromise("loaded");
226+ Promise resolved = makePromise("resolved");
227+ Promise loaded = makePromise("loaded");
233228
234229 loaded->ready([=](QString error, QVariantMap result) {
235230 qDebug() << "//loaded" << error;
@@ -242,9 +237,9 @@ void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, · 6 unmodified lines
242237 if (!error.isEmpty() || !AssetUtils::isValidHash(hash)) {
243238 loaded->reject(error.isEmpty() ? "internal hash error: " + hash : error, result);
244239 } else {
245− loadAsset(hash, decompress, responseType)
246− ->mixin(result)
247− ->ready([this, loaded, hash](QString error, QVariantMap result) {
240+ Promise promise = loadAsset(hash, decompress, responseType);
241+ promise->mixin(result);
242+ promise->ready([this, loaded, hash](QString error, QVariantMap result) {
248243 qDebug() << "//getAssetInfo/loadAsset" << error << hash;
249244 loaded->resolve(NoError, result);
250245 });
@@ -254,11 +249,11 @@ void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, · 3 unmodified lines
254249 if (AssetUtils::isValidHash(asset)) {
255250 resolved->resolve(NoError, { { "hash", asset } });
256251 } else {
257− getAssetInfo(asset)
258− ->ready([this, resolved](QString error, QVariantMap result) {
259− qDebug() << "//getAssetInfo" << error << result.value("hash") << result.value("path");
252+ Promise promise = getAssetInfo(asset);
253+ promise->ready([this, resolved](QString error, QVariantMap result) {
254+ qDebug() << "//getAssetInfo" << error << result.value("hash") << result.value("path");
260255 resolved->resolve(error, result);
261− });
256+ });
262257 }
263258 }
264259
@@ -286,34 +281,40 @@ void AssetScriptingInterface::decompressData(QScriptValue options, QScriptValue · 21 unmodified lines
286281 if (responseType.isEmpty()) {
287282 responseType = "text";
288283 }
289− decompressBytes(dataByteArray)
290− ->ready([=](QString error, QVariantMap result) {
291− if (responseType == "arraybuffer") {
284+ Promise promise = decompressBytes(dataByteArray);
285+ promise->ready([=](QString error, QVariantMap result) {
286+ if (responseType == "arraybuffer") {
287+ jsCallback(handler, error, result);
288+ } else {
289+ Promise promise = convertBytes(result.value("data").toByteArray(), responseType);
290+ promise->mixin(result);
291+ promise->ready([=](QString error, QVariantMap result) {
292292 jsCallback(handler, error, result);
293− } else {
294− convertBytes(result.value("data").toByteArray(), responseType)
295− ->mixin(result)
296− ->ready([=](QString error, QVariantMap result) {
297− jsCallback(handler, error, result);
298− });
299− }
293+ });
294+ }
300295 });
301296 }
302297
298+namespace {
299+ const int32_t DEFAULT_GZIP_COMPRESSION_LEVEL = -1;
300+ const int32_t MAX_GZIP_COMPRESSION_LEVEL = 9;
301+}
302+
303303 void AssetScriptingInterface::compressData(QScriptValue options, QScriptValue scope, QScriptValue callback) {
304+
304305 auto data = options.property("data");
305306 QByteArray dataByteArray = data.isString() ?
306307 data.toString().toUtf8() :
307308 qscriptvalue_cast<QByteArray>(data);
308309 auto handler = makeScopedHandlerObject(scope, callback);
309310 auto level = options.property("level").toInt32();
310− if (level < -1 || level > 9) {
311− level = -1;
311+ if (level < DEFAULT_GZIP_COMPRESSION_LEVEL || level > MAX_GZIP_COMPRESSION_LEVEL) {
312+ level = DEFAULT_GZIP_COMPRESSION_LEVEL;
312313 }
313− compressBytes(dataByteArray, level)
314− ->ready([=](QString error, QVariantMap result) {
315− jsCallback(handler, error, result);
316− });
314+ Promise promise = compressBytes(dataByteArray, level);
315+ promise->ready([=](QString error, QVariantMap result) {
316+ jsCallback(handler, error, result);
317+ });
317318 }
318319
319320 void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) {
@@ -327,7 +328,6 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 7 unmodified lines
327328 QByteArray dataByteArray = data.isString() ?
328329 data.toString().toUtf8() :
329330 qscriptvalue_cast<QByteArray>(data);
330− //auto rawByteLength = dataByteArray.size();
331331
332332 JS_ASSERT(path.isEmpty() || AssetUtils::isValidFilePath(path),
333333 QString("expected valid ATP file path '%1' ('%2')").arg(rawPath).arg(path));
@@ -338,18 +338,17 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 4 unmodified lines
338338 .arg(data.toVariant().typeName())
339339 .arg(dataByteArray.size()));
340340
341−
342341 // [compressed] => uploaded to server => [mapped to path]
343− Promise prepared = makePromise("putAsset::prepared"),
344− uploaded = makePromise("putAsset::uploaded"),
345− finished = makePromise("putAsset::finished");
342+ Promise prepared = makePromise("putAsset::prepared");
343+ Promise uploaded = makePromise("putAsset::uploaded");
344+ Promise finished = makePromise("putAsset::finished");
346345
347346 if (compress) {
348347 qDebug() << "putAsset::compressBytes...";
349− compressBytes(dataByteArray, -1)
350− ->finally([=](QString error, QVariantMap result) {
351− qDebug() << "//putAsset::compressedBytes" << error << result.keys();
352− prepared->handle(error, result);
348+ Promise promise = compressBytes(dataByteArray, DEFAULT_GZIP_COMPRESSION_LEVEL);
349+ promise->finally([=](QString error, QVariantMap result) {
350+ qDebug() << "//putAsset::compressedBytes" << error << result.keys();
351+ prepared->handle(error, result);
353352 });
354353 } else {
355354 prepared->resolve(NoError, {{ "data", dataByteArray }});
@@ -357,12 +356,12 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 1 unmodified line
357356
358357 prepared->ready([=](QString error, QVariantMap result) {
359358 qDebug() << "//putAsset::prepared" << error << result.value("data").toByteArray().size() << result.keys();
360− uploadBytes(result.value("data").toByteArray())
361− ->mixin(result)
362− ->ready([=](QString error, QVariantMap result) {
363− qDebug() << "===========//putAsset::prepared/uploadBytes" << error << result.keys();
364− uploaded->handle(error, result);
365− });
359+ Promise promise = uploadBytes(result.value("data").toByteArray());
360+ promise->mixin(result);
361+ promise->ready([=](QString error, QVariantMap result) {
362+ qDebug() << "===========//putAsset::prepared/uploadBytes" << error << result.keys();
363+ uploaded->handle(error, result);
364+ });
366365 });
367366
368367 uploaded->ready([=](QString error, QVariantMap result) {
@@ -374,10 +373,10 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 5 unmodified lines
374373 finished->reject("path mapping requested, but did not receive valid hash", result);
375374 } else {
376375 qDebug() << "symlinkAsset" << hash << path << QThread::currentThread();
377− symlinkAsset(hash, path)
378− ->mixin(result)
379− ->ready([=](QString error, QVariantMap result) {
380− finished->handle(error, result);
376+ Promise promise = symlinkAsset(hash, path);
377+ promise->mixin(result);
378+ promise->ready([=](QString error, QVariantMap result) {
379+ finished->handle(error, result);
381380 qDebug() << "//symlinkAsset" << hash << path << result.keys();
382381 });
383382 }