// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Reactive.Concurrency;
namespace System.Reactive.Disposables
{
///
/// Represents a disposable resource whose disposal invocation will be scheduled on the specified .
///
public sealed class ScheduledDisposable : ICancelable
{
private SingleAssignmentDisposableValue _disposable;
///
/// Initializes a new instance of the class that uses an on which to dispose the disposable.
///
/// Scheduler where the disposable resource will be disposed on.
/// Disposable resource to dispose on the given scheduler.
/// or is null.
public ScheduledDisposable(IScheduler scheduler, IDisposable disposable)
{
Scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
_disposable.Disposable = disposable ?? throw new ArgumentNullException(nameof(disposable));
}
///
/// Gets the scheduler where the disposable resource will be disposed on.
///
public IScheduler Scheduler { get; }
///
/// Gets the underlying disposable. After disposal, the result is undefined.
///
public IDisposable Disposable => _disposable.Disposable ?? Disposables.Disposable.Empty;
///
/// Gets a value that indicates whether the object is disposed.
///
public bool IsDisposed => _disposable.IsDisposed;
///
/// Disposes the wrapped disposable on the provided scheduler.
///
public void Dispose() => Scheduler.ScheduleAction(this, scheduler => scheduler._disposable.Dispose());
}
}