mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
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:
parent
2778d11cc0
commit
ed1bedc770
@ -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>
|
||||
|
||||
@ -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| {
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user