-
Notifications
You must be signed in to change notification settings - Fork 170
Description
Motivated by the use case of a community member, we can consider providing an API to take messages directly from a subscription instead of passing each message into a callback one at a time.
This may be useful for cases where a user would rather poll for new messages at a predefined rate, with no immediate action being needed when a message arrives.
I'd propose these two APIs for the sake of convenience and ergonomics without loss of generality (I'm very open to changing the names of structs and functions:
impl<Message> SubscriptionTaker<Message> {
pub fn take_with(&self, &mut msg: Message) -> Result<(), _> { ... }
pub fn take(&self) -> Result<Message, _> {
let mut msg = Message::default();
let r = self.take_with(&mut msg);
r.map(|_| msg)
}
}
impl<Message> LatestSubscriptionMessage<Message> {
pub fn get(&mut self) -> &mut Message { ... }
}
The LatestMessage
struct would be a light wrapper around SubscriptionTaker
which is specialized for the case where you only need the most recent message. It lets the user define an initial message value and then each time you call .get()
it will attempt to update the message. Whether or not there was an update, it will provide a borrow of the most recent message value. The same message instance will be recycled as new messages come in.
The more general SubscriptionTaker::take_with
will allow users to specify a message instance to pass the new message data into, if any new data is available. If there is no new message data, this will return an Err.
Maintainers can feel free to assign this ticket to me.