public void TopShouldRefresh() { var source = new SourceCache <int, int>(i => i); var dest = source.Connect().Top(Comparer <int> .Create((_, __) => 0), 2).AsObservableCache(); source.AddOrUpdate(Enumerable.Range(0, 5)); dest.Items.Should().BeEquivalentTo(Enumerable.Range(0, 2)); source.RemoveKey(0); source.RemoveKey(1); dest.Items.Should().BeEquivalentTo(Enumerable.Range(2, 2)); }
public DirectoryListVM(ISourceDirectoryService sourceDirectoryService) { this.WhenActivated(disposables => { AddDirectory = ReactiveCommand.Create( () => { var card = Locator.Current.GetService <IDirectoryCard>(); card.SetEditMode(true); card.EditModeChanges .Where(x => x == false) .Take(1) .Subscribe(_ => _temporaryDirectoryCards.RemoveKey(card.Id)); _temporaryDirectoryCards.AddOrUpdate(card); } ); sourceDirectoryService.Directories .Connect() .Transform(model => Locator.Current.GetService <IDirectoryCard>() .SetSourceModel(model) ).FullJoin( _temporaryDirectoryCards.Connect(), x => x.Id, (saved, temp) => saved.HasValue ? saved.Value : temp.Value ) // basically a concat .Bind(out var directoryCards) .Subscribe() .DisposeWith(disposables); this.DirectoryCards = directoryCards; }); }
public TagService() { _tagDtos.AddOrUpdate(new[] { new TagDto { Id = 1, Name = @"Country\Canada\Alberta" }, new TagDto { Id = 2, Name = @"Country\Canada\British Columbia" }, new TagDto { Id = 3, Name = @"Country\USA\California" }, new TagDto { Id = 4, Name = @"Country\USA\Texas" } }); _tagDtos .Connect() .Transform(dto => { var names = dto.Name.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); var results = new TagLeaf[names.Length]; var parentId = ""; for (var i = 0; i < names.Length; i++) { var name = names[i]; var id = $"{parentId}{name}\\"; results[i] = new TagLeaf(id, parentId, dto.Id, name); parentId = id; } return(new TagBranch(dto.Id, results)); }) .ForEachChange(change => { var branch = change.Current; switch (change.Reason) { case ChangeReason.Remove: var lastLeaf = branch.Leaves.Last(); _tagLeafs.RemoveKey(lastLeaf.Id); break; case ChangeReason.Add: foreach (var leaf in branch.Leaves) { if (_tagLeafs.Keys.Contains(leaf.Id)) { continue; } _tagLeafs.AddOrUpdate(leaf); } break; } }) .Subscribe(); }
public void RemoveAccount(Account account) { _accounts.RemoveKey(account.Address); if (nethereumHostProvider.SelectedAccount == account.Address) { var firstAccount = Accounts.KeyValues.FirstOrDefault().Value; nethereumHostProvider.SetSelectedAccount(firstAccount); } }
public void RemoveKey(string key) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentException("message", nameof(key)); } _cache.RemoveKey(key); }
public new WeatherForecast Remove(Guid itemId) { var item = base.Remove(itemId); if (item != null) { sourceCache.RemoveKey(item.Id); } return(item); }
public void TransformManyWithKey() { var children = Enumerable.Range(1, 100).Select(i => new Person("Name" + i, i)).ToArray(); int childIndex = 0; var parents = Enumerable.Range(1, 50) .Select(i => { var parent = new Parent(i, new[] { children[childIndex], children[childIndex + 1] }); childIndex = childIndex + 2; return(parent); }).ToArray(); using (var source = new SourceCache <Parent, int>(x => x.Id)) using (var aggregator = source.Connect() .TransformMany(p => p.Children, c => c.Name) .AsAggregator()) { source.AddOrUpdate(parents); aggregator.Data.Count.Should().Be(100); //add a child to an observable collection and check the new item is added parents[0].Children.Add(new Person("NewlyAddded", 100)); aggregator.Data.Count.Should().Be(101); ////remove first parent and check children have gone source.RemoveKey(1); aggregator.Data.Count.Should().Be(98); //check items can be cleared and then added back in var childrenInZero = parents[1].Children.ToArray(); parents[1].Children.Clear(); aggregator.Data.Count.Should().Be(96); parents[1].Children.AddRange(childrenInZero); aggregator.Data.Count.Should().Be(98); //replace produces an update var replacedChild = parents[1].Children[0]; parents[1].Children[0] = new Person("Replacement", 100); aggregator.Data.Count.Should().Be(98); aggregator.Data.Lookup(replacedChild.Name).HasValue.Should().BeFalse(); aggregator.Data.Lookup("Replacement").HasValue.Should().BeTrue(); } }
public void FlattenObservableCollectionWithProjectionFromObservableCache() { var children = new[] { new NestedChild("A", "ValueA"), new NestedChild("B", "ValueB"), new NestedChild("C", "ValueC"), new NestedChild("D", "ValueD"), new NestedChild("E", "ValueE"), new NestedChild("F", "ValueF") }; var parents = new[] { new ClassWithNestedObservableCollection(1, new[] { children[0], children[1] }), new ClassWithNestedObservableCollection(2, new[] { children[2], children[3] }), new ClassWithNestedObservableCollection(3, new[] { children[4] }) }; using (var source = new SourceCache <ClassWithNestedObservableCollection, int>(x => x.Id)) using (var sut = source.Connect() .AutoRefreshOnObservable(self => self.Children.ToObservableChangeSet()) .TransformMany(parent => parent.Children.Select(c => new ProjectedNestedChild(parent, c)), c => c.Child.Name) .AsObservableCache()) { source.AddOrUpdate(parents); sut.Count.Should().Be(5); sut.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children.Take(5).Select(c => new ProjectedNestedChild(p, c)))); //add a child to the observable collection parents[2].Children.Add(children[5]); sut.Count.Should().Be(6); sut.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children.Select(c => new ProjectedNestedChild(p, c)))); //remove a parent and check children have moved source.RemoveKey(1); sut.Count.Should().Be(4); sut.Items.ShouldBeEquivalentTo(parents.Skip(1).SelectMany(p => p.Children.Select(c => new ProjectedNestedChild(p, c)))); //add a parent and check items have been added back in source.AddOrUpdate(parents[0]); sut.Count.Should().Be(6); sut.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children.Select(c => new ProjectedNestedChild(p, c)))); } }
public void FlattenObservableCollection() { var children = new[] { new NestedChild("A", "ValueA"), new NestedChild("B", "ValueB"), new NestedChild("C", "ValueC"), new NestedChild("D", "ValueD"), new NestedChild("E", "ValueE"), new NestedChild("F", "ValueF") }; var parents = new[] { new ClassWithNestedObservableCollection(1, new[] { children[0], children[1] }), new ClassWithNestedObservableCollection(2, new[] { children[2], children[3] }), new ClassWithNestedObservableCollection(3, new[] { children[4] }) }; using (var source = new SourceCache <ClassWithNestedObservableCollection, int>(x => x.Id)) using (var sut = new FlattenNestedObservableCollection(source)) { source.AddOrUpdate(parents); sut.Children.Count.Should().Be(5); sut.Children.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children.Take(5))); //add a child to the observable collection parents[2].Children.Add(children[5]); sut.Children.Count.Should().Be(6); sut.Children.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children)); //remove a parent and check children have moved source.RemoveKey(1); sut.Children.Count.Should().Be(4); sut.Children.Items.ShouldBeEquivalentTo(parents.Skip(1).SelectMany(p => p.Children)); //add a parent and check items have been added back in source.AddOrUpdate(parents[0]); sut.Children.Count.Should().Be(6); sut.Children.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children)); } }
public void AutoRefresh(ForceEvaluationMode mode) { var items = new List <MutableThing> { new MutableThing(1, "A"), new MutableThing(2, "A"), new MutableThing(3, "B"), new MutableThing(4, "C"), new MutableThing(5, "D"), new MutableThing(6, "D"), }; //result should only be true when all items are set to true using (var cache = new SourceCache <MutableThing, int>(m => m.Id)) { var sut = mode == ForceEvaluationMode.Cache ? new AutoRefreshForPropertyChanges(cache) : new AutoRefreshForPropertyChanges(cache.Connect()); int count = 0; sut.DistinctCount.Subscribe(result => count = result); cache.AddOrUpdate(items); count.Should().Be(4); //check mutating a value works items[2].Value = "A"; count.Should().Be(3); //check remove works cache.RemoveKey(4); count.Should().Be(2); //check add works cache.AddOrUpdate(new MutableThing(10, "z")); count.Should().Be(3); } }
private SteamConnectService() { mCurrent = this; SteamApps = new SourceCache <SteamApp, uint>(t => t.AppId); DownloadApps = new SourceCache <SteamApp, uint>(t => t.AppId); DownloadApps .Connect() .Subscribe(_ => { foreach (var app in DownloadApps.Items) { var optional = SteamApps.Lookup(app.AppId); if (optional.HasValue) { var value = optional.Value; value.InstalledDir = app.InstalledDir; value.State = app.State; value.SizeOnDisk = app.SizeOnDisk; value.LastOwner = app.LastOwner; value.BytesToDownload = app.BytesToDownload; value.BytesDownloaded = app.BytesDownloaded; value.BytesToStage = app.BytesToStage; value.BytesStaged = app.BytesStaged; value.LastUpdated = app.LastUpdated; } } }); this.WhenValueChanged(x => x.IsWatchSteamDownloading, false) .Subscribe(x => { if (x) { InitializeDownloadGameList(); if (OperatingSystem2.IsLinux) { IPlatformService.Instance.TryGetSystemUserPassword(); } SteamTool.StartWatchSteamDownloading(app => { var optional = DownloadApps.Lookup(app.AppId); if (!optional.HasValue) { DownloadApps.AddOrUpdate(app); } else { var current = optional.Value; current.InstalledDir = app.InstalledDir; current.State = app.State; current.SizeOnDisk = app.SizeOnDisk; current.LastOwner = app.LastOwner; current.BytesToDownload = app.BytesToDownload; current.BytesDownloaded = app.BytesDownloaded; current.BytesToStage = app.BytesToStage; current.BytesStaged = app.BytesStaged; current.LastUpdated = app.LastUpdated; } if (WatchDownloadingSteamAppIds.Contains(app.AppId)) { if (app.IsDownloading) { app.IsWatchDownloading = true; } else { WatchDownloadingSteamAppIds.Remove(app.AppId); if (!WatchDownloadingSteamAppIds.Any()) { WatchDownloadingComplete(); } } } }, appid => { DownloadApps.RemoveKey(appid); }); } else { SteamTool.StopWatchSteamDownloading(); } }); }
protected void RemoveKey(string item) { _sourceCache.RemoveKey(item); }