Let's say, we have a class:
public class Foo
{
public string Do(int param)
{
}
}
I'd like to create an observable of values that are being produced by Do method. One way to do it would be to create an event which is being called from Do and use Observable.FromEvent to create the observable. But somehow I don't feel good about creation of an event just for the sake of the task. Is there a better way to do it?
You can use Observable. defer instead. It accepts a function that returns an Observable or an Observable-like thing (read: Promise, Array, Iterators).
There are two main methods to create Observables in RxJS. Subjects and Operators. We will take a look at both of these!
Matt's answer made me thinking about this:
public class Foo
{
private readonly Subject<string> _doValues = new Subject<string>();
public IObservable<string> DoValues { get { return _doValues; } }
public string Do(int param)
{
var ret = (param * 2).ToString();
_doValues.OnNext(ret);
return ret;
}
}
var foo = new Foo();
foo.DoValues.Subscribe(Console.WriteLine);
foo.Do(2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With