Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix duplicate emission in Delay with immediate selector #1626

Merged
merged 1 commit into from
Oct 27, 2021
Merged
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
24 changes: 16 additions & 8 deletions Rx.NET/Source/src/System.Reactive/Linq/Observable/Delay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ private sealed class DelayObserver : SafeObserver<TDelay>
{
private readonly _ _parent;
private readonly TSource _value;
private bool _once;

public DelayObserver(_ parent, TSource value)
{
Expand All @@ -690,12 +691,16 @@ public DelayObserver(_ parent, TSource value)

public override void OnNext(TDelay value)
{
lock (_parent._gate)
if (!_once)
{
_parent.ForwardOnNext(_value);
_once = true;
lock (_parent._gate)
{
_parent.ForwardOnNext(_value);

_parent._delays.Remove(this);
_parent.CheckDone();
_parent._delays.Remove(this);
_parent.CheckDone();
}
}
}

Expand All @@ -709,12 +714,15 @@ public override void OnError(Exception error)

public override void OnCompleted()
{
lock (_parent._gate)
if (!_once)
{
_parent.ForwardOnNext(_value);
lock (_parent._gate)
{
_parent.ForwardOnNext(_value);

_parent._delays.Remove(this);
_parent.CheckDone();
_parent._delays.Remove(this);
_parent.CheckDone();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,18 @@ public void Delay_Duration_SelectorThrows2()
);
}

[Fact]
public void Delay_Duration_Selector_Immediately()
{
var list = new List<int>();

Observable.Range(1, 5)
.Delay(_ => Observable.Return(1))
.Subscribe(list.Add);

Assert.Equal(new List<int>() { 1, 2, 3, 4, 5 }, list);
}

[Fact]
public void Delay_Duration_InnerDone()
{
Expand Down