Skip to content

Commit

Permalink
Allow applications to dispatch from the main thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Mar 30, 2022
1 parent ecdeacb commit f4c704c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
47 changes: 31 additions & 16 deletions src/main/java/dev/webview/Webview.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,46 @@ public void eval(@NonNull String script) {
}

public void bind(@NonNull String name, @NonNull ConsumingProducer<JsonArray, JsonElement> 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);
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/dev/webview/WebviewNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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);

}

0 comments on commit f4c704c

Please sign in to comment.