You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.8 KiB
99 lines
2.8 KiB
using System; |
|
|
|
namespace UniRx.Operators |
|
{ |
|
internal class RepeatObservable<T> : OperatorObservableBase<T> |
|
{ |
|
readonly T value; |
|
readonly int? repeatCount; |
|
readonly IScheduler scheduler; |
|
|
|
public RepeatObservable(T value, int? repeatCount, IScheduler scheduler) |
|
: base(scheduler == Scheduler.CurrentThread) |
|
{ |
|
this.value = value; |
|
this.repeatCount = repeatCount; |
|
this.scheduler = scheduler; |
|
} |
|
|
|
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel) |
|
{ |
|
observer = new Repeat(observer, cancel); |
|
|
|
if (repeatCount == null) |
|
{ |
|
return scheduler.Schedule((Action self) => |
|
{ |
|
observer.OnNext(value); |
|
self(); |
|
}); |
|
} |
|
else |
|
{ |
|
if (scheduler == Scheduler.Immediate) |
|
{ |
|
var count = this.repeatCount.Value; |
|
for (int i = 0; i < count; i++) |
|
{ |
|
observer.OnNext(value); |
|
} |
|
observer.OnCompleted(); |
|
return Disposable.Empty; |
|
} |
|
else |
|
{ |
|
var currentCount = this.repeatCount.Value; |
|
return scheduler.Schedule((Action self) => |
|
{ |
|
if (currentCount > 0) |
|
{ |
|
observer.OnNext(value); |
|
currentCount--; |
|
} |
|
|
|
if (currentCount == 0) |
|
{ |
|
observer.OnCompleted(); |
|
return; |
|
} |
|
|
|
self(); |
|
}); |
|
} |
|
} |
|
} |
|
|
|
class Repeat : OperatorObserverBase<T, T> |
|
{ |
|
public Repeat(IObserver<T> observer, IDisposable cancel) |
|
: base(observer, cancel) |
|
{ |
|
} |
|
|
|
public override void OnNext(T value) |
|
{ |
|
try |
|
{ |
|
base.observer.OnNext(value); |
|
} |
|
catch |
|
{ |
|
Dispose(); |
|
throw; |
|
} |
|
} |
|
|
|
public override void OnError(Exception error) |
|
{ |
|
try { observer.OnError(error); } |
|
finally { Dispose(); } |
|
} |
|
|
|
public override void OnCompleted() |
|
{ |
|
try { observer.OnCompleted(); } |
|
finally { Dispose(); } |
|
} |
|
} |
|
} |
|
} |