Skip to content

Instantly share code, notes, and snippets.

@dlhartveld
Last active December 13, 2015 18:39
Show Gist options
  • Save dlhartveld/4957285 to your computer and use it in GitHub Desktop.
Save dlhartveld/4957285 to your computer and use it in GitHub Desktop.
A Rx.NET hands-on-lab ex. 7 variant that uses TAP This version allows the user to type in a prefix string, for which the results will be looked up at the dictionary service. The results will be printed on the console.
static DictServiceSoapClient client = new DictServiceSoapClient("DictServiceSoapClient");
static TextBox txt = new TextBox();
static Form form = new Form() { Controls = { txt } };
static void Work()
{
var input = Observable.FromEventPattern(txt, "TextChanged")
.Throttle(TimeSpan.FromMilliseconds(200))
.Select(x => ((TextBox)x.Sender).Text)
.Where(x => x.Length > 2)
.DistinctUntilChanged()
.Do(x => Console.WriteLine("Input changed: {0}", x));
var words = from term in input
from response in RetrieveMatchesInDictForPrefix(term).TakeUntil(input)
from word in WordsIn(response)
select word;
using (words.Subscribe(
el => Console.WriteLine("Got word: {0}", el),
ex => Console.WriteLine("Something went wrong: {0}", ex.Message),
() => Console.WriteLine("Completed.")
))
{
Application.Run(form);
}
}
static IObservable<DictionaryWord[]> RetrieveMatchesInDictForPrefix(string prefix)
{
return Observable.FromAsync(() => RetrieveMatchInDictAsync(prefix));
}
static async Task<DictionaryWord[]> RetrieveMatchInDictAsync(string prefix)
{
return await client.MatchInDictAsync("wn", prefix, "prefix");
}
static IObservable<string> WordsIn(DictionaryWord[] response)
{
return response.ToObservable().Select(word => word.Word);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment