A small ref struct disposable used to temporarily disable reactive tracking. On construction it saves the previous Reactive.Runtime.IsUntracking value and sets IsUntracking = true, and on Dispose it restores the previous value.
namespace Sandbox.Reactivity;
/// <summary>
/// A disposable that disables reactivity tracking when constructed, and re-enables it when it's disposed.
/// </summary>
public readonly ref struct UntrackScope : IDisposable
{
private readonly bool _previousIsUntracking;
public UntrackScope()
{
_previousIsUntracking = Reactive.Runtime.IsUntracking;
Reactive.Runtime.IsUntracking = true;
}
public void Dispose()
{
Reactive.Runtime.IsUntracking = _previousIsUntracking;
}
}