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:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 ###
|
||||
|
||||
|
||||
@@ -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 ###
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user