Added take until
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Callable, Any, Optional
|
||||
from typing import Callable, Any, Optional, Type
|
||||
|
||||
from cpl_core.type import T
|
||||
from cpl_reactive_extensions.abc.operator import Operator
|
||||
from cpl_reactive_extensions.abc.subscribable import Subscribable
|
||||
from cpl_reactive_extensions.subscriber import Observer, Subscriber
|
||||
from cpl_reactive_extensions.subscription import Subscription
|
||||
@@ -19,6 +18,16 @@ class Observable(Subscribable):
|
||||
self._source: Optional[Observable] = None
|
||||
self._operator: Optional[Callable] = None
|
||||
|
||||
@staticmethod
|
||||
def from_observable(obs: Observable):
|
||||
def inner(subscriber: Subscriber):
|
||||
if "subscribe" not in dir(obs):
|
||||
raise TypeError("Unable to lift unknown Observable type")
|
||||
|
||||
return obs.subscribe(subscriber)
|
||||
|
||||
return Observable(inner)
|
||||
|
||||
@staticmethod
|
||||
def from_list(values: list):
|
||||
i = 0
|
||||
|
14
src/cpl_reactive_extensions/operators/take_until.py
Normal file
14
src/cpl_reactive_extensions/operators/take_until.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from cpl_core.type import T
|
||||
from cpl_reactive_extensions import Subscriber, Observable
|
||||
from cpl_reactive_extensions.operator_subscriber import OperatorSubscriber
|
||||
from cpl_reactive_extensions.utils import operate
|
||||
|
||||
|
||||
def take_until(notifier: Observable):
|
||||
def init(source: Observable, subscriber: Subscriber):
|
||||
Observable.from_observable(notifier).subscribe(OperatorSubscriber(subscriber, lambda: subscriber.complete()))
|
||||
|
||||
if not subscriber.closed:
|
||||
source.subscribe(subscriber)
|
||||
|
||||
return operate(init)
|
@@ -1,4 +1,5 @@
|
||||
from typing import Any, Optional
|
||||
from types import NoneType
|
||||
from typing import Any, Optional, Type
|
||||
|
||||
from cpl_core.type import T
|
||||
from cpl_reactive_extensions.abc.observer import Observer
|
||||
@@ -8,11 +9,11 @@ from cpl_reactive_extensions.subscription import Subscription
|
||||
|
||||
|
||||
class Subject(Observable, Observer):
|
||||
def __init__(self, _t: type):
|
||||
def __init__(self, _t: Type[T]):
|
||||
Observable.__init__(self)
|
||||
|
||||
self.is_closed = False
|
||||
self._t = _t
|
||||
self._t = _t if _t is not None else NoneType
|
||||
self._current_observers: Optional[list[Observer]] = None
|
||||
|
||||
self.closed = False
|
||||
|
Reference in New Issue
Block a user