Skip to content

Instantly share code, notes, and snippets.

@RolandPheasant
Last active July 14, 2016 19:22
Show Gist options
  • Save RolandPheasant/23ff2ce5ecd86ef960d8cccef1f4f2b6 to your computer and use it in GitHub Desktop.
Save RolandPheasant/23ff2ce5ecd86ef960d8cccef1f4f2b6 to your computer and use it in GitHub Desktop.
public static IObservable<IChangeSet<WidgetListItemViewModel, int>> TransformWithupdate(this IObservable<IChangeSet<Widget, int>> source )
{
return Observable.Create<IChangeSet<WidgetListItemViewModel, int>>(observer =>
{
var shared = source
.Transform(w => new WidgetListItemViewModel(w))
.Publish();
//create cache which never replaces the original problem
var nonUpdatingObservableCache = shared
.WhereReasonsAreNot(ChangeReason.Update)
.AsObservableCache();
//transform the result and manually update original items
var withoutUpdates = shared
.Select(changes =>
{
var altered = new List<Change<WidgetListItemViewModel, int>>(changes.Count);
foreach (var change in changes)
switch (change.Reason)
{
case ChangeReason.Update:
//get the original from the non-updating cache
var original = nonUpdatingObservableCache.Lookup(change.Key)
.ValueOrThrow(()=>new Exception("There should always be something matching the key"));
//directly update original view model
var updated = change.Current;
original.Distance = updated.Distance;
//evaluate is a command to operators to self-requery when there are in-line changes [should automatically re-sort]
altered.Add(new Change<WidgetListItemViewModel, int>(ChangeReason.Evaluate, change.Key, original));
break;
default:
altered.Add(change);
break;
}
return new ChangeSet<WidgetListItemViewModel, int>(altered);
})
.Where(changes => changes.Count != 0)
.SubscribeSafe(observer);
return new CompositeDisposable(withoutUpdates,nonUpdatingObservableCache , shared.Connect(), withoutUpdates);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment