This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public IObservable<KeyValuePair<string, bool>> ValidateUrlAsync(string uri) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IScheduler | |
{ | |
IDisposable Schedule(Action action); | |
IDisposable Schedule(Action action, TimeSpan dueTime); | |
DateTimeOffset Now { get; } | |
} |
OlderNewer