Hello!
I was trying to implement a subscription allowing for two-way communication as described in the docs.
In my attempt, the main application implementation creates a subscription that sends a message “Ready” along with a Sender<Order>
transmitter to send follow up requests. That message is supposed to be received in the update
function.
However, the message never appears. A stripped-down sample of my code is shown below:
use std::{
any::TypeId,
sync::mpsc::{self, Receiver, Sender},
};
use iced::{
executor, subscription,
widget::{column, text},
Application, Command, Theme,
};
struct Viewer;
enum Order {}
#[derive(Clone, Debug)]
enum Message {
SubscriptionReady(Sender<Order>),
}
impl Application for Viewer {
type Executor = executor::Default;
type Message = Message;
type Theme = Theme;
type Flags = ();
fn new(_: Self::Flags) -> (Self, iced::Command<Self::Message>) {
return (Viewer, Command::none());
}
fn title(&self) -> String {
return "Subscription confusion".to_string();
}
fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
match message {
Message::SubscriptionReady(_) => println!("Subscription ready!"), // NEVER PRINTED
};
Command::none()
}
fn view(&self) -> iced::Element<'_, Self::Message, Self::Theme, iced::Renderer> {
column![text("Hello, world.")].into()
}
fn subscription(&self) -> iced::Subscription<Self::Message> {
struct Worker;
enum State {
Starting,
Ready(Receiver<Order>),
}
subscription::channel(TypeId::of::<Worker>(), 10, |mut output| async move {
let mut state = State::Starting;
loop {
match state {
State::Starting => {
let (tx, rx) = mpsc::channel();
output.try_send(Message::SubscriptionReady(tx)).unwrap();
state = State::Ready(rx);
}
State::Ready(_) => {
// calculations...
}
}
}
})
}
}
fn main() {
Viewer::run(iced::Settings::default()).unwrap();
}
How can I fix this issue?
Thanks!