Skip to content

Commit

Permalink
fixed BitwiseCombineLatest
Browse files Browse the repository at this point in the history
  • Loading branch information
semiversus committed Dec 10, 2020
1 parent 2e05b1d commit b0b1820
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.2

* fixed behaviour for BitwiseCombineLatest when a Publisher has state NONE

## 2.0.1

* fixed problem in `Publisher.register_on_subscription_callback()` when subscriptions already are available
Expand Down
21 changes: 16 additions & 5 deletions broqer/op/bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
from typing import Any, Dict # noqa: F401

# pylint: disable=cyclic-import
from broqer import Publisher, Subscriber, NONE
from broqer import Publisher, Subscriber, NONE, SubscriptionDisposable
from broqer.op import build_map_factory

from broqer.operator import MultiOperator


class BitwiseCombineLatest(MultiOperator):
""" Bitwise combine the latest emit of multiple publishers and emit the
combination
combination. If a publisher is not emitting or is not defined for a bit, the
init value will be used.
:param bit_publisher_mapping: dictionary with bit index as key and source
publisher as value
:param init: optional init value used for undefined bits
publisher as value
:param init: optional init value used for undefined bits (or initial state)
"""
def __init__(self, publisher_bit_mapping: Dict, init: int = 0) -> None:
MultiOperator.__init__(self, *publisher_bit_mapping)
Expand All @@ -28,6 +29,16 @@ def __init__(self, publisher_bit_mapping: Dict, init: int = 0) -> None:
self._missing = set(self._orginators)
self._publisher_bit_mapping = publisher_bit_mapping

def subscribe(self, subscriber: 'Subscriber',
prepend: bool = False) -> SubscriptionDisposable:
disposable = MultiOperator.subscribe(self, subscriber, prepend)

if self._missing:
self._missing.clear()
Publisher.notify(self, self._state)

return disposable

def unsubscribe(self, subscriber: Subscriber) -> None:
MultiOperator.unsubscribe(self, subscriber)
if not self._subscriptions:
Expand All @@ -44,7 +55,7 @@ def get(self):
value = publisher.get()

if value is NONE:
return NONE
continue

if value:
state |= 1 << bit_index
Expand Down
3 changes: 3 additions & 0 deletions tests/test_op_bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
(0, [(0, True), (1, False), (4, True)],
[(NONE, True, NONE)],
[17, 19]),
(0, [(0, True), (1, NONE), (4, True)],
[(NONE, True, NONE)],
[17, 19]),
(~0, [(0, True)],
[(True,), (False,), (True,)],
[~0, ~0, ~1, ~0]),
Expand Down

0 comments on commit b0b1820

Please sign in to comment.