Skip to content

Commit

Permalink
extended .reset_state() functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
semiversus committed Dec 23, 2020
1 parent a518d67 commit bb67299
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.0

* .reset_state is now calling .reset_state for all subscribers

## 2.0.3

* prevent iteration over a publisher
Expand Down
6 changes: 0 additions & 6 deletions broqer/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ def unsubscribe(self, subscriber: Subscriber) -> None:
def notify(self, value: TValue) -> None:
raise ValueError('Operator doesn\'t support .notify()')

def reset_state(self, value: TValue = None) -> None:
raise ValueError('Operator doesn\'t support .reset_state()')

@abstractmethod
def emit(self, value: typing.Any, who: Publisher) -> None:
""" Send new value to the operator
Expand Down Expand Up @@ -106,9 +103,6 @@ def unsubscribe(self, subscriber: Subscriber) -> None:
def notify(self, value: TValue) -> None:
raise ValueError('Operator doesn\'t support .notify()')

def reset_state(self, value: TValue = None) -> None:
raise ValueError('Operator doesn\'t support .reset_state()')

@abstractmethod
def emit(self, value: typing.Any, who: Publisher) -> None:
""" Send new value to the operator
Expand Down
18 changes: 6 additions & 12 deletions broqer/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,14 @@ def notify(self, value: TValue) -> None:
for subscriber in tuple(self._subscriptions):
subscriber.emit(value, who=self)

@overload # noqa: F811
def reset_state(self, value: TValue) -> None:
""" Variant for reseting to a specified value """
def reset_state(self) -> None:
""" Resets the state. Calling this method will not trigger a
notification, but will call .reset_state for all subscribers
@overload # noqa: F811
def reset_state(self) -> None: # noqa: F811
""" Variant for resetting to NONE """

def reset_state(self, value=NONE) -> None: # noqa: F811
""" Resets the state. Calling this method will not trigger an emit.
:param value: Optional value to set the internal state
"""
self._state = value
self._state = NONE
for subscriber in tuple(self._subscriptions):
subscriber.reset_state()

@property
def subscriptions(self) -> Tuple['Subscriber', ...]:
Expand Down
5 changes: 5 additions & 0 deletions broqer/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ def emit(self, value: Any, who: 'Publisher') -> None:
:param value: value to be send
:param who: reference to which publisher is emitting
"""

def reset_state(self) -> None:
""" Will be called by assigned publisher, when publisher was called
to reset its state
"""
14 changes: 4 additions & 10 deletions tests/test_core_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ def test_reset_state():
m.assert_not_called()

# test .reset_state() before and after subscribing
p.reset_state('test')
assert p.get() == 'test'
p.reset_state()
assert p.get() == NONE

p.subscribe(Sink(m, 2))

m.assert_called_once_with(2, 'test')
m.assert_not_called()

m.reset_mock()

Expand All @@ -214,13 +214,7 @@ def test_reset_state():
m.reset_mock()

# test no subscribers get notified
p.reset_state('test')
m.assert_not_called()

assert p.get() == 'test'

# test default argument NONE for .reset_state()
p.reset_state()
m.assert_not_called()

assert p.get() == NONE
assert p.get() == NONE

0 comments on commit bb67299

Please sign in to comment.