jso apis: new convention for event registration

This commit is contained in:
Alexey Andreev 2024-04-12 13:45:12 +02:00
parent 272f55b383
commit 97aa42d87b
35 changed files with 364 additions and 80 deletions

View File

@ -22,6 +22,7 @@ import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.Registration;
import org.teavm.jso.dom.xml.Document;
@JSClass
@ -65,29 +66,40 @@ public class XMLHttpRequest implements JSObject, EventTarget {
@JSProperty("onreadystatechange")
public native void setOnReadyStateChange(EventListener<Event> handler);
@JSProperty("onabort")
public native void onAbort(EventListener<ProgressEvent> eventListener);
public final Registration onReadyStateChange(EventListener<Event> handler) {
return onEvent("readystatechange", handler);
}
@JSProperty("onerror")
public native void onError(EventListener<ProgressEvent> eventListener);
public final Registration onAbort(EventListener<ProgressEvent> eventListener) {
return onEvent("abort", eventListener);
}
@JSProperty("onload")
public native void onLoad(EventListener<ProgressEvent> eventListener);
public final Registration onError(EventListener<ProgressEvent> eventListener) {
return onEvent("error", eventListener);
}
@JSProperty("onloadstart")
public native void onLoadStart(EventListener<ProgressEvent> eventListener);
public final Registration onLoad(EventListener<ProgressEvent> eventListener) {
return onEvent("load", eventListener);
}
@JSProperty("onloadend")
public native void onLoadEnd(EventListener<ProgressEvent> eventListener);
public final Registration onLoadStart(EventListener<ProgressEvent> eventListener) {
return onEvent("loadstart", eventListener);
}
@JSProperty("onprogress")
public native void onProgress(EventListener<ProgressEvent> eventListener);
public final Registration onLoadEnd(EventListener<ProgressEvent> eventListener) {
return onEvent("loadend", eventListener);
}
@JSProperty("ontimeout")
public native void onTimeout(EventListener<ProgressEvent> eventListener);
public final Registration onProgress(EventListener<ProgressEvent> eventListener) {
return onEvent("progress", eventListener);
}
public final void onComplete(Runnable runnable) {
setOnReadyStateChange(() -> {
public final Registration onTimeout(EventListener<ProgressEvent> eventListener) {
return onEvent("timeout", eventListener);
}
public final Registration onComplete(Runnable runnable) {
return onReadyStateChange(event -> {
if (getReadyState() == DONE) {
runnable.run();
}

View File

@ -25,30 +25,54 @@ import org.teavm.jso.dom.events.KeyboardEventTarget;
import org.teavm.jso.dom.events.LoadEventTarget;
import org.teavm.jso.dom.events.MessageEvent;
import org.teavm.jso.dom.events.MouseEventTarget;
import org.teavm.jso.dom.events.Registration;
import org.teavm.jso.dom.events.StorageEvent;
public interface WindowEventTarget extends EventTarget, FocusEventTarget, MouseEventTarget, KeyboardEventTarget,
LoadEventTarget, GamepadEventTarget {
@Deprecated
default void listenBeforeOnload(EventListener<Event> listener) {
addEventListener("beforeunload", listener);
}
@Deprecated
default void neglectBeforeOnload(EventListener<Event> listener) {
removeEventListener("beforeunload", listener);
}
@Deprecated
default void listenMessage(EventListener<MessageEvent> listener) {
addEventListener("message", listener);
}
@Deprecated
default void neglectMessage(EventListener<MessageEvent> listener) {
removeEventListener("message", listener);
}
@Deprecated
default void listenHashChange(EventListener<HashChangeEvent> listener) {
addEventListener("hashchange", listener);
}
@Deprecated
default void neglectHashChange(EventListener<HashChangeEvent> listener) {
removeEventListener("hashchange", listener);
}
default Registration onBeforeUnload(EventListener<Event> listener) {
return onEvent("beforeunload", listener);
}
default Registration onMessage(EventListener<MessageEvent> listener) {
return onEvent("message", listener);
}
default Registration onHashChange(EventListener<HashChangeEvent> listener) {
return onEvent("hashchange", listener);
}
default Registration onStorage(EventListener<StorageEvent> listener) {
return onEvent("storage", listener);
}
}

View File

@ -27,4 +27,14 @@ public interface EventTarget extends JSObject {
void removeEventListener(String type, EventListener<?> listener);
boolean dispatchEvent(Event evt);
default Registration onEvent(String type, boolean useCapture, EventListener<?> listener) {
addEventListener(type, listener, useCapture);
return new Registration(this, type, listener, useCapture);
}
default Registration onEvent(String type, EventListener<?> listener) {
addEventListener(type, listener);
return new Registration(this, type, listener, null);
}
}

View File

@ -16,19 +16,31 @@
package org.teavm.jso.dom.events;
public interface FocusEventTarget extends EventTarget {
@Deprecated
default void listenFocus(EventListener<Event> listener) {
addEventListener("focus", listener);
}
@Deprecated
default void neglectFocus(EventListener<Event> listener) {
removeEventListener("focus", listener);
}
@Deprecated
default void listenBlur(EventListener<Event> listener) {
addEventListener("blur", listener);
}
@Deprecated
default void neglectBlur(EventListener<Event> listener) {
removeEventListener("blur", listener);
}
default Registration onFocus(EventListener<? super Event> listener) {
return onEvent("focus", listener);
}
default Registration onBlur(EventListener<? super Event> listener) {
return onEvent("blur", listener);
}
}

View File

@ -18,20 +18,31 @@ package org.teavm.jso.dom.events;
import org.teavm.jso.gamepad.GamepadEvent;
public interface GamepadEventTarget extends EventTarget {
@Deprecated
default void listenGamepadConnected(EventListener<GamepadEvent> listener) {
addEventListener("gamepadconnected", listener);
}
@Deprecated
default void neglectGamepadConnected(EventListener<GamepadEvent> listener) {
removeEventListener("gamepadconnected", listener);
}
@Deprecated
default void listenGamepadDisconnected(EventListener<GamepadEvent> listener) {
addEventListener("gamepaddisconnected", listener);
}
@Deprecated
default void neglectGamepadDisconnected(EventListener<GamepadEvent> listener) {
removeEventListener("gamepaddisconnected", listener);
}
default Registration onGamepadConnected(EventListener<? super Event> listener) {
return onEvent("gamepadconnected", listener);
}
default Registration onGamepadDisconnected(EventListener<? super Event> listener) {
return onEvent("gamepaddisconnected", listener);
}
}

View File

@ -16,27 +16,45 @@
package org.teavm.jso.dom.events;
public interface KeyboardEventTarget extends EventTarget {
@Deprecated
default void listenKeyDown(EventListener<KeyboardEvent> listener) {
addEventListener("keydown", listener);
}
@Deprecated
default void neglectKeyDown(EventListener<KeyboardEvent> listener) {
removeEventListener("keydown", listener);
}
@Deprecated
default void listenKeyUp(EventListener<KeyboardEvent> listener) {
addEventListener("keyup", listener);
}
@Deprecated
default void neglectKeyUp(EventListener<KeyboardEvent> listener) {
removeEventListener("keyup", listener);
}
@Deprecated
default void listenKeyPress(EventListener<KeyboardEvent> listener) {
addEventListener("keypress", listener);
}
@Deprecated
default void neglectKeyPress(EventListener<KeyboardEvent> listener) {
removeEventListener("keypress", listener);
}
default Registration onKeyDown(EventListener<KeyboardEvent> listener) {
return onEvent("keydown", listener);
}
default Registration onKeyUp(EventListener<KeyboardEvent> listener) {
return onEvent("keyup", listener);
}
default Registration onKeyPress(EventListener<KeyboardEvent> listener) {
return onEvent("keypress", listener);
}
}

View File

@ -16,11 +16,17 @@
package org.teavm.jso.dom.events;
public interface LoadEventTarget extends EventTarget {
@Deprecated
default void listenLoad(EventListener<Event> listener) {
addEventListener("load", listener);
}
@Deprecated
default void neglectLoad(EventListener<Event> listener) {
addEventListener("load", listener);
}
default Registration onLoad(EventListener<Event> listener) {
return onEvent("load", listener);
}
}

View File

@ -16,75 +16,129 @@
package org.teavm.jso.dom.events;
public interface MouseEventTarget extends EventTarget {
@Deprecated
default void listenClick(EventListener<MouseEvent> listener) {
addEventListener("click", listener);
}
@Deprecated
default void neglectClick(EventListener<MouseEvent> listener) {
removeEventListener("click", listener);
}
@Deprecated
default void listenDoubleClick(EventListener<MouseEvent> listener) {
addEventListener("dblclick", listener);
}
@Deprecated
default void neglectDoubleClick(EventListener<MouseEvent> listener) {
removeEventListener("dblclick", listener);
}
@Deprecated
default void listenMouseDown(EventListener<MouseEvent> listener) {
addEventListener("mousedown", listener);
}
@Deprecated
default void neglectMouseDown(EventListener<MouseEvent> listener) {
removeEventListener("mousedown", listener);
}
@Deprecated
default void listenMouseUp(EventListener<MouseEvent> listener) {
addEventListener("mouseup", listener);
}
@Deprecated
default void neglectMouseUp(EventListener<MouseEvent> listener) {
removeEventListener("mouseup", listener);
}
@Deprecated
default void listenMouseMove(EventListener<MouseEvent> listener) {
addEventListener("mousemove", listener);
}
@Deprecated
default void neglectMouseMove(EventListener<MouseEvent> listener) {
removeEventListener("mousemove", listener);
}
@Deprecated
default void listenMouseOver(EventListener<MouseEvent> listener) {
addEventListener("mouseover", listener);
}
@Deprecated
default void neglectMouseOver(EventListener<MouseEvent> listener) {
removeEventListener("mouseover", listener);
}
@Deprecated
default void listenMouseEnter(EventListener<MouseEvent> listener) {
addEventListener("mouseenter", listener);
}
@Deprecated
default void neglectMouseEnter(EventListener<MouseEvent> listener) {
removeEventListener("mouseenter", listener);
}
@Deprecated
default void listenMouseLeave(EventListener<MouseEvent> listener) {
addEventListener("mouseleave", listener);
}
@Deprecated
default void neglectMouseLeave(EventListener<MouseEvent> listener) {
removeEventListener("mouseleave", listener);
}
@Deprecated
default void listenMouseOut(EventListener<MouseEvent> listener) {
addEventListener("mouseout", listener);
}
@Deprecated
default void neglectMouseOut(EventListener<MouseEvent> listener) {
removeEventListener("mouseout", listener);
}
default Registration onClick(EventListener<MouseEvent> listener) {
return onEvent("click", listener);
}
default Registration onDoubleClick(EventListener<MouseEvent> listener) {
return onEvent("dblclick", listener);
}
default Registration onMouseDown(EventListener<MouseEvent> listener) {
return onEvent("mousedown", listener);
}
default Registration onMouseUp(EventListener<MouseEvent> listener) {
return onEvent("mouseup", listener);
}
default Registration onMouseMove(EventListener<MouseEvent> listener) {
return onEvent("mousemove", listener);
}
default Registration onMouseOver(EventListener<MouseEvent> listener) {
return onEvent("mouseover", listener);
}
default Registration onMouseEnter(EventListener<MouseEvent> listener) {
return onEvent("mouseenter", listener);
}
default Registration onMouseLeave(EventListener<MouseEvent> listener) {
return onEvent("mouseleave", listener);
}
default Registration onMouseOut(EventListener<MouseEvent> listener) {
return onEvent("mouseout", listener);
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2024 konsoletyper.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.jso.dom.events;
public class Registration {
private EventTarget target;
private String type;
private EventListener<?> listener;
private Boolean useCapture;
Registration(EventTarget target, String type, EventListener<?> listener, Boolean useCapture) {
this.target = target;
this.type = type;
this.listener = listener;
this.useCapture = useCapture;
}
public void dispose() {
if (target != null) {
if (useCapture != null) {
target.removeEventListener(type, listener, useCapture);
} else {
target.removeEventListener(type, listener);
}
target = null;
type = null;
listener = null;
useCapture = null;
}
}
}

View File

@ -18,10 +18,6 @@ package org.teavm.jso.dom.events;
import org.teavm.jso.JSProperty;
import org.teavm.jso.browser.Storage;
/**
*
* @author Junji Takakura
*/
public interface StorageEvent extends Event {
@JSProperty

View File

@ -16,11 +16,17 @@
package org.teavm.jso.dom.events;
public interface WheelEventTarget extends EventTarget {
@Deprecated
default void listenWheel(EventListener<WheelEvent> listener) {
addEventListener("wheel", listener);
}
@Deprecated
default void neglectWheel(EventListener<WheelEvent> listener) {
removeEventListener("wheel", listener);
}
default Registration onWheel(EventListener<WheelEvent> listener) {
return onEvent("wheel", listener);
}
}

View File

@ -18,9 +18,6 @@ package org.teavm.jso.geolocation;
import org.teavm.jso.JSFunctor;
import org.teavm.jso.JSObject;
/**
*
*/
@JSFunctor
public interface PositionHandler extends JSObject {
void handlePosition(Position position);

View File

@ -17,6 +17,7 @@ package org.teavm.jso.webaudio;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.Registration;
public interface AudioBufferSourceNode extends AudioNode {
@JSProperty
@ -55,6 +56,10 @@ public interface AudioBufferSourceNode extends AudioNode {
@JSProperty("onended")
EventListener<MediaEvent> getOnEnded();
default Registration onEnded(EventListener<MediaEvent> eventListener) {
return onEvent("ended", eventListener);
}
void start(double when, double offset, double duration);
void start(double when, double offset);

View File

@ -20,13 +20,16 @@ import org.teavm.jso.JSByRef;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.Registration;
import org.teavm.jso.dom.html.HTMLMediaElement;
import org.teavm.jso.typedarrays.ArrayBuffer;
import org.teavm.jso.typedarrays.Float32Array;
@JSClass
public class AudioContext implements JSObject {
public class AudioContext implements JSObject, EventTarget {
public static final String STATE_SUSPENDED = "suspended";
public static final String STATE_RUNNING = "running";
public static final String STATE_CLOSE = "close";
@ -52,6 +55,10 @@ public class AudioContext implements JSObject {
@JSProperty("onstatechange")
public native EventListener<MediaEvent> getOnStateChange();
public final Registration onStateChange(EventListener<MediaEvent> listener) {
return onEvent("statechange", listener);
}
public native void suspend();
public native void resume();
@ -131,4 +138,19 @@ public class AudioContext implements JSObject {
@JSBody(script = "return new Context();")
@Deprecated
public static native AudioContext create();
@Override
public native void addEventListener(String type, EventListener<?> listener, boolean useCapture);
@Override
public native void addEventListener(String type, EventListener<?> listener);
@Override
public native void removeEventListener(String type, EventListener<?> listener, boolean useCapture);
@Override
public native void removeEventListener(String type, EventListener<?> listener);
@Override
public native boolean dispatchEvent(Event evt);
}

View File

@ -17,8 +17,9 @@ package org.teavm.jso.webaudio;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventTarget;
public interface AudioNode extends JSObject {
public interface AudioNode extends JSObject, EventTarget {
String CHANNEL_COUNT_MODE_MAX = "max";
String CHANNEL_COUNT_MODE_CLAMPED_MAX = "clamped-max";
String CHANNEL_COUNT_MODE_EXPLICIT = "explicit";

View File

@ -19,8 +19,10 @@ import org.teavm.jso.JSByRef;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.Registration;
public interface AudioWorker extends JSObject {
public interface AudioWorker extends EventTarget {
@JSProperty
AudioWorkerParamDescriptor[] getParameters();
@ -30,12 +32,20 @@ public interface AudioWorker extends JSObject {
@JSProperty(value = "onmessage")
EventListener<MediaEvent> getOnMessage();
default Registration onMessage(EventListener<MediaEvent> listener) {
return onEvent("message", listener);
}
@JSProperty(value = "onloaded")
void setOnLoaded(EventListener<MediaEvent> event);
@JSProperty(value = "onloaded")
EventListener<MediaEvent> getOnLoaded();
default Registration onLoaded(EventListener<MediaEvent> listener) {
return onEvent("loaded", listener);
}
void terminate();
void postMessage(JSObject message, @JSByRef JSObject[] transfer);

View File

@ -15,11 +15,12 @@
*/
package org.teavm.jso.webaudio;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.Registration;
public interface AudioWorkerGlobalScope extends JSObject {
public interface AudioWorkerGlobalScope extends EventTarget {
@JSProperty
float getSampleRate();
@ -29,12 +30,20 @@ public interface AudioWorkerGlobalScope extends JSObject {
@JSProperty("onaudioprocess")
EventListener<MediaEvent> getOnAudioProcess();
default Registration onAudioProcess(EventListener<MediaEvent> listener) {
return onEvent("audioprocess", listener);
}
@JSProperty("onnodecreate")
void setOnNodeCreate(EventListener<MediaEvent> event);
@JSProperty("onnodecreate")
EventListener<MediaEvent> getOnNodeCreate();
default Registration onNodeCreate(EventListener<MediaEvent> listener) {
return onEvent("nodecreate", listener);
}
@JSProperty
AudioWorkerParamDescriptor[] getParameters();

View File

@ -19,6 +19,7 @@ import org.teavm.jso.JSByRef;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.Registration;
public interface AudioWorkerNode extends AudioNode {
@JSProperty("onmessage")
@ -27,6 +28,10 @@ public interface AudioWorkerNode extends AudioNode {
@JSProperty("onmessage")
EventListener<MediaEvent> getOnMessage();
default Registration onMessage(EventListener<MediaEvent> listener) {
return onEvent("message", listener);
}
void postMessage(JSObject message, @JSByRef JSObject[] transfer);
void postMessage(JSObject message, JSObject transfer);

View File

@ -15,6 +15,7 @@
*/
package org.teavm.jso.webaudio;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSArray;
@ -23,9 +24,9 @@ public interface AudioWorkerNodeCreationEvent extends MediaEvent {
AudioWorkerNodeProcessor getNode();
@JSProperty
JSArray getInputs();
JSArray<JSObject> getInputs();
@JSProperty
JSArray getOutputs();
JSArray<JSObject> getOutputs();
}

View File

@ -19,6 +19,7 @@ import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.Registration;
public interface AudioWorkerNodeProcessor extends EventTarget {
@JSProperty("onmessage")
@ -27,6 +28,10 @@ public interface AudioWorkerNodeProcessor extends EventTarget {
@JSProperty("onmessage")
EventListener<MediaEvent> getOnMessage();
default Registration onMessage(EventListener<MediaEvent> listener) {
return onEvent("message", listener);
}
void postMessage(JSObject message, JSObject[] transfer);
void postMessage(JSObject message, JSObject transfer);

View File

@ -18,6 +18,7 @@ package org.teavm.jso.webaudio;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.Registration;
@JSClass
public class OfflineAudioContext extends AudioContext {
@ -27,6 +28,10 @@ public class OfflineAudioContext extends AudioContext {
@JSProperty("oncomplete")
public native EventListener<OfflineAudioCompletionEvent> getOnComplete();
public final Registration onComplete(EventListener<OfflineAudioCompletionEvent> listener) {
return onEvent("complete", listener);
}
public native AudioBuffer startRendering();
@Override

View File

@ -17,6 +17,7 @@ package org.teavm.jso.webaudio;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.Registration;
public interface OscillatorNode extends AudioNode {
String TYPE_SINE = "sine";
@ -43,6 +44,10 @@ public interface OscillatorNode extends AudioNode {
@JSProperty("onended")
EventListener<MediaEvent> getOnEnded();
default Registration onEnded(EventListener<MediaEvent> listener) {
return onEvent("ended", listener);
}
void start(double when);
void start();

View File

@ -17,6 +17,7 @@ package org.teavm.jso.webaudio;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.Registration;
public interface ScriptProcessorNode extends AudioNode {
@JSProperty("onaudioprocess")
@ -25,6 +26,10 @@ public interface ScriptProcessorNode extends AudioNode {
@JSProperty("onaudioprocess")
void setOnAudioProcess(EventListener<AudioProcessingEvent> event);
default Registration onAudioProcess(EventListener<AudioProcessingEvent> listener) {
return onEvent("audioprocess", listener);
}
@JSProperty
int getBufferSize();
}

View File

@ -21,12 +21,14 @@ import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.MessageEvent;
import org.teavm.jso.dom.events.Registration;
import org.teavm.jso.typedarrays.ArrayBuffer;
import org.teavm.jso.typedarrays.ArrayBufferView;
@JSClass
public class WebSocket implements JSObject {
public class WebSocket implements JSObject, EventTarget {
public WebSocket(String url) {
}
@ -36,17 +38,21 @@ public class WebSocket implements JSObject {
public WebSocket(String url, String[] protocols) {
}
@JSProperty("onclose")
public native void onClose(EventListener<CloseEvent> eventListener);
public final Registration onClose(EventListener<CloseEvent> eventListener) {
return onEvent("close", eventListener);
}
@JSProperty("onerror")
public native void onError(EventListener<Event> eventListener);
public final Registration onError(EventListener<Event> eventListener) {
return onEvent("error", eventListener);
}
@JSProperty("onmessage")
public native void onMessage(EventListener<MessageEvent> eventListener);
public final Registration onMessage(EventListener<MessageEvent> eventListener) {
return onEvent("message", eventListener);
}
@JSProperty("onopen")
public native void onOpen(EventListener<Event> eventListener);
public final Registration onOpen(EventListener<Event> eventListener) {
return onEvent("open", eventListener);
}
@JSBody(params = "url", script = "return new WebSocket(url);")
@Deprecated
@ -95,4 +101,19 @@ public class WebSocket implements JSObject {
@JSBody(script = "return typeof WebSocket !== 'undefined';")
public static native boolean isSupported();
@Override
public native void addEventListener(String type, EventListener<?> listener, boolean useCapture);
@Override
public native void addEventListener(String type, EventListener<?> listener);
@Override
public native void removeEventListener(String type, EventListener<?> listener, boolean useCapture);
@Override
public native void removeEventListener(String type, EventListener<?> listener);
@Override
public native boolean dispatchEvent(Event evt);
}

View File

@ -16,12 +16,13 @@
package org.teavm.jso.workers;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.ErrorEvent;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.Registration;
public interface AbstractWorker extends JSObject, EventTarget {
@JSProperty("onerror")
void onError(EventListener<ErrorEvent> listener);
default Registration onError(EventListener<ErrorEvent> listener) {
return onEvent("error", listener);
}
}

View File

@ -15,10 +15,10 @@
*/
package org.teavm.jso.workers;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.MessageEvent;
import org.teavm.jso.dom.events.Registration;
public interface MessagePort extends EventTarget {
void postMessage(Object message);
@ -27,9 +27,11 @@ public interface MessagePort extends EventTarget {
void close();
@JSProperty("onmessage")
void onMessage(EventListener<MessageEvent> message);
default Registration onMessage(EventListener<MessageEvent> message) {
return onEvent("message", message);
}
@JSProperty("onmessageerror")
void onMessageError(EventListener<MessageEvent> message);
default Registration onMessageError(EventListener<MessageEvent> message) {
return onEvent("messageerror", message);
}
}

View File

@ -16,11 +16,14 @@
package org.teavm.jso.workers;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.ErrorEvent;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.Registration;
@JSClass
public class SharedWorker implements AbstractWorker {
public SharedWorker(String url) {
}
@ -32,9 +35,6 @@ public class SharedWorker implements AbstractWorker {
@JSProperty
public native MessagePort getPort();
@Override
public native void onError(EventListener<ErrorEvent> listener);
@Override
public native void addEventListener(String type, EventListener<?> listener, boolean useCapture);

View File

@ -22,6 +22,7 @@ import org.teavm.jso.dom.events.ErrorEvent;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.MessageEvent;
import org.teavm.jso.dom.events.Registration;
@JSClass
public class Worker implements AbstractWorker {
@ -32,16 +33,14 @@ public class Worker implements AbstractWorker {
@Deprecated
public static native Worker create(String url);
@JSProperty("onmessage")
public native void onMessage(EventListener<MessageEvent> listener);
public Registration onMessage(EventListener<MessageEvent> listener) {
return onEvent("message", listener);
}
public native void postMessage(Object message);
public native void terminate();
@Override
public native void onError(EventListener<ErrorEvent> listener);
@Override
public native void addEventListener(String type, EventListener<?> listener, boolean useCapture);

View File

@ -31,7 +31,7 @@ public final class Client {
}
public static void main(String[] args) {
helloButton.listenClick(evt -> sayHello());
helloButton.onClick(evt -> sayHello());
}
private static void sayHello() {

View File

@ -15,7 +15,7 @@
*/
plugins {
kotlin("multiplatform") version "1.9.20"
kotlin("jvm") version "1.9.20"
war
id("org.teavm")
}
@ -29,6 +29,4 @@ teavm.js {
mainClass = "org.teavm.samples.kotlin.HelloKt"
}
kotlin {
jvm()
}
kotlin.jvmToolchain(17)

View File

@ -20,5 +20,5 @@ import org.teavm.jso.browser.*
fun main() {
val document = Window.current().document
document.getElementById("hello-kotlin").addEventListener("click") { Window.alert("Hello, developer!") }
document.getElementById("hello-kotlin").onClick { Window.alert("Hello, developer!") }
}

View File

@ -11,7 +11,7 @@ object Client {
val exprElem = doc.getElementById("expr").asInstanceOf[HTMLInputElement]
val calcElem = doc.getElementById("calculate")
val resultList = doc.getElementById("result-list")
calcElem.listenClick((e: MouseEvent) => {
calcElem.onClick((e: MouseEvent) => {
parse(exprElem.getValue.toSeq) match {
case (None, _) => Window.alert("Error parsing expression");
case (Some(x), Nil) =>

View File

@ -26,7 +26,7 @@ import org.teavm.samples.software3d.scenes.geometry
fun worker() {
val worker = RenderWorker()
Window.worker().listenMessage {
Window.worker().onMessage {
val dataJson = it.data as JSMapLike<*>
when ((dataJson["type"] as JSString).stringValue()) {
"init" -> worker.init(dataJson)

View File

@ -37,7 +37,7 @@ public final class Storage {
}
HTMLButtonElement saveButton = document.getElementById("save-button").cast();
saveButton.listenClick(e -> {
saveButton.onClick(e -> {
var key = document.getElementById("key").<HTMLInputElement>cast().getValue();
var value = document.getElementById("value").<HTMLInputElement>cast().getValue();
@ -47,7 +47,7 @@ public final class Storage {
}
});
HTMLButtonElement deleteButton = document.getElementById("delete-button").cast();
deleteButton.listenClick(e -> {
deleteButton.onClick(e -> {
String key = document.getElementById("key").<HTMLInputElement>cast().getValue();
if (key != null && !key.isEmpty()) {
storage.removeItem(key);
@ -55,7 +55,7 @@ public final class Storage {
}
});
HTMLButtonElement deleteAllButton = document.getElementById("delete-all-button").cast();
deleteAllButton.listenClick(e -> {
deleteAllButton.onClick(e -> {
storage.clear();
draw();
});

View File

@ -57,30 +57,30 @@ public final class Video {
var divButtons = document.createElement("div")
.withAttr("id", "button")
.withChild("button", elem -> elem.withText("load()").listenClick(evt -> video.load()))
.withChild("button", elem -> elem.withText("play()").listenClick(evt -> video.play()))
.withChild("button", elem -> elem.withText("pause()").listenClick(evt -> video.pause()))
.withChild("button", elem -> elem.withText("load()").onClick(evt -> video.load()))
.withChild("button", elem -> elem.withText("play()").onClick(evt -> video.play()))
.withChild("button", elem -> elem.withText("pause()").onClick(evt -> video.pause()))
.withChild("br")
.withChild("button", elem -> elem.withText("currentTime+=10")
.listenClick(evt -> video.addCurrentTime(10)))
.onClick(evt -> video.addCurrentTime(10)))
.withChild("button", elem -> elem.withText("currentTime-=10")
.listenClick(evt -> video.addCurrentTime(-10)))
.onClick(evt -> video.addCurrentTime(-10)))
.withChild("button", elem -> elem.withText("currentTime-=50")
.listenClick(evt -> video.setCurrentTime(50)))
.onClick(evt -> video.setCurrentTime(50)))
.withChild("br")
.withChild("button", elem -> elem.withText("playbackRate++")
.listenClick(evt -> video.addPlaybackRate(1)))
.onClick(evt -> video.addPlaybackRate(1)))
.withChild("button", elem -> elem.withText("playbackRate--")
.listenClick(evt -> video.addPlaybackRate(-1)))
.onClick(evt -> video.addPlaybackRate(-1)))
.withChild("button", elem -> elem.withText("playbackRate+=0.1")
.listenClick(evt -> video.addPlaybackRate(0.1)))
.onClick(evt -> video.addPlaybackRate(0.1)))
.withChild("button", elem -> elem.withText("playbackRate-=0.1")
.listenClick(evt -> video.addPlaybackRate(-0.1)))
.onClick(evt -> video.addPlaybackRate(-0.1)))
.withChild("br")
.withChild("button", elem -> elem.withText("volume+=1").listenClick(evt -> video.addVolume(0.1F)))
.withChild("button", elem -> elem.withText("volume-=1").listenClick(evt -> video.addVolume(-0.1F)))
.withChild("button", elem -> elem.withText("mute").listenClick(evt -> video.setMuted(true)))
.withChild("button", elem -> elem.withText("unmute").listenClick(evt -> video.setMuted(false)));
.withChild("button", elem -> elem.withText("volume+=1").onClick(evt -> video.addVolume(0.1F)))
.withChild("button", elem -> elem.withText("volume-=1").onClick(evt -> video.addVolume(-0.1F)))
.withChild("button", elem -> elem.withText("mute").onClick(evt -> video.setMuted(true)))
.withChild("button", elem -> elem.withText("unmute").onClick(evt -> video.setMuted(false)));
var body = document.getBody();
body.appendChild(divVideo);