system.reactive

Merging a changing collection of observables

北城余情 提交于 2019-12-12 20:34:00
问题 We have a class Thing that implements IObservable<Thing> . In another class, there is a collection of Thing s , and that class needs to react to updates from all those observables in a unified manner. The obvious way to do that is Observable.Merge() , and that generally works; however , when the collection changes, we also need to subscribe to any new Thing s in our merged subscription (and in theory unsubscribe from all the removed ones, but that seems less problematic - they just won't

Reactive Rx zip queue in .Net

南楼画角 提交于 2019-12-12 20:05:01
问题 I am fairly new to the concept of reactive programming. I am using Bonsai, which exposes some but not all .Net rx commands through c#. I am trying to get a behavior like this marble diagram: input1: ---1--------2--------3--------4--------5--------6--------7 input2: -------abc----------------------------------def----------- result: ------------a--------b--------c--------c---------d-------e Basically, input 2 generates waves of events that should be stored in a queue. Input 1 acts as a trigger

Learning Rx: How to use .Scan on the output of .Window for an observable sequence of bool values

左心房为你撑大大i 提交于 2019-12-12 15:48:14
问题 I have a sequence of true false values like so var alternatingTrueFalse = Observable.Generate( true, _ => true, x => !x, x => x, _ => TimeSpan.FromMilliseconds(new Random().Next(2000))) .Take(20).Publish(); alternatingTrueFalse.Connect(); var buffered = alternatingTrueFalse .Buffer(TimeSpan.FromMilliseconds(500)) .Subscribe(x => Console.WriteLine($"{TimeStamp} {string.Join(",", x)}")); I want to look at the sequence in terms of 500 ms (max) windows / buffers. If there is only one true value

How to correlate function inputs and outputs?

非 Y 不嫁゛ 提交于 2019-12-12 14:47:21
问题 Consider the simple program below. It has an observable of integers and a function to calculate whether the most recently published integer is even or odd. Unexpectedly, the program reports whether the most recent number is even/odd BEFORE it reports that the number changed. static void Main(string[] args) { int version = 0; var numbers = new Subject<int>(); IObservable<bool> isNumberEven = numbers.Select(i => i % 2 == 0); isNumberEven .Select(i => new { IsEven = i, Version = Interlocked

How can I observe values in a non blocking way using Rx?

做~自己de王妃 提交于 2019-12-12 12:12:46
问题 I'm trying to observe on a timer which its handler is longer then the interval. in order to do so I want to schedule the observation on some kind of threadPool, task pool or something. I tried threadpool, taskpool, and newthread and none of them worked. Does anyone knows how to do it ? example: var disposable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMilliseconds(100)).ObserveOn(Scheduler.NewThread). Subscribe(x => { count++; Thread.Sleep(TimeSpan.FromMilliseconds(1000)); }); Thread

How to get the latest value from a ReplaySubject<T> before completion

喜欢而已 提交于 2019-12-12 12:07:42
问题 I need a way of grabbing the most recent item added to a ReplaySubject that matches certain criteria. The sample code below does what I need it to do but it doesn't feel like the correct approach: static void Main(string[] args) { var o = new ReplaySubject<string>(); o.OnNext("blueberry"); o.OnNext("chimpanzee"); o.OnNext("abacus"); o.OnNext("banana"); o.OnNext("apple"); o.OnNext("cheese"); var latest = o.Where(i => i.StartsWith("b")) .Latest().First(); Console.WriteLine(latest); Console

Throttle only if specific condition met

随声附和 提交于 2019-12-12 11:53:09
问题 I have an observable that I am subscribing on. This obsevable will be returning an object that has a property called ActivationType that can be set multiple times. What I am trying to achieve is log a message whenever ActivationType is set to "Type1". However, if ActivationType is set to "Type2", log the message only once and wait for 30 seconds before logging again if ActivationType is "Type2". So if I have: myObservable .Where(o => o.ActivationType == "Type1" || o.ActivationType == "Type2")

Cross thread exception when using RX Throttle

こ雲淡風輕ζ 提交于 2019-12-12 11:15:52
问题 I am getting Invalid cross-thread access. When using RX Throttle Here is my code: yObs.SubscribeOnDispatcher() .DistinctUntilChanged() .Throttle(TimeSpan.FromMilliseconds(33)) .SkipWhile(y => !_isDragging) .Subscribe(y => { // Exception when trying to access image image.RenderTransform = new CompositeTransform() { TranslateY = -y }; _vm.UpdateContentDrag(y / image.ActualHeight * 100); }); But if I omit throttle everything works. As far as I understand Throttle uses thread pool so OnNext doesn

Rx - unsubscribing from events

ε祈祈猫儿з 提交于 2019-12-12 10:38:06
问题 I have an INotifyPropertyChanged object, Foo. I turn Foo into an observable stream of events using Rx's FromEvent method: var myFoo = new Foo(); var eventStream = Observable.FromEvent<PropertyChangedEventArgs>(myFoo, "PropertyChanged"); Now I want to listen for a particular property changed, and if .Progress == 100, unsubscribe: eventStream .Where(e => myFoo.Progress == 100) .Subscribe(OnFooFinished); How can I unsubscribe when Progress == 100? If I add a .Take(1) call after the .Where clause

How to take first occurrence and then supress events for 2 seconds (RxJS)

给你一囗甜甜゛ 提交于 2019-12-12 08:43:38
问题 I think RxJS should perfectly fit to supress dublicate button clicks for 2 seconds. However, Im struggleing with the implementation. var $button = $('#myButton').button(); $button .toObservable("click") //.Throttle(2000) // Wouldn't fire the first event instantly :-( .Subscribe(function(){ alert('clicked'); }); I already created a jsFiddle for your convenience. You need to scroll down in this fiddle, because I just pasted Rx inside as I couldn't find a CDN. http://jsfiddle.net/cburgdorf/mUMFA