Skip to content

Commit

Permalink
Drop reference to cached items asap in to_xxx (#509)
Browse files Browse the repository at this point in the history
This reduces memory consumption on cases where the cached items are
quite big and: The observable is not disposed immediately or the
completion callback triggers other computations.
  • Loading branch information
MainRo committed May 26, 2020
1 parent b0ade08 commit e0fa087
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rx/core/operators/todict.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def on_next(x: Any) -> None:
m[key] = element

def on_completed() -> None:
nonlocal m
observer.on_next(m)
m = dict()
observer.on_completed()

return source.subscribe_(on_next, observer.on_error, on_completed, scheduler)
Expand Down
2 changes: 2 additions & 0 deletions rx/core/operators/tofuture.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ def on_error(err):
future.set_exception(err)

def on_completed():
nonlocal last_value
if has_value:
future.set_result(last_value)
else:
future.set_exception(SequenceContainsNoElementsError())
last_value = None

source.subscribe_(on_next, on_error, on_completed, scheduler=scheduler)

Expand Down
2 changes: 2 additions & 0 deletions rx/core/operators/toiterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def on_next(item):
queue.append(item)

def on_completed():
nonlocal queue
observer.on_next(queue)
queue = []
observer.on_completed()

return source.subscribe_(on_next, observer.on_error, on_completed, scheduler)
Expand Down
2 changes: 2 additions & 0 deletions rx/core/operators/toset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def subscribe(observer, scheduler=None):
s = set()

def on_completed():
nonlocal s
observer.on_next(s)
s = set()
observer.on_completed()

return source.subscribe_(s.add, observer.on_error, on_completed, scheduler)
Expand Down

0 comments on commit e0fa087

Please sign in to comment.