123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using System;
- namespace UniRx.Operators
- {
- internal class CreateObservable<T> : OperatorObservableBase<T>
- {
- readonly Func<IObserver<T>, IDisposable> subscribe;
- public CreateObservable(Func<IObserver<T>, IDisposable> subscribe)
- : base(true) // fail safe
- {
- this.subscribe = subscribe;
- }
- public CreateObservable(Func<IObserver<T>, IDisposable> subscribe, bool isRequiredSubscribeOnCurrentThread)
- : base(isRequiredSubscribeOnCurrentThread)
- {
- this.subscribe = subscribe;
- }
- protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
- {
- observer = new Create(observer, cancel);
- return subscribe(observer) ?? Disposable.Empty;
- }
- class Create : OperatorObserverBase<T, T>
- {
- public Create(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
- {
- }
- public override void OnNext(T value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- internal class CreateObservable<T, TState> : OperatorObservableBase<T>
- {
- readonly TState state;
- readonly Func<TState, IObserver<T>, IDisposable> subscribe;
- public CreateObservable(TState state, Func<TState, IObserver<T>, IDisposable> subscribe)
- : base(true) // fail safe
- {
- this.state = state;
- this.subscribe = subscribe;
- }
- public CreateObservable(TState state, Func<TState, IObserver<T>, IDisposable> subscribe, bool isRequiredSubscribeOnCurrentThread)
- : base(isRequiredSubscribeOnCurrentThread)
- {
- this.state = state;
- this.subscribe = subscribe;
- }
- protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
- {
- observer = new Create(observer, cancel);
- return subscribe(state, observer) ?? Disposable.Empty;
- }
- class Create : OperatorObserverBase<T, T>
- {
- public Create(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
- {
- }
- public override void OnNext(T value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- internal class CreateSafeObservable<T> : OperatorObservableBase<T>
- {
- readonly Func<IObserver<T>, IDisposable> subscribe;
- public CreateSafeObservable(Func<IObserver<T>, IDisposable> subscribe)
- : base(true) // fail safe
- {
- this.subscribe = subscribe;
- }
- public CreateSafeObservable(Func<IObserver<T>, IDisposable> subscribe, bool isRequiredSubscribeOnCurrentThread)
- : base(isRequiredSubscribeOnCurrentThread)
- {
- this.subscribe = subscribe;
- }
- protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
- {
- observer = new CreateSafe(observer, cancel);
- return subscribe(observer) ?? Disposable.Empty;
- }
- class CreateSafe : OperatorObserverBase<T, T>
- {
- public CreateSafe(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
- {
- }
- public override void OnNext(T value)
- {
- try
- {
- base.observer.OnNext(value);
- }
- catch
- {
- Dispose(); // safe
- throw;
- }
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- }
|