Bug 1392954 - Fix DebuggerClient.requester Promise response. r=ochameau

The promise returned by a function created with DebuggerClient.requester was
resolved with the raw response, i.e. without any modifications that could
happen in the after callback.

MozReview-Commit-ID: Bd81eTsZ9YB

--HG--
extra : rebase_source : 304a93aa90f5100b60cd27dcb88d4b101a307661
extra : source : 00159b917049461606286cd2fa13e5699b56fd37
This commit is contained in:
Nicolas Chevobbe 2017-08-23 11:42:44 +02:00
Родитель 6dd64b7cd3
Коммит 7d098a2512
1 изменённых файлов: 24 добавлений и 15 удалений

Просмотреть файл

@ -283,6 +283,7 @@ DebuggerClient.requester = function (packetSkeleton, config = {}) {
if (thisCallback) {
thisCallback(response);
}
return response;
}, "DebuggerClient.requester request callback"));
}, "DebuggerClient.requester");
};
@ -702,45 +703,53 @@ DebuggerClient.prototype = {
if (!packet.to) {
throw Error("'" + type + "' request packet has no destination.");
}
// The onResponse callback might modify the response, so we need to call
// it and resolve the promise with its result if it's truthy.
const safeOnResponse = response => {
if (!onResponse) {
return response;
}
return onResponse(response) || response;
};
if (this._closed) {
let msg = "'" + type + "' request packet to " +
"'" + packet.to + "' " +
"can't be sent as the connection is closed.";
let resp = { error: "connectionClosed", message: msg };
if (onResponse) {
onResponse(resp);
}
return promise.reject(resp);
return promise.reject(safeOnResponse(resp));
}
let request = new Request(packet);
request.format = "json";
request.stack = getStack();
if (onResponse) {
request.on("json-reply", onResponse);
}
this._sendOrQueueRequest(request);
// Implement a Promise like API on the returned object
// that resolves/rejects on request response
let deferred = promise.defer();
function listenerJson(resp) {
request.off("json-reply", listenerJson);
request.off("bulk-reply", listenerBulk);
removeRequestListeners();
if (resp.error) {
deferred.reject(resp);
deferred.reject(safeOnResponse(resp));
} else {
deferred.resolve(resp);
deferred.resolve(safeOnResponse(resp));
}
}
function listenerBulk(resp) {
removeRequestListeners();
deferred.resolve(safeOnResponse(resp));
}
const removeRequestListeners = () => {
request.off("json-reply", listenerJson);
request.off("bulk-reply", listenerBulk);
deferred.resolve(resp);
}
};
request.on("json-reply", listenerJson);
request.on("bulk-reply", listenerBulk);
this._sendOrQueueRequest(request);
request.then = deferred.promise.then.bind(deferred.promise);
return request;