diff --git a/src/R3/EventFactory.cs b/src/R3/EventFactory.cs deleted file mode 100644 index 086a0698..00000000 --- a/src/R3/EventFactory.cs +++ /dev/null @@ -1,122 +0,0 @@ -using R3.Internal; - -namespace R3; - -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 - - public static CompletableEvent Range(int start, int count) - { - return new Range(start, count); - } - - public static CompletableEvent ToEvent(this IEnumerable source) - { - return new EnumerableToEvent(source); - } - - public static CompletableEvent Timer(TimeSpan dueTime, TimeProvider timeProvider) - { - return new Timer(dueTime, timeProvider); - } -} - -internal sealed class Range : CompletableEvent -{ - readonly int start; - readonly int count; - - public Range(int start, int count) - { - this.start = start; - this.count = count; - } - - protected override IDisposable SubscribeCore(Subscriber subscriber) - { - for (int i = 0; i < count; i++) - { - subscriber.OnNext(start + i); - } - subscriber.OnCompleted(default); - return Disposable.Empty; - } -} - -internal class EnumerableToEvent(IEnumerable source) : CompletableEvent -{ - protected override IDisposable SubscribeCore(Subscriber subscriber) - { - foreach (var message in source) - { - subscriber.OnNext(message); - } - subscriber.OnCompleted(default); - return Disposable.Empty; - } -} - -internal sealed class Timer : CompletableEvent -{ - readonly TimeSpan dueTime; - readonly TimeProvider timeProvider; - - public Timer(TimeSpan dueTime, TimeProvider timeProvider) - { - this.dueTime = dueTime; - this.timeProvider = timeProvider; - } - - protected override IDisposable SubscribeCore(Subscriber subscriber) - { - var method = new _Timer(subscriber); - method.Timer = timeProvider.CreateStoppedTimer(_Timer.timerCallback, method); - method.Timer.InvokeOnce(dueTime); - return method; - } - - sealed class _Timer(Subscriber subscriber) : IDisposable - { - public static readonly TimerCallback timerCallback = NextTick; - - Subscriber 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; - } - } -} diff --git a/src/R3/Factories/Range.cs b/src/R3/Factories/Range.cs new file mode 100644 index 00000000..74e45ec9 --- /dev/null +++ b/src/R3/Factories/Range.cs @@ -0,0 +1,35 @@ +namespace R3 +{ + public static partial class EventFactory + { + public static CompletableEvent Range(int start, int count) + { + return new R3.Factories.Range(start, count); + } + } +} + +namespace R3.Factories +{ + internal sealed class Range : CompletableEvent + { + readonly int start; + readonly int count; + + public Range(int start, int count) + { + this.start = start; + this.count = count; + } + + protected override IDisposable SubscribeCore(Subscriber subscriber) + { + for (int i = 0; i < count; i++) + { + subscriber.OnNext(start + i); + } + subscriber.OnCompleted(default); + return Disposable.Empty; + } + } +} diff --git a/src/R3/Operators/Return.cs b/src/R3/Factories/Return.cs similarity index 98% rename from src/R3/Operators/Return.cs rename to src/R3/Factories/Return.cs index aed7b470..24f6172f 100644 --- a/src/R3/Operators/Return.cs +++ b/src/R3/Factories/Return.cs @@ -1,6 +1,6 @@ namespace R3 { - public static partial class EventFactory + public static partial class _EventFactory { public static CompletableEvent Return(TMessage value) { @@ -31,7 +31,7 @@ public static CompletableEvent Return(TMessage value, } } -namespace R3.Operators +namespace R3.Factories { internal class Return(TMessage value, TimeSpan dueTime, TimeProvider timeProvider) : CompletableEvent { diff --git a/src/R3/Factories/Timer.cs b/src/R3/Factories/Timer.cs new file mode 100644 index 00000000..0b09e242 --- /dev/null +++ b/src/R3/Factories/Timer.cs @@ -0,0 +1,62 @@ +namespace R3 +{ + public static partial class EventFactory + { + public static CompletableEvent Timer(TimeSpan dueTime, TimeProvider timeProvider) + { + return new R3.Factories.Timer(dueTime, timeProvider); + } + } +} + +namespace R3.Factories +{ + internal sealed class Timer : CompletableEvent + { + readonly TimeSpan dueTime; + readonly TimeProvider timeProvider; + + public Timer(TimeSpan dueTime, TimeProvider timeProvider) + { + this.dueTime = dueTime; + this.timeProvider = timeProvider; + } + + protected override IDisposable SubscribeCore(Subscriber subscriber) + { + var method = new _Timer(subscriber); + method.Timer = timeProvider.CreateStoppedTimer(_Timer.timerCallback, method); + method.Timer.InvokeOnce(dueTime); + return method; + } + + sealed class _Timer(Subscriber subscriber) : IDisposable + { + public static readonly TimerCallback timerCallback = NextTick; + + Subscriber 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; + } + } + } +} diff --git a/src/R3/Factories/ToEvent.cs b/src/R3/Factories/ToEvent.cs new file mode 100644 index 00000000..20c9bcce --- /dev/null +++ b/src/R3/Factories/ToEvent.cs @@ -0,0 +1,26 @@ +namespace R3 +{ + public static partial class EventFactory + { + public static CompletableEvent ToEvent(this IEnumerable source) + { + return new ToEvent(source); + } + } +} + +namespace R3.Factories +{ + internal class ToEvent(IEnumerable source) : CompletableEvent + { + protected override IDisposable SubscribeCore(Subscriber subscriber) + { + foreach (var message in source) + { + subscriber.OnNext(message); + } + subscriber.OnCompleted(default); + return Disposable.Empty; + } + } +} diff --git a/src/R3/Factories/_EventFactory.cs b/src/R3/Factories/_EventFactory.cs new file mode 100644 index 00000000..a6dd4ce9 --- /dev/null +++ b/src/R3/Factories/_EventFactory.cs @@ -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 +{ +} diff --git a/src/R3/Operators/_Operators.cs b/src/R3/Operators/_Operators.cs new file mode 100644 index 00000000..e6c98822 --- /dev/null +++ b/src/R3/Operators/_Operators.cs @@ -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 +{ + +} diff --git a/src/R3/R3.csproj b/src/R3/R3.csproj index 46e80f2c..74a7e818 100644 --- a/src/R3/R3.csproj +++ b/src/R3/R3.csproj @@ -25,6 +25,7 @@ + diff --git a/tests/R3.Tests/OperatorTests/ReturnTest.cs b/tests/R3.Tests/OperatorTests/ReturnTest.cs index 128c8915..0438e83f 100644 --- a/tests/R3.Tests/OperatorTests/ReturnTest.cs +++ b/tests/R3.Tests/OperatorTests/ReturnTest.cs @@ -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)); @@ -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);