I want to allow JavaScript to register events, with a syntax like
object.on('eventName', (event) => {
event.something();
});
The event
object provides different methods depending on what 'eventName'
is (depending on what type of event I'd like to add a handler for).
I will need to create host objects for object
and for event
, I'm assuming the class needed for object
will have to look something like
public class Events {
public void on(String eventName, X<FirstEvent> event) {
// ...
}
public void on(String eventName, X<SecondEvent> event) {
// ...
}
}
If there are two types of events.
But I don't know how to get graalvm to automatically choose the correct on
method, and I don't know what type to use for the second argument (it has to be a JavaScript anonymous function which accepts a single argument of a certain type, and must be runnable later by my Java code).
How can I store JavaScript anonymous function
(event) => {
event.something();
}
as some kind of Java Runnable, and make sure GraalVM knows which on
method to use depending on the value of the first string argument (because the event
objects passed will be different for different events).
I think this may be a super complex question, I'm finding it tricky to phrase properly.
I'd like to create a way for JavaScript to register event handlers that are functions that will be called at later times with certain arguments.