File: singleassignmentdisposable.py

package info (click to toggle)
python-rx 4.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: python: 39,070; javascript: 77; makefile: 24
file content (53 lines) | stat: -rw-r--r-- 1,595 bytes parent folder | download
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
from threading import RLock
from typing import Optional

from reactivex.abc import DisposableBase


class SingleAssignmentDisposable(DisposableBase):
    """Single assignment disposable.

    Represents a disposable resource which only allows a single
    assignment of its underlying disposable resource. If an underlying
    disposable resource has already been set, future attempts to set the
    underlying disposable resource will throw an Error."""

    def __init__(self) -> None:
        """Initializes a new instance of the SingleAssignmentDisposable
        class.
        """
        self.is_disposed: bool = False
        self.current: Optional[DisposableBase] = None
        self.lock = RLock()

        super().__init__()

    def get_disposable(self) -> Optional[DisposableBase]:
        return self.current

    def set_disposable(self, value: DisposableBase) -> None:
        if self.current:
            raise Exception("Disposable has already been assigned")

        with self.lock:
            should_dispose = self.is_disposed
            if not should_dispose:
                self.current = value

        if self.is_disposed and value:
            value.dispose()

    disposable = property(get_disposable, set_disposable)

    def dispose(self) -> None:
        """Sets the status to disposed"""
        old: Optional[DisposableBase] = None

        with self.lock:
            if not self.is_disposed:
                self.is_disposed = True
                old = self.current
                self.current = None

        if old is not None:
            old.dispose()