From 2c39f2274a916feaf9b49f95538ad21e2b520e64 Mon Sep 17 00:00:00 2001 From: Ivan Kosarev Date: Fri, 19 Feb 2021 22:13:47 +0200 Subject: [PATCH] [#18] Introduce dispatchers. --- zx/_emulator.py | 13 ++++++------- zx/_machine.py | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/zx/_emulator.py b/zx/_emulator.py index 4e75bae..a93e365 100644 --- a/zx/_emulator.py +++ b/zx/_emulator.py @@ -22,6 +22,7 @@ from ._gui import ScreenWindow from ._keyboard import KeyboardState from ._keyboard import KEYS +from ._machine import Dispatcher from ._machine import RunEvents from ._machine import Spectrum48 from ._rzx import RZXFile @@ -63,12 +64,10 @@ def __init__(self, speed_factor=1.0, profile=None, devices=None): self.__events_to_signal = RunEvents.NO_EVENTS # Don't even create the window on full throttle. - self.__devices = devices if devices is not None else [] if devices is None and self.__speed_factor is not None: - self.__devices = [ScreenWindow(self)] + devices = Dispatcher([ScreenWindow(self)]) - # TODO: Eliminate the undescored version. - self.devices = self.__devices + self.devices = devices self.__keyboard_state = KeyboardState() self.set_on_input_callback(self.__on_input) @@ -100,7 +99,7 @@ def _is_tape_paused(self): def __pause_tape(self, is_paused=True): self._tape_player.pause(is_paused) - self.notify_devices(TapeStateUpdated()) + self.devices.notify(TapeStateUpdated()) def __unpause_tape(self): self.__pause_tape(is_paused=False) @@ -278,7 +277,7 @@ def __run_quantum(self, speed_factor=None): creator_info = self.__playback_player.find_recording_info_chunk() if True: # TODO - self.notify_devices(QuantumRun()) + self.devices.notify(QuantumRun()) # TODO: For debug purposes. ''' @@ -323,7 +322,7 @@ def __run_quantum(self, speed_factor=None): self.render_screen() pixels = self.get_frame_pixels() - self.notify_devices(ScreenUpdated(pixels)) + self.devices.notify(ScreenUpdated(pixels)) self._tape_player.skip_rest_of_frame() self._emulation_time.advance(1 / 50) diff --git a/zx/_machine.py b/zx/_machine.py index df6c371..be241fc 100644 --- a/zx/_machine.py +++ b/zx/_machine.py @@ -342,6 +342,21 @@ def install_snapshot(self, snapshot): setattr(self, field, value) +class Dispatcher(object): + def __init__(self, devices=None): + if devices is None: + devices = [] + + self.__devices = list(devices) + + def __iter__(self): + yield from self.__devices + + def notify(self, event): + for device in self: + device.on_event(event) + + class Spectrum48(_Spectrum48Base, MachineState): # Memory marks. __NO_MARKS = 0 @@ -369,10 +384,6 @@ def __exit__(self, type, value, tb): def stop(self): raise EmulationExit() - def notify_devices(self, event): - for device in self.devices: - device.on_event(event) - @property def paused(self): return self.__paused @@ -380,7 +391,7 @@ def paused(self): @paused.setter def paused(self, value): self.__paused = value - self.notify_devices(PauseStateUpdated()) + self.devices.notify(PauseStateUpdated()) def set_breakpoints(self, addr, size): self.mark_addrs(addr, size, self.__BREAKPOINT_MARK)