Example #1
0
        public static Action <Action> ForEach <T>(this IEnumerable <T> source, Action <Action> ready, Action <T, int, Action> handler, Action done)
        {
            var c = new FutureStream();
            var e = default(IEnumerator <T>);
            var i = -1;
            var r = new Future();

            var MoveNext = default(Action <Action>);

            //Console.WriteLine("ForEach");

            MoveNext =
                SignalNext =>
            {
                if (e.MoveNext())
                {
                    //Console.WriteLine("ForEach handler");

                    i++;
                    c.Continue(MoveNext);

                    //Console.WriteLine("ForEach handler invoke");

                    handler(e.Current, i, SignalNext);
                }
                else
                {
                    //Console.WriteLine("ForEach done");

                    e.Dispose();
                    e        = null;
                    c        = null;
                    MoveNext = null;

                    if (done != null)
                    {
                        done();
                    }

                    r.Signal();
                }
            };

            var SignalFirst = c.Continue(
                SignalNext =>
            {
                //Console.WriteLine("ForEach SignalFirst");

                e = source.AsEnumerable().GetEnumerator();

                MoveNext(SignalNext);
            }
                );

            if (ready != null)
            {
                ready(SignalFirst);
            }
            else
            {
                SignalFirst();
            }

            return(r.Continue);
        }
Example #2
0
 public FutureStream()
 {
     this.Gate = new Future();
 }