Multiple subscriptions

I’m trying to setup multiple subscriptions that are dynamic.

In my main app subscriptions as:

        let main_subscription = event::listen().map(MainMessage::IcedEvent);
        let gesture_subscription = self.gesture_handler.subscription();
        Subscription::batch(vec![main_subscription, gesture_subscription]) // <- gesture subscription not liked....

and helper.subscription is like so:

    fn subscription(&self) -> Subscription<Instant> {
        let tick = match self.timer_enabled {
            true => {
                time::every(Duration::from_millis(100)).map(|_| self.update()) // every function not found in iced::time?!
            }, 
            false => Subscription::none(),
        };
        tick
    }

How can i set this up?
That way the helper can toggle on and off its own update()

im using the example from : Subscription in iced - Rust so the return type on the function of the child is different…

pub fn subscription(&self) -> Subscription<MainMessage> {
    Subscription::batch(vec![
        event::listen().map(MainMessage::IcedEvent),
        helper.subscription().map(MainMessage::Helper)
    ])
}

Also MainMessage sounds fishy. It’s “best practice” to only have one Message in each module. I would rename MainMessage as Message and just use helper::Message to refer to the helper variant.

// main.rs
pub enum Message {
    IcedEvent(iced::Event),
    Helper(helper::Message),

    // your other variants...
}

thanks, ive just updated my comment as i just found that!
I have some other issues.

The function types are not matching. Can i call a function directly, or does it all need to go through the main app update message system?

Use .map() like I did in my comment. Values returned by children functions are generally mapped to a variant in the equivalent parent enum.

You will generally .map() your children’s view (the returning Element), update (the returning Task<Message>) and subscription (the returning Subscription) functions.

ok thanks. its almost there…

the last part… time::every function from the example is not found. Is there another way to do this now?

Edit found the feature its behind!

1 Like