-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
namespace R3 | ||
{ | ||
public static partial class EventFactory | ||
{ | ||
public static CompletableEvent<TMessage, Unit> Empty<TMessage>() | ||
{ | ||
return new Empty<TMessage>(); | ||
} | ||
|
||
public static CompletableEvent<TMessage, Unit> Empty<TMessage>(TimeProvider timeProvider) | ||
{ | ||
return Empty<TMessage>(TimeSpan.Zero, timeProvider); | ||
} | ||
|
||
public static CompletableEvent<TMessage, Unit> Empty<TMessage>(TimeSpan dueTime, TimeProvider timeProvider) | ||
{ | ||
return new EmptyT<TMessage>(dueTime, timeProvider); | ||
} | ||
} | ||
} | ||
|
||
namespace R3.Factories | ||
{ | ||
internal sealed class Empty<TMessage> : CompletableEvent<TMessage, Unit> | ||
{ | ||
protected override IDisposable SubscribeCore(Subscriber<TMessage, Unit> subscriber) | ||
{ | ||
subscriber.OnCompleted(default); | ||
return Disposable.Empty; | ||
} | ||
} | ||
|
||
internal sealed class EmptyT<TMessage>(TimeSpan dueTime, TimeProvider timeProvider) : CompletableEvent<TMessage, Unit> | ||
{ | ||
protected override IDisposable SubscribeCore(Subscriber<TMessage, Unit> subscriber) | ||
{ | ||
var method = new _Empty(subscriber); | ||
method.Timer = timeProvider.CreateStoppedTimer(_Empty.timerCallback, method); | ||
method.Timer.InvokeOnce(dueTime); | ||
return method; | ||
} | ||
|
||
sealed class _Empty(Subscriber<TMessage, Unit> subscriber) : IDisposable | ||
{ | ||
public static readonly TimerCallback timerCallback = NextTick; | ||
|
||
readonly Subscriber<TMessage, Unit> subscriber = subscriber; | ||
|
||
public ITimer? Timer { get; set; } | ||
|
||
static void NextTick(object? state) | ||
{ | ||
var self = (_Empty)state!; | ||
try | ||
{ | ||
self.subscriber.OnCompleted(); | ||
} | ||
finally | ||
{ | ||
self.Dispose(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Timer?.Dispose(); | ||
Timer = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.Extensions.Time.Testing; | ||
|
||
namespace R3.Tests.FactoryTests; | ||
|
||
public class EmptyTest | ||
{ | ||
[Fact] | ||
public void Empty() | ||
{ | ||
using var list = EventFactory.Empty<int>().LiveRecord(); | ||
list.AssertIsCompleted(); | ||
} | ||
|
||
[Fact] | ||
public void EmptyWithTime() | ||
{ | ||
var fakeTime = new FakeTimeProvider(); | ||
using var list = EventFactory.Empty<int>(TimeSpan.FromSeconds(5), fakeTime).LiveRecord(); | ||
|
||
fakeTime.Advance(TimeSpan.FromSeconds(4)); | ||
list.AssertIsNotCompleted(); | ||
|
||
fakeTime.Advance(TimeSpan.FromSeconds(1)); | ||
list.AssertIsCompleted(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
tests/R3.Tests/OperatorTests/ReturnTest.cs → tests/R3.Tests/FactoryTests/ReturnTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters