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 <[email protected]>
  • Loading branch information
ItsChaceD and jcesarmobile authored Feb 1, 2024
1 parent b853d06 commit 5d294c9
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
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 5d294c9

Please sign in to comment.