X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FEventHandler.java;h=648ac75533d39cc7e7a8bb7258ca2f0c0efb38de;hb=7b75443b8305c6a24d3c08520e1785e9d39960e4;hp=cb74075277b76454accd94abe5ee800846048820;hpb=9760d533f7ecac9bb7a8249f9b557e3f995c276d;p=ldk-java diff --git a/src/main/java/org/ldk/structs/EventHandler.java b/src/main/java/org/ldk/structs/EventHandler.java index cb740752..648ac755 100644 --- a/src/main/java/org/ldk/structs/EventHandler.java +++ b/src/main/java/org/ldk/structs/EventHandler.java @@ -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); + 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); }; } }