Skip to content

Commit

Permalink
fix(http): better handling of active requests and shutting down grace…
Browse files Browse the repository at this point in the history
…fully

Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
  • Loading branch information
ItsChaceD and jcesarmobile committed Feb 1, 2024
1 parent af97904 commit a56e845
Showing 1 changed file with 18 additions and 9 deletions.
Expand Up @@ -24,7 +24,7 @@
)
public class CapacitorHttp extends Plugin {

private Map<Runnable, PluginCall> activeRequests = new HashMap<>();
private final Map<Runnable, PluginCall> activeRequests = new HashMap<>();
private final ExecutorService executor = Executors.newCachedThreadPool();

@Override
Expand Down Expand Up @@ -59,17 +59,26 @@ protected void handleOnDestroy() {
}

private void http(final PluginCall call, final String httpMethod) {
Runnable asyncHttpCall = () -> {
try {
JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge());
call.resolve(response);
} catch (Exception e) {
call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e);
Runnable asyncHttpCall = new Runnable() {
@Override
public void run() {
try {
JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge());
call.resolve(response);
} catch (Exception e) {
call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e);
} finally {
activeRequests.remove(this);
}
}
};

activeRequests.put(asyncHttpCall, call);
executor.submit(asyncHttpCall);
if (!executor.isShutdown()) {
activeRequests.put(asyncHttpCall, call);
executor.submit(asyncHttpCall);
} else {
call.reject("Failed to execute request - Http Plugin was shutdown");
}
}

@JavascriptInterface
Expand Down

0 comments on commit a56e845

Please sign in to comment.