Skip to content
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

Remove listeners in UndoAction and RedoAction for memory efficiency #11839

Merged
merged 8 commits into from
Oct 4, 2024
17 changes: 13 additions & 4 deletions src/main/java/org/jabref/gui/undo/RedoAction.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.jabref.gui.undo;

import java.util.Optional;
import java.util.function.Supplier;

import javax.swing.undo.CannotRedoException;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.WeakChangeListener;


import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
Expand All @@ -21,11 +26,15 @@ public RedoAction(Supplier<LibraryTab> tabSupplier, DialogService dialogService,
this.tabSupplier = tabSupplier;
this.dialogService = dialogService;

// TODO: The old listener should be removed. Otherwise, memory consumption will increase.
stateManager.activeTabProperty().addListener((observable, oldValue, activeLibraryTab) -> {
ChangeListener<Optional<LibraryTab>> listener = (observable, oldValue, activeLibraryTab) -> {
activeLibraryTab.ifPresent(libraryTab ->
this.executable.bind(libraryTab.getUndoManager().getRedoableProperty()));
});
this.executable.bind(libraryTab.getUndoManager().getRedoableProperty()));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty line is not necessary, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this empty line is for exists to separate lines 30 and 32 from each other. But I can remove.

oldValue.ifPresent(libraryTab -> this.executable.unbind());
};

WeakChangeListener<Optional<LibraryTab>> weakListener = new WeakChangeListener<>(listener);
stateManager.activeTabProperty().addListener(weakListener);
}

@Override
Expand Down
Loading