@@ -86,7 +86,7 @@ impl<Payload: 'static + Send> WorkerState<Payload> {
86
86
( receiver, notice_receiver)
87
87
}
88
88
89
-
89
+ /// Listen to activity that happens with this worker's primitives.
90
90
pub fn listen < F > ( & self , mut f : F ) -> ActivityListener < Payload >
91
91
where
92
92
F : FnMut ( & mut Payload ) + ' static + Send + Sync ,
@@ -227,6 +227,15 @@ impl<Payload> IntoWorkerOptions<Payload> for Payload {
227
227
}
228
228
}
229
229
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
230
239
pub struct ActivityListener < Payload > {
231
240
callback : Arc < Mutex < Option < ActivityListenerCallback > > > ,
232
241
_ignore : std:: marker:: PhantomData < Payload > ,
@@ -242,6 +251,7 @@ impl<Payload> Clone for ActivityListener<Payload> {
242
251
}
243
252
244
253
impl < Payload : ' static + Send + Sync > ActivityListener < Payload > {
254
+ /// Change the callback for this listener.
245
255
pub fn set_callback < F > ( & self , mut f : F )
246
256
where
247
257
F : FnMut ( & mut Payload ) + ' static + Send + Sync ,
@@ -259,16 +269,25 @@ impl<Payload: 'static + Send + Sync> ActivityListener<Payload> {
259
269
* self . callback . lock ( ) . unwrap ( ) = Some ( ActivityListenerCallback :: Listen ( f) ) ;
260
270
}
261
271
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
+
262
277
fn new ( callback : ActivityListenerCallback ) -> Self {
263
278
let callback = Arc :: new ( Mutex :: new ( Some ( callback) ) ) ;
264
279
Self { callback, _ignore : Default :: default ( ) }
265
280
}
266
281
}
267
282
283
+ /// This type is used by executor runtimes to keep track of listeners.
268
284
pub type WeakActivityListener = Weak < Mutex < Option < ActivityListenerCallback > > > ;
269
285
286
+ /// Enum for the different types of callbacks that a listener may have
270
287
pub enum ActivityListenerCallback {
288
+ /// The listener is listening
271
289
Listen ( Box < dyn FnMut ( & mut dyn Any ) + ' static + Send > ) ,
290
+ /// The listener is inert
272
291
Inert ,
273
292
}
274
293
0 commit comments