Scheduling a IEnumerable periodically with .NET reactive extensions

后端 未结 5 485
闹比i
闹比i 2021-01-24 05:21

Say for example I have an enumerable

dim e = Enumerable.Range(0, 1024)

I\'d like to be able to do

dim o = e.ToObservable(Timesp         


        
5条回答
  •  盖世英雄少女心
    2021-01-24 05:39

    I have also looked for the solution and after reading the intro to rx made my self one: There is an Observable.Generate() overload which I have used to make my own ToObservable() extension method, taking TimeSpan as period:

    public static class MyEx {
        public static IObservable ToObservable(this IEnumerable enumerable, TimeSpan period) 
        {
            return Observable.Generate(
                enumerable.GetEnumerator(), 
                x => x.MoveNext(),
                x => x, 
                x => x.Current, 
                x => period);
        }
        public static IObservable ToObservable(this IEnumerable enumerable, Func getPeriod) 
        {
            return Observable.Generate(
                enumerable.GetEnumerator(), 
                x => x.MoveNext(),
                x => x, 
                x => x.Current, 
                x => getPeriod(x.Current));
        }
    }
    

    Already tested in LINQPad. Only concerning about what happens with the enumerator instance after the resulting observable is e.g. disposed. Any corrections appreciated.

提交回复
热议问题