Skip to content

Commit

Permalink
add dendent_subscribe for Values
Browse files Browse the repository at this point in the history
  • Loading branch information
semiversus committed Sep 26, 2022
1 parent 2321bb8 commit 459933c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* fixed typo ("orginator" fixed to "origintator")
* add log output for default error_handler
* add `dependent_subscribe` function for `Values`

## 2.4.0

Expand Down
14 changes: 14 additions & 0 deletions broqer/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,17 @@ def emit(self, value: Any,
raise ValueError('Emit from non assigned publisher')

return Publisher.notify(self, value)


def dependent_subscribe(publisher: Publisher, value: Value):
""" Let `value` subscribe to `publisher` only when `value` itself is subscribed
:param publisher: publisher to be subscribed, when value is subscribed
:param value: value, which will receive .emit calls from `publisher`
"""
def _on_subscription(existing_subscription: bool):
if existing_subscription:
publisher.subscribe(value)
else:
publisher.unsubscribe(value)

value.register_on_subscription_callback(_on_subscription)

0 comments on commit 459933c

Please sign in to comment.