Files
cneicy 50da51bd7f
Publish UPM package / publish (push) Failing after 1s
chore: initialize standalone UPM package
2026-08-26 02:50:25 +08:00

54 lines
1.5 KiB
C#

#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);
}
}