.net - What is the IObservable.Subscribe(IObserver<T> observer) overload for? -
when writing .subscribe
expressions find resharper has chosen following overload me, located in mscorlib, version=4.0.0.0:
namespace system { public interface iobservable<out t> { idisposable subscribe(iobserver<t> observer); } }
this seems different overloads take action
, it's coming mscorlib , not system.reactive.*
expect of reactive stuff be.
what overload do? how should used? how iobserver<t>
relate action
? , why single overload in mscorlib?
to clear, isn't overload, that's core of rx really. other subscribe
methods, , other operators matter, you're used extension methods call that.
if @ documentation or rx, you'll see creators viewed push-based side of linq. lot of things mirror image of see in linq. iobservable
mirror of ienumerable
, , iobserver
mirror of ienumerator
.
however, because push opposite of pull, rx versions opposite of pull-based counterpart:
ienumerable
defines 1 method producesienumerator
.iobservable
defines 1 method takes iniobserver
.- if think of
ienumerator.movenext()
+ienumerator.current
1 operation, can return in 1 of 3 ways: next element returned, end of collection reached, or exception thrown. similarly,iobserver
has handle 3 cases: next element (onnext
), end of stream (oncompleted
), or exception (onerror
).
the more familiar 'overloads' of subscribe
extension methods this:
public static idisposable subscribe<t>(this iobservable<t> source, action<t> onnext) { return subscribe(onnext, e => {/*onerror */}, () => {/*oncompleted*/); } public static idisposable subscribe<t>(this iobservable<t> source, action<t> onnext, action<exception> onerror, action oncompleted) { source.subscribe(new anonymousobserver<t>(onnext, onerror, oncompleted)); }
Comments
Post a Comment