2025-10-03 00:02:43 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
namespace UniRx
|
|
|
|
|
|
{
|
|
|
|
|
|
public sealed class MultipleAssignmentDisposable : IDisposable, ICancelable
|
|
|
|
|
|
{
|
2026-03-20 11:56:50 -04:00
|
|
|
|
private static readonly BooleanDisposable True = new(true);
|
|
|
|
|
|
private IDisposable current;
|
2025-10-03 00:02:43 -04:00
|
|
|
|
|
2026-03-20 11:56:50 -04:00
|
|
|
|
private readonly object gate = new();
|
2025-10-03 00:02:43 -04:00
|
|
|
|
|
|
|
|
|
|
public IDisposable Disposable
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (gate)
|
|
|
|
|
|
{
|
2026-03-20 11:56:50 -04:00
|
|
|
|
return current == True
|
2025-10-03 00:02:43 -04:00
|
|
|
|
? UniRx.Disposable.Empty
|
|
|
|
|
|
: current;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
var shouldDispose = false;
|
|
|
|
|
|
lock (gate)
|
|
|
|
|
|
{
|
2026-03-20 11:56:50 -04:00
|
|
|
|
shouldDispose = current == True;
|
|
|
|
|
|
if (!shouldDispose) current = value;
|
2025-10-03 00:02:43 -04:00
|
|
|
|
}
|
2026-03-20 11:56:50 -04:00
|
|
|
|
|
|
|
|
|
|
if (shouldDispose && value != null) value.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsDisposed
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (gate)
|
2025-10-03 00:02:43 -04:00
|
|
|
|
{
|
2026-03-20 11:56:50 -04:00
|
|
|
|
return current == True;
|
2025-10-03 00:02:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
IDisposable old = null;
|
|
|
|
|
|
|
|
|
|
|
|
lock (gate)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (current != True)
|
|
|
|
|
|
{
|
|
|
|
|
|
old = current;
|
|
|
|
|
|
current = True;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (old != null) old.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|