Pauan / rust-signals

Zero-cost functional reactive Signals for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BroadcasterSignal.second()

alanpoon opened this issue · comments

BroadcasterSignal.first() returns the initial value of the Mutable. However, I want to return the first changed value of the Mutable. I can't use BroadcasterSignal.for_each() or BroadcasterSignal.map as they do not terminate. It would be good also to be able to pass comparsion function to end the poll.

commented

I'm curious what your use case is, why do you need the first changed value, and why do you only need 1 value?

Here is my use case scs/substrate-api-client#48. I am doing async await on websocket hence i only need one value, if not it will await forever. I need to pass the websocket result to node module and to be used in react.

commented

I took a look at your code, and it seems you aren't using Signals properly. A Signal is supposed to be for a value which changes over time.

So for example, you might have a Mutable<Time> which changes every hour. Then you can listen for those changes. So you should think of it as being similar to events. The point is that the value is changing, it isn't a single value.

But in your case, you just want to retrieve a single value. So you should use oneshot::channel instead, like this:

async fn _get_request(url: String, jsonreq: String) -> Result<String, &'static str> {
    let (sender, receiver) = oneshot::channel();

    rpc::get(url, jsonreq, sender);

    let result = receiver.await.unwrap();

    result
}

This will be more efficient than using Signals, and it also matches with what you're trying to do.