lbry-sdk/lbry/wallet/stream.py

162 lines
4.9 KiB
Python
Raw Normal View History

import asyncio
2018-05-25 08:03:25 +02:00
class BroadcastSubscription:
def __init__(self, controller, on_data, on_error, on_done):
self._controller = controller
self._previous = self._next = None
self._on_data = on_data
self._on_error = on_error
self._on_done = on_done
self.is_paused = False
self.is_canceled = False
self.is_closed = False
def pause(self):
self.is_paused = True
def resume(self):
self.is_paused = False
def cancel(self):
self._controller._cancel(self)
self.is_canceled = True
@property
def can_fire(self):
return not any((self.is_paused, self.is_canceled, self.is_closed))
def _add(self, data):
if self.can_fire and self._on_data is not None:
2018-11-19 04:54:00 +01:00
return self._on_data(data)
2018-05-25 08:03:25 +02:00
2018-10-15 04:16:51 +02:00
def _add_error(self, exception):
2018-05-25 08:03:25 +02:00
if self.can_fire and self._on_error is not None:
2018-11-19 04:54:00 +01:00
return self._on_error(exception)
2018-05-25 08:03:25 +02:00
def _close(self):
2018-11-19 04:54:00 +01:00
try:
if self.can_fire and self._on_done is not None:
return self._on_done()
finally:
self.is_closed = True
2018-05-25 08:03:25 +02:00
class StreamController:
2019-08-07 07:48:40 +02:00
def __init__(self, merge_repeated_events=False):
2018-05-25 08:03:25 +02:00
self.stream = Stream(self)
self._first_subscription = None
self._last_subscription = None
2019-08-07 07:48:40 +02:00
self._last_event = None
self._merge_repeated = merge_repeated_events
2018-05-25 08:03:25 +02:00
@property
def has_listener(self):
return self._first_subscription is not None
@property
def _iterate_subscriptions(self):
next_sub = self._first_subscription
while next_sub is not None:
subscription = next_sub
next_sub = next_sub._next
2018-05-25 08:03:25 +02:00
yield subscription
2018-11-19 04:54:00 +01:00
def _notify_and_ensure_future(self, notify):
tasks = []
2018-05-25 08:03:25 +02:00
for subscription in self._iterate_subscriptions:
2018-11-19 04:54:00 +01:00
maybe_coroutine = notify(subscription)
if asyncio.iscoroutine(maybe_coroutine):
tasks.append(maybe_coroutine)
if tasks:
return asyncio.ensure_future(asyncio.wait(tasks))
else:
f = asyncio.get_event_loop().create_future()
f.set_result(None)
return f
def add(self, event):
2019-08-07 07:48:40 +02:00
skip = self._merge_repeated and event == self._last_event
self._last_event = event
2018-11-19 04:54:00 +01:00
return self._notify_and_ensure_future(
2019-08-07 07:48:40 +02:00
lambda subscription: None if skip else subscription._add(event)
2018-11-19 04:54:00 +01:00
)
2018-05-25 08:03:25 +02:00
2018-10-15 04:16:51 +02:00
def add_error(self, exception):
2018-11-19 04:54:00 +01:00
return self._notify_and_ensure_future(
lambda subscription: subscription._add_error(exception)
)
2018-05-25 08:03:25 +02:00
def close(self):
for subscription in self._iterate_subscriptions:
subscription._close()
def _cancel(self, subscription):
previous = subscription._previous
next_sub = subscription._next
2018-05-25 08:03:25 +02:00
if previous is None:
self._first_subscription = next_sub
2018-05-25 08:03:25 +02:00
else:
previous._next = next_sub
if next_sub is None:
2018-05-25 08:03:25 +02:00
self._last_subscription = previous
else:
next_sub._previous = previous
2018-05-25 08:03:25 +02:00
subscription._next = subscription._previous = subscription
def _listen(self, on_data, on_error, on_done):
subscription = BroadcastSubscription(self, on_data, on_error, on_done)
old_last = self._last_subscription
self._last_subscription = subscription
subscription._previous = old_last
subscription._next = None
if old_last is None:
self._first_subscription = subscription
else:
old_last._next = subscription
return subscription
class Stream:
def __init__(self, controller):
self._controller = controller
def listen(self, on_data, on_error=None, on_done=None):
return self._controller._listen(on_data, on_error, on_done)
2018-10-15 04:16:51 +02:00
def where(self, condition) -> asyncio.Future:
future = asyncio.get_event_loop().create_future()
2018-05-25 15:54:01 +02:00
def where_test(value):
if condition(value):
2018-10-15 04:16:51 +02:00
self._cancel_and_callback(subscription, future, value)
2018-05-25 15:54:01 +02:00
subscription = self.listen(
where_test,
2018-10-15 04:16:51 +02:00
lambda exception: self._cancel_and_error(subscription, future, exception)
2018-05-25 15:54:01 +02:00
)
2018-10-15 04:16:51 +02:00
return future
2018-05-25 15:54:01 +02:00
2018-05-25 08:03:25 +02:00
@property
def first(self):
2018-10-15 04:16:51 +02:00
future = asyncio.get_event_loop().create_future()
2018-05-25 08:03:25 +02:00
subscription = self.listen(
2019-08-23 06:50:25 +02:00
lambda value: not future.done() and self._cancel_and_callback(subscription, future, value),
lambda exception: not future.done() and self._cancel_and_error(subscription, future, exception)
2018-05-25 08:03:25 +02:00
)
2018-10-15 04:16:51 +02:00
return future
2018-05-25 08:03:25 +02:00
@staticmethod
2018-10-15 04:16:51 +02:00
def _cancel_and_callback(subscription: BroadcastSubscription, future: asyncio.Future, value):
2018-05-25 08:03:25 +02:00
subscription.cancel()
2018-10-15 04:16:51 +02:00
future.set_result(value)
2018-05-25 08:03:25 +02:00
@staticmethod
2018-10-15 04:16:51 +02:00
def _cancel_and_error(subscription: BroadcastSubscription, future: asyncio.Future, exception):
2018-05-25 08:03:25 +02:00
subscription.cancel()
2018-10-15 04:16:51 +02:00
future.set_exception(exception)