Skip to content

Fix: AsObservable immediately calls Dispose on completion #331

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/R3/Operators/AsObservable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,32 @@ internal sealed class AsObservable<T>(Observable<T> observable) : Observable<T>
{
protected override IDisposable SubscribeCore(Observer<T> observer)
{
return observable.Subscribe(observer.Wrap());
return observable.Subscribe(new AsObservableObserver(observer));
}

sealed class AsObservableObserver(Observer<T> observer) : Observer<T>
{
protected override bool AutoDisposeOnCompleted => false;

protected override void OnNextCore(T value)
{
observer.OnNext(value);
}

protected override void OnErrorResumeCore(Exception error)
{
observer.OnErrorResume(error);
}

protected override void OnCompletedCore(Result result)
{
observer.OnCompleted(result);
}

protected override void DisposeCore()
{
observer.Dispose();
}
}
}

Expand All @@ -35,6 +60,8 @@ public IDisposable Subscribe(IObserver<T> observer)

sealed class ObserverToObserver(IObserver<T> observer) : Observer<T>
{
protected override bool AutoDisposeOnCompleted => false;

protected override void OnNextCore(T value)
{
observer.OnNext(value);
Expand Down
18 changes: 17 additions & 1 deletion tests/R3.Tests/OperatorTests/AsObservableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ public void AsObservable()
}

[Fact]
public void AsSystemObservable()
public void AsObservableWithDelay()
{
var p = new Subject<int>();
var fakeFrameProvider = new FakeFrameProvider();

var l = p.AsObservable().DelayFrame(1, fakeFrameProvider).ToLiveList();
p.OnNext(1);
p.OnNext(2);
p.OnNext(3);
p.OnCompleted();
fakeFrameProvider.Advance();

l.AssertEqual([1, 2, 3]);
l.AssertIsCompleted();
}

[Fact]
public void AsSystemObservable()
{
{
var p = new Subject<int>();
var l = new List<int>();
Expand Down