KEYCLOAK-15721 KeycloakPromise sometimes doesn't work

This commit is contained in:
vmuzikar 2020-09-25 19:32:45 +02:00 committed by Pedro Igor
parent 04415d34ea
commit fbe18e67c3
2 changed files with 24 additions and 35 deletions

View file

@ -74,38 +74,6 @@
} }
} }
function toKeycloakPromise(promise) {
promise.__proto__ = KeycloakPromise.prototype;
return promise;
}
function KeycloakPromise(executor) {
return toKeycloakPromise(new Promise(executor));
}
KeycloakPromise.prototype = Object.create(Promise.prototype);
KeycloakPromise.prototype.constructor = KeycloakPromise;
KeycloakPromise.prototype.success = function(callback) {
logPromiseDeprecation();
var promise = this.then(function handleSuccess(value) {
callback(value);
});
return toKeycloakPromise(promise);
};
KeycloakPromise.prototype.error = function(callback) {
logPromiseDeprecation();
var promise = this.catch(function handleError(error) {
callback(error);
});
return toKeycloakPromise(promise);
};
function Keycloak (config) { function Keycloak (config) {
if (!(this instanceof Keycloak)) { if (!(this instanceof Keycloak)) {
return new Keycloak(config); return new Keycloak(config);
@ -1208,10 +1176,31 @@
p.reject(result); p.reject(result);
} }
}; };
p.promise = new KeycloakPromise(function(resolve, reject) { p.promise = new Promise(function(resolve, reject) {
p.resolve = resolve; p.resolve = resolve;
p.reject = reject; p.reject = reject;
}); });
p.promise.success = function(callback) {
logPromiseDeprecation();
this.then(function handleSuccess(value) {
callback(value);
});
return this;
}
p.promise.error = function(callback) {
logPromiseDeprecation();
this.catch(function handleError(error) {
callback(error);
});
return this;
}
return p; return p;
} }

View file

@ -708,9 +708,9 @@ public class JavascriptAdapterTest extends AbstractJavascriptTest {
@Test @Test
public void testRefreshTokenWithDeprecatedPromiseHandles() { public void testRefreshTokenWithDeprecatedPromiseHandles() {
String refreshWithDeprecatedHandles = "var callback = arguments[arguments.length - 1];" + String refreshWithDeprecatedHandles = "var callback = arguments[arguments.length - 1];" +
" window.keycloak.updateToken(9999).then(function (refreshed) {" + " window.keycloak.updateToken(9999).success(function (refreshed) {" +
" callback('Success handle');" + " callback('Success handle');" +
" }).catch(function () {" + " }).error(function () {" +
" callback('Error handle');" + " callback('Error handle');" +
" });"; " });";