public IDisposable Subscribe(IObserver <T> observer) { Source.Subscribe(observer); return(Disposable.Create(() => Source.Select(_ => new Unit()) .Amb(Observable.Timer(Timeout).Select(_ => new Unit())) .Subscribe(_ => Subscription.Dispose()) )); }
public ShellViewModel(IProvider provider) { _topBrush = new ObservableProperty <Brush>(DefaultBrush, this, () => TopBrush); _leftBrush = new ObservableProperty <Brush>(DefaultBrush, this, () => LeftBrush); _rightBrush = new ObservableProperty <Brush>(DefaultBrush, this, () => RightBrush); _bottomBrush = new ObservableProperty <Brush>(DefaultBrush, this, () => BottomBrush); _leftTopShelf = new ObservableProperty <Brush>(DefaultBrush, this, () => LeftTopShelf); _leftMiddleShelf = new ObservableProperty <Brush>(DefaultBrush, this, () => LeftMiddleShelf); _leftBottomShelf = new ObservableProperty <Brush>(DefaultBrush, this, () => LeftBottomShelf); _rightTopShelf = new ObservableProperty <Brush>(DefaultBrush, this, () => RightTopShelf); _rightMiddleShelf = new ObservableProperty <Brush>(DefaultBrush, this, () => RightMiddleShelf); _rightBottomShelf = new ObservableProperty <Brush>(DefaultBrush, this, () => RightBottomShelf); _fps = new ObservableProperty <int>(0, this, () => Fps); _source = provider.Processed.Select(processed => processed.Layers.FirstOrDefault()).Where(value => value != null).ObserveOnDispatcher().Publish(); /* * _source.Select(processed => processed.Left) * .Select(colors => ToShelveColor(colors, Shelf.Top)) * .StartWith(DefaultColor) * .Buffer(2) * .Select(colors => new { Shelf = Shelf.Top, Color = colors[1], Difference = ColorDifference.FindDifference(colors[0], colors[1]) }); * * _source.Select(processed => processed.Left).Select(colors => ToShelveColor(colors, Shelf.Middle)); * _source.Select(processed => processed.Left).Select(colors => ToShelveColor(colors, Shelf.Bottom)); */ _brushSubscription = new CompositeDisposable( _source.Buffer(TimeSpan.FromSeconds(1)).Select(buffer => buffer.Count).Subscribe(_fps), _source.Select(processed => processed.Top).Select(colors => AsGradientBrush(colors, Side.Top)).Subscribe(_topBrush), _source.Select(processed => processed.Left).Select(colors => AsGradientBrush(colors, Side.Left)).Subscribe(_leftBrush), _source.Select(processed => processed.Right).Select(colors => AsGradientBrush(colors, Side.Right)).Subscribe(_rightBrush), _source.Select(processed => processed.Bottom).Select(colors => AsGradientBrush(colors, Side.Bottom)).Subscribe(_bottomBrush) ); }
private IObservable <TOutput> GetOutputStreamImpl() { return(Observable.Create <TOutput>(obs => { _logger.LogInformation("Got stream request from client"); var sotw = _cacheStatedUpdates.TakeUntilInclusive(x => !x.IsStale) .Select(CreateResponseFromCacheState); return sotw.Concat(_events.Select(evt => MapSingleEventToUpdateDto(_cacheState.State, evt))) .Merge(_connectionChanged.Where(x => !x.IsConnected).Select(_ => GetDisconnectedStaleUpdate())) .Where(IsValidUpdate) .Subscribe(obs); })); }
public IDisposable Schedule(Job job, CancellationToken cancellationToken = default) { var unschedule = _scheduler // .ToList the results so that all triggers have the chance to evaluate the tick. .Select(tick => job.Triggers.Where(t => t.Matches(tick)).ToList()) //.Where(tick => job.Triggers.Where(t => t.Matches(tick)).ToList().Any()) .Where(triggers => triggers.Any()) .Subscribe(_ => job.Execute(cancellationToken)); return(Disposable.Create(() => { job.Continuation.Wait(job.UnscheduleTimeout); unschedule.Dispose(); })); }
private IObservable <TOutput> GetOutputStreamImpl() { return(Observable.Create <TOutput>(obs => { if (_log.IsEnabled(LogEventLevel.Information)) { _log.Information("Got stream request from client"); } var sotw = _stateOfTheWorldUpdates.TakeUntilInclusive(x => !x.IsStale) .Select(CreateResponseFromStateOfTheWorld); return sotw.Concat(_events.Select(evt => MapSingleEventToUpdateDto(_stateOfTheWorldContainer.StateOfTheWorld, evt))) .Merge(_connectionChanged.Where(x => !x.IsConnected).Select(_ => GetDisconnectedStaleUpdate())) .Where(IsValidUpdate) .Subscribe(obs); })); }
public FSViewModel(IObservable <EventPattern <RoutedEventArgs> > uiAddQuery) { _storeSubject = GetFileSystemStream().Replay(); //Compute aggregates - 1st attempt IObservable <double> fileLenthgs = _storeSubject.Select(fcf => (double)fcf.Length); IObservable <int> fileCounts = fileLenthgs.Scan(0, (count, _) => count + 1); IObservable <double> fileLenRunningSums = fileLenthgs.Scan((sum, next) => sum + next); IObservable <FileChangeAggregate> aggCountSum = fileCounts.Zip(fileLenRunningSums, (cnt, sum) => new FileChangeAggregate() { Sum = sum, Count = cnt }); IObservable <FileChangeAggregate> aggCountSumMean = aggCountSum.Zip(fileLenthgs.Mean(), (fca, mean) => { fca.Mean = mean; return(fca); }); IObservable <FileChangeAggregate> aggCountSumMeanStdDev = aggCountSumMean.Zip(fileLenthgs.StdDev(), (fca, stddev) => { fca.StdDev = stddev; return(fca); }); uiAddQuery.Subscribe(_ignore => { Func <FileChangeFact, bool> fltr = _ => true; if (!String.IsNullOrEmpty(FilterPath)) { string fp = FilterPath; // need to copy the value from the UI based DP so that the RX based Where() clause can safely access it fltr = fci => fci.Path.StartsWith(fp, StringComparison.CurrentCultureIgnoreCase); } QuerySubscriptions.Add(new QuerySubscriptionDO(FilterPath, _storeSubject.Where(fltr))); }); //Wire up UI FSItems = new ObservableCollection <FileChangeFact>(); QuerySubscriptions = new ObservableCollection <QuerySubscriptionDO>(); aggCountSumMeanStdDev.ObserveOnDispatcher().Subscribe(fca => TotalAggregate = fca); FSItems.Insert(_storeSubject); _storeSubject.Connect(); }
public async Task <ExchangeOrder> SubmitOrder(OrderSide oSide, OrderType oType, decimal price, decimal size) { //use factory to create an Order with the right ID var order = _orderFactory.CreateOrder(oSide, oType, price, size); //Define the Order's lifetime stream //IObservable<ExchangeOrder> orderLifeTimeStream = _orderBookStream.Select(ob => ob.Orders[order.ID]) // .TakeWhileInclusive(o => (o.Status != ExchangeOrderStatus.Closed) || (o.Status != ExchangeOrderStatus.Canceled)) // .LastAsync() // .Do(Console.WriteLine); IObservable <ExchangeOrder> firstCloseOrder = _orderBookStream.Select(ob => ob.Orders[order.ID]) .Do(Console.WriteLine) .Where(o => (o.Status == ExchangeOrderStatus.Closed) || (o.Status == ExchangeOrderStatus.Canceled)) .FirstAsync().Replay(); //pass order to a subject that is merged with the random orders _submittedOrders.OnNext(order); //wait until the order is fully filled var orderAtEnd = await firstCloseOrder; return(orderAtEnd); }
public IObservable <TSlice> Select <TSlice>(Func <TApplicationState, TSlice> selector) { return(storeStateStream.Select(state => selector(state)).DistinctUntilChanged()); }
private IObservable <decimal> Create(int divisor) { return(_countersObservable.Select(x => Decimal.Round((decimal)WorkingSetPrivate(x.MemoryCounter) / divisor, 2)) .DistinctUntilChanged()); }
// NOTE: Works with discreet intervals. Needs refactoring to work with rolling/continuous intervals. public static IObservable <T> LimitTo <T, TPeriod>( this IObservable <T> source, int count, IObservable <TPeriod> periodSource, IScheduler scheduler = null) { return(Observable.Create <T>(o => { scheduler = scheduler ?? Scheduler.Default; var sem = new SemaphoreSlim(0, count); var queue = new ConcurrentQueue <T>(); var queueComplete = new Subject <Unit>(); bool isSourceComplete = false; IConnectableObservable <T> publishedSource = source.Publish(); IDisposable sourceSub = publishedSource .Finally(() => isSourceComplete = true) .Subscribe(Ignore, o.OnError); IObservable <Unit> timer = periodSource .TakeUntil(queueComplete) .Select(_ => { sem.ReleaseMax(count); return (Unit.Default); }); bool GetNext(out T value) { value = default(T); if (queue.IsEmpty) { if (isSourceComplete) { queueComplete.OnNext(Unit.Default); } return false; } return sem.Wait(0) && queue.TryDequeue(out value); } IEnumerable <T> GetQueueValues(Unit dummy) { while (GetNext(out T value)) { yield return value; } } IDisposable sub = publishedSource .Select(x => { queue.Enqueue(x); return Unit.Default; }) .Merge(timer, scheduler) .SelectMany(GetQueueValues) .Subscribe(o); return new CompositeDisposable(sub, sourceSub, publishedSource.Connect(), queueComplete, sem); })); }