views:

50

answers:

1

I'm still fairly new to Rx and am having a hard time figuring out how to express this (seemingly) simple subscription. I'm looking for something like this:

  1. Start: InTransaction.Where(inTransaction => inTransaction)
  2. If: ItemChanged or On FlagChanged, let Changed = true
  3. End: InTransaction.Where(inTransaction => !inTransaction)

All of the above are observables. So upon a transaction starting, I want to start paying attention to change notifications, and no matter how many of them come in, I want to just remember that any have been received. When the transaction ends, I want to call my handler to update the visual state.

There's a lot of fun examples online showing how to do this for mouse drags. The only problem I'm having is that I don't want to get every single changed event. I want to just know if any have been hit before the transaction ends.

Can anyone help point me on the right track?

Update: my current algorithm looks something like this:

bool pendingRefresh = false;

Observable
    .Merge(
        _selectionChanged,
        _objectManager
            .PropertiesChanged
            .Where(objects => objects.Contains(_selectedObject)))
    .Subscribe(_ => pendingRefresh = true);

_actionManager
    .IsInTransaction.Where(isIn => !isIn)
    .Throttle(TimeSpan.FromSeconds(0.15))
    .Subscribe(_ => 
        {
            if (pendingRefresh)
            {
                pendingRefresh = false;
                Refresh();
            }
        });

Works fine but I was wondering if I could get away with a single subscription.

+2  A: 

Hi Scott,

looks like you're on the right track. If you don't care about how how many change notifications come in, look at the Any operator: public static IObservable Any(this IObservable source)

this will fire only once, the moment the first message ever comes in it will fire true, if OnCompleted happens without any message come in it will fire false.

Hope this helps.

Jeffrey

Jeffrey van Gogh