Skip to content

Commit

Permalink
Merge pull request #25 from Cysharp/feature/CurrentFrameProperty
Browse files Browse the repository at this point in the history
Add ILogicLooper.CurrentFrame property
  • Loading branch information
mayuki committed Feb 14, 2024
2 parents 219d7c6 + 4eea677 commit 94fb0ae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/LogicLooper/ILogicLooper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ILogicLooper : IDisposable
int Id { get; }

/// <summary>
/// Gets an approximately count of running actions.
/// Gets an approximate count of running actions.
/// </summary>
int ApproximatelyRunningActions { get; }

Expand All @@ -25,6 +25,11 @@ public interface ILogicLooper : IDisposable
/// </summary>
double TargetFrameRate { get; }

/// <summary>
/// Gets a current frame that elapsed since beginning the looper is started.
/// </summary>
long CurrentFrame { get; }

/// <summary>
/// Registers a loop-frame action to the looper and returns <see cref="Task"/> to wait for completion.
/// </summary>
Expand Down
19 changes: 7 additions & 12 deletions src/LogicLooper/LogicLooper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,26 @@ public sealed class LogicLooper : ILogicLooper, IDisposable
private int _isShuttingDown = 0;
private long _frame = 0;

/// <summary>
/// Gets a unique identifier of the looper.
/// </summary>
/// <inheritdoc/>
public int Id => _looperId;

/// <summary>
/// Gets an approximately count of running actions.
/// </summary>
/// <inheritdoc/>
public int ApproximatelyRunningActions => _tail;

/// <summary>
/// Gets a duration of the last processed frame.
/// </summary>
/// <inheritdoc/>
public TimeSpan LastProcessingDuration => TimeSpan.FromMilliseconds(_lastProcessingDuration);

/// <summary>
/// Gets a target frame rate of the looper.
/// </summary>
/// <inheritdoc/>
public double TargetFrameRate => _targetFrameRate;

/// <summary>
/// Gets a unique identifier of the managed thread.
/// </summary>
internal int ThreadId => _runLoopThread.ManagedThreadId;

/// <inheritdoc/>
public long CurrentFrame => _frame;

public LogicLooper(int targetFrameRate, int initialActionsCapacity = 16)
: this(TimeSpan.FromMilliseconds(1000 / (double)targetFrameRate), initialActionsCapacity)
{
Expand Down
3 changes: 3 additions & 0 deletions src/LogicLooper/ManualLogicLooper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public sealed class ManualLogicLooper : ILogicLooper
/// <inheritdoc />
public double TargetFrameRate { get; }

/// <inheritdoc />
public long CurrentFrame => _frame;

public ManualLogicLooper(double targetFrameRate)
{
if (targetFrameRate == 0) throw new ArgumentOutOfRangeException(nameof(targetFrameRate), "TargetFrameRate must be greater than 0.");
Expand Down
4 changes: 4 additions & 0 deletions test/LogicLooper.Test/ManualLogicLooperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ public void Tick()
looper.ApproximatelyRunningActions.Should().Be(1);

count.Should().Be(0);
looper.CurrentFrame.Should().Be(0);
looper.Tick().Should().BeTrue();
count.Should().Be(1);
looper.CurrentFrame.Should().Be(1);
looper.Tick().Should().BeTrue();
count.Should().Be(2);
looper.CurrentFrame.Should().Be(2);
looper.Tick().Should().BeFalse();
count.Should().Be(3);
looper.CurrentFrame.Should().Be(3);

looper.ApproximatelyRunningActions.Should().Be(0);
t1.IsCompletedSuccessfully.Should().BeTrue();
Expand Down

0 comments on commit 94fb0ae

Please sign in to comment.