Skip to content
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

Change subscription::run to take a function pointer #1723

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions native/src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,24 @@ where
})
}

/// Returns a [`Subscription`] that will call the given function to create and
/// asynchronously run the given [`Stream`].
pub fn run<S, Message>(builder: fn() -> S) -> Subscription<Message>
where
S: Stream<Item = Message> + MaybeSend + 'static,
Message: 'static,
{
Subscription::from_recipe(Runner {
id: builder,
spawn: move |_| builder(),
})
}

/// Returns a [`Subscription`] that will create and asynchronously run the
/// given [`Stream`].
///
/// The `id` will be used to uniquely identify the [`Subscription`].
pub fn run<I, S, Message>(id: I, stream: S) -> Subscription<Message>
pub fn run_with_id<I, S, Message>(id: I, stream: S) -> Subscription<Message>
where
I: Hash + 'static,
S: Stream<Item = Message> + MaybeSend + 'static,
Expand Down Expand Up @@ -199,7 +212,7 @@ where
use futures::future::{self, FutureExt};
use futures::stream::StreamExt;

run(
run_with_id(
id,
futures::stream::unfold(initial, move |state| f(state).map(Some))
.filter_map(future::ready),
Expand Down