-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom "Disposable" from create #196
Comments
There isn't a trait like |
If I understand it correctly in Rx For example in the Rx documentation for create there's this example: var ob = Observable.Create<string>(
observer =>
{
var timer = new System.Timers.Timer();
timer.Enabled = true;
timer.Interval = 100;
timer.Elapsed += OnTimerElapsed;
timer.Start();
return ()=>{
timer.Elapsed -= OnTimerElapsed;
timer.Dispose();
};
}); Which returns an IDisposable that when disposed removes the event delegate from the timer and disposes the timer. A very naive implementation in rxRust could be something like: let ob = observable::create(|observer| {
let mut i = 0;
loop {
std::thread::sleep(Duration::from_millis(100));
observer.next(i);
i += 1;
}
});
let pool = ThreadPool::new().unwrap();
let mut subs = ob.into_shared().subscribe_on(pool).into_shared().subscribe(|v| println!("threaded timer {v}"));
std::thread::sleep(Duration::from_secs(1));
subs.unsubscribe(); But unsubscribe doesn't really finish the subscription as there's no way to notify the observable that the timer should be stopped. I would have expected the equivalent rust code could return a |
In rxRust the infinite loop that block you unsubscribe. There are some differences between Rust with C#, Rust not have a default event loop, so you can't create a timer with callback like that, but you can use |
I think @arturoc is right. The API does feel incomplete for create. pub fn create<F, Item, Err>(subscribe: F) -> ObservableFn<F, Item, Err>
where
F: FnOnce(&mut dyn Observer<Item = Item, Err = Err>),
{
ObservableFn(subscribe, TypeHint::new())
} It seems to me that create should probably have this API instead: pub fn create<F, Sub, Item, Err>(subscribe: F) -> ObservableFn<F, Item, Err>
where
F: FnOnce(&mut dyn Observer<Item = Item, Err = Err>) -> Sub,
Sub: SubscriptionLike
{
ObservableFn(subscribe, TypeHint::new())
} This way, whenever you subscribe to an observable, you should get a So a rough example might look like: let obs = observable::create(|mut subscriber| {
let timer_handle = five_min_callback_timer(|| {
subscriber.next(0);
subscriber.complete();
});
create_subscription(move || {
// Any custom cleanup code can go here.
// In this example, that's cleaning up a timer.
drop(timer_handle);
})
})
let sub = obs.subscribe(|v| println!("{:?}", v));
// ... some time later
// Clean up resources, the user defined SubscriptionLike should be invoked at some point here
sub.unsubscribe();
// By this line, `handle` has been consumed |
@arturoc good to me. And we can let |
I don't have the time to send a PR right now but what @Trequetrum posts is what I was talking about. I think implementing |
When creating a new
Observable
usingcreate
the original c# implementation allows to return a custom IDisposable so one can have more control over the completion of the Observable. With rxRust there's apparently no way to do that?The text was updated successfully, but these errors were encountered: