Skip to content

Commit

Permalink
factories
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 10, 2023
1 parent 400d079 commit 252ea29
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 128 deletions.
122 changes: 0 additions & 122 deletions src/R3/EventFactory.cs

This file was deleted.

35 changes: 35 additions & 0 deletions src/R3/Factories/Range.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace R3
{
public static partial class EventFactory
{
public static CompletableEvent<int, Unit> Range(int start, int count)
{
return new R3.Factories.Range(start, count);
}
}
}

namespace R3.Factories
{
internal sealed class Range : CompletableEvent<int, Unit>
{
readonly int start;
readonly int count;

public Range(int start, int count)
{
this.start = start;
this.count = count;
}

protected override IDisposable SubscribeCore(Subscriber<int, Unit> subscriber)
{
for (int i = 0; i < count; i++)
{
subscriber.OnNext(start + i);
}
subscriber.OnCompleted(default);
return Disposable.Empty;
}
}
}
4 changes: 2 additions & 2 deletions src/R3/Operators/Return.cs → src/R3/Factories/Return.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace R3
{
public static partial class EventFactory
public static partial class _EventFactory
{
public static CompletableEvent<TMessage, Unit> Return<TMessage>(TMessage value)
{
Expand Down Expand Up @@ -31,7 +31,7 @@ public static CompletableEvent<TMessage, Unit> Return<TMessage>(TMessage value,
}
}

namespace R3.Operators
namespace R3.Factories
{
internal class Return<TMessage>(TMessage value, TimeSpan dueTime, TimeProvider timeProvider) : CompletableEvent<TMessage, Unit>
{
Expand Down
62 changes: 62 additions & 0 deletions src/R3/Factories/Timer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace R3
{
public static partial class EventFactory
{
public static CompletableEvent<Unit, Unit> Timer(TimeSpan dueTime, TimeProvider timeProvider)
{
return new R3.Factories.Timer(dueTime, timeProvider);
}
}
}

namespace R3.Factories
{
internal sealed class Timer : CompletableEvent<Unit, Unit>
{
readonly TimeSpan dueTime;
readonly TimeProvider timeProvider;

public Timer(TimeSpan dueTime, TimeProvider timeProvider)
{
this.dueTime = dueTime;
this.timeProvider = timeProvider;
}

protected override IDisposable SubscribeCore(Subscriber<Unit, Unit> subscriber)
{
var method = new _Timer(subscriber);
method.Timer = timeProvider.CreateStoppedTimer(_Timer.timerCallback, method);
method.Timer.InvokeOnce(dueTime);
return method;
}

sealed class _Timer(Subscriber<Unit, Unit> subscriber) : IDisposable
{
public static readonly TimerCallback timerCallback = NextTick;

Subscriber<Unit, Unit> subscriber = subscriber;

public ITimer? Timer { get; set; }

static void NextTick(object? state)
{
var self = (_Timer)state!;
try
{
self.subscriber.OnNext(default);
self.subscriber.OnCompleted();
}
finally
{
self.Dispose();
}
}

public void Dispose()
{
Timer?.Dispose();
Timer = null;
}
}
}
}
26 changes: 26 additions & 0 deletions src/R3/Factories/ToEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace R3
{
public static partial class EventFactory
{
public static CompletableEvent<TMessage, Unit> ToEvent<TMessage>(this IEnumerable<TMessage> source)
{
return new ToEvent<TMessage>(source);
}
}
}

namespace R3.Factories
{
internal class ToEvent<TMessage>(IEnumerable<TMessage> source) : CompletableEvent<TMessage, Unit>
{
protected override IDisposable SubscribeCore(Subscriber<TMessage, Unit> subscriber)
{
foreach (var message in source)
{
subscriber.OnNext(message);
}
subscriber.OnCompleted(default);
return Disposable.Empty;
}
}
}
29 changes: 29 additions & 0 deletions src/R3/Factories/_EventFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace R3
{
// TODO: this is working space, will remove this file after complete.

public static partial class EventFactory
{
// TODO: Empty, Never, Throw, Defer, DeferAsync, FromAsync, FromAsyncPattern, FromEvent, FromEventPattern, Interval, Range, Repeat, Start, Timer, Using, Create

// TODO: Convert
// ToArray
// ToAsync
// ToDictionary
// ToEnumerable
// ToEvent
// ToEventPattern
// ToList
// ToLookup
// ToObservable

// AsObservable
// AsSingleUnitObservable
// AsUnitObservable

}
}

namespace R3.Factories
{
}
14 changes: 14 additions & 0 deletions src/R3/Operators/_Operators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace R3
{
// TODO: this is working space, will remove this file after complete.

public static partial class EventExtensions
{

}
}

namespace R3.Operators
{

}
1 change: 1 addition & 0 deletions src/R3/R3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ItemGroup>
<Using Include="R3" />
<Using Include="R3.Operators" />
<Using Include="R3.Factories" />
<Using Include="R3.Internal" />
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions tests/R3.Tests/OperatorTests/ReturnTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ public class ReturnTest
public void Return()
{
{
using var list = EventFactory.Return(10).LiveRecord();
using var list = _EventFactory.Return(10).LiveRecord();
list.AssertEqual([10]);
list.AssertIsCompleted();
}
{
var fakeTime = new FakeTimeProvider();

using var list = EventFactory.Return(10, TimeSpan.Zero, fakeTime).LiveRecord();
using var list = _EventFactory.Return(10, TimeSpan.Zero, fakeTime).LiveRecord();
list.AssertEqual([10]);
list.AssertIsCompleted();
}
{
var fakeTime = new FakeTimeProvider();

using var list = EventFactory.Return(10, TimeSpan.FromSeconds(5), fakeTime).LiveRecord();
using var list = _EventFactory.Return(10, TimeSpan.FromSeconds(5), fakeTime).LiveRecord();
list.AssertEqual([]);

fakeTime.Advance(TimeSpan.FromSeconds(4));
Expand All @@ -38,7 +38,7 @@ public void Return()
[Fact]
public void ReturnThreadPoolScheduleOptimized()
{
using var list = EventFactory.Return(10).LiveRecord();
using var list = _EventFactory.Return(10).LiveRecord();

Thread.Sleep(1);

Expand Down

0 comments on commit 252ea29

Please sign in to comment.