Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

BroadcasterSignal.second() #14

Closed
alanpoon opened this issue Dec 16, 2019 · 3 comments · May be fixed by scs/substrate-api-client#48
Closed

BroadcasterSignal.second() #14

alanpoon opened this issue Dec 16, 2019 · 3 comments · May be fixed by scs/substrate-api-client#48

Comments

@alanpoon
Copy link

alanpoon commented Dec 16, 2019

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.

@Pauan
Copy link
Owner

Pauan commented Dec 17, 2019

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

@alanpoon
Copy link
Author

alanpoon commented Dec 17, 2019

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.

@Pauan
Copy link
Owner

Pauan commented Jan 5, 2020

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.

@Pauan Pauan closed this as completed Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants