Esempio n. 1
0
        public static IObservable <double> Animate(
            this IObservable <double> destinations,
            Func <double, double, IObservable <double> > animate)
        {
            return(Observable
                   .Create <double>(observer =>
            {
                var results = new BehaviorSubject <double>(double.NaN);

                return Disposable.Combine(
                    results,

                    results.Skip(1).Subscribe(observer),

                    destinations.SubscribeUsing(destination =>
                {
                    var source = results.Value;

                    if (double.IsNaN(source))
                    {
                        // No previous value, jump to destination
                        results.OnNext(destination);
                        return Disposable.Empty;
                    }

                    // we have a source and a destination, let's animate between them
                    // and emit the results (until we're disposed by a new destination)
                    return animate(source, destination).Subscribe(results.OnNext);
                }));
            })
                   .Replay(1).RefCount());
        }
Esempio n. 2
0
        internal RedisLogger(string key, ILog log, IRedisConnectionFactory redisConnectionFactory)
        {
            this.key = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", log.Logger.Name, key);
            this.log = log;
            this.messagesSubject = new ReplaySubject<Tuple<string, string>>(100, TimeSpan.FromSeconds(5));
            this.retry = new BehaviorSubject<bool>(false);

            var redisOnConnectionAction = new Action<Task<RedisConnection>>(task =>
            {
                if (task.IsCompleted && !task.IsFaulted)
                {
                    Interlocked.CompareExchange<RedisConnection>(ref this.redisConnection, task.Result, null);
                    subscription = messagesSubject.TakeUntil(retry.Skip(1)).Subscribe((item) => 
                        {
                            redisConnection.Publish(item.Item1, item.Item2).ContinueWith(taskWithException =>
                                {
                                    taskWithException.Exception.Handle(ex => true);
                                }, TaskContinuationOptions.OnlyOnFaulted);
                        });
                }
            });

            var redisOnErrorAction = new Action<ErrorEventArgs>(ex =>
                {
                    if (ex.IsFatal)
                    {
                        retry.OnNext(true);
                        Interlocked.Exchange<RedisConnection>(ref this.redisConnection, null);
                    }
                });

            Action subscribeAction = () =>
            {
                var connectionTask = redisConnectionFactory.CreateRedisConnection();
                connectionTask.ContinueWith(taskConnection =>
                    {
                        if (!taskConnection.IsFaulted)
                        {
                            taskConnection.ContinueWith(redisOnConnectionAction);
                            taskConnection.Result.Error += (_, err) => redisOnErrorAction(err);
                        }
                        else
                        {
                            taskConnection.Exception.Handle(_ => true);
                            this.retry.OnNext(true);
                        }
                    });
            };

            retry.Subscribe(val =>
                {
                    if (val)
                        Observable.Timer(TimeSpan.FromSeconds(10)).Subscribe(_ => subscribeAction()); 
                    else
                        subscribeAction();
                });
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReactiveCommand"/> class.
        /// </summary>
        /// <param name="canExecute">if set to <c>true</c> [can execute].</param>
        public ReactiveCommand(bool canExecute = false)
        {
            _canExecuteSource = new BehaviorSubject <bool>(canExecute);
            _canExecuteSource
            .Zip(_canExecuteSource.Skip(1),
                 (x, y) =>
                 x != y)
            .Where(x =>
                   x && CanExecuteChanged != null)
            .Subscribe(_ =>
                       CanExecuteChanged(this, EventArgs.Empty));


            _executeSource = new Subject <EventPattern <EventCommandEventArgs> >();
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReactiveCommand"/> class.
        /// </summary>
        /// <param name="canExecute">if set to <c>true</c> [can execute].</param>
        public ReactiveCommand(bool canExecute = false)
		{
			_canExecuteSource = new BehaviorSubject<bool>(canExecute);
			_canExecuteSource
				.Zip(_canExecuteSource.Skip(1),
					(x, y) =>
						x != y)
				.Where(x =>
					x && CanExecuteChanged != null)
				.Subscribe(_ =>
					CanExecuteChanged(this, EventArgs.Empty));


			_executeSource = new Subject<EventPattern<EventCommandEventArgs>>();
		}
Esempio n. 5
0
 private void InitIpAddressProperty()
 {
     Observable
     .Defer(() => Observable.Return(GetTurncatedIpAddress()))
     .SelectMany(turncatedIp => BlobCache
                 .UserAccount
                 .GetObject <string>("ipAddress")
                 .Catch(Observable.Return($"{turncatedIp}.0"))
                 .Merge(_hostAddressSubject
                        .Skip(1)
                        .DoWhere(x => x == 256, _ => ConnectionState = ConnectionState.Failed("Couldn't connect to Clock"))
                        .Where(x => x < 256 && ConnectionState.Type != StateType.Connected)
                        .Select(x => $"{turncatedIp}.{x}")))
     .Catch <string, Exception>(HandleException <string>)
     .ToPropertyEx(this, x => x.IpAddress);
 }
Esempio n. 6
0
        public IObservable <Position> Listen(int minUpdateTime, double minUpdateDist, bool includeHeading = false)
        {
            DateTimeOffset?lastStamp = null;
            Position       lastPos   = null;

            return(_latestPosition.Skip(1)
                   .Timestamp(_scheduler)
                   .Where(x => {
                var ret = lastStamp == null || x.Timestamp - lastStamp > TimeSpan.FromMilliseconds(minUpdateTime);
                lastStamp = x.Timestamp;
                return ret;
            })
                   .Where(x => {
                var ret = lastPos == null || distForPos(lastPos, x.Value) > minUpdateDist;
                lastPos = x.Value;
                return ret;
            })
                   .Select(x => x.Value));
        }
Esempio n. 7
0
        public void Test3()
        {
            IObservable <int> CreateRecursive(int count)
            {
                var source   = new BehaviorSubject <int>(0);
                var transfer = source.Select(x => ++ x);
                var guard    = source.Skip(count).Subscribe(_ => source.OnCompleted());

                return(Observable.Create <int>(observer => new CompositeDisposable
                {
                    source.Subscribe(observer),
                    transfer.Subscribe(source),
                    guard
                }));
            }

            var res = CreateRecursive(5).ToEnumerable();

            CollectionAssert.AreEqual(new [] { 0, 1, 2, 3, 4 }, res);
        }