using System;
namespace UniRx
{
///
/// Notify value on setuped scheduler.
///
public class ScheduledNotifier : IObservable, IProgress
{
readonly IScheduler scheduler;
readonly Subject trigger = new Subject();
///
/// Use scheduler is Scheduler.DefaultSchedulers.ConstantTimeOperations.
///
public ScheduledNotifier()
{
this.scheduler = Scheduler.DefaultSchedulers.ConstantTimeOperations;
}
///
/// Use scheduler is argument's scheduler.
///
public ScheduledNotifier(IScheduler scheduler)
{
if (scheduler == null)
{
throw new ArgumentNullException("scheduler");
}
this.scheduler = scheduler;
}
///
/// Push value to subscribers on setuped scheduler.
///
public void Report(T value)
{
scheduler.Schedule(() => trigger.OnNext(value));
}
///
/// Push value to subscribers on setuped scheduler.
///
public IDisposable Report(T value, TimeSpan dueTime)
{
var cancel = scheduler.Schedule(dueTime, () => trigger.OnNext(value));
return cancel;
}
///
/// Push value to subscribers on setuped scheduler.
///
public IDisposable Report(T value, DateTimeOffset dueTime)
{
var cancel = scheduler.Schedule(dueTime, () => trigger.OnNext(value));
return cancel;
}
///
/// Subscribe observer.
///
public IDisposable Subscribe(IObserver observer)
{
if (observer == null)
{
throw new ArgumentNullException("observer");
}
return trigger.Subscribe(observer);
}
}
}