raw_input/subscription.rs
1use crate::dispatcher::{CALLBACKS, Status};
2
3/// A handle that allows control over an active event subscription.
4///
5/// It can be used to pause, resume, or permanently remove a callback.
6pub struct SubscriptionHandle {
7 pub(crate) id: u64,
8}
9
10impl SubscriptionHandle {
11 /// Pauses the subscription. The callback will not be executed until `resume` is called.
12 ///
13 /// # Example
14 /// ```no_run
15 /// handle.pause();
16 /// ```
17 pub fn pause(&self) {
18 if let Some(mut subscriber) = CALLBACKS.get_mut(&self.id) {
19 subscriber.status = Status::Paused;
20 }
21 }
22
23 /// Resumes a previously paused subscription.
24 ///
25 /// # Example
26 /// ```no_run
27 /// handle.resume();
28 /// ```
29 pub fn resume(&self) {
30 if let Some(mut subscriber) = CALLBACKS.get_mut(&self.id) {
31 subscriber.status = Status::Active;
32 }
33 }
34
35 /// Removes the subscription from the dispatcher.
36 /// The callback will be dropped and never called again.
37 ///
38 /// # Example
39 /// ```no_run
40 /// handle.unsubscribe();
41 /// ```
42 pub fn unsubscribe(self) {
43 CALLBACKS.remove(&self.id);
44 }
45}