Skip to content

Commit

Permalink
Keep subscription alive
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Sep 12, 2024
1 parent 9fbae51 commit 12b13df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,10 @@ public class FakeParentLibraryItemModel : LibraryItemModel
public override IReadOnlyCollection<LibraryItemId> 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
Expand Down Expand Up @@ -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));
}
});
}

Expand All @@ -75,7 +69,7 @@ protected override void Dispose(bool disposing)
{
if (disposing)
{
Disposable.Dispose(_modelActivationDisposable, _activationSelectionDisposable);
Disposable.Dispose(_modelActivationDisposable, _libraryItemsDisposable);
}

LibraryItems = null!;
Expand Down
18 changes: 6 additions & 12 deletions src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class LibraryItemModel : TreeDataGridItemModel<LibraryItemModel, EntityId
public virtual IReadOnlyCollection<LibraryItemId> GetLoadoutItemIds() => _fixedId;

private readonly IDisposable _modelActivationDisposable;
private readonly IDisposable _activationSelectionDisposable;
private readonly SerialDisposable _linkedLoadoutItemsDisposable = new();

public LibraryItemModel(LibraryItemId libraryItemId)
{
Expand All @@ -52,15 +52,6 @@ public LibraryItemModel(LibraryItemId libraryItemId)
var canInstall = IsInstalledInLoadout.Select(static b => !b);
InstallCommand = canInstall.ToReactiveCommand<Unit, IReadOnlyCollection<LibraryItemId>>(_ => 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");
Expand Down Expand Up @@ -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));
}
});
}

Expand All @@ -113,7 +107,7 @@ protected override void Dispose(bool disposing)
Disposable.Dispose(
InstallCommand,
_modelActivationDisposable,
_activationSelectionDisposable,
_linkedLoadoutItemsDisposable,
FormattedCreatedAtDate,
FormattedInstalledDate,
ItemSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,18 @@ public class FakeParentLoadoutItemModel : LoadoutItemModel
public override IReadOnlyCollection<LoadoutItemId> 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));
}
});
}

Expand All @@ -44,7 +39,7 @@ protected override void Dispose(bool disposing)
{
if (disposing)
{
Disposable.Dispose(_modelActivationDisposable, _activationSelectionDisposable);
Disposable.Dispose(_modelActivationDisposable, _loadoutItemIdsDisposable);
}

LoadoutItemIds = null!;
Expand Down

0 comments on commit 12b13df

Please sign in to comment.