@@ -86,7 +86,7 @@ impl<Payload: 'static + Send> WorkerState<Payload> {
8686 ( receiver, notice_receiver)
8787 }
8888
89-
89+ /// Listen to activity that happens with this worker's primitives.
9090 pub fn listen < F > ( & self , mut f : F ) -> ActivityListener < Payload >
9191 where
9292 F : FnMut ( & mut Payload ) + ' static + Send + Sync ,
@@ -227,6 +227,15 @@ impl<Payload> IntoWorkerOptions<Payload> for Payload {
227227 }
228228}
229229
230+ /// `ActivityListener` is used to watch the activity of the primitives of a [`Worker`].
231+ ///
232+ /// Each listener will be triggered each time a primitive is active on the `Worker`.
233+ /// Listeners will not be triggered when something other than a primitive modifies
234+ /// the payload, which can happen through [interior mutability][1] or by one of
235+ /// the other listeners, so this is not a sure-fire way to track changes in the
236+ /// payload.
237+ ///
238+ /// [1]: https://doc.rust-lang.org/book/ch15-05-interior-mutability.html
230239pub struct ActivityListener < Payload > {
231240 callback : Arc < Mutex < Option < ActivityListenerCallback > > > ,
232241 _ignore : std:: marker:: PhantomData < Payload > ,
@@ -242,6 +251,7 @@ impl<Payload> Clone for ActivityListener<Payload> {
242251}
243252
244253impl < Payload : ' static + Send + Sync > ActivityListener < Payload > {
254+ /// Change the callback for this listener.
245255 pub fn set_callback < F > ( & self , mut f : F )
246256 where
247257 F : FnMut ( & mut Payload ) + ' static + Send + Sync ,
@@ -259,16 +269,25 @@ impl<Payload: 'static + Send + Sync> ActivityListener<Payload> {
259269 * self . callback . lock ( ) . unwrap ( ) = Some ( ActivityListenerCallback :: Listen ( f) ) ;
260270 }
261271
272+ /// Change the listener to be inert but remain alive.
273+ pub fn set_inert ( & self ) {
274+ * self . callback . lock ( ) . unwrap ( ) = Some ( ActivityListenerCallback :: Inert ) ;
275+ }
276+
262277 fn new ( callback : ActivityListenerCallback ) -> Self {
263278 let callback = Arc :: new ( Mutex :: new ( Some ( callback) ) ) ;
264279 Self { callback, _ignore : Default :: default ( ) }
265280 }
266281}
267282
283+ /// This type is used by executor runtimes to keep track of listeners.
268284pub type WeakActivityListener = Weak < Mutex < Option < ActivityListenerCallback > > > ;
269285
286+ /// Enum for the different types of callbacks that a listener may have
270287pub enum ActivityListenerCallback {
288+ /// The listener is listening
271289 Listen ( Box < dyn FnMut ( & mut dyn Any ) + ' static + Send > ) ,
290+ /// The listener is inert
272291 Inert ,
273292}
274293
0 commit comments