Skip to content

Commit f4c704c

Browse files
committed
Allow applications to dispatch from the main thread.
1 parent ecdeacb commit f4c704c

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

src/main/java/dev/webview/Webview.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,31 +155,46 @@ public void eval(@NonNull String script) {
155155
}
156156

157157
public void bind(@NonNull String name, @NonNull ConsumingProducer<JsonArray, JsonElement> handler) {
158-
N.webview_bind($pointer, name, new BindCallback() {
159-
@Override
160-
public void callback(long seq, String req, long arg) {
161-
try {
162-
JsonArray arguments = Rson.DEFAULT.fromJson(req, JsonArray.class);
163-
158+
N.webview_bind(
159+
$pointer,
160+
name,
161+
new BindCallback() {
162+
@Override
163+
public void callback(long seq, String req, long arg) {
164164
try {
165-
@Nullable
166-
JsonElement result = handler.produce(arguments);
167-
168-
N.webview_return($pointer, seq, false, Rson.DEFAULT.toJsonString(result));
169-
} catch (Exception e) {
170-
N.webview_return($pointer, seq, true, e.getMessage());
165+
JsonArray arguments = Rson.DEFAULT.fromJson(req, JsonArray.class);
166+
167+
try {
168+
@Nullable
169+
JsonElement result = handler.produce(arguments);
170+
171+
N.webview_return($pointer, seq, false, Rson.DEFAULT.toJsonString(result));
172+
} catch (Exception e) {
173+
N.webview_return($pointer, seq, true, e.getMessage());
174+
}
175+
} catch (JsonParseException e) {
176+
e.printStackTrace();
171177
}
172-
} catch (JsonParseException e) {
173-
e.printStackTrace();
174178
}
175-
}
176-
}, 0);
179+
},
180+
0
181+
);
177182
}
178183

179184
public void unbind(@NonNull String name) {
180185
N.webview_unbind($pointer, name);
181186
}
182187

188+
public void dispatch(@NonNull Runnable handler) {
189+
N.webview_dispatch(
190+
$pointer,
191+
($pointer, arg) -> {
192+
handler.run();
193+
},
194+
0
195+
);
196+
}
197+
183198
@Override
184199
public void run() {
185200
N.webview_run($pointer);

src/main/java/dev/webview/WebviewNative.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ static interface BindCallback extends Callback {
7474

7575
}
7676

77+
/**
78+
* Used in {@link webview_dispatch}
79+
*/
80+
static interface DispatchCallback extends Callback {
81+
82+
/**
83+
* @param $pointer The pointer of the webview
84+
* @param arg Unused
85+
*/
86+
void callback(long $pointer, long arg);
87+
88+
}
89+
7790
/**
7891
* Creates a new webview instance. If debug is true - developer tools will be
7992
* enabled (if the platform supports them). Window parameter can be a pointer to
@@ -173,7 +186,7 @@ static interface BindCallback extends Callback {
173186
* @param callback The callback to be called
174187
* @param arg Unused
175188
*/
176-
void webview_bind(long $pointer, @NonNull String name, BindCallback callback, long arg);
189+
void webview_bind(long $pointer, @NonNull String name, @NonNull BindCallback callback, long arg);
177190

178191
/**
179192
* Remove the native callback specified.
@@ -194,4 +207,14 @@ static interface BindCallback extends Callback {
194207
*/
195208
void webview_return(long $pointer, long seq, boolean isError, String result);
196209

210+
/**
211+
* Dispatches the callback on the UI thread, only effective while
212+
* {@link #webview_run(long)} is blocking.
213+
*
214+
* @param $pointer The instance pointer of the webview
215+
* @param callback The callback to be called
216+
* @param arg Unused
217+
*/
218+
void webview_dispatch(long $pointer, @NonNull DispatchCallback callback, long arg);
219+
197220
}

0 commit comments

Comments
 (0)