Skip to content

Instantly share code, notes, and snippets.

View anaisbetts's full-sized avatar

Ani Betts anaisbetts

View GitHub Profile
namespace TheApp
{
public partial class MainWindow : Window
{
IObservable<MouseEventArgs> foo;
public MainWindow()
{
var foo = Observable.FromEvent<MouseEventArgs>(this, "PreviewMouseMove")
.Where(x => { var pos = x.EventArgs.GetPosition(this); return (pos.X < 10 && pos.Y < 10); })
Test content
private static bool ValidateUri(string Uri)
{
try
{
var request = WebRequest.Create(Uri) as HttpWebRequest;
var response = request.GetResponse() as HttpWebResponse;
return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
public IObservable<KeyValuePair<string, bool>> ValidateUrlAsync(string uri)
IObservable<KeyValuePair<string, bool>> ValidateUriAsync(string Uri)
{
var request = WebRequest.Create(Uri);
// This gives us a "GetResponseAsync" function whose prototype is
// IObservable[WebResponse] GetResponseAsync() - just like the sync
// version, but the return value wrapped in IObservable.
var response_fetcher = Observable.FromAsyncPattern<WebResponse>(
request.BeginGetResponse, request.EndGetResponse);
IObservable<IDictionary<string, bool>> ValidateManyUrisAsync(string[] Uris)
{
// Aggregate will take a "future list" and return a future with only one item
// (Just like LINQ's Aggregate takes a list and returns a single value)
// For every result that ValidateUrlAsync provides, we will execute the
// Aggregate block. When SelectMany completes, Aggregate can finally
// return its one result
return Uris.ToObservable()
// Block until I get the result
// Equivalent of Task.Wait
var output = ValidateUriAsync("http://foo.com").First();
// Don't block, but notify me when we produce a result
// Equivalent(*) to Task.ContinueWith
ValidateUriAsync("http://bar.com").Subscribe(result => {
Console.WriteLine(result);
});
//
// Get the webpage and block till it finishes (aka make an async func
// into a sync func)
//
string contents = GetWebpageAsStringAsync("http://foo").First();
// ...or wire up a callback
GetWebpageAsStringAsync("http://foo").Subscribe(x => {
[Fact]
public void FetchImageFromSiteCommandTest()
{
// Replace the immediate scheduler with an event loop (a thread who just
// waits in the background to process stuff as it arrives, one at a time)
var origSched = RxApp.DeferredScheduler;
RxApp.DeferredScheduler = new EventLoopScheduler();
// MyCoolViewModel has an ICommand called FetchImageFromSite
var fixture = new MyCoolViewModel();
public interface IScheduler
{
IDisposable Schedule(Action action);
IDisposable Schedule(Action action, TimeSpan dueTime);
DateTimeOffset Now { get; }
}