Created
January 24, 2013 18:54
-
-
Save brenoferreira/4626423 to your computer and use it in GitHub Desktop.
Incomplete ObservableCollection with Rx
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Reactive.Disposables; | |
namespace ReactiveExtensions.Rx | |
{ | |
public class ObservableCollection<T> : IList<T>, System.IObservable<T> | |
{ | |
private IList<T> list; | |
private IList<IObserver<T>> observers; | |
public ObservableCollection () | |
{ | |
this.list = new List<T>(); | |
this.observers = new List<IObserver<T>>(); | |
} | |
#region IObservable implementation | |
public IDisposable Subscribe (IObserver<T> observer) | |
{ | |
this.observers.Add (observer); | |
return Disposable.Create(() => this.observers.Remove(observer)); | |
} | |
private void NotifyNext(T item) | |
{ | |
foreach (var observer in observers) { | |
observer.OnNext(item); | |
} | |
} | |
#endregion | |
#region IList implementation | |
public int IndexOf (T item) | |
{ | |
return this.list.IndexOf(item); | |
} | |
public void Insert (int index, T item) | |
{ | |
this.list.Insert(index, item); | |
this.NotifyNext(item); | |
} | |
public void RemoveAt (int index) | |
{ | |
this.list.RemoveAt(0); | |
} | |
public T this [int index] { | |
get { | |
return this.list[index]; | |
} | |
set { | |
this.list[index] = value; | |
} | |
} | |
#endregion | |
#region ICollection implementation | |
public void Add (T item) | |
{ | |
this.list.Add (item); | |
this.NotifyNext(item); | |
} | |
public void Clear () | |
{ | |
this.list.Clear(); | |
} | |
public bool Contains (T item) | |
{ | |
return this.list.Contains(item); | |
} | |
public void CopyTo (T[] array, int arrayIndex) | |
{ | |
this.list.CopyTo(array, arrayIndex); | |
} | |
public bool Remove (T item) | |
{ | |
return this.list.Remove(item); | |
} | |
public int Count | |
{ | |
get { return this.list.Count; } | |
} | |
public Boolean IsReadOnly | |
{ | |
get { return this.list.IsReadOnly; } | |
} | |
#endregion | |
#region IEnumerable implementation | |
public IEnumerator<T> GetEnumerator () | |
{ | |
return this.list.GetEnumerator(); | |
} | |
#endregion | |
#region IEnumerable implementation | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () | |
{ | |
return this.list.GetEnumerator(); | |
} | |
#endregion | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment