Skip to content

Commit

Permalink
Fix implementation of rx.__iter__ (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Sep 25, 2023
1 parent eed20b6 commit 9b7382c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 3 additions & 14 deletions param/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,20 +941,9 @@ def __iter__(self):
return
elif not isinstance(self._current, Iterable):
raise TypeError(f'cannot unpack non-iterable {type(self._current).__name__} object.')
iterator = []
def iterate(value):
if iterator:
iterate = iterator[0]
else:
iterate = iter(value)
iterator.append(iterate)
try:
yield next(iterate)
except StopIteration as e:
iterator.clear()
raise e
for item in self._apply_operator(iterate):
yield item
items = self._apply_operator(list)
for i in range(len(self._current)):
yield items[i]

def _eval_operation(self, obj, operation):
fn, args, kwargs = operation['fn'], operation['args'], operation['kwargs']
Expand Down
14 changes: 14 additions & 0 deletions tests/testreactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ def test_reactive_iter():
assert a.rx.resolve() == 'b'
assert b.rx.resolve() == 'a'

def test_reactive_multi_iter():
i = rx(('a', 'b'))
a1, b1 = i
a2, b2 = i
assert a1.rx.resolve() == 'a'
assert b1.rx.resolve() == 'b'
assert a2.rx.resolve() == 'a'
assert b2.rx.resolve() == 'b'
i.rx.set_input(('b', 'a'))
assert a1.rx.resolve() == 'b'
assert b1.rx.resolve() == 'a'
assert a2.rx.resolve() == 'b'
assert b2.rx.resolve() == 'a'

def test_reactive_is():
i = rx(None)
is_ = i.rx.is_(None)
Expand Down

0 comments on commit 9b7382c

Please sign in to comment.