use of elemental2.core.Global in project gwtproject by treblereel.
the class JsonpRequest method registerCallbacks.
/**
* Registers the callback methods that will be called when the JSONP response comes back. 2
* callbacks are created, one to return the value, and one to notify a failure.
*
* @param callbacks the global JS object which stores callbacks
*/
@SuppressWarnings("unchecked")
private void registerCallbacks(JsonpCallbacks callbacks, boolean canHaveMultipleRequestsForId) {
JsonpCallback jsonpCallback = new JsonpCallback();
jsonpCallback.onSuccess = data -> {
if (data instanceof JsBoolean) {
JsBoolean bool = (JsBoolean) data;
onSuccess((T) Boolean.valueOf(bool.valueOf()));
} else if (data instanceof JsNumber) {
JsNumber number = (JsNumber) data;
if (expectInteger) {
onSuccess((T) Integer.valueOf((int) number.valueOf()));
} else {
onSuccess((T) Double.valueOf(number.valueOf()));
}
} else {
onSuccess((T) data);
}
};
if (failureCallbackParam != null) {
jsonpCallback.onFailure = message -> onFailure((String) message);
}
if (canHaveMultipleRequestsForId) {
// In this case, we keep a wrapper, with a list of callbacks. Since the
// response for the request is the same each time, we call all of the
// callbacks as soon as any response comes back.
JsonpCallback maybeWrapper = CALLBACKS.get(callbackId);
if (maybeWrapper == null) {
maybeWrapper = new JsonpCallback();
maybeWrapper.callbackList = new JsArray<>();
// final for lambdas
final JsonpCallback wrapper = maybeWrapper;
wrapper.onSuccess = data -> {
while (wrapper.callbackList.length > 0) {
wrapper.callbackList.shift().onSuccess.accept(data);
}
};
wrapper.onFailure = message -> {
while (wrapper.callbackList.length > 0) {
wrapper.callbackList.shift().onFailure.accept(message);
}
};
CALLBACKS.set(callbackId, wrapper);
}
maybeWrapper.callbackList.push(jsonpCallback);
} else {
// In this simple case, just associate the callback directly with the
// particular id in the callbacks object
CALLBACKS.set(callbackId, jsonpCallback);
}
}