Skip to content

Commit

Permalink
AsObservable
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 22, 2023
1 parent a1519c0 commit b48113d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
26 changes: 2 additions & 24 deletions src/R3/Observable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace R3;

public abstract class Observable<T> : IObservable<T>
public abstract class Observable<T>
{
[StackTraceHidden, DebuggerStepThrough]
public IDisposable Subscribe(Observer<T> observer)
Expand All @@ -29,14 +29,9 @@ public IDisposable Subscribe(Observer<T> observer)
}

protected abstract IDisposable SubscribeCore(Observer<T> observer);

IDisposable IObservable<T>.Subscribe(IObserver<T> observer)
{
return Subscribe(observer.ToObserver()); // convert IObserver<T> to Observer<T>
}
}

public abstract class Observer<T> : IDisposable, IObserver<T>
public abstract class Observer<T> : IDisposable
{
#if DEBUG
[Obsolete("Only allow in Event<T>.")]
Expand Down Expand Up @@ -122,21 +117,4 @@ public void Dispose()

[StackTraceHidden, DebuggerStepThrough]
protected virtual void DisposeCore() { }

// IObserver<T> bridge

void IObserver<T>.OnNext(T value)
{
OnNext(value);
}

void IObserver<T>.OnError(Exception error)
{
OnCompleted(Result.Failure(error));
}

void IObserver<T>.OnCompleted()
{
OnCompleted(Result.Success);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
public static partial class ObservableExtensions
{
// TODO: more overload?
public static IObservable<T> ToIObservable<T>(this Observable<T> source)
public static IObservable<T> AsObservable<T>(this Observable<T> source)
{
return new ToIObservable<T>(source);
return new AsObservable<T>(source);
}
}

internal sealed class ToIObservable<T>(Observable<T> source) : IObservable<T>
internal sealed class AsObservable<T>(Observable<T> source) : IObservable<T>
{
public IDisposable Subscribe(IObserver<T> observer)
{
return source.Subscribe(new ObserverToobserver(observer));
return source.Subscribe(new ObserverToObserver(observer));
}

sealed class ObserverToobserver(IObserver<T> observer) : Observer<T>
sealed class ObserverToObserver(IObserver<T> observer) : Observer<T>
{
protected override void OnNextCore(T value)
{
Expand Down

0 comments on commit b48113d

Please sign in to comment.