File: observer.py

package info (click to toggle)
python-rx 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,204 kB
  • sloc: python: 39,525; javascript: 77; makefile: 24
file content (113 lines) | stat: -rw-r--r-- 3,520 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from __future__ import annotations

from typing import TYPE_CHECKING, Callable, Optional, TypeVar

from reactivex import abc
from reactivex.internal.basic import default_error, noop
from reactivex.typing import OnCompleted, OnError, OnNext

_T_in = TypeVar("_T_in", contravariant=True)

if TYPE_CHECKING:
    from reactivex.notification import Notification
else:

    class Notification:
        pass


class Observer(abc.ObserverBase[_T_in], abc.DisposableBase):
    """Base class for implementations of the Observer class. This base
    class enforces the grammar of observers where OnError and
    OnCompleted are terminal messages.
    """

    def __init__(
        self,
        on_next: Optional[OnNext[_T_in]] = None,
        on_error: Optional[OnError] = None,
        on_completed: Optional[OnCompleted] = None,
    ) -> None:
        self.is_stopped = False
        self._handler_on_next: OnNext[_T_in] = on_next or noop
        self._handler_on_error: OnError = on_error or default_error
        self._handler_on_completed: OnCompleted = on_completed or noop

    def on_next(self, value: _T_in) -> None:
        """Notify the observer of a new element in the sequence."""
        if not self.is_stopped:
            self._on_next_core(value)

    def _on_next_core(self, value: _T_in) -> None:
        """For Subclassing purpose. This method is called by `on_next()`
        method until the observer is stopped.
        """
        self._handler_on_next(value)

    def on_error(self, error: Exception) -> None:
        """Notify the observer that an exception has occurred.

        Args:
            error: The error that occurred.
        """

        if not self.is_stopped:
            self.is_stopped = True
            self._on_error_core(error)

    def _on_error_core(self, error: Exception) -> None:
        """For Subclassing purpose. This method is called by `on_error()`
        method until the observer is stopped.
        """
        self._handler_on_error(error)

    def on_completed(self) -> None:
        """Notifies the observer of the end of the sequence."""

        if not self.is_stopped:
            self.is_stopped = True
            self._on_completed_core()

    def _on_completed_core(self) -> None:
        """For Subclassing purpose. This method is called by `on_completed()`
        method until the observer is stopped.
        """
        self._handler_on_completed()

    def dispose(self) -> None:
        """Disposes the observer, causing it to transition to the
        stopped state."""
        self.is_stopped = True

    def fail(self, exn: Exception) -> bool:
        if not self.is_stopped:
            self.is_stopped = True
            self._on_error_core(exn)
            return True

        return False

    def throw(self, error: Exception) -> None:
        import traceback

        traceback.print_stack()
        raise error

    def to_notifier(self) -> Callable[[Notification[_T_in]], None]:
        """Creates a notification callback from an observer.

        Returns the action that forwards its input notification to the
        underlying observer."""

        def func(notifier: Notification[_T_in]) -> None:
            return notifier.accept(self)

        return func

    def as_observer(self) -> abc.ObserverBase[_T_in]:
        """Hides the identity of an observer.

        Returns an observer that hides the identity of the specified
        observer.
        """
        return Observer(self.on_next, self.on_error, self.on_completed)