Browse files

Cleaned up dead code, made minor readability improvements.

  • Loading branch information...
1 parent c6e516a commit a655578cf6981a73f7b9ecc5d2380f98824f97ec @paulbatum committed Jul 19, 2009
Showing with 95 additions and 109 deletions.
  1. +2 −2 ButtonExtensions.cs
  2. +2 −62 EventWrapper.cs
  3. +1 −2 Extensions.cs
  4. +20 −15 Form1.cs
  5. +3 −3 IObservable.cs
  6. +2 −2 ObserverBuilder.cs
  7. +64 −0 ObserverCollection.cs
  8. +1 −0 Reactive.csproj
  9. +0 −23 SelectObservable.cs
View
4 ButtonExtensions.cs
@@ -4,10 +4,10 @@ namespace Reactive
{
public static class ButtonExtensions
{
- public static IObservable<MouseEventArgs> GetMouseDowns(this Button b)
+ public static IObservable<MouseEventArgs> GetMouseDowns(this Button button)
{
var wrapper = new EventWrapper<MouseEventArgs>();
- b.MouseDown += wrapper.Handle;
+ button.MouseDown += wrapper.Handler;
return wrapper;
}
}
View
64 EventWrapper.cs
@@ -1,75 +1,15 @@
using System;
-using System.Collections.Generic;
using System.Windows.Forms;
namespace Reactive
{
- public class ObserverCollection<T> : IObservable<T>
- {
- protected IList<IObserver<T>> _observers;
-
- public ObserverCollection()
- {
- _observers = new List<IObserver<T>>();
- }
-
- public virtual IDisposable Subscribe(IObserver<T> observer)
- {
- _observers.Add(observer);
- return new Disposer(() => Remove(observer));
- }
-
- public void NotifyNext(T t)
- {
- foreach (IObserver<T> observer in _observers)
- observer.OnNext(t);
- }
-
- public void NotifyDone()
- {
- foreach (IObserver<T> observer in _observers)
- observer.OnDone();
- }
-
- public void NotifyError(Exception e)
- {
- foreach (IObserver<T> observer in _observers)
- observer.OnError(e);
- }
-
- protected virtual void Remove(IObserver<T> observer)
- {
- _observers.Remove(observer);
- }
-
- public void Clear()
- {
- _observers.Clear();
- }
-
- protected class Disposer : IDisposable
- {
- private readonly Action _disposeAction;
-
- public Disposer(Action disposeAction)
- {
- _disposeAction = disposeAction;
- }
-
- public void Dispose()
- {
- _disposeAction();
- }
- }
- }
-
public class EventWrapper<T> : IObservable<T> where T : EventArgs
{
private readonly ObserverCollection<T> _observers = new ObserverCollection<T>();
- public void Handle(object sender, T t)
+ public void Handler(object sender, T eventArgs)
{
- _observers.NotifyNext(t);
+ _observers.NotifyNext(eventArgs);
}
public IDisposable Subscribe(IObserver<T> observer)
View
3 Extensions.cs
@@ -6,8 +6,7 @@ public static class Extensions
{
public static IObservable<TResult> Select<T, TResult>(this IObservable<T> observable, Func<T, TResult> func)
{
- return new SelectObservable<T, TResult>(observable, func);
- //return ObservableBuilder.Create((IObserver<TResult> observer) => observable.Subscribe(ObserverBuilder.Create(observer, (T a) => observer.OnNext(func(a)))));
+ return new SelectObservable<T, TResult>(observable, func);
}
public static IObservable<TResult> SelectMany<TSource, TCollection, TResult>(this IObservable<TSource> source, Func<TSource, IObservable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
View
35 Form1.cs
@@ -15,18 +15,23 @@ public Form1()
{
InitializeComponent();
- Func<MouseEventArgs, int> slowOperation = args =>
- {
- System.Threading.Thread.Sleep(3000);
- if(args.Button == MouseButtons.Middle)
- throw new Exception("MIDDLE BUTTON NOT ALLOWED!!!");
- return args.X;
- };
+ //Func<MouseEventArgs, int> slowOperation = args =>
+ //{
+ // System.Threading.Thread.Sleep(3000);
+ // if(args.Button == MouseButtons.Middle)
+ // throw new Exception("MIDDLE BUTTON NOT ALLOWED!!!");
+ // return args.X;
+ //};
+
+ //IObservable<string> messages = from md in button1.GetMouseDowns()
+ // from x in slowOperation.AsAsyncObservable(md)
+ // where md.Button == MouseButtons.Right
+ // select "Mouse down: " + x + "\n";
+
+ IObservable<MouseEventArgs> mouseEvents = button1.GetMouseDowns();
- IObservable<string> messages = from md in button1.GetMouseDowns()
- from x in slowOperation.AsAsyncObservable(md)
- where md.Button == MouseButtons.Right
- select "Mouse down: " + x + "\n";
+ IObservable<string> messages = from md in mouseEvents
+ select "Mouse down at: " + md.X + "\n";
messages.Subscribe(new TextBoxUpdater(textBox1));
}
@@ -40,25 +45,25 @@ public TextBoxUpdater(TextBox textBox)
_textBox = textBox;
}
- private void SetText(string text)
+ private void AppendText(string text)
{
Action textboxUpdater = () => _textBox.AppendText(text);
_textBox.BeginInvoke(textboxUpdater);
}
public void OnNext(string s)
{
- SetText(s);
+ AppendText(s);
}
public void OnDone()
{
- SetText("Done\n");
+ AppendText("Done\n");
}
public void OnError(Exception e)
{
- SetText("Error: " + e.Message);
+ AppendText("Error: " + e.Message);
}
}
View
6 IObservable.cs
@@ -9,7 +9,7 @@ public interface IObservable<T>
public interface IObserver<T>
{
- void OnNext(T t);
+ void OnNext(T item);
void OnDone();
void OnError(Exception e);
}
@@ -23,9 +23,9 @@ public DefaultObserver(Action<T> action)
_action = action;
}
- public void OnNext(T t)
+ public void OnNext(T item)
{
- _action(t);
+ _action(item);
}
View
4 ObserverBuilder.cs
@@ -20,9 +20,9 @@ public ObserverWrapper(IObserver<T1> observer, Action<T2> onNext)
_onNext = onNext;
}
- public void OnNext(T2 t)
+ public void OnNext(T2 item)
{
- _onNext(t);
+ _onNext(item);
}
public void OnDone()
View
64 ObserverCollection.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+
+namespace Reactive
+{
+ public class ObserverCollection<T> : IObservable<T>
+ {
+ protected IList<IObserver<T>> _observers;
+
+ public ObserverCollection()
+ {
+ _observers = new List<IObserver<T>>();
+ }
+
+ public virtual IDisposable Subscribe(IObserver<T> observer)
+ {
+ _observers.Add(observer);
+ return new Disposer(() => Remove(observer));
+ }
+
+ public void NotifyNext(T t)
+ {
+ foreach (IObserver<T> observer in _observers)
+ observer.OnNext(t);
+ }
+
+ public void NotifyDone()
+ {
+ foreach (IObserver<T> observer in _observers)
+ observer.OnDone();
+ }
+
+ public void NotifyError(Exception e)
+ {
+ foreach (IObserver<T> observer in _observers)
+ observer.OnError(e);
+ }
+
+ protected virtual void Remove(IObserver<T> observer)
+ {
+ _observers.Remove(observer);
+ }
+
+ public void Clear()
+ {
+ _observers.Clear();
+ }
+
+ protected class Disposer : IDisposable
+ {
+ private readonly Action _disposeAction;
+
+ public Disposer(Action disposeAction)
+ {
+ _disposeAction = disposeAction;
+ }
+
+ public void Dispose()
+ {
+ _disposeAction();
+ }
+ }
+ }
+}
View
1 Reactive.csproj
@@ -61,6 +61,7 @@
</Compile>
<Compile Include="IObservable.cs" />
<Compile Include="ObserverBuilder.cs" />
+ <Compile Include="ObserverCollection.cs" />
<Compile Include="SelectManyObservable.cs" />
<Compile Include="SelectObservable.cs" />
<Compile Include="Program.cs" />
View
23 SelectObservable.cs
@@ -19,27 +19,4 @@ public IDisposable Subscribe(IObserver<T2> observer)
}
}
- //public class ObservableBuilder
- //{
- // public static IObservable<T> Create<T>(Func<IObserver<T>, IDisposable> subscribe)
- // {
- // return new ObservableWrapper<T>(subscribe);
- // }
-
- // private class ObservableWrapper<T> : IObservable<T>
- // {
- // private readonly Func<IObserver<T>, IDisposable> _subscribe;
-
- // public ObservableWrapper(Func<IObserver<T>, IDisposable> subscribe)
- // {
- // _subscribe = subscribe;
- // }
-
- // public IDisposable Subscribe(IObserver<T> observer)
- // {
- // return _subscribe(observer);
- // }
- // }
- //}
-
}

0 comments on commit a655578

Please sign in to comment.