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

channel helper for subscription #1786

Merged
merged 3 commits into from
Apr 11, 2023
Merged

channel helper for subscription #1786

merged 3 commits into from
Apr 11, 2023

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Apr 11, 2023

This PR implements a channel helper function to create a Subscription.

The channel function signature looks like this:

pub fn channel<I, Fut, Message>(
    id: I,
    size: usize,
    f: impl Fn(mpsc::Sender<Message>) -> Fut + MaybeSend + Sync + 'static,
) -> Subscription<Message>
where
    I: Hash + 'static,
    Fut: Future<Output = Never> + MaybeSend + 'static,
    Message: 'static + MaybeSend,

The helper creates an mpsc channel with the given size and provides the Sender end to the closure f. The resulting Future will be run concurrently and the Subscription will produce the messages published to the Sender.

Notice how the Future produces a Never type. This effectively means the Future can never finish by itself. It can only be terminated by the Application.

This is a simpler version of unfold where the future can simply run indefinitely with a loop while publishing to the Sender instead of continuously returning to produce messages.

The unfold helper has also been changed to always produce a Message when returning, as opposed to allowing None to only change state. If state changes are necessary without producing output, the channel helper is recommended.

Closes #1553.

@hecrj hecrj added the feature New feature or request label Apr 11, 2023
@hecrj hecrj added this to the 0.9.0 milestone Apr 11, 2023
@hecrj hecrj merged commit ca828f0 into master Apr 11, 2023
@hecrj hecrj deleted the feature/subscription-channel branch April 11, 2023 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request subscription
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant