public static IObservable <Unit> ForEachAsync <T>(this IAsyncStreamReader <T> stream, Action <T> action)
        {
            return(Observable.CreateWithState <Unit, Tuple <IAsyncStreamReader <T>, Action <T> > >(Tuple.Create(stream, action), (state0, observer) =>
            {
                var disp = new MultipleAssignmentDisposable();

                var worker = new AsyncStreamReaderForEachAsync_ <T>(disp, state0.Item1, state0.Item2, observer);
                worker.ConsumeNext();

                return disp;
            }));
        }
        /// <summary>
        /// 指定されたファイルを読み込む
        /// 読み込み処理は同期的に実行するためキャンセルはできない
        /// </summary>
        private IObservable <string> ReadFile(string path)
        {
            return(Observable.CreateWithState <string, string>(path, (p, observer) =>
            {
                using (var r = new StreamReader(p))
                {
                    observer.OnNext(r.ReadToEnd());
                }

                observer.OnCompleted();

                return Disposable.Empty;
            }));
        }
Пример #3
0
    private IObservable <int> CreateCountObservable(int count)
    {
        if (count <= 0)
        {
            return(Observable.Empty <int>());
        }

        return(Observable.CreateWithState <int, int>(state: count, subscribe: (max, observer) =>
        {
            for (var i = 0; i < max; i++)
            {
                observer.OnNext(max);
            }

            observer.OnCompleted();

            return Disposable.Empty;
        }));
    }
        public static IObservable <T> AsObservable <T>(this IAsyncStreamReader <T> stream, bool observeOnMainThread = true, IDisposable streamingResult = null)
        {
            var seq = Observable.CreateWithState <T, Tuple <IAsyncStreamReader <T>, IDisposable> >(Tuple.Create(stream, streamingResult), (state, observer) =>
            {
                var disp = new MultipleAssignmentDisposable();
                var b    = new AsyncStreamReaderAsObservable_ <T>(disp, state.Item1, observer, state.Item2);
                b.ConsumeNext();

                if (state.Item2 == null)
                {
                    return(disp);
                }
                else
                {
                    return(StableCompositeDisposable.Create(disp, state.Item2));
                }
            });

            return((observeOnMainThread) ? seq.ObserveOnMainThread() : seq);
        }
        /// <summary>
        /// 指定個数、連続した値を発行する(Rangeと同じ挙動)Observableを作って返す
        /// </summary>
        /// <param name="count">発行個数</param>
        /// <returns></returns>
        IObservable <int> CreateCountObservable(int count)
        {
            // countが0以下の場合はOnCompletedメッセージのみを返す
            if (count <= 0)
            {
                return(Observable.Empty <int>());
            }

            // 指定した数だけ連続する値を発行する
            return(Observable.CreateWithState <int, int>(
                       state: count,
                       subscribe: (maxCount, observer) =>
            {
                // 第一引数にstateで指定した値(この場合はcount)が渡される
                // つまり maxCount = count
                for (int i = 0; i < maxCount; i++)
                {
                    observer.OnNext(maxCount);
                }

                observer.OnCompleted();
                return Disposable.Empty;
            }));
        }