From ae23564709fb38381894712671dd1dea100e3b73 Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Wed, 3 Jan 2024 09:53:53 -0600 Subject: [PATCH 1/2] Update set_timer patch --- platform_wasm/pygame/timer.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/platform_wasm/pygame/timer.py b/platform_wasm/pygame/timer.py index af6fd29..e096a37 100644 --- a/platform_wasm/pygame/timer.py +++ b/platform_wasm/pygame/timer.py @@ -81,7 +81,12 @@ async def fire_event(thread_uuid): loop_counter = 0 while True: await asyncio.sleep(dlay) - if event_loop.is_closed() or event not in THREADS or THREADS[event] != thread_uuid or (loops and loop_counter >= loops): + if ( + event_loop.is_closed() + or event not in THREADS + or THREADS[event] != thread_uuid + or (loops and loop_counter >= loops) + ): break pygame.event.post(cevent) From 91f79fd3304e0f4d74917de29bc197a0c7ccaeee Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Thu, 22 Feb 2024 07:05:05 -0500 Subject: [PATCH 2/2] Apply formatting and make args consistent with pygame API docs --- platform_wasm/pygame/timer.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/platform_wasm/pygame/timer.py b/platform_wasm/pygame/timer.py index e096a37..9679ce4 100644 --- a/platform_wasm/pygame/timer.py +++ b/platform_wasm/pygame/timer.py @@ -56,15 +56,20 @@ THREADS = {} -def patch_set_timer(arg: Union[int, pygame.event.Event], millis: int, loops: int = 0): - """Patches the pygame.time.set_timer function to use gthreads""" - +def patch_set_timer( + event: Union[int, pygame.event.Event], + millis: int, + loops: int = 0): + """repeatedly create an event on the event queue + + Patches the pygame.time.set_timer function to use gthreads + """ dlay = float(millis) / 1000 - if isinstance(arg, pygame.event.Event): - event = int(arg) - cevent = arg + if isinstance(event, pygame.event.Event): + event_type = event.type + cevent = event else: - event = int(arg) + event_type = int(event) cevent = pygame.event.Event(event) event_loop = asyncio.get_event_loop() @@ -83,8 +88,8 @@ async def fire_event(thread_uuid): await asyncio.sleep(dlay) if ( event_loop.is_closed() - or event not in THREADS - or THREADS[event] != thread_uuid + or event_type not in THREADS + or THREADS[event_type] != thread_uuid or (loops and loop_counter >= loops) ): break @@ -97,12 +102,12 @@ async def fire_event(thread_uuid): # stale threads will be terminated thread_uuid = uuid.uuid4() Thread(target=fire_event, args=[thread_uuid]).start() - THREADS[event] = thread_uuid + THREADS[event_type] = thread_uuid else: # This cancels the timer for the event if event in THREADS: - del THREADS[event] + del THREADS[event_type] pygame.time.set_timer = patch_set_timer