Пример #1
0
        public MsgDispathcer()
        {
            observers     = new List <IObserver <Msg> >();
            mMsgDispather = AnonymousObservable.Create <Msg>(observer =>
            {
                observers.Add(observer);//对观察者自定义的注册行为



                return(Disposable.Create(() => observers.Remove(observer)));//对观察者自定义的注销行为
            });
        }
Пример #2
0
 /// <summary>
 /// 已经学习
 /// </summary>
 /// <param name="source">Source.</param>
 /// <param name="predicate">Predicate.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static IObservable <T> Where <T>(this IObservable <T> previousObservable, Func <T, bool> predicate)
 {
     return(AnonymousObservable.Create <T>(nextObserver =>                     //看上去不一样 神经病啊 明明一样 为什么要这么写?
     {
         return previousObservable.Subscribe(AnonymousObserver.Create <T>(x => //关键字 同样是 新的观察者
         {                                                                     //也就是说对这个obs注册的观察者 自动转换成一个新的观察者 !!!
             if (predicate(x))
             {
                 nextObserver.OnNext(x);
             }
         }, nextObserver.OnError, nextObserver.OnCompleted));
     }));
 }
Пример #3
0
        public static IObservable <T> Merge <T>(this IObservable <IObservable <T> > sources)
        {
            return(AnonymousObservable.Create <T>(observer =>
            {
                var group = new CompositeDisposable();

                var first = sources.Subscribe(innerSource =>
                {
                    var d = innerSource.Subscribe(observer.OnNext);
                    group.Add(d);
                }, observer.OnError, observer.OnCompleted);

                group.Add(first);

                return group;
            }));
        }
Пример #4
0
        public static IObservable <T> ObserveOn <T>(this IObservable <T> source, IScheduler scheduler)
        {
            return(AnonymousObservable.Create <T>(observer =>
            {
                var group = new CompositeDisposable();

                var first = source.Subscribe(x =>
                {
                    var d = scheduler.Schedule(() => observer.OnNext(x));
                    group.Add(d);
                }, observer.OnError, observer.OnCompleted);

                group.Add(first);

                return group;
            }));
        }
Пример #5
0
        // TODO:need scheduler
        /// <summary>
        /// 已经讲完
        /// </summary>
        /// <param name="start">Start.</param>
        /// <param name="count">Count.</param>
        public static IObservable <int> Range(int start, int count)
        {
            return(AnonymousObservable.Create <int>(observer =>
            {
                try
                {
                    for (int i = 0; i < count; i++)
                    {
                        observer.OnNext(start++);
                    }
                    observer.OnCompleted();
                }
                catch (Exception ex)
                {
                    observer.OnError(ex);
                }

                // TODO:Cancellable!
                return Disposable.Empty;
            }));
        }
Пример #6
0
        //static IEnumerator PostWWW(string url, WWWForm form, Action<string> onSuccess, Action<string> onError)
        //{

        //    using (var www = new WWW(url, form))
        //    {

        //    }
        //}

        //public static IObservable<string> Post(string url)
        //{

        //}

        public static IObservable <string> Get(string url)
        {
            return(AnonymousObservable.Create <string>(observer =>
            {
                var e = GetWWW(url, x =>  //断点1
                {
                    try
                    {
                        observer.OnNext(x);//断点2
                        observer.OnCompleted();
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                    }
                }, x => observer.OnError(new Exception(x)));
                //断点3
                GameLoopDispatcher.StartCoroutineRx(e);//开启一个协程 上面这个e直接替换这个e也是可以的
                //如上所示三个断点 经测试 执行顺序是1 3 2 也就是说 很正常的注册流程 开启一个协程就结束了 并没有花里胡哨的操作
                return Disposable.Empty;
            }));
        }