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
8 changes: 4 additions & 4 deletions examples/dashboard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
13 changes: 8 additions & 5 deletions src/html/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,23 @@ 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));
}

/// 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| {
Expand Down
4 changes: 2 additions & 2 deletions src/services/fetch/std_web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/services/fetch/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down