This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using ShrinkEventBus;
|
||||
using Unity.Collections;
|
||||
|
||||
namespace ShrinkEventBus.Entities
|
||||
{
|
||||
/// <summary>Burst-safe producer queue; playback enters the regular Bus instance.</summary>
|
||||
public sealed class ShrinkEcsEventQueue<TEvent> : IDisposable where TEvent : unmanaged, IShrinkEvent
|
||||
{
|
||||
private NativeQueue<TEvent> _queue;
|
||||
|
||||
public ShrinkEcsEventQueue(Allocator allocator = Allocator.Persistent)
|
||||
{
|
||||
_queue = new NativeQueue<TEvent>(allocator);
|
||||
Writer = new ShrinkEcsEventWriter<TEvent>(_queue.AsParallelWriter());
|
||||
}
|
||||
|
||||
public ShrinkEcsEventWriter<TEvent> Writer { get; }
|
||||
|
||||
public int Playback(IShrinkEventBus bus)
|
||||
{
|
||||
if (bus == null)
|
||||
throw new ArgumentNullException(nameof(bus));
|
||||
var count = 0;
|
||||
while (_queue.TryDequeue(out var eventData))
|
||||
{
|
||||
bus.Post(in eventData);
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_queue.IsCreated)
|
||||
_queue.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct ShrinkEcsEventWriter<TEvent> where TEvent : unmanaged, IShrinkEvent
|
||||
{
|
||||
private readonly NativeQueue<TEvent>.ParallelWriter _writer;
|
||||
|
||||
internal ShrinkEcsEventWriter(NativeQueue<TEvent>.ParallelWriter writer)
|
||||
{
|
||||
_writer = writer;
|
||||
}
|
||||
|
||||
public void Post(in TEvent eventData) => _writer.Enqueue(eventData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user