Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions nion/utils/Stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading