using System;
using System.Collections.Generic;
using UnityEngine;
namespace Uigenx
{
///
/// Owns a group of subscriptions so a screen can tear everything down in one call. Disposal is
/// idempotent and one faulty entry never stops the rest.
///
public sealed class UgxDisposableSet : IDisposable
{
readonly List _items;
public UgxDisposableSet(int capacity = 4) => _items = new List(capacity < 1 ? 1 : capacity);
public int Count => _items.Count;
public IDisposable Add(IDisposable item)
{
if (item != null) _items.Add(item);
return item;
}
public void Dispose()
{
for (int i = _items.Count - 1; i >= 0; i--)
{
try
{
_items[i].Dispose();
}
catch (Exception e)
{
Debug.LogError("[ugx] disposing a subscription threw: " + e);
}
}
_items.Clear();
}
}
}