Flack
Flack

Reputation: 5899

Reactive extensions Sample or Throttle?

I have a listBox and am observing when items are selected:

selectedItems.Subscribe(DoWorkWIthSelectedItems);

The observable returns an array of viewModel items and in DoWorkWIthSelectedItems I go through the list of selected items and do some work. However, since the user may be spamming selections on/off pretty rapidly, whenever work is done on an item, I don't want any work to be done on that item again for 30 seconds. After 30 seconds, if the item is selected again, go ahead and work on it.

Is there an Rx way to do this? I am not sure if it would be Sample or Throttle. Furthermore, I don't know if with Rx I am able to distinguish between items in the array that are good to be worked on or should be ignored. Would I need an additional property on the viewModel item to indicate some 'working' state?

Thanks.

Upvotes: 4

Views: 1052

Answers (1)

Bryan Anderson
Bryan Anderson

Reputation: 16129

No matter what there's going to have to be some state. I think your simplest solution would be something like (pseudocode)

var recentlyUsed = new ConcurrentDictionary<T, DateTime>();
...
selectedItems
    .Do(/* remove expired items from recentlyUsed */)
    .Where(/* items are not in recently used */)
    .Do(/* add items to recently used */)
    .Subscribe(DoWorkWIthSelectedItems);  

Upvotes: 1

Related Questions