@@ -53,7 +53,7 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VA
53
53
use crate :: ln:: wire:: Encode ;
54
54
use crate :: chain:: keysinterface:: { Sign , KeysInterface , KeysManager , Recipient } ;
55
55
use crate :: util:: config:: { UserConfig , ChannelConfig } ;
56
- use crate :: util:: events:: { EventHandler , EventsProvider , MessageSendEvent , MessageSendEventsProvider , ClosureReason , HTLCDestination } ;
56
+ use crate :: util:: events:: { Event , EventHandler , EventsProvider , MessageSendEvent , MessageSendEventsProvider , ClosureReason , HTLCDestination } ;
57
57
use crate :: util:: { byte_utils, events} ;
58
58
use crate :: util:: wakers:: { Future , Notifier } ;
59
59
use crate :: util:: scid_utils:: fake_scid;
@@ -5588,6 +5588,37 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
5588
5588
pub fn clear_pending_payments ( & self ) {
5589
5589
self . pending_outbound_payments . lock ( ) . unwrap ( ) . clear ( )
5590
5590
}
5591
+
5592
+ /// Processes any events asynchronously in the order they were generated since the last call
5593
+ /// using the given event handler.
5594
+ ///
5595
+ /// See the trait-level documentation of [`EventsProvider`] for requirements.
5596
+ pub async fn process_pending_events_async < Future : core:: future:: Future , H : Fn ( Event ) -> Future > ( & self , handler : H ) {
5597
+ // We'll acquire our total consistency lock until the returned future completes so that
5598
+ // we can be sure no other persists happen while processing events.
5599
+ let _read_guard = self . total_consistency_lock . read ( ) . unwrap ( ) ;
5600
+
5601
+ let mut result = NotifyOption :: SkipPersist ;
5602
+
5603
+ // TODO: This behavior should be documented. It's unintuitive that we query
5604
+ // ChannelMonitors when clearing other events.
5605
+ if self . process_pending_monitor_events ( ) {
5606
+ result = NotifyOption :: DoPersist ;
5607
+ }
5608
+
5609
+ let mut pending_events = mem:: replace ( & mut * self . pending_events . lock ( ) . unwrap ( ) , vec ! [ ] ) ;
5610
+ if !pending_events. is_empty ( ) {
5611
+ result = NotifyOption :: DoPersist ;
5612
+ }
5613
+
5614
+ for event in pending_events. drain ( ..) {
5615
+ handler ( event) . await ;
5616
+ }
5617
+
5618
+ if result == NotifyOption :: DoPersist {
5619
+ self . persistence_notifier . notify ( ) ;
5620
+ }
5621
+ }
5591
5622
}
5592
5623
5593
5624
impl < M : Deref , T : Deref , K : Deref , F : Deref , L : Deref > MessageSendEventsProvider for ChannelManager < M , T , K , F , L >
0 commit comments