Improve flexibility of message sending API (#999)

* init

* minimum update

* update batch_callback

* update callback

* trigger CI/CD

* Add IntoIter trait

* update

* Fix docs

* update tests

* update examples

* remove into from example

* format

* revert changes

* formatt
This commit is contained in:
yossarian 2020-03-14 04:23:41 +01:00 committed by GitHub
parent 2778d11cc0
commit ed1bedc770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 12 deletions

View File

@ -201,19 +201,19 @@ impl Component for Model {
</button>
{ self.view_data() }
<button disabled=self.ws.is_some()
onclick=self.link.callback(|_| WsAction::Connect.into())>
onclick=self.link.callback(|_| WsAction::Connect)>
{ "Connect To WebSocket" }
</button>
<button disabled=self.ws.is_none()
onclick=self.link.callback(|_| WsAction::SendData(false).into())>
onclick=self.link.callback(|_| WsAction::SendData(false))>
{ "Send To WebSocket" }
</button>
<button disabled=self.ws.is_none()
onclick=self.link.callback(|_| WsAction::SendData(true).into())>
onclick=self.link.callback(|_| WsAction::SendData(true))>
{ "Send To WebSocket [binary]" }
</button>
<button disabled=self.ws.is_none()
onclick=self.link.callback(|_| WsAction::Disconnect.into())>
onclick=self.link.callback(|_| WsAction::Disconnect)>
{ "Close WebSocket connection" }
</button>
</nav>

View File

@ -108,10 +108,12 @@ impl<COMP: Component> Scope<COMP> {
}
/// Send a message to the component
pub fn send_message(&self, msg: COMP::Message) {
self.update(ComponentUpdate::Message(msg));
pub fn send_message<T>(&self, msg: T)
where
T: Into<COMP::Message>,
{
self.update(ComponentUpdate::Message(msg.into()));
}
/// Send a batch of messages to the component
pub fn send_message_batch(&self, messages: Vec<COMP::Message>) {
self.update(ComponentUpdate::MessageBatch(messages));
@ -119,9 +121,10 @@ impl<COMP: Component> Scope<COMP> {
/// This method creates a `Callback` which will send a message to the linked component's
/// update method when invoked.
pub fn callback<F, IN>(&self, function: F) -> Callback<IN>
pub fn callback<F, IN, M>(&self, function: F) -> Callback<IN>
where
F: Fn(IN) -> COMP::Message + 'static,
M: Into<COMP::Message>,
F: Fn(IN) -> M + 'static,
{
let scope = self.clone();
let closure = move |input| {

View File

@ -291,10 +291,10 @@ impl FetchService {
///# fn update(&mut self, msg: Self::Message) -> bool {unimplemented!()}
///# fn view(&self) -> Html {unimplemented!()}
///# }
///# pub enum Msg {}
///# pub enum Msg { }
///# fn dont_execute() {
///# let link: ComponentLink<Comp> = unimplemented!();
///# let callback = link.callback(|response: Response<Result<String, anyhow::Error>>| unimplemented!());
///# let callback = link.callback(|response: Response<Result<String, anyhow::Error>>| -> Msg { unimplemented!() });
/// let request = fetch::Request::get("/path/")
/// .body(Nothing)
/// .unwrap();

View File

@ -298,7 +298,7 @@ impl FetchService {
///# pub enum Msg {}
///# fn dont_execute() {
///# let link: ComponentLink<Comp> = unimplemented!();
///# let callback = link.callback(|response: Response<Result<String, Error>>| unimplemented!());
///# let callback = link.callback(|response: Response<Result<String, Error>>| -> Msg { unimplemented!() });
/// let request = fetch::Request::get("/path/")
/// .body(Nothing)
/// .unwrap();