From 7895a59af651dfa5aca74536697bf30736859425 Mon Sep 17 00:00:00 2001 From: Chris Meyer <34664+cmeyer@users.noreply.github.com> Date: Thu, 11 Sep 2025 14:26:54 -0700 Subject: [PATCH] Add compare parameter to MapStream. --- nion/utils/Stream.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nion/utils/Stream.py b/nion/utils/Stream.py index 6230c8d..ed6439f 100644 --- a/nion/utils/Stream.py +++ b/nion/utils/Stream.py @@ -125,19 +125,20 @@ def _send_value(self) -> None: class MapStream(AbstractStream[OT], typing.Generic[T, OT]): """A stream that applies a function when input streams change.""" - def __init__(self, stream: AbstractStream[T], value_fn: typing.Callable[[typing.Optional[T]], typing.Optional[OT]]) -> None: + def __init__(self, stream: AbstractStream[T], value_fn: typing.Callable[[typing.Optional[T]], typing.Optional[OT]], cmp: typing.Optional[EqualityOperator] = None) -> None: super().__init__() # outgoing messages self.value_stream = Event.Event() # references self.__stream = stream + cmp = cmp if cmp else operator.eq # initialize values self.__value: typing.Optional[OT] = None # listen for display changes def update_value(stream: MapStream[T, OT], value: typing.Optional[T]) -> None: new_value = value_fn(value) - if new_value != stream.value: + if not cmp(new_value, stream.value): stream.send_value(new_value) # use weak_partial to avoid holding references to self.