1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
9 * A trait indicating an object may generate events.
11 * Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
15 * See [`process_pending_events`] for requirements around event processing.
17 * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
18 * event since the last invocation. The handler must either act upon the event immediately
19 * or preserve it for later handling.
21 * Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
22 * consult the provider's documentation on the implication of processing events and how a handler
23 * may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
24 * [`ChainMonitor::process_pending_events`]).
26 * (C-not implementable) As there is likely no reason for a user to implement this trait on their
29 * [`process_pending_events`]: Self::process_pending_events
30 * [`handle_event`]: EventHandler::handle_event
31 * [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
32 * [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
34 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
35 public class EventsProvider extends CommonBase {
36 final bindings.LDKEventsProvider bindings_instance;
37 EventsProvider(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
38 private EventsProvider(bindings.LDKEventsProvider arg) {
39 super(bindings.LDKEventsProvider_new(arg));
40 this.ptrs_to.add(arg);
41 this.bindings_instance = arg;
43 @Override @SuppressWarnings("deprecation")
44 protected void finalize() throws Throwable {
45 if (ptr != 0) { bindings.EventsProvider_free(ptr); } super.finalize();
48 public static interface EventsProviderInterface {
50 * Processes any events generated since the last call using the given event handler.
52 * Subsequent calls must only process new events. However, handlers must be capable of handling
53 * duplicate events across process restarts. This may occur if the provider was recovered from
54 * an old state (i.e., it hadn't been successfully persisted after processing pending events).
56 void process_pending_events(EventHandler handler);
58 private static class LDKEventsProviderHolder { EventsProvider held; }
59 public static EventsProvider new_impl(EventsProviderInterface arg) {
60 final LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder();
61 impl_holder.held = new EventsProvider(new bindings.LDKEventsProvider() {
62 @Override public void process_pending_events(long handler) {
63 EventHandler ret_hu_conv = new EventHandler(null, handler);
64 ret_hu_conv.ptrs_to.add(this);
65 arg.process_pending_events(ret_hu_conv);
68 return impl_holder.held;
71 * Processes any events generated since the last call using the given event handler.
73 * Subsequent calls must only process new events. However, handlers must be capable of handling
74 * duplicate events across process restarts. This may occur if the provider was recovered from
75 * an old state (i.e., it hadn't been successfully persisted after processing pending events).
77 public void process_pending_events(EventHandler handler) {
78 bindings.EventsProvider_process_pending_events(this.ptr, handler == null ? 0 : handler.ptr);
79 this.ptrs_to.add(handler);