using System; using System.Collections.Generic; namespace UniRx.Operators { internal class ToListObservable : OperatorObservableBase> { readonly IObservable source; public ToListObservable(IObservable source) : base(source.IsRequiredSubscribeOnCurrentThread()) { this.source = source; } protected override IDisposable SubscribeCore(IObserver> observer, IDisposable cancel) { return source.Subscribe(new ToList(observer, cancel)); } class ToList : OperatorObserverBase> { readonly List list = new List(); public ToList(IObserver> observer, IDisposable cancel) : base(observer, cancel) { } public override void OnNext(TSource value) { try { list.Add(value); // sometimes cause error on multithread } catch (Exception ex) { try { observer.OnError(ex); } finally { Dispose(); } return; } } public override void OnError(Exception error) { try { observer.OnError(error); } finally { Dispose(); } } public override void OnCompleted() { base.observer.OnNext(list); try { observer.OnCompleted(); } finally { Dispose(); }; } } } }