config: Rework unsubscribing

Unsubscribe by object comparison, making separate tracking of
subscriptions and a dedicated method for clearing them obsolete.
This commit is contained in:
Peter Eisenmann
2024-06-21 01:42:09 +02:00
parent 22294dd65d
commit 65dff4b20c
5 changed files with 12 additions and 24 deletions

View File

@@ -155,11 +155,11 @@ class Config:
self.subscriptions[variable] = [func]
func(self.variables[variable])
def unsubscribe(self, variable, func):
if variable in self.subscriptions and func in self.subscriptions[variable]:
self.subscriptions[variable].remove(func)
else:
print(f'Unsubscribing non-subscribed function {func}!')
def unsubscribe(self, obj):
for subs in self.subscriptions.values():
for func in subs:
if func.__self__ == obj:
subs.remove(func)
config = Config()

View File

@@ -8,13 +8,6 @@ class Page:
image: Union[str, Path, None] = None
can_reload: bool = False
def _subscribe(self, variable, func):
config.subscribe(variable, func)
if hasattr(self, "subscriptions"):
self.subscriptions.append((variable, func))
else:
self.subscriptions = [(variable, func)]
### dummy stubs ###
def load(self):
@@ -31,10 +24,3 @@ class Page:
Called before the page is no longer shown. Used for e.g. storing current enrty values.
'''
pass
### public methods ###
def cancel_subscriptions(self):
if hasattr(self, "subscriptions"):
for variable, func in self.subscriptions:
config.unsubscribe(variable, func)

View File

@@ -47,7 +47,7 @@ class KeyboardLayoutPage(Gtk.Box, Page):
self.layout_list.bind_model(
self.model, lambda o: ProgressRow(o.name, o))
self._subscribe('keyboard_language', self._update_keyboard_language)
config.subscribe('keyboard_language', self._update_keyboard_language)
### callbacks ###
@@ -82,7 +82,7 @@ class KeyboardOverviewPage(Gtk.Box, Page):
keyboard = get_default_layout(language_code)
set_system_keyboard_layout(keyboard_info=keyboard)
self._subscribe('keyboard_layout', self._update_primary_layout)
config.subscribe('keyboard_layout', self._update_primary_layout)
### callbacks ###

View File

@@ -28,8 +28,8 @@ class LocalePage(Gtk.Box, Page):
timezone = get_current_timezone()
config.set('timezone', timezone)
self._subscribe('formats_ui', self._update_formats)
self._subscribe('timezone', self._update_timezone)
config.subscribe('formats_ui', self._update_formats)
config.subscribe('timezone', self._update_timezone)
### callbacks ###

View File

@@ -4,6 +4,8 @@ from pathlib import Path
from gi.repository import Adw, Gtk
from .config import config
def reset_model(model, new_values):
'''
@@ -109,7 +111,7 @@ class PageWrapper(Adw.Bin):
self.content.set_child(self.page)
def __del__(self):
self.page.cancel_subscriptions()
config.unsubscribe(self.page)
def get_page(self):
return self.page