Skip to content

Commit

Permalink
ManualFrameProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 14, 2023
1 parent a635b3c commit 3b92a99
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
19 changes: 17 additions & 2 deletions sandbox/ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using R3;
using R3.Internal;
using System.Diagnostics;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
Expand Down Expand Up @@ -44,21 +45,35 @@



source.DoOnDisposed(() => {/*Console.WriteLine("DISPOSED")*/}).WriteLine();
source.DoOnDisposed(() => { Console.WriteLine("DISPOSED"); }).WriteLine();

SubscriptionTracker.ForEachActiveTask(x =>
{
Console.WriteLine(x);
});



Console.WriteLine("BeforeId:" + Thread.CurrentThread.ManagedThreadId);

// TODO: if WaitAsync is using, not disposed???
await source.WaitAsync();
Console.WriteLine("Press Key to done.");


await Task.Yield();

Console.ReadLine();


SubscriptionTracker.ForEachActiveTask(x =>
{
Console.WriteLine(x);
});

Console.WriteLine("----------------");
Console.WriteLine("AfterId:" + Thread.CurrentThread.ManagedThreadId);


public static class Extensions
{
public static IDisposable WriteLine<T>(this Event<T> source)
Expand Down
64 changes: 58 additions & 6 deletions src/R3/FrameProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,70 @@

public abstract class FrameProvider
{
protected event Action<Exception>? UnhandledException;
public abstract long GetFrameCount();
public abstract void Register(IFrameRunnerWorkItem callback);

protected void OnUnhandledException(Exception ex)
{
UnhandledException?.Invoke(ex);
}
}

public interface IFrameRunnerWorkItem
{
// true, continue
bool MoveNext(long frameCount);
}

public sealed class ManualFrameProvider : FrameProvider
{
long frameCount;
FreeListCore<IFrameRunnerWorkItem> list = new FreeListCore<IFrameRunnerWorkItem>();

public override long GetFrameCount()
{
return frameCount;
}

public override void Register(IFrameRunnerWorkItem callback)
{
list.Add(callback);
}

public void Advance()
{
Advance(1);
}

public void Advance(int advanceCount)
{
for (int i = 0; i < advanceCount; i++)
{
RunLoop();
}
}

void RunLoop()
{
var span = list.AsSpan();
for (int i = 0; i < span.Length; i++)
{
ref readonly var item = ref span[i];
if (item != null)
{
try
{
if (!item.MoveNext(frameCount))
{
list.Remove(i);
}
}
catch (Exception ex)
{
list.Remove(i);
try
{
EventSystem.GetUnhandledExceptionHandler().Invoke(ex);
}
catch { }
}
}
}
frameCount++;
}
}
4 changes: 1 addition & 3 deletions src/R3/Operators/DoOnDisposed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public static Event<T> DoOnDisposed<T, TState>(this Event<T> source, Action<TSta
}
}



internal sealed class DoOnDisposed<T>(Event<T> source, Action action) : Event<T>
sealed class DoOnDisposed<T>(Event<T> source, Action action) : Event<T>
{
protected override IDisposable SubscribeCore(Subscriber<T> subscriber)
{
Expand Down
2 changes: 1 addition & 1 deletion src/R3/ThreadSleepFrameProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void Run()
list.Remove(i);
try
{
OnUnhandledException(ex);
EventSystem.GetUnhandledExceptionHandler().Invoke(ex);
}
catch { }
}
Expand Down

0 comments on commit 3b92a99

Please sign in to comment.