using System; using System.Collections.Generic; namespace UniRx.Operators { internal class ToArrayObservable : OperatorObservableBase { readonly IObservable source; public ToArrayObservable(IObservable source) : base(source.IsRequiredSubscribeOnCurrentThread()) { this.source = source; } protected override IDisposable SubscribeCore(IObserver observer, IDisposable cancel) { return source.Subscribe(new ToArray(observer, cancel)); } class ToArray : OperatorObserverBase { readonly List list = new List(); public ToArray(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() { TSource[] result; try { result = list.ToArray(); } catch (Exception ex) { try { observer.OnError(ex); } finally { Dispose(); } return; } base.observer.OnNext(result); try { observer.OnCompleted(); } finally { Dispose(); }; } } } }