@@ -31,12 +31,29 @@ impl<X: Send + Sync + 'static + Clone> Publisher<X, SubscriptionFunc<X>> {
31
31
self . notify_observers ( Arc :: new ( val) ) ;
32
32
}
33
33
34
- pub fn subscribe ( & mut self , s : Arc < SubscriptionFunc < X > > ) {
35
- self . add_observer ( s) ;
34
+ pub fn subscribe ( & mut self , s : Arc < SubscriptionFunc < X > > ) -> Arc < SubscriptionFunc < X > > {
35
+ self . add_observer ( s. clone ( ) ) ;
36
+ s
36
37
}
37
- pub fn subscribe_fn ( & mut self , func : impl FnMut ( Arc < X > ) + Send + Sync + ' static ) {
38
+ pub fn subscribe_fn (
39
+ & mut self ,
40
+ func : impl FnMut ( Arc < X > ) + Send + Sync + ' static ,
41
+ ) -> Arc < SubscriptionFunc < X > > {
38
42
self . subscribe ( Arc :: new ( SubscriptionFunc :: new ( func) ) )
39
43
}
44
+ pub fn map < Z : Send + Sync + ' static + Clone > (
45
+ & mut self ,
46
+ func : impl FnMut ( Arc < X > ) -> Z + Send + Sync + ' static + Clone ,
47
+ ) -> Arc < SubscriptionFunc < X > > {
48
+ let _func = Arc :: new ( func) ;
49
+ self . subscribe_fn ( move |x : Arc < X > | {
50
+ let mut func = _func. clone ( ) ;
51
+ ( Arc :: make_mut ( & mut func) ) ( x) ;
52
+ } )
53
+ }
54
+ pub fn unsubscribe ( & mut self , s : Arc < SubscriptionFunc < X > > ) {
55
+ self . delete_observer ( s) ;
56
+ }
40
57
41
58
pub fn subscribe_on ( & mut self , h : Option < Arc < Mutex < Handler + ' static > > > ) {
42
59
self . sub_handler = h;
@@ -121,9 +138,9 @@ fn test_publisher_new() {
121
138
latch2. countdown ( ) ;
122
139
} ) ) ;
123
140
pub2. subscribe ( s) ;
124
- pub2. subscribe ( Arc :: new ( SubscriptionFunc :: new ( move |x : Arc < String > | {
141
+ pub2. map ( move |x : Arc < String > | {
125
142
println ! ( "pub2-s2 I got {:?}" , x) ;
126
- } ) ) ) ;
143
+ } ) ;
127
144
128
145
{
129
146
let h = & mut _h. lock ( ) . unwrap ( ) ;
0 commit comments