Skip to content

Commit a6c6eb1

Browse files
committed
x-bow: make for_each run forever
1 parent a845f8d commit a6c6eb1

File tree

4 files changed

+27
-32
lines changed

4 files changed

+27
-32
lines changed

x-bow/src/path_ext.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub trait PathExt: Path {
245245
/// # use x_bow::{Path, PathExt};
246246
/// # use std::cell::Ref;
247247
/// # async fn example(path: &impl Path) {
248-
/// let stream = path.signal_stream();
248+
/// let stream = path.signal_stream(true);
249249
/// // is equivalent to
250250
/// let stream = futures_lite::stream::once(()) // fire immediately in the beginning...
251251
/// .chain(path.until_change()) // and after every change
@@ -262,8 +262,7 @@ pub trait PathExt: Path {
262262
/// Execute the given function with the data as argument. Repeat every time
263263
/// the data changes.
264264
///
265-
/// The return future finishes when the data couldn't be accessed
266-
/// (when [borrow_opt][PathExt::borrow_opt] returns None).
265+
/// The return future never finishes.
267266
///
268267
/// ```
269268
/// # use x_bow::{Path, PathExt};
@@ -294,8 +293,7 @@ pub trait PathExt: Path {
294293
/// the execution is canceled (by dropping the Future) so that the new
295294
/// one can start.
296295
///
297-
/// The return future finishes when the data couldn't be accessed
298-
/// (when [borrow_opt][PathExt::borrow_opt] returns None).
296+
/// The return future never finishes.
299297
///
300298
/// ```
301299
/// # use x_bow::{Path, PathExt};
@@ -308,9 +306,12 @@ pub trait PathExt: Path {
308306
/// # todo!();
309307
/// # }
310308
/// // when the URL changes, fetch the content and set the text
311-
/// path_to_url.for_each_async(|url: &String| async move {
312-
/// let content = fetch_content(url).await;
313-
/// ui_element.set_Text(&content);
309+
/// path_to_url.for_each_async(|url: &String| {
310+
/// let url = url.clone();
311+
/// async move {
312+
/// let content = fetch_content(&url).await;
313+
/// ui_element.set_text(&content);
314+
/// }
314315
/// }).await;
315316
/// # }
316317
/// ```
@@ -331,10 +332,7 @@ pub trait PathExt: Path {
331332
/// the data will be updated to that value. The `on_change` function
332333
/// won't be called for changes that come in this way.
333334
///
334-
/// The future finishes when either
335-
/// * the `incoming_changes` stream yield None
336-
/// * the data couldn't be accessed
337-
/// ([borrow_opt][PathExt::borrow_opt] returns None)
335+
/// The future finishes when the `incoming_changes` stream yield None.
338336
///
339337
/// ```
340338
/// # use x_bow::{Path, PathExt};

x-bow/src/path_ext/bind_for_each.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,33 @@ where
3838
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
3939
let mut this = self.project();
4040

41-
match this.incoming.as_mut().poll_next(cx) {
42-
Poll::Ready(Some(val)) => {
43-
if let Some(bm) = this.path.borrow_opt_mut().as_deref_mut() {
44-
*bm = val;
45-
} else {
41+
let mut val = None;
42+
loop {
43+
match this.incoming.as_mut().poll_next(cx) {
44+
Poll::Ready(Some(v)) => {
45+
val = Some(v);
46+
}
47+
Poll::Ready(None) => {
4648
return Poll::Ready(());
4749
}
48-
let _ = this.until_change.as_mut().poll_next(cx);
50+
Poll::Pending => break,
4951
}
50-
Poll::Ready(None) => {
52+
}
53+
if let Some(val) = val {
54+
if let Some(bm) = this.path.borrow_opt_mut().as_deref_mut() {
55+
*bm = val;
56+
} else {
5157
return Poll::Ready(());
5258
}
53-
Poll::Pending => {}
59+
let _ = this.until_change.as_mut().poll_next(cx);
5460
}
61+
5562
let first = !this.until_change.has_been_polled();
5663
if first | this.until_change.as_mut().poll_next(cx).is_ready() {
5764
if let Some(data) = this.path.borrow_opt().as_deref() {
5865
(this.closure)(data);
59-
Poll::Pending
60-
} else {
61-
Poll::Ready(())
6266
}
63-
} else {
64-
Poll::Pending
6567
}
68+
Poll::Pending
6669
}
6770
}

x-bow/src/path_ext/for_each.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@ impl<'a, P: Path + ?Sized, C: FnMut(&P::Out) + Unpin> Future for ForEach<'a, P,
3030
if first | Pin::new(&mut this.until_change).poll_next(cx).is_ready() {
3131
if let Some(data) = this.path.borrow_opt().as_deref() {
3232
(this.closure)(data);
33-
Poll::Pending
34-
} else {
35-
Poll::Ready(())
3633
}
37-
} else {
38-
Poll::Pending
3934
}
35+
Poll::Pending
4036
}
4137
}

x-bow/src/path_ext/for_each_async.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ impl<'a, P: Path + ?Sized, F: Future<Output = ()>, C: FnMut(&P::Out) -> F + Unpi
4444
if let Some(data) = this.path.borrow_opt().as_deref() {
4545
let fut = (this.closure)(data);
4646
this.future.set(Some(fut));
47-
} else {
48-
return Poll::Ready(());
4947
}
5048
}
5149
if let Some(fut) = this.future.as_mut().as_pin_mut() {

0 commit comments

Comments
 (0)