curds: Rename notification "main" parameter to "idle"

And always run tasks marked for the idle queue in the idle queue, even
if we're in the main thread.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-12-12 12:06:37 -05:00
parent 3a939f92e1
commit 735f2bba6e
7 changed files with 33 additions and 31 deletions

View File

@ -3,7 +3,7 @@ import threading
from gi.repository import GLib
events = { }
main_queue = [ ]
idle_queue = [ ]
event_lock = threading.Lock()
def cancel(name, func):
@ -16,31 +16,30 @@ def cancel(name, func):
def clear():
events.clear()
main_queue.clear()
idle_queue.clear()
def notify(name, *args):
is_main = threading.current_thread() == threading.main_thread()
for (func, main) in events.get(name, []):
if main == True and is_main == False:
for (func, idle) in events.get(name, []):
if idle == True:
with event_lock:
if (func, args) not in main_queue:
main_queue.append((func, args))
if len(main_queue) == 1:
if (func, args) not in idle_queue:
idle_queue.append((func, args))
if len(idle_queue) == 1:
GLib.idle_add(notify_idle)
else:
func(*args)
def notify_idle():
with event_lock:
if len(main_queue) == 0:
if len(idle_queue) == 0:
return False
(func, args) = main_queue.pop(0)
more = len(main_queue) > 0
(func, args) = idle_queue.pop(0)
more = len(idle_queue) > 0
func(*args)
return more
def register(name, func, main=False):
def register(name, func, idle=False):
cb = events.setdefault(name, [])
if func in [ n[0] for n in cb ]:
return
cb.append((func, main))
cb.append((func, idle))

View File

@ -42,5 +42,5 @@ def init():
global UpNext
Starred = Root.lookup("Playlists").lookup("Starred")
UpNext = Root.lookup("Up Next")
notify.register("save-playlists", save, main=True)
notify.register("save-playlists", save, idle=True)
notify.register("save-data", save)

View File

@ -31,7 +31,7 @@ class TestNotify(unittest.TestCase):
def test_notify_init(self):
self.assertEqual(notify.events, {})
self.assertEqual(notify.main_queue, [])
self.assertEqual(notify.idle_queue, [])
self.assertFalse(notify.event_lock.locked())
def test_notify_register(self):
@ -49,7 +49,7 @@ class TestNotify(unittest.TestCase):
notify.main_q = [ 1, 2, 3 ]
notify.clear()
self.assertEqual(notify.events, {})
self.assertEqual(notify.main_queue, [])
self.assertEqual(notify.idle_queue, [])
def test_notify_cancel(self):
notify.events = { "on-test" : [ (self.on_test1, False),
@ -72,6 +72,9 @@ class TestNotify(unittest.TestCase):
notify.notify("on-test", "It Worked", "CoolCoolCool")
self.assertEqual(self.test_count, 1)
self.assertEqual(self.test_arg1, "It Worked")
self.assertEqual(self.test_arg2, None)
while Gtk.events_pending(): Gtk.main_iteration_do(True)
self.assertEqual(self.test_arg2, "CoolCoolCool")
def test_notify_bg_thread(self):
@ -81,13 +84,13 @@ class TestNotify(unittest.TestCase):
self.notify_thread("on-test", "It Worked", "CoolCoolCool")
self.assertEqual(self.test_count, 0)
self.assertEqual(self.test_arg1, "It Worked")
self.assertEqual(notify.main_queue, [ (self.on_test1, ("It Worked", "CoolCoolCool")) ])
self.assertEqual(notify.idle_queue, [ (self.on_test1, ("It Worked", "CoolCoolCool")) ])
self.notify_thread("on-test", "It Worked", "CoolCoolCool")
self.assertEqual(self.test_count, 0)
self.assertEqual(notify.main_queue, [ (self.on_test1, ("It Worked", "CoolCoolCool")) ])
self.assertEqual(notify.idle_queue, [ (self.on_test1, ("It Worked", "CoolCoolCool")) ])
while Gtk.events_pending(): Gtk.main_iteration_do(True)
self.assertEqual(self.test_count, 1)
self.assertEqual(notify.main_queue, [ ])
self.assertEqual(notify.idle_queue, [ ])
self.assertFalse(notify.notify_idle())

View File

@ -110,7 +110,7 @@ Entry.connect("activate", on_activate_entry)
def on_show_more(visible):
Box.set_visible(visible)
curds.notify.register("show-more", on_show_more, main=True)
curds.notify.register("show-more", on_show_more)
def state_changed(state):
@ -134,4 +134,4 @@ def reset():
if PopCB != None:
GLib.source_remove(PopCB)
PopCB = None
curds.notify.register("show-more", on_show_more)
curds.notify.register("show-more", on_show_more)

View File

@ -107,5 +107,5 @@ class NodeTreeModel(GObject.GObject, Gtk.TreeModel):
return iter
def reset(self):
curds.notify.register("node-inserted", self.on_node_inserted, main=True)
curds.notify.register("playlist-changed", self.on_playlist_changed, main=True)
curds.notify.register("node-inserted", self.on_node_inserted, idle=True)
curds.notify.register("playlist-changed", self.on_playlist_changed, idle=True)

View File

@ -80,14 +80,14 @@ def on_node_inserted(plist, index):
path = Filter.convert_child_path_to_path(child)
TreeView.expand_to_path(path)
curds.notify.register("node-inserted", on_node_inserted, main=True)
curds.notify.register("node-inserted", on_node_inserted, idle=True)
def on_show_more(visible):
Entry.set_visible(visible)
Separator.set_visible(visible)
curds.notify.register("show-more", on_show_more, main=True)
curds.notify.register("show-more", on_show_more)
def reset():
@ -99,5 +99,5 @@ def reset():
Filter.clear_cache()
on_show_more(False)
TreeView.set_model(Filter)
curds.notify.register("show-more", on_show_more, main=True)
curds.notify.register("node-inserted", on_node_inserted, main=True)
curds.notify.register("show-more", on_show_more)
curds.notify.register("node-inserted", on_node_inserted, idle=True)

View File

@ -122,7 +122,7 @@ def on_add_track(plist, track, index):
Model.row_inserted(Gtk.TreePath(NextPos), iter)
NextPos += 1
curds.notify.register("add-track", on_add_track, main=True)
curds.notify.register("add-track", on_add_track, idle=True)
def on_remove_track(plist, track, index):
@ -145,7 +145,7 @@ def on_playlist_changed(plist):
if plist == Model.playlist:
set_runtime(plist.runtime())
curds.notify.register("playlist-changed", on_playlist_changed, main=True)
curds.notify.register("playlist-changed", on_playlist_changed, idle=True)
def scroll_to(index):
@ -194,8 +194,8 @@ switch(curds.playlist.lookup("Collection"))
def reset():
switch(curds.playlist.lookup("Collection"))
curds.notify.register("add-track", on_add_track, main=True)
curds.notify.register("add-track", on_add_track, idle=True)
curds.notify.register("remove-track", on_remove_track)
curds.notify.register("playlist-changed", on_playlist_changed, main=True)
curds.notify.register("playlist-changed", on_playlist_changed, idle=True)
curds.notify.register("stream-start", on_stream_start)
curds.notify.register("show-more", on_show_more)