Merge pull request #126 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / EventHandler.java
index 2de4e0c04f84da82a0d64c3c49736b795ad4db06..648ac75533d39cc7e7a8bb7258ca2f0c0efb38de 100644 (file)
@@ -9,6 +9,10 @@ import javax.annotation.Nullable;
 
 /**
  * A trait implemented for objects handling events from [`EventsProvider`].
+ * 
+ * An async variation also exists for implementations of [`EventsProvider`] that support async
+ * event handling. The async event handler should satisfy the generic bounds: `F:
+ * core::future::Future, H: Fn(Event) -> F`.
  */
 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
 public class EventHandler extends CommonBase {
@@ -23,7 +27,19 @@ public class EventHandler extends CommonBase {
        protected void finalize() throws Throwable {
                if (ptr != 0) { bindings.EventHandler_free(ptr); } super.finalize();
        }
-
+       /**
+        * Destroys the object, freeing associated resources. After this call, any access
+        * to this object may result in a SEGFAULT or worse.
+        *
+        * You should generally NEVER call this method. You should let the garbage collector
+        * do this for you when it finalizes objects. However, it may be useful for types
+        * which represent locks and should be closed immediately to avoid holding locks
+        * until the GC runs.
+        */
+       public void destroy() {
+               if (ptr != 0) { bindings.EventHandler_free(ptr); }
+               ptr = 0;
+       }
        public static interface EventHandlerInterface {
                /**
                 * Handles the given [`Event`].
@@ -37,7 +53,8 @@ public class EventHandler extends CommonBase {
                final LDKEventHandlerHolder impl_holder = new LDKEventHandlerHolder();
                impl_holder.held = new EventHandler(new bindings.LDKEventHandler() {
                        @Override public void handle_event(long event) {
-                               Event event_hu_conv = Event.constr_from_ptr(event);
+                               org.ldk.structs.Event event_hu_conv = org.ldk.structs.Event.constr_from_ptr(event);
+                               if (event_hu_conv != null) { event_hu_conv.ptrs_to.add(this); };
                                arg.handle_event(event_hu_conv);
                                Reference.reachabilityFence(arg);
                        }
@@ -49,10 +66,11 @@ public class EventHandler extends CommonBase {
         * 
         * See [`EventsProvider`] for details that must be considered when implementing this method.
         */
-       public void handle_event(Event event) {
-               bindings.EventHandler_handle_event(this.ptr, event == null ? 0 : event.ptr & ~1);
+       public void handle_event(org.ldk.structs.Event event) {
+               bindings.EventHandler_handle_event(this.ptr, event.ptr);
                Reference.reachabilityFence(this);
                Reference.reachabilityFence(event);
+               if (this != null) { this.ptrs_to.add(event); };
        }
 
 }