-
-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow connecting to any IEnumerable<T> #132
Comments
Maybe some thin wrapper over IEnumerable that implements IObservable<IChangeSet> ? |
Something like this (for both list and cache) var myObservableChangeSet = myEnumerable.ToObservableChangeSet() and implement a light weight source to act as the backing store public class OneTimeSourceList<T>: IObservableList<T>
{
\\implement with simplified internals
} |
Yes, exactly! |
Would this not work...
|
I believe that would violate that line. Would work but a bit of extra overhead. |
Is it extra overhead though? I gotta download the MS source to see what it is actually doing. |
Next version... internal class OneTimeSourceList<T> : IObservableList<T>
{
internal OneTimeSourceList(IEnumerable<T> source)
=> Items = source;
public IEnumerable<T> Items { get; }
public IObservable<int> CountChanged
=> Observable.Empty<int>();
public int Count
=> Items.Count();
public IObservable<IChangeSet<T>> Connect(Func<T, bool> predicate = null)
=> Observable.Defer(() =>
{
var change = new Change<T>(ListChangeReason.Add, Items);
var changeSet = new ChangeSet<T>(new[] {change});
return Observable.Return(changeSet);
});
public void Dispose()
{
}
}
public static class ObservableListEx
{
public static IObservable<IChangeSet<T>> ToObservableChangeSet<T>(this IEnumerable<T> self)
=> new OneTimeSourceList<T>(self).Connect();
} |
Something like that is perfect |
Not sure the OneTimeSourceList is even needed to be honest, the Observable.Defer() code could be inlined into the extension method |
So like a ienumerable.tochangeset or something. |
public static class EnumerableExtensions
{
public static IObservable<IChangeSet<T>> ToObservableChangeSet<T>(this IEnumerable<T> self)
=> Observable.Defer(() =>
{
var change = new Change<T>(ListChangeReason.Add, self);
var changeSet = new ChangeSet<T>(new[] { change });
return Observable.Return(changeSet);
});
} Example usage... IObservable<IChangeSet<T>> result = myEnumerable.ToObservableChangeSet(); |
That's even better |
I guess the only disadvantage of the above is it's not really observable. Although it keeps it consistent so it's transparent for observable collections etc. |
Why isn't it Observable? I guess if you mean, if you then add an item to the source enumerable after calling this extension, then no you wont get any additional changeset notification for that add, but I don't see any way around that due to the source being a plain old enumerable. |
The inline approach looks pretty much perfect to me. I was going to point out that the "Not observable" in that sense is precisely the benefit of this proposal - my suggestion was inherently intended for scenarios where the underlying |
Yea, I think the extension method should cover your needs then. I'll submit a pull request later this evening with some unit tests if the proposed solution is acceptable |
Yeah I agree, just the only other alternative is instead of using ToObservableChangeSet() as a name use ToChangeSet() I think the I prefer keeping one consistent naming scheme myself though. |
Awesome. Could you do the same but add a key selector overload so it can equally apply to cache. Also could you add the extensions to ObservableListEx and ObservableCacheEx respectively |
Yep, will do Roland. |
#132 - Allow connecting to any IEnumerable<T>
Now that we have
AutoRefresh()
andFilter()
it would make sense to be able to connect to any type of enumerable, even immutable ones, because the combination ofAutoRefresh()
andFilter()
basically introduces the observability.Now we have to do something like this:
I.e. wrap the array in an observable collection that will not be used for anything, just take up unnecessary memory.
Would be nicer if we could do something like this:
The implementation should preferably not simply create a wrapper
ObservableCollection<T>
behind the scenes, because then this improvement would be little more than syntactic sugar.The text was updated successfully, but these errors were encountered: