From 12b13df4e7b596615990925eef485a353baef0c7 Mon Sep 17 00:00:00 2001 From: erri120 Date: Thu, 12 Sep 2024 14:31:13 +0200 Subject: [PATCH] Keep subscription alive --- .../LibraryPage/FakeParentLibraryItemModel.cs | 18 ++++++------------ .../Pages/LibraryPage/LibraryItemModel.cs | 18 ++++++------------ .../LoadoutPage/FakeParentLoadoutItemModel.cs | 19 +++++++------------ 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/NexusMods.App.UI/Pages/LibraryPage/FakeParentLibraryItemModel.cs b/src/NexusMods.App.UI/Pages/LibraryPage/FakeParentLibraryItemModel.cs index 2ea905b2e6..f8a771612c 100644 --- a/src/NexusMods.App.UI/Pages/LibraryPage/FakeParentLibraryItemModel.cs +++ b/src/NexusMods.App.UI/Pages/LibraryPage/FakeParentLibraryItemModel.cs @@ -17,19 +17,10 @@ public class FakeParentLibraryItemModel : LibraryItemModel public override IReadOnlyCollection GetLoadoutItemIds() => LibraryItems.Select(static item => item.LibraryItemId).ToArray(); private readonly IDisposable _modelActivationDisposable; - private readonly IDisposable _activationSelectionDisposable; + private readonly SerialDisposable _libraryItemsDisposable = new(); public FakeParentLibraryItemModel(LibraryItemId libraryItemId) : base(libraryItemId) { - _activationSelectionDisposable = Activation.CombineLatest(IsSelected, (a, b) => (a, b)).Subscribe(this, static (tuple, self) => - { - var (isActivating, isSelected) = tuple; - if (!isActivating && !isSelected) - { - self.LibraryItems.Clear(); - } - }); - _modelActivationDisposable = WhenModelActivated(this, static (model, disposables) => { model.NumInstalledObservable @@ -64,7 +55,10 @@ public FakeParentLibraryItemModel(LibraryItemId libraryItemId) : base(libraryIte }) .AddTo(disposables); - model.LibraryItemsObservable.OnUI().SubscribeWithErrorLogging(changeSet => model.LibraryItems.ApplyChanges(changeSet)).AddTo(disposables); + if (model._libraryItemsDisposable.Disposable is null) + { + model._libraryItemsDisposable.Disposable = model.LibraryItemsObservable.OnUI().SubscribeWithErrorLogging(changeSet => model.LibraryItems.ApplyChanges(changeSet)); + } }); } @@ -75,7 +69,7 @@ protected override void Dispose(bool disposing) { if (disposing) { - Disposable.Dispose(_modelActivationDisposable, _activationSelectionDisposable); + Disposable.Dispose(_modelActivationDisposable, _libraryItemsDisposable); } LibraryItems = null!; diff --git a/src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs b/src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs index 03e36ab3fb..b70c13631c 100644 --- a/src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs +++ b/src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs @@ -43,7 +43,7 @@ public class LibraryItemModel : TreeDataGridItemModel GetLoadoutItemIds() => _fixedId; private readonly IDisposable _modelActivationDisposable; - private readonly IDisposable _activationSelectionDisposable; + private readonly SerialDisposable _linkedLoadoutItemsDisposable = new(); public LibraryItemModel(LibraryItemId libraryItemId) { @@ -52,15 +52,6 @@ public LibraryItemModel(LibraryItemId libraryItemId) var canInstall = IsInstalledInLoadout.Select(static b => !b); InstallCommand = canInstall.ToReactiveCommand>(_ => GetLoadoutItemIds(), initialCanExecute: false); - _activationSelectionDisposable = Activation.CombineLatest(IsSelected, (a, b) => (a, b)).Subscribe(this, static (tuple, self) => - { - var (isActivating, isSelected) = tuple; - if (!isActivating && !isSelected) - { - self.LinkedLoadoutItems.Clear(); - } - }); - _modelActivationDisposable = WhenModelActivated(this, static (model, disposables) => { Debug.Assert(model.Ticker is not null, "should've been set before activation"); @@ -93,7 +84,10 @@ public LibraryItemModel(LibraryItemId libraryItemId) model.FormattedCreatedAtDate.Value = FormatDate(DateTime.Now, model.CreatedAtDate.Value); model.FormattedInstalledDate.Value = FormatDate(DateTime.Now, model.InstalledDate.Value); - model.LinkedLoadoutItemsObservable.OnUI().SubscribeWithErrorLogging(changeSet => model.LinkedLoadoutItems.ApplyChanges(changeSet)).AddTo(disposables); + if (model._linkedLoadoutItemsDisposable.Disposable is null) + { + model._linkedLoadoutItemsDisposable.Disposable = model.LinkedLoadoutItemsObservable.OnUI().SubscribeWithErrorLogging(changeSet => model.LinkedLoadoutItems.ApplyChanges(changeSet)); + } }); } @@ -113,7 +107,7 @@ protected override void Dispose(bool disposing) Disposable.Dispose( InstallCommand, _modelActivationDisposable, - _activationSelectionDisposable, + _linkedLoadoutItemsDisposable, FormattedCreatedAtDate, FormattedInstalledDate, ItemSize, diff --git a/src/NexusMods.App.UI/Pages/LoadoutPage/FakeParentLoadoutItemModel.cs b/src/NexusMods.App.UI/Pages/LoadoutPage/FakeParentLoadoutItemModel.cs index 1ef3241e7c..41cad3e01f 100644 --- a/src/NexusMods.App.UI/Pages/LoadoutPage/FakeParentLoadoutItemModel.cs +++ b/src/NexusMods.App.UI/Pages/LoadoutPage/FakeParentLoadoutItemModel.cs @@ -17,23 +17,18 @@ public class FakeParentLoadoutItemModel : LoadoutItemModel public override IReadOnlyCollection GetLoadoutItemIds() => LoadoutItemIds; private readonly IDisposable _modelActivationDisposable; - private readonly IDisposable _activationSelectionDisposable; + private readonly SerialDisposable _loadoutItemIdsDisposable = new(); public FakeParentLoadoutItemModel() : base(default(LoadoutItemId)) { - _activationSelectionDisposable = Activation.CombineLatest(IsSelected, (a, b) => (a, b)).Subscribe(this, static (tuple, self) => - { - var (isActivating, isSelected) = tuple; - if (!isActivating && !isSelected) - { - self.LoadoutItemIds.Clear(); - } - }); - _modelActivationDisposable = WhenModelActivated(this, static (model, disposables) => { model.InstalledAtObservable.OnUI().Subscribe(date => model.InstalledAt.Value = date).AddTo(disposables); - model.LoadoutItemIdsObservable.OnUI().SubscribeWithErrorLogging(changeSet => model.LoadoutItemIds.ApplyChanges(changeSet)).AddTo(disposables); + + if (model._loadoutItemIdsDisposable.Disposable is null) + { + model._loadoutItemIdsDisposable.Disposable = model.LoadoutItemIdsObservable.OnUI().SubscribeWithErrorLogging(changeSet => model.LoadoutItemIds.ApplyChanges(changeSet)); + } }); } @@ -44,7 +39,7 @@ protected override void Dispose(bool disposing) { if (disposing) { - Disposable.Dispose(_modelActivationDisposable, _activationSelectionDisposable); + Disposable.Dispose(_modelActivationDisposable, _loadoutItemIdsDisposable); } LoadoutItemIds = null!;