-
-
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
10 changed files
with
175 additions
and
94 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,88 @@ | ||
namespace R3; | ||
|
||
public static partial class Event | ||
{ | ||
public static Event<Unit> EveryUpdate() | ||
{ | ||
return new EveryUpdate(EventSystem.DefaultFrameProvider, CancellationToken.None, cancelImmediately: false); | ||
} | ||
|
||
public static Event<Unit> EveryUpdate(CancellationToken cancellationToken) | ||
{ | ||
return new EveryUpdate(EventSystem.DefaultFrameProvider, cancellationToken, cancelImmediately: false); | ||
} | ||
|
||
public static Event<Unit> EveryUpdate(FrameProvider frameProvider) | ||
{ | ||
return new EveryUpdate(frameProvider, CancellationToken.None, cancelImmediately: false); | ||
} | ||
|
||
public static Event<Unit> EveryUpdate(FrameProvider frameProvider, CancellationToken cancellationToken) | ||
{ | ||
return new EveryUpdate(frameProvider, cancellationToken, cancelImmediately: false); | ||
} | ||
|
||
public static Event<Unit> EveryUpdate(FrameProvider frameProvider, CancellationToken cancellationToken, bool cancelImmediately) | ||
{ | ||
return new EveryUpdate(frameProvider, cancellationToken, cancelImmediately: cancelImmediately); | ||
} | ||
} | ||
|
||
|
||
internal sealed class EveryUpdate(FrameProvider frameProvider, CancellationToken cancellationToken, bool cancelImmediately) : Event<Unit> | ||
{ | ||
protected override IDisposable SubscribeCore(Subscriber<Unit> subscriber) | ||
{ | ||
var runner = new EveryUpdateRunnerWorkItem(subscriber, cancellationToken, cancelImmediately); | ||
frameProvider.Register(runner); | ||
return runner; | ||
} | ||
|
||
class EveryUpdateRunnerWorkItem : IFrameRunnerWorkItem, IDisposable | ||
{ | ||
Subscriber<Unit> subscriber; | ||
CancellationToken cancellationToken; | ||
CancellationTokenRegistration cancellationTokenRegistration; | ||
bool isDisposed; | ||
|
||
public EveryUpdateRunnerWorkItem(Subscriber<Unit> subscriber, CancellationToken cancellationToken, bool cancelImmediately) | ||
{ | ||
this.subscriber = subscriber; | ||
this.cancellationToken = cancellationToken; | ||
|
||
if (cancelImmediately && cancellationToken.CanBeCanceled) | ||
{ | ||
cancellationTokenRegistration = cancellationToken.UnsafeRegister(static state => | ||
{ | ||
var s = (EveryUpdateRunnerWorkItem)state!; | ||
s.subscriber.OnCompleted(); | ||
s.Dispose(); | ||
}, this); | ||
} | ||
} | ||
|
||
public bool MoveNext(long frameCount) | ||
{ | ||
if (isDisposed) | ||
{ | ||
return false; | ||
} | ||
|
||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
subscriber.OnCompleted(); | ||
Dispose(); | ||
return false; | ||
} | ||
|
||
subscriber.OnNext(default); | ||
return true; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
isDisposed = true; | ||
cancellationTokenRegistration.Dispose(); | ||
} | ||
} | ||
} |
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
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.Tests.FactoryTests; | ||
|
||
public class EveryUpdateTest | ||
{ | ||
[Fact] | ||
public void EveryUpdate() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
var frameProvider = new ManualFrameProvider(); | ||
|
||
var list = Event.EveryUpdate(frameProvider, cts.Token).Select(_ => frameProvider.GetFrameCount()).ToLiveList(); | ||
|
||
list.AssertEqual([]); | ||
|
||
frameProvider.Advance(); | ||
list.AssertEqual([0]); | ||
|
||
frameProvider.Advance(3); | ||
list.AssertEqual([0, 1, 2, 3]); | ||
|
||
cts.Cancel(); | ||
list.AssertIsNotCompleted(); | ||
|
||
frameProvider.Advance(); | ||
list.AssertIsCompleted(); | ||
} | ||
|
||
[Fact] | ||
public void EveryUpdateCancelImmediate() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
var frameProvider = new ManualFrameProvider(); | ||
|
||
var list = Event.EveryUpdate(frameProvider, cts.Token, cancelImmediately: true).Select(_ => frameProvider.GetFrameCount()).ToLiveList(); | ||
|
||
list.AssertEqual([]); | ||
|
||
frameProvider.Advance(); | ||
list.AssertEqual([0]); | ||
|
||
frameProvider.Advance(3); | ||
list.AssertEqual([0, 1, 2, 3]); | ||
|
||
cts.Cancel(); | ||
list.AssertIsCompleted(); | ||
|
||
frameProvider.Advance(); | ||
list.AssertEqual([0, 1, 2, 3]); | ||
list.AssertIsCompleted(); | ||
} | ||
|
||
[Fact] | ||
public void EveryUpdateDispose() | ||
{ | ||
var frameProvider = new ManualFrameProvider(); | ||
|
||
var list = Event.EveryUpdate(frameProvider).Select(_ => frameProvider.GetFrameCount()).ToLiveList(); | ||
|
||
list.AssertEqual([]); | ||
|
||
frameProvider.Advance(); | ||
list.AssertEqual([0]); | ||
|
||
frameProvider.Advance(3); | ||
list.AssertEqual([0, 1, 2, 3]); | ||
|
||
list.Dispose(); | ||
frameProvider.Advance(); | ||
list.AssertEqual([0, 1, 2, 3]); | ||
} | ||
} |
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
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