using System;
using System.Collections.ObjectModel;
using Firebase.Database.Streaming;
namespace Firebase.Database
{
///
/// Extensions for .
///
public static class ObservableExtensions
{
///
/// Starts observing on given firebase observable and propagates event into an .
///
/// The observable.
/// Type of entity.
/// The .
public static ObservableCollection AsObservableCollection(this IObservable> observable)
{
var collection = new ObservableCollection();
observable.Subscribe(f =>
{
if (f.EventType == FirebaseEventType.InsertOrUpdate)
{
var i = collection.IndexOf(f.Object);
if (i >= 0) collection.RemoveAt(i);
collection.Add(f.Object);
}
else
{
collection.Remove(f.Object);
}
});
return collection;
}
}
}