Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Easy timeout/setInterval #131

Closed
theduke opened this issue Jun 7, 2019 · 3 comments · Fixed by #380
Closed

Easy timeout/setInterval #131

theduke opened this issue Jun 7, 2019 · 3 comments · Fixed by #380
Labels
API design Proposal for a new or updated Seed API.

Comments

@theduke
Copy link

theduke commented Jun 7, 2019

It would be great to have a easy way to schedule timeouts and intervals.

Something like:

orders.interval(1000, Msg::Tick);
orders.timeout(500, Msg::Delayed);

As mentioned in the futures cancellation issue, it would be great to return a drop guard that clears the timeout/interval when dropped.

For inspiration, draco does it this way.

@MartinKavik
Copy link
Member

MartinKavik commented Jun 7, 2019

interval seems to me more like a subscription than order/cmd. So I think it would make more sense to use it like e.g. keyboard events - in .window_events(..) (or a new concept like .subscriptions(..) which will be like window_events(..) , but more general).
There are more subscriptions which we can implement (e.g. OnRequestAnimationFrame, OnWebsocketMesssage, etc.) so I think it isn't good idea to implement interval as Orders's method because it will be mess in the future and it's not very flexible.

Alternative can be also something like this:

orders.subscribe(Sub::interval(1000, Msg::Tick));

timeout makes more sense imho, but we can reuse existing Api and be prepared for more similar cases - i.e. something like:

orders.perform_cmd(Cmd::timeout(500, Msg::Delayed));

So it's clear on the first look that it's a command and we can easily add more commands in the future.

@tiberiusferreira
Copy link
Contributor

tiberiusferreira commented Oct 11, 2019

Is there a way to use stream now? I wanted to make an API call each X seconds. I tried to use


fn fetch_data() -> impl Future<Item = Msg, Error = Msg> {
    let url = "https://api.github.com/repos/david-oconnor/seed/branches/master";
    Request::new(url).fetch_json_data(Msg::DataFetched)
}

fn init(_: Url, orders: &mut impl Orders<Msg>) -> Init<Model> {
    orders
        .send_msg(Msg::StartGeoTracking);


    let (app, msg_mapper) = (orders.clone_app(), orders.msg_mapper());

    let strm = IntervalStream::new(1000)
        .map(
            move |_|{
            fetch_data()
        }
        )
        .then(
            |_| Ok(Msg::Listening)
        );
    orders.perform_cmd(strm);

    let map = initMap();
    Init::new(Model{
        curr_lat: None,
        curr_longi: None,
        markers: vec![],
        map
    })

}

But I get:

 the trait `futures::future::Future` is not implemented for `futures::stream::then::Then<futures::stream::map::Map<gloo_timers::future::IntervalStream, [closure@src/lib.rs:167:14: 169:10]>, [closure@src/lib.rs:170:12: 170:34], std::result::Result<Msg, _>>`

Is there a workaround for now?

@MartinKavik
Copy link
Member

    let stream = IntervalStream::new(1_000).for_each(move |_| {
        let condition = true;
        if !condition {
            return Err(())
        }
        app.update(msg_mapper(Msg::DoSomething));
        Ok(())
    })
        .then(|_| Ok(Msg::Finished));

    orders.perform_cmd(stream);

We should return back to this issue once async/await is stable and libraries like reqwest are ready so we can try more real-world cases.

@MartinKavik MartinKavik mentioned this issue Mar 7, 2020
@MartinKavik MartinKavik added this to the 2. Stable basic Seed API milestone Mar 9, 2020
@MartinKavik MartinKavik added the API design Proposal for a new or updated Seed API. label Mar 9, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
API design Proposal for a new or updated Seed API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants