Skip to content

Commit 76016ff

Browse files
committed
Implement publisher.map()
1 parent 97009d0 commit 76016ff

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fp_rust"
3-
version = "0.1.21"
3+
version = "0.1.22"
44
license = "MIT"
55
authors = ["JunYi JohnTeee Lee <[email protected]>"]
66
include = ["src/**/*.rs", "Cargo.toml"]

publish.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cargo publish && git push && git push --tag

src/publisher.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,29 @@ impl<X: Send + Sync + 'static + Clone> Publisher<X, SubscriptionFunc<X>> {
3131
self.notify_observers(Arc::new(val));
3232
}
3333

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
3637
}
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>> {
3842
self.subscribe(Arc::new(SubscriptionFunc::new(func)))
3943
}
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+
}
4057

4158
pub fn subscribe_on(&mut self, h: Option<Arc<Mutex<Handler + 'static>>>) {
4259
self.sub_handler = h;
@@ -121,9 +138,9 @@ fn test_publisher_new() {
121138
latch2.countdown();
122139
}));
123140
pub2.subscribe(s);
124-
pub2.subscribe(Arc::new(SubscriptionFunc::new(move |x: Arc<String>| {
141+
pub2.map(move |x: Arc<String>| {
125142
println!("pub2-s2 I got {:?}", x);
126-
})));
143+
});
127144

128145
{
129146
let h = &mut _h.lock().unwrap();

0 commit comments

Comments
 (0)