Skip to content

Commit

Permalink
Empty
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 10, 2023
1 parent 252ea29 commit cc57fcc
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
71 changes: 71 additions & 0 deletions src/R3/Factories/Empty.cs
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;
}
}
}
}
7 changes: 4 additions & 3 deletions src/R3/Factories/_EventFactory.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace R3
{
// TODO: this is working space, will remove this file after complete.

public static partial class EventFactory
{
// TODO: this is working space, will remove this file after complete.

// TODO: Empty, Never, Throw, Defer, DeferAsync, FromAsync, FromAsyncPattern, FromEvent, FromEventPattern, Interval, Range, Repeat, Start, Timer, Using, Create

// TODO: Convert
Expand All @@ -20,10 +20,11 @@ public static partial class EventFactory
// AsObservable
// AsSingleUnitObservable
// AsUnitObservable

}
}

namespace R3.Factories
{


}
2 changes: 2 additions & 0 deletions src/R3/Operators/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public static partial class EventExtensions
// TODO: CompletableEvent.Select
// TODO: Element index overload

// TODO: Select for TComplete

public static Event<TResult> Select<TMessage, TResult>(this Event<TMessage> source, Func<TMessage, TResult> selector)
{
return new Select<TMessage, TResult>(source, selector);
Expand Down
26 changes: 26 additions & 0 deletions tests/R3.Tests/FactoryTests/EmptyTest.cs
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Time.Testing;

namespace R3.Tests.OperatorTests;
namespace R3.Tests.FactoryTests;

public class ReturnTest
{
Expand Down

0 comments on commit cc57fcc

Please sign in to comment.