1

It has been suggested to me that I ought to use Rx to debounce input for an AutoCompleteBox where the suggestion list comes from a web service. Obviously I don't want to run a slow query for updates on every keystroke, but on the other hand you also don't want too long a lag after the user pauses.

I had done it like this

public partial class MainPage : PhoneApplicationPage
{
  Timer _timerName;

  public MainPage()
  {
    InitializeComponent();
    _timerName = new Timer(QueryForNameSuggestions);
  }

  void QueryForNameSuggestions(object state)
  {
    //
  }

  private void searchtermName_TextChanged(object sender, RoutedEventArgs e)
  {
    //Each keystroke resets the timer, 500ms after you stop typing it queries
    _timerName.Change(500, Timeout.Infinite);
  }

}

which is easy to understand and works nicely; every keystroke (re)starts a half second wait culminating in a query.

I really have no idea how to do the same thing with Rx, but I'm willing to entertain the possibility that it will be an improvement. Could someone show me how this would be done with Rx, and explain what's better about the Rx way?

Rx: How can I respond immediately, and throttle subsequent requests seems close but I don't see how you'd detect incoming keystrokes without polling the value of the AutoCompleteBox, which doesn't seem like an improvement to me.

Community
  • 1
  • 1
Peter Wone
  • 17,965
  • 12
  • 82
  • 134
  • possible duplicate of [How can I combine IObservable.Throttle() with some other event source using Reactive Extensions?](http://stackoverflow.com/questions/19566517/how-can-i-combine-iobservablet-throttle-with-some-other-event-source-using-r) – James World Nov 03 '13 at 16:03
  • It's not a duplicate, I've read that and it doesn't give me any idea why Rx is a better idea. The code is a great deal harder to fathom and it's longer. You will note that the accepted answer is quite different from the accepted answer to the alleged duplicate. – Peter Wone Nov 03 '13 at 22:45
  • The accepted answer is a link without context, when that link rots, it will be useless (there's loads of comment on meta about why this is a bad thing). The linked duplicate does provide a specific solution to the coding problem, but doesn't get into the opinionated, subjective part about whether or not Rx is "better". I'm glad the link helped you though, it's a great talk. Bart de Smet (a genius with a wonderful accent IMHO :)) has posted lots of videos on Channel9 you might find interesting. – James World Nov 03 '13 at 23:33
  • I don't think link rot will be an issue in this case. MS keeps channel9 for ages and by the time it dies no one will care. I love his accent but not while I'm trying to absorb a mind-bending paradigm shift. – Peter Wone Nov 03 '13 at 23:48
  • Also, that's a fault with the answer, not the question. – Peter Wone Nov 04 '13 at 00:50

1 Answers1

2

Take a look at this demo: Curing Your Event Processing Blues with Reactive Extensions (Rx)

StaWho
  • 2,488
  • 17
  • 24
  • Thank you for reading and understanding the nature of my question. This is *exactly* what I wanted. If only he didn't sound like Swedish Chef :) – Peter Wone Nov 03 '13 at 22:48