Skip to content

Commit

Permalink
Merge pull request #1626 from dotnet/DelaySelectorFix
Browse files Browse the repository at this point in the history
  • Loading branch information
clairernovotny committed Oct 27, 2021
2 parents 1ff45fb + bf87332 commit ddf3537
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
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

0 comments on commit ddf3537

Please sign in to comment.