From f4c704cffac06df78fcab2a79acee98df7ef0d0f Mon Sep 17 00:00:00 2001 From: Ender <33337309+e3ndr@users.noreply.github.com> Date: Wed, 30 Mar 2022 14:19:42 -0500 Subject: [PATCH] Allow applications to dispatch from the main thread. --- src/main/java/dev/webview/Webview.java | 47 +++++++++++++------- src/main/java/dev/webview/WebviewNative.java | 25 ++++++++++- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/main/java/dev/webview/Webview.java b/src/main/java/dev/webview/Webview.java index b380585..9dc1395 100644 --- a/src/main/java/dev/webview/Webview.java +++ b/src/main/java/dev/webview/Webview.java @@ -155,31 +155,46 @@ public void eval(@NonNull String script) { } public void bind(@NonNull String name, @NonNull ConsumingProducer handler) { - N.webview_bind($pointer, name, new BindCallback() { - @Override - public void callback(long seq, String req, long arg) { - try { - JsonArray arguments = Rson.DEFAULT.fromJson(req, JsonArray.class); - + N.webview_bind( + $pointer, + name, + new BindCallback() { + @Override + public void callback(long seq, String req, long arg) { try { - @Nullable - JsonElement result = handler.produce(arguments); - - N.webview_return($pointer, seq, false, Rson.DEFAULT.toJsonString(result)); - } catch (Exception e) { - N.webview_return($pointer, seq, true, e.getMessage()); + JsonArray arguments = Rson.DEFAULT.fromJson(req, JsonArray.class); + + try { + @Nullable + JsonElement result = handler.produce(arguments); + + N.webview_return($pointer, seq, false, Rson.DEFAULT.toJsonString(result)); + } catch (Exception e) { + N.webview_return($pointer, seq, true, e.getMessage()); + } + } catch (JsonParseException e) { + e.printStackTrace(); } - } catch (JsonParseException e) { - e.printStackTrace(); } - } - }, 0); + }, + 0 + ); } public void unbind(@NonNull String name) { N.webview_unbind($pointer, name); } + public void dispatch(@NonNull Runnable handler) { + N.webview_dispatch( + $pointer, + ($pointer, arg) -> { + handler.run(); + }, + 0 + ); + } + @Override public void run() { N.webview_run($pointer); diff --git a/src/main/java/dev/webview/WebviewNative.java b/src/main/java/dev/webview/WebviewNative.java index 45d35f3..3d1a4d5 100644 --- a/src/main/java/dev/webview/WebviewNative.java +++ b/src/main/java/dev/webview/WebviewNative.java @@ -74,6 +74,19 @@ static interface BindCallback extends Callback { } + /** + * Used in {@link webview_dispatch} + */ + static interface DispatchCallback extends Callback { + + /** + * @param $pointer The pointer of the webview + * @param arg Unused + */ + void callback(long $pointer, long arg); + + } + /** * Creates a new webview instance. If debug is true - developer tools will be * enabled (if the platform supports them). Window parameter can be a pointer to @@ -173,7 +186,7 @@ static interface BindCallback extends Callback { * @param callback The callback to be called * @param arg Unused */ - void webview_bind(long $pointer, @NonNull String name, BindCallback callback, long arg); + void webview_bind(long $pointer, @NonNull String name, @NonNull BindCallback callback, long arg); /** * Remove the native callback specified. @@ -194,4 +207,14 @@ static interface BindCallback extends Callback { */ void webview_return(long $pointer, long seq, boolean isError, String result); + /** + * Dispatches the callback on the UI thread, only effective while + * {@link #webview_run(long)} is blocking. + * + * @param $pointer The instance pointer of the webview + * @param callback The callback to be called + * @param arg Unused + */ + void webview_dispatch(long $pointer, @NonNull DispatchCallback callback, long arg); + }