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

Improve flexibility of message sending API #999

Merged
merged 14 commits into from
Mar 14, 2020

Conversation

captain-yossarian
Copy link
Contributor

Related to #932

@captain-yossarian captain-yossarian changed the title Improve flexibility of message sending API [wip]Improve flexibility of message sending API Mar 4, 2020
@captain-yossarian captain-yossarian changed the title [wip]Improve flexibility of message sending API Improve flexibility of message sending API Mar 7, 2020
@captain-yossarian
Copy link
Contributor Author

captain-yossarian commented Mar 7, 2020

@jstarry Could you please take a look?
I'm stuck here

self.update(ComponentUpdate::MessageBatch(messages));
pub fn send_message_batch<T>(&self, messages: T)
where
T: Into<Vec<COMP::Message>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out this link: https://rust-lang.github.io/api-guidelines/flexibility.html#functions-minimize-assumptions-about-parameters-by-using-generics-c-generic

Let's use I: IntoIterator<Item = COMP::Message> for the batch methods

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jstarry Thanks for the help!

@jstarry
Copy link
Member

jstarry commented Mar 8, 2020

In examples/dashboard/src/lib.rs you can drop the .into() because your new api changes makes that unnecessary. I think that will get rid of the errors.

Instead of

 onclick=self.link.callback(|_| WsAction::Connect.into())>

do

 onclick=self.link.callback(|_| WsAction::Connect)>

@jstarry
Copy link
Member

jstarry commented Mar 8, 2020

in src/services/fetch/std_web.rs there's a failing doc test. The issue is that Rust can't tell what the closure return type should be because the closure is "unimplemented":

    ///# let callback = link.callback(|response: Response<Result<String, anyhow::Error>>| unimplemented!());

You can fix that by adding a return type to the closure like this:

    ///# let callback = link.callback(|response: Response<Result<String, anyhow::Error>>| -> Msg { unimplemented!() });

You can run the doc tests like this:

cargo test --doc --features doc_test,wasm_test,yaml,msgpack,cbor,std_web

@captain-yossarian
Copy link
Contributor Author

@jstarry Thank you very much, I'm appreciate it!

@captain-yossarian
Copy link
Contributor Author

@jstarry Tests are passed. Could you please check the types?

src/html/scope.rs Outdated Show resolved Hide resolved
@@ -133,9 +139,10 @@ impl<COMP: Component> Scope<COMP> {

/// This method creates a `Callback` which will send a batch of messages back to the linked
/// component's update method when called.
pub fn batch_callback<F, IN>(&self, function: F) -> Callback<IN>
pub fn batch_callback<F, IN, M: Into<Vec<COMP::Message>>>(&self, function: F) -> Callback<IN>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, here two trait bounds, only need one

/// Send a batch of messages to the component
pub fn send_message_batch(&self, messages: Vec<COMP::Message>) {
self.update(ComponentUpdate::MessageBatch(messages));
pub fn send_message_batch<I>(&self, messages: I)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I don't think this change helps the API. It actually goes against the guidelines: https://rust-lang.github.io/api-guidelines/flexibility.html#caller-decides-where-to-copy-and-place-data-c-caller-control

Since we require ownership over the Vec<COMP::Message> we should have that be the method argument, otherwise we may unnecessarily allocate.

Please revert these changes and the changes to callback_batch. Sorry!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, should I keep IntoIterator + Into?
If no, could you please specify more precisely?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like all changes in this PR reverted for send_message_batch and callback_batch

@jstarry jstarry merged commit ed1bedc into yewstack:master Mar 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants