Bug 979134: share wrapDomRequestAsPromise. r=hsinyi

This commit is contained in:
Vicamo Yang 2014-04-09 13:16:03 +08:00
Родитель 4deaf08058
Коммит 52aa848043
1 изменённых файлов: 40 добавлений и 24 удалений

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

@ -46,6 +46,33 @@ function runEmulatorCmdSafe(aCommand) {
return deferred.promise;
}
/**
* Wrap DOMRequest onsuccess/onerror events to Promise resolve/reject.
*
* Fulfill params: A DOMEvent.
* Reject params: A DOMEvent.
*
* @param aRequest
* A DOMRequest instance.
*
* @return A deferred promise.
*/
function wrapDomRequestAsPromise(aRequest) {
let deferred = Promise.defer();
ok(aRequest instanceof DOMRequest,
"aRequest is instanceof " + aRequest.constructor);
aRequest.addEventListener("success", function(aEvent) {
deferred.resolve(aEvent);
});
aRequest.addEventListener("error", function(aEvent) {
deferred.reject(aEvent);
});
return deferred.promise;
}
/**
* Get mozSettings value specified by @aKey.
*
@ -65,19 +92,14 @@ function runEmulatorCmdSafe(aCommand) {
* @return A deferred promise.
*/
function getSettings(aKey, aAllowError) {
let deferred = Promise.defer();
let request = navigator.mozSettings.createLock().get(aKey);
request.addEventListener("success", function(aEvent) {
ok(true, "getSettings(" + aKey + ") - success");
deferred.resolve(aEvent.target.result[aKey]);
});
request.addEventListener("error", function() {
ok(aAllowError, "getSettings(" + aKey + ") - error");
deferred.reject();
});
return deferred.promise;
return wrapDomRequestAsPromise(request)
.then(function resolve(aEvent) {
ok(true, "getSettings(" + aKey + ") - success");
return aEvent.target.result[aKey];
}, function reject(aEvent) {
ok(aAllowError, "getSettings(" + aKey + ") - error");
});
}
/**
@ -97,19 +119,13 @@ function getSettings(aKey, aAllowError) {
* @return A deferred promise.
*/
function setSettings(aSettings, aAllowError) {
let deferred = Promise.defer();
let request = navigator.mozSettings.createLock().set(aSettings);
request.addEventListener("success", function() {
ok(true, "setSettings(" + JSON.stringify(aSettings) + ")");
deferred.resolve();
});
request.addEventListener("error", function() {
ok(aAllowError, "setSettings(" + JSON.stringify(aSettings) + ")");
deferred.reject();
});
return deferred.promise;
return wrapDomRequestAsPromise(request)
.then(function resolve() {
ok(true, "setSettings(" + JSON.stringify(aSettings) + ")");
}, function reject() {
ok(aAllowError, "setSettings(" + JSON.stringify(aSettings) + ")");
});
}
/**