ycpp 0.1.3__cp312-abi3-win_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ycpp/__init__.py +110 -0
- ycpp/_array.py +318 -0
- ycpp/_awareness.py +268 -0
- ycpp/_doc.py +718 -0
- ycpp/_event.py +188 -0
- ycpp/_id_map.py +620 -0
- ycpp/_map.py +267 -0
- ycpp/_provider.py +180 -0
- ycpp/_snapshot.py +28 -0
- ycpp/_sticky_index.py +59 -0
- ycpp/_stream.py +91 -0
- ycpp/_sync.py +192 -0
- ycpp/_text.py +239 -0
- ycpp/_transaction.py +287 -0
- ycpp/_typed.py +122 -0
- ycpp/_undo.py +201 -0
- ycpp/_update.py +18 -0
- ycpp/_xml.py +452 -0
- ycpp/_ycpp.lib +0 -0
- ycpp/_ycpp.pyd +0 -0
- ycpp-0.1.3.data/headers/fast_float/ascii_number.h +766 -0
- ycpp-0.1.3.data/headers/fast_float/bigint.h +638 -0
- ycpp-0.1.3.data/headers/fast_float/constexpr_feature_detect.h +53 -0
- ycpp-0.1.3.data/headers/fast_float/decimal_to_binary.h +212 -0
- ycpp-0.1.3.data/headers/fast_float/digit_comparison.h +455 -0
- ycpp-0.1.3.data/headers/fast_float/fast_float.h +91 -0
- ycpp-0.1.3.data/headers/fast_float/fast_table.h +708 -0
- ycpp-0.1.3.data/headers/fast_float/float_common.h +1432 -0
- ycpp-0.1.3.data/headers/fast_float/parse_number.h +488 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_arithmetic.hpp +267 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_bit.hpp +230 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_cast.hpp +43 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_complex.hpp +112 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_details.hpp +386 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_logical.hpp +240 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_math.hpp +2601 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_memory.hpp +1006 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_rounding.hpp +72 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_swizzle.hpp +260 -0
- ycpp-0.1.3.data/headers/xsimd/arch/common/xsimd_common_trigo.hpp +996 -0
- ycpp-0.1.3.data/headers/xsimd/arch/utils/shifts.hpp +82 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx.hpp +2111 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx2.hpp +1591 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512bw.hpp +724 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512cd.hpp +28 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512dq.hpp +270 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512er.hpp +17 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512f.hpp +2919 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512ifma.hpp +17 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512pf.hpp +17 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512vbmi.hpp +76 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512vbmi2.hpp +130 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512vnni_avx512bw.hpp +17 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avx512vnni_avx512vbmi2.hpp +17 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_avxvnni.hpp +17 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_common.hpp +26 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_common_fwd.hpp +127 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_constants.hpp +393 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_emulated.hpp +787 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_fma3_avx.hpp +93 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_fma3_avx2.hpp +46 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_fma3_sse.hpp +97 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_fma4.hpp +92 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_i8mm_neon64.hpp +17 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_isa.hpp +146 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_neon.hpp +3580 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_neon64.hpp +1614 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_rvv.hpp +1515 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_scalar.hpp +1210 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sse2.hpp +2333 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sse3.hpp +66 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sse4_1.hpp +510 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sse4_2.hpp +44 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_ssse3.hpp +186 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_sve.hpp +1186 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_vsx.hpp +916 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_vxe.hpp +787 -0
- ycpp-0.1.3.data/headers/xsimd/arch/xsimd_wasm.hpp +1819 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_arch.hpp +242 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_config.hpp +620 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features.hpp +50 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_arm.hpp +132 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_ppc.hpp +54 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_riscv.hpp +95 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_s390x.hpp +56 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpu_features_x86.hpp +1044 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_cpuid.hpp +144 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_getauxval.hpp +159 -0
- ycpp-0.1.3.data/headers/xsimd/config/xsimd_macros.hpp +76 -0
- ycpp-0.1.3.data/headers/xsimd/math/xsimd_rem_pio2.hpp +707 -0
- ycpp-0.1.3.data/headers/xsimd/memory/xsimd_aligned_allocator.hpp +349 -0
- ycpp-0.1.3.data/headers/xsimd/memory/xsimd_alignment.hpp +102 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_all_registers.hpp +57 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_api.hpp +3106 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx2_register.hpp +44 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512bw_register.hpp +51 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512cd_register.hpp +51 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512dq_register.hpp +51 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512er_register.hpp +51 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512f_register.hpp +77 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512ifma_register.hpp +51 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512pf_register.hpp +51 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512vbmi2_register.hpp +51 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512vbmi_register.hpp +51 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512vnni_avx512bw_register.hpp +54 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx512vnni_avx512vbmi2_register.hpp +53 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avx_register.hpp +64 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_avxvnni_register.hpp +44 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_batch.hpp +1665 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_batch_constant.hpp +571 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_batch_fwd.hpp +41 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_common_arch.hpp +49 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_emulated_register.hpp +80 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_fma3_avx2_register.hpp +50 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_fma3_avx_register.hpp +50 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_fma3_sse_register.hpp +50 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_fma4_register.hpp +50 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_i8mm_neon64_register.hpp +55 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_neon64_register.hpp +55 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_neon_register.hpp +130 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_register.hpp +96 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_rvv_register.hpp +491 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_sse2_register.hpp +59 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_sse3_register.hpp +49 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_sse4_1_register.hpp +48 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_sse4_2_register.hpp +48 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_ssse3_register.hpp +48 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_sve_register.hpp +216 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_traits.hpp +408 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_utils.hpp +462 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_vsx_register.hpp +77 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_vxe_register.hpp +86 -0
- ycpp-0.1.3.data/headers/xsimd/types/xsimd_wasm_register.hpp +59 -0
- ycpp-0.1.3.data/headers/xsimd/utils/bits.hpp +122 -0
- ycpp-0.1.3.data/headers/xsimd/utils/xsimd_type_traits.hpp +145 -0
- ycpp-0.1.3.data/headers/xsimd/xsimd.hpp +43 -0
- ycpp-0.1.3.dist-info/DELVEWHEEL +2 -0
- ycpp-0.1.3.dist-info/METADATA +15 -0
- ycpp-0.1.3.dist-info/RECORD +142 -0
- ycpp-0.1.3.dist-info/WHEEL +4 -0
- ycpp.libs/msvcp140-9cb625fcbc2fbbd6fca1aee8b24b0ebe.dll +0 -0
- ycpp.libs/msvcp140_1-84ac1bc84e09d193dd6ad98c84c0fd94.dll +0 -0
ycpp/_doc.py
ADDED
|
@@ -0,0 +1,718 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import threading
|
|
5
|
+
import uuid
|
|
6
|
+
import weakref
|
|
7
|
+
from functools import partial
|
|
8
|
+
from typing import Generic, TypeVar, get_origin
|
|
9
|
+
|
|
10
|
+
from anyio import (
|
|
11
|
+
BrokenResourceError,
|
|
12
|
+
ClosedResourceError,
|
|
13
|
+
Lock as AsyncLock,
|
|
14
|
+
create_memory_object_stream,
|
|
15
|
+
)
|
|
16
|
+
from anyio.streams.memory import MemoryObjectSendStream
|
|
17
|
+
|
|
18
|
+
from . import _ycpp
|
|
19
|
+
from ._stream import UnsubscribableStream, _with_unsubscribe
|
|
20
|
+
from ._event import (
|
|
21
|
+
ArrayEvent,
|
|
22
|
+
MapEvent,
|
|
23
|
+
_AsyncCallbackInSyncTransaction,
|
|
24
|
+
SubdocsEvent,
|
|
25
|
+
Subscription,
|
|
26
|
+
TextEvent,
|
|
27
|
+
TransactionEvent,
|
|
28
|
+
arg_count,
|
|
29
|
+
invoke,
|
|
30
|
+
invoke_deep,
|
|
31
|
+
)
|
|
32
|
+
from ._array import Array
|
|
33
|
+
from ._map import Map
|
|
34
|
+
from ._text import Text
|
|
35
|
+
from ._transaction import NewTransaction, ReadTransaction, Transaction
|
|
36
|
+
from ._xml import XmlElement, XmlEvent, XmlFragment, XmlText
|
|
37
|
+
|
|
38
|
+
T = TypeVar("T")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _new_client_id() -> int:
|
|
42
|
+
return int.from_bytes(os.urandom(4), "little")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _live_delta(target, delta: list) -> list:
|
|
46
|
+
"""Yjs deltas name a nested type by position; pycrdt hands out the live wrapper instead."""
|
|
47
|
+
position = 0
|
|
48
|
+
for operation in delta:
|
|
49
|
+
if "insert" in operation:
|
|
50
|
+
values = operation["insert"]
|
|
51
|
+
for offset, value in enumerate(values):
|
|
52
|
+
if isinstance(value, dict) and not value:
|
|
53
|
+
values[offset] = target._value_at(position + offset)
|
|
54
|
+
position += len(values)
|
|
55
|
+
elif "retain" in operation:
|
|
56
|
+
position += operation["retain"]
|
|
57
|
+
return delta
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _empty_root(root):
|
|
61
|
+
"""An empty root of the same kind, so a restored document names its roots like the origin."""
|
|
62
|
+
if isinstance(root, Text):
|
|
63
|
+
return Text()
|
|
64
|
+
if isinstance(root, Array):
|
|
65
|
+
return Array()
|
|
66
|
+
if type(root) is XmlFragment:
|
|
67
|
+
return XmlFragment()
|
|
68
|
+
return Map()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class _Dispatcher:
|
|
72
|
+
"""Routes low-level observer calls back to the owning Doc.
|
|
73
|
+
|
|
74
|
+
The low-level doc holds this object, so it must not hold the Doc: a strong reference would make
|
|
75
|
+
the Doc <-> extension pair an uncollectable cycle. The weak reference always resolves while an
|
|
76
|
+
event can fire, since an event only fires from a call on the Doc.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
__slots__ = ("_doc",)
|
|
80
|
+
|
|
81
|
+
def __init__(self, doc: "Doc") -> None:
|
|
82
|
+
self._doc = weakref.ref(doc)
|
|
83
|
+
|
|
84
|
+
def doc_event(
|
|
85
|
+
self, observer: int, update: bytes, before: bytes, after: bytes
|
|
86
|
+
) -> None:
|
|
87
|
+
doc = self._doc()
|
|
88
|
+
if doc is not None:
|
|
89
|
+
doc._dispatch_doc(observer, update, before, after)
|
|
90
|
+
|
|
91
|
+
def map_event(self, observer: int, payload: dict) -> None:
|
|
92
|
+
doc = self._doc()
|
|
93
|
+
if doc is not None:
|
|
94
|
+
doc._dispatch_map(observer, payload)
|
|
95
|
+
|
|
96
|
+
def text_event(self, observer: int, delta: list) -> None:
|
|
97
|
+
doc = self._doc()
|
|
98
|
+
if doc is not None:
|
|
99
|
+
doc._dispatch_text(observer, delta)
|
|
100
|
+
|
|
101
|
+
def array_event(self, observer: int, delta: list) -> None:
|
|
102
|
+
doc = self._doc()
|
|
103
|
+
if doc is not None:
|
|
104
|
+
doc._dispatch_array(observer, delta)
|
|
105
|
+
|
|
106
|
+
def deep_event(self, observer: int, payloads: list) -> None:
|
|
107
|
+
doc = self._doc()
|
|
108
|
+
if doc is not None:
|
|
109
|
+
doc._dispatch_deep(observer, payloads)
|
|
110
|
+
|
|
111
|
+
def xml_event(self, observer: int, payload: dict) -> None:
|
|
112
|
+
doc = self._doc()
|
|
113
|
+
if doc is not None:
|
|
114
|
+
doc._dispatch_xml(observer, payload)
|
|
115
|
+
|
|
116
|
+
def subdocs_event(self, observer: int, payload: dict) -> None:
|
|
117
|
+
doc = self._doc()
|
|
118
|
+
if doc is not None:
|
|
119
|
+
doc._dispatch_subdocs(observer, payload)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class Doc(Generic[T]):
|
|
123
|
+
"""A shared document, shaped like pycrdt's Doc."""
|
|
124
|
+
|
|
125
|
+
def __init__(
|
|
126
|
+
self,
|
|
127
|
+
init: dict | None = None,
|
|
128
|
+
*,
|
|
129
|
+
client_id: int | None = None,
|
|
130
|
+
skip_gc: bool | None = None,
|
|
131
|
+
guid: str | None = None,
|
|
132
|
+
doc=None,
|
|
133
|
+
Model=None,
|
|
134
|
+
allow_multithreading: bool = False,
|
|
135
|
+
) -> None:
|
|
136
|
+
if doc is not None:
|
|
137
|
+
raise TypeError("wrapping an existing document is not supported by ycpp")
|
|
138
|
+
if guid is not None and not isinstance(guid, str):
|
|
139
|
+
raise ValueError("guid must be a string")
|
|
140
|
+
if client_id is not None:
|
|
141
|
+
if not isinstance(client_id, int) or isinstance(client_id, bool):
|
|
142
|
+
raise ValueError(f"client_id must be an int, got {type(client_id)}")
|
|
143
|
+
if client_id >= 2**53:
|
|
144
|
+
raise ValueError("client_id cannot overflow 53 bits")
|
|
145
|
+
if skip_gc is not None and not isinstance(skip_gc, bool):
|
|
146
|
+
raise ValueError(f"skip_gc must be a bool, got {type(skip_gc)}")
|
|
147
|
+
# Generated eagerly: a subdocument's guid is read from the native document when a parent
|
|
148
|
+
# commits, before anyone asks the Python wrapper for it.
|
|
149
|
+
if guid is None:
|
|
150
|
+
guid = str(uuid.uuid4())
|
|
151
|
+
low = _ycpp.Doc(
|
|
152
|
+
_new_client_id() if client_id is None else client_id, not bool(skip_gc)
|
|
153
|
+
)
|
|
154
|
+
low.set_guid(guid)
|
|
155
|
+
self._setup(low, guid, allow_multithreading)
|
|
156
|
+
for key, value in (init or {}).items():
|
|
157
|
+
self[key] = value
|
|
158
|
+
self._Model = Model
|
|
159
|
+
|
|
160
|
+
def _setup(self, low, guid, allow_multithreading=False) -> None:
|
|
161
|
+
self._doc = low
|
|
162
|
+
self._guid = guid
|
|
163
|
+
self._local = threading.local()
|
|
164
|
+
# Present from the start: reading a threading.local attribute that does not exist costs an
|
|
165
|
+
# AttributeError inside the C implementation, which is slower than the check it feeds.
|
|
166
|
+
self._local.errors = []
|
|
167
|
+
self._subscriptions = []
|
|
168
|
+
self._roots = {}
|
|
169
|
+
self._Model = None
|
|
170
|
+
self._dispatcher = _Dispatcher(self)
|
|
171
|
+
# One token per distinct origin value: two origins compare with == and never share a token,
|
|
172
|
+
# whatever their hashes are. A transaction carries the token natively, so an undo manager
|
|
173
|
+
# tracks exactly the origins a caller named.
|
|
174
|
+
self._origin_tokens: dict = {}
|
|
175
|
+
# The Python object behind each token, so an event delivered outside any Python block can
|
|
176
|
+
# report the origin the write carried.
|
|
177
|
+
self._origin_values: dict = {}
|
|
178
|
+
self._next_origin_token = 0
|
|
179
|
+
# The origins of the transactions currently open, keyed by hash, as pycrdt's Doc._origins.
|
|
180
|
+
self._origins: dict = {}
|
|
181
|
+
self._allow_multithreading = allow_multithreading
|
|
182
|
+
self._txn_lock = threading.Lock()
|
|
183
|
+
self._txn_async_lock = AsyncLock()
|
|
184
|
+
# A document that does not allow multithreading runs one transaction at a time across every
|
|
185
|
+
# thread: this counts the natively open ones, whichever thread opened each.
|
|
186
|
+
self._txn_count_lock = threading.Lock()
|
|
187
|
+
self._open_transactions = 0
|
|
188
|
+
# A handle onto a subdocument inside another one: every root it has was started before it
|
|
189
|
+
# was nested, so none of them reads as None the way a root only seen in an update does.
|
|
190
|
+
self._subdoc = False
|
|
191
|
+
# The anyio task group an async transaction runs its observer coroutines in, shared with the
|
|
192
|
+
# event callbacks like pycrdt's Doc._task_group.
|
|
193
|
+
self._task_group = None
|
|
194
|
+
self._doc_observers = {}
|
|
195
|
+
self._map_observers = {}
|
|
196
|
+
self._text_observers = {}
|
|
197
|
+
self._array_observers = {}
|
|
198
|
+
self._deep_observers = {}
|
|
199
|
+
self._xml_observers = {}
|
|
200
|
+
self._subdoc_observers = {}
|
|
201
|
+
self._send_streams: dict[
|
|
202
|
+
bool, set[MemoryObjectSendStream[TransactionEvent | SubdocsEvent]]
|
|
203
|
+
] = {False: set(), True: set()}
|
|
204
|
+
self._event_subscription: dict[bool, Subscription] = {}
|
|
205
|
+
# Events are produced on the writer's thread and consumed from other ones; events() and
|
|
206
|
+
# unsubscribe mutate these maps while _send_event iterates them.
|
|
207
|
+
self._events_lock = threading.Lock()
|
|
208
|
+
|
|
209
|
+
@classmethod
|
|
210
|
+
def _wrapped(cls, low) -> "Doc":
|
|
211
|
+
"""A subdocument handle: the binding owns the document, this wrapper carries the same one."""
|
|
212
|
+
wrapped = cls.__new__(cls)
|
|
213
|
+
wrapped._setup(low, low.guid())
|
|
214
|
+
wrapped._subdoc = True
|
|
215
|
+
return wrapped
|
|
216
|
+
|
|
217
|
+
@classmethod
|
|
218
|
+
def from_snapshot(cls, snapshot, doc: "Doc") -> "Doc":
|
|
219
|
+
"""A new document as it was at the snapshot, with the origin's client id, gc and guid."""
|
|
220
|
+
restored = cls.__new__(cls)
|
|
221
|
+
low = _ycpp.Doc.from_snapshot(snapshot._snapshot, doc._doc)
|
|
222
|
+
restored._setup(low, low.guid())
|
|
223
|
+
for key, root in doc._roots.items():
|
|
224
|
+
restored[key] = _empty_root(root)
|
|
225
|
+
return restored
|
|
226
|
+
|
|
227
|
+
@property
|
|
228
|
+
def client_id(self) -> int:
|
|
229
|
+
return self._doc.client_id()
|
|
230
|
+
|
|
231
|
+
@property
|
|
232
|
+
def guid(self) -> str:
|
|
233
|
+
# Generated on first read: a Doc that never exposes its identifier does not pay for a
|
|
234
|
+
# uuid4 (an os.urandom call) at construction.
|
|
235
|
+
value = self._guid
|
|
236
|
+
if value is None:
|
|
237
|
+
value = self._guid = str(uuid.uuid4())
|
|
238
|
+
return value
|
|
239
|
+
|
|
240
|
+
def _stack(self) -> list:
|
|
241
|
+
stack = getattr(self._local, "stack", None)
|
|
242
|
+
if stack is None:
|
|
243
|
+
stack = self._local.stack = []
|
|
244
|
+
return stack
|
|
245
|
+
|
|
246
|
+
def _origin_token(self, origin) -> int:
|
|
247
|
+
"""The native token for a Python origin: equal origins share one, unequal ones never do."""
|
|
248
|
+
try:
|
|
249
|
+
token = self._origin_tokens.get(origin)
|
|
250
|
+
except TypeError as error:
|
|
251
|
+
raise TypeError("Origin must be hashable") from error
|
|
252
|
+
if token is None:
|
|
253
|
+
self._next_origin_token += 1
|
|
254
|
+
token = self._next_origin_token
|
|
255
|
+
self._origin_tokens[origin] = token
|
|
256
|
+
self._origin_values[token] = origin
|
|
257
|
+
return token
|
|
258
|
+
|
|
259
|
+
def _current_transaction(self):
|
|
260
|
+
return self._txn
|
|
261
|
+
|
|
262
|
+
@property
|
|
263
|
+
def _txn(self):
|
|
264
|
+
"""The transaction open on this thread, as pycrdt's Doc._txn: a second block joins it.
|
|
265
|
+
|
|
266
|
+
The stack holds weak references, so a Transaction dropped without leaving its block can be
|
|
267
|
+
committed by its finalizer instead of staying open until the document dies.
|
|
268
|
+
"""
|
|
269
|
+
stack = self._stack()
|
|
270
|
+
while stack:
|
|
271
|
+
current = stack[-1]()
|
|
272
|
+
if current is not None:
|
|
273
|
+
return current
|
|
274
|
+
stack.pop()
|
|
275
|
+
return None
|
|
276
|
+
|
|
277
|
+
def transaction(self, origin=None) -> Transaction:
|
|
278
|
+
current = self._txn
|
|
279
|
+
if current is not None:
|
|
280
|
+
if origin is not None and origin != current.origin:
|
|
281
|
+
raise RuntimeError(
|
|
282
|
+
"Nested transactions must have same origin as root transaction"
|
|
283
|
+
)
|
|
284
|
+
return current
|
|
285
|
+
return Transaction(self, origin)
|
|
286
|
+
|
|
287
|
+
def new_transaction(self, origin=None, timeout=None) -> NewTransaction:
|
|
288
|
+
return NewTransaction(self, origin, timeout=timeout)
|
|
289
|
+
|
|
290
|
+
def _read_transaction(self, transaction) -> ReadTransaction:
|
|
291
|
+
return ReadTransaction(self, transaction.origin)
|
|
292
|
+
|
|
293
|
+
def _begin_transaction(self, origin) -> int:
|
|
294
|
+
# A document that does not allow multithreading refuses a transaction opened while another
|
|
295
|
+
# one is still running, in any thread, as Yrs does; the caller gets a plain error.
|
|
296
|
+
counted = False
|
|
297
|
+
if not self._allow_multithreading:
|
|
298
|
+
with self._txn_count_lock:
|
|
299
|
+
if self._open_transactions:
|
|
300
|
+
raise RuntimeError("Already in a transaction")
|
|
301
|
+
self._open_transactions += 1
|
|
302
|
+
counted = True
|
|
303
|
+
try:
|
|
304
|
+
self._doc.set_local_origin(
|
|
305
|
+
None if origin is None else self._origin_token(origin)
|
|
306
|
+
)
|
|
307
|
+
return self._doc.begin_transaction()
|
|
308
|
+
except BaseException:
|
|
309
|
+
if counted:
|
|
310
|
+
self._release_transaction_slot()
|
|
311
|
+
raise
|
|
312
|
+
|
|
313
|
+
def _end_transaction(self, token) -> None:
|
|
314
|
+
self._doc.end_transaction(token)
|
|
315
|
+
if not self._allow_multithreading:
|
|
316
|
+
self._release_transaction_slot()
|
|
317
|
+
|
|
318
|
+
def _release_transaction_slot(self) -> None:
|
|
319
|
+
with self._txn_count_lock:
|
|
320
|
+
if self._open_transactions:
|
|
321
|
+
self._open_transactions -= 1
|
|
322
|
+
|
|
323
|
+
def _errors(self) -> list:
|
|
324
|
+
errors = getattr(self._local, "errors", None)
|
|
325
|
+
if errors is None:
|
|
326
|
+
errors = self._local.errors = []
|
|
327
|
+
return errors
|
|
328
|
+
|
|
329
|
+
def _add_exception(self, error) -> None:
|
|
330
|
+
self._errors().append(error)
|
|
331
|
+
|
|
332
|
+
def _raise_observer_errors(self) -> None:
|
|
333
|
+
# The common call has nothing to raise. Another thread has no list until one of its observer
|
|
334
|
+
# callbacks fails, hence the try.
|
|
335
|
+
try:
|
|
336
|
+
errors = self._local.errors
|
|
337
|
+
except AttributeError:
|
|
338
|
+
return
|
|
339
|
+
if errors:
|
|
340
|
+
exceptions = tuple(errors)
|
|
341
|
+
errors.clear()
|
|
342
|
+
for error in exceptions:
|
|
343
|
+
# An async observer reached from a sync transaction raises on the caller's side, as
|
|
344
|
+
# pycrdt's async wrapper does, instead of joining the collected group.
|
|
345
|
+
if isinstance(error, _AsyncCallbackInSyncTransaction):
|
|
346
|
+
raise error
|
|
347
|
+
raise ExceptionGroup("Observer callback error", exceptions)
|
|
348
|
+
|
|
349
|
+
def _unobserve(self, observer: int) -> None:
|
|
350
|
+
self._doc_observers.pop(observer, None)
|
|
351
|
+
self._map_observers.pop(observer, None)
|
|
352
|
+
self._text_observers.pop(observer, None)
|
|
353
|
+
self._array_observers.pop(observer, None)
|
|
354
|
+
self._deep_observers.pop(observer, None)
|
|
355
|
+
self._xml_observers.pop(observer, None)
|
|
356
|
+
self._subdoc_observers.pop(observer, None)
|
|
357
|
+
self._doc.unobserve(observer)
|
|
358
|
+
|
|
359
|
+
def _observe_map(self, ref) -> int:
|
|
360
|
+
return self._doc.observe_map(ref, self._dispatcher.map_event)
|
|
361
|
+
|
|
362
|
+
def _observe_text(self, ref) -> int:
|
|
363
|
+
return self._doc.observe_text(ref, self._dispatcher.text_event)
|
|
364
|
+
|
|
365
|
+
def _observe_array(self, ref) -> int:
|
|
366
|
+
return self._doc.observe_array(ref, self._dispatcher.array_event)
|
|
367
|
+
|
|
368
|
+
def _observe_deep_map(self, ref) -> int:
|
|
369
|
+
return self._doc.observe_deep_map(ref, self._dispatcher.deep_event)
|
|
370
|
+
|
|
371
|
+
def _observe_deep_text(self, ref) -> int:
|
|
372
|
+
return self._doc.observe_deep_text(ref, self._dispatcher.deep_event)
|
|
373
|
+
|
|
374
|
+
def _observe_deep_array(self, ref) -> int:
|
|
375
|
+
return self._doc.observe_deep_array(ref, self._dispatcher.deep_event)
|
|
376
|
+
|
|
377
|
+
def _dispatch_doc(self, observer: int, update, before, after) -> None:
|
|
378
|
+
entry = self._doc_observers.get(observer)
|
|
379
|
+
if entry is None:
|
|
380
|
+
return
|
|
381
|
+
callback, argc = entry
|
|
382
|
+
event = TransactionEvent(update, before, after, self)
|
|
383
|
+
invoke(self, callback, argc, event)
|
|
384
|
+
|
|
385
|
+
def _dispatch_map(self, observer: int, payload: dict) -> None:
|
|
386
|
+
entry = self._map_observers.get(observer)
|
|
387
|
+
if entry is None:
|
|
388
|
+
return
|
|
389
|
+
target, callback, argc = entry
|
|
390
|
+
event = MapEvent(target, self._wrap_map_values(payload), self)
|
|
391
|
+
invoke(self, callback, argc, event)
|
|
392
|
+
|
|
393
|
+
def _event_transaction(self) -> Transaction:
|
|
394
|
+
current = self._current_transaction()
|
|
395
|
+
if current is not None:
|
|
396
|
+
return current
|
|
397
|
+
# No Python transaction block is open: the origin the write carried natively is the one an
|
|
398
|
+
# undo or redo ran under, and an observer must see it.
|
|
399
|
+
token = self._doc.event_origin()
|
|
400
|
+
if token is None:
|
|
401
|
+
return Transaction._from_native(self, None)
|
|
402
|
+
return Transaction._from_native(self, self._origin_values.get(token, token))
|
|
403
|
+
|
|
404
|
+
def _dispatch_text(self, observer: int, delta: list) -> None:
|
|
405
|
+
entry = self._text_observers.get(observer)
|
|
406
|
+
if entry is None:
|
|
407
|
+
return
|
|
408
|
+
target, callback, argc = entry
|
|
409
|
+
event = TextEvent(target, delta, self)
|
|
410
|
+
invoke(self, callback, argc, event)
|
|
411
|
+
|
|
412
|
+
def _dispatch_array(self, observer: int, delta: list) -> None:
|
|
413
|
+
entry = self._array_observers.get(observer)
|
|
414
|
+
if entry is None:
|
|
415
|
+
return
|
|
416
|
+
target, callback, argc = entry
|
|
417
|
+
event = ArrayEvent(target, _live_delta(target, delta), self)
|
|
418
|
+
invoke(self, callback, argc, event)
|
|
419
|
+
|
|
420
|
+
def _wrap_target(self, target):
|
|
421
|
+
if isinstance(target, _ycpp.MapRef):
|
|
422
|
+
return Map(_doc=self, _integrated=target)
|
|
423
|
+
if isinstance(target, _ycpp.TextRef):
|
|
424
|
+
return Text(_doc=self, _integrated=target)
|
|
425
|
+
if isinstance(target, _ycpp.XmlFragmentRef):
|
|
426
|
+
return XmlFragment(_doc=self, _integrated=target)
|
|
427
|
+
if isinstance(target, _ycpp.XmlElementRef):
|
|
428
|
+
return XmlElement(_doc=self, _integrated=target)
|
|
429
|
+
if isinstance(target, _ycpp.XmlTextRef):
|
|
430
|
+
return XmlText(_doc=self, _integrated=target)
|
|
431
|
+
return Array(_doc=self, _integrated=target)
|
|
432
|
+
|
|
433
|
+
def _dispatch_deep(self, observer: int, payloads: list) -> None:
|
|
434
|
+
entry = self._deep_observers.get(observer)
|
|
435
|
+
if entry is None:
|
|
436
|
+
return
|
|
437
|
+
callback, argc = entry
|
|
438
|
+
events = []
|
|
439
|
+
for payload in payloads:
|
|
440
|
+
target = self._wrap_target(payload["target"])
|
|
441
|
+
path = payload["path"]
|
|
442
|
+
if payload["kind"] == "map":
|
|
443
|
+
events.append(
|
|
444
|
+
MapEvent(target, self._wrap_map_values(payload["keys"]), self, path)
|
|
445
|
+
)
|
|
446
|
+
elif payload["kind"] == "text":
|
|
447
|
+
events.append(TextEvent(target, payload["delta"], self, path))
|
|
448
|
+
elif payload["kind"] == "xml":
|
|
449
|
+
events.append(
|
|
450
|
+
XmlEvent(
|
|
451
|
+
target,
|
|
452
|
+
payload["children_changed"],
|
|
453
|
+
self._wrap_map_values(payload["keys"]),
|
|
454
|
+
payload["delta"],
|
|
455
|
+
self,
|
|
456
|
+
path,
|
|
457
|
+
)
|
|
458
|
+
)
|
|
459
|
+
else:
|
|
460
|
+
events.append(ArrayEvent(target, payload["delta"], self, path))
|
|
461
|
+
invoke_deep(self, callback, argc, events)
|
|
462
|
+
|
|
463
|
+
def _dispatch_xml(self, observer: int, payload: dict) -> None:
|
|
464
|
+
entry = self._xml_observers.get(observer)
|
|
465
|
+
if entry is None:
|
|
466
|
+
return
|
|
467
|
+
node, callback, argc = entry
|
|
468
|
+
event = XmlEvent(
|
|
469
|
+
node,
|
|
470
|
+
payload["children_changed"],
|
|
471
|
+
self._wrap_map_values(payload["keys"]),
|
|
472
|
+
payload["delta"],
|
|
473
|
+
self,
|
|
474
|
+
)
|
|
475
|
+
invoke(self, callback, argc, event)
|
|
476
|
+
|
|
477
|
+
def _wrap_map_values(self, payload: dict) -> dict:
|
|
478
|
+
for change in payload.values():
|
|
479
|
+
value = change.get("newValue")
|
|
480
|
+
if isinstance(value, _ycpp.MapRef):
|
|
481
|
+
change["newValue"] = Map(_doc=self, _integrated=value)
|
|
482
|
+
return payload
|
|
483
|
+
|
|
484
|
+
def _dispatch_subdocs(self, observer: int, payload: dict) -> None:
|
|
485
|
+
entry = self._subdoc_observers.get(observer)
|
|
486
|
+
if entry is None:
|
|
487
|
+
return
|
|
488
|
+
callback, argc = entry
|
|
489
|
+
event = SubdocsEvent(
|
|
490
|
+
list(payload["added"]),
|
|
491
|
+
list(payload["removed"]),
|
|
492
|
+
list(payload["loaded"]),
|
|
493
|
+
self,
|
|
494
|
+
)
|
|
495
|
+
invoke(self, callback, argc, event)
|
|
496
|
+
|
|
497
|
+
def get_state(self) -> bytes:
|
|
498
|
+
return self._doc.state()
|
|
499
|
+
|
|
500
|
+
def get_update(self, state: bytes | None = None) -> bytes:
|
|
501
|
+
return self._doc.update(state)
|
|
502
|
+
|
|
503
|
+
def _copy_doc(self) -> "Doc":
|
|
504
|
+
"""A fresh document with the same root names and kinds."""
|
|
505
|
+
return Doc({key: _empty_root(root) for key, root in self._roots.items()})
|
|
506
|
+
|
|
507
|
+
def _validate(self, candidate) -> None:
|
|
508
|
+
values = {name: candidate[name].to_py() for name in self._Model.model_fields}
|
|
509
|
+
self._Model(**values)
|
|
510
|
+
|
|
511
|
+
def apply_update(self, update) -> None:
|
|
512
|
+
if self._Model is not None:
|
|
513
|
+
# Validation runs on the state the document would have: its own, local writes included,
|
|
514
|
+
# plus this update. A rejected update or a failed decode never reaches the real document.
|
|
515
|
+
candidate = self._copy_doc()
|
|
516
|
+
candidate.apply_update(self.get_update())
|
|
517
|
+
candidate.apply_update(update)
|
|
518
|
+
self._validate(candidate)
|
|
519
|
+
self._doc.apply_update(update)
|
|
520
|
+
|
|
521
|
+
def _root(self, key: str) -> Map:
|
|
522
|
+
root = self._roots.get(key)
|
|
523
|
+
if root is None:
|
|
524
|
+
root = Map(_doc=self, _integrated=self._doc.get_map(key))
|
|
525
|
+
self._roots[key] = root
|
|
526
|
+
return root
|
|
527
|
+
|
|
528
|
+
def _array(self, key: str) -> Array:
|
|
529
|
+
array = self._roots.get(key)
|
|
530
|
+
if array is None:
|
|
531
|
+
array = Array(_doc=self, _integrated=self._doc.get_array(key))
|
|
532
|
+
self._roots[key] = array
|
|
533
|
+
return array
|
|
534
|
+
|
|
535
|
+
def _text(self, key: str) -> Text:
|
|
536
|
+
text = self._roots.get(key)
|
|
537
|
+
if text is None:
|
|
538
|
+
text = Text(_doc=self, _integrated=self._doc.get_text(key))
|
|
539
|
+
self._roots[key] = text
|
|
540
|
+
return text
|
|
541
|
+
|
|
542
|
+
def _fragment(self, key: str) -> XmlFragment:
|
|
543
|
+
fragment = self._roots.get(key)
|
|
544
|
+
if fragment is None:
|
|
545
|
+
fragment = XmlFragment()
|
|
546
|
+
fragment._get_or_insert(self, key)
|
|
547
|
+
fragment._init()
|
|
548
|
+
self._roots[key] = fragment
|
|
549
|
+
return fragment
|
|
550
|
+
|
|
551
|
+
def __setitem__(self, key, value) -> None:
|
|
552
|
+
if not isinstance(key, str):
|
|
553
|
+
raise RuntimeError("Key must be of type string")
|
|
554
|
+
if isinstance(value, Text):
|
|
555
|
+
value._integrate(self, self._doc.get_text(key))
|
|
556
|
+
elif isinstance(value, Array):
|
|
557
|
+
value._integrate(self, self._doc.get_array(key))
|
|
558
|
+
elif isinstance(value, Map):
|
|
559
|
+
value._integrate(self, self._doc.get_map(key))
|
|
560
|
+
else:
|
|
561
|
+
from ._xml import _XmlBase
|
|
562
|
+
|
|
563
|
+
if not isinstance(value, _XmlBase):
|
|
564
|
+
raise TypeError(
|
|
565
|
+
"ycpp supports Map, Text, Array and XmlFragment root types only"
|
|
566
|
+
)
|
|
567
|
+
# XmlText and XmlElement refuse this with a ValueError, as pycrdt does.
|
|
568
|
+
value._get_or_insert(self, key)
|
|
569
|
+
value._init()
|
|
570
|
+
self._roots[key] = value
|
|
571
|
+
|
|
572
|
+
def _started_root(self, key: str):
|
|
573
|
+
"""A subdocument handle wraps the roots it already holds: nesting them started them all."""
|
|
574
|
+
kinds = dict(self._doc.root_types())
|
|
575
|
+
if key not in kinds:
|
|
576
|
+
return None
|
|
577
|
+
kind = kinds[key]
|
|
578
|
+
if kind == 0:
|
|
579
|
+
return self._array(key)
|
|
580
|
+
if kind == 1:
|
|
581
|
+
return self._root(key)
|
|
582
|
+
if kind == 2:
|
|
583
|
+
return self._text(key)
|
|
584
|
+
return self._fragment(key)
|
|
585
|
+
|
|
586
|
+
def __getitem__(self, key):
|
|
587
|
+
if not isinstance(key, str):
|
|
588
|
+
raise RuntimeError("Key must be of type string")
|
|
589
|
+
root = self._roots.get(key)
|
|
590
|
+
if root is None and key not in self._doc.root_names():
|
|
591
|
+
raise KeyError(key)
|
|
592
|
+
if root is None and self._subdoc:
|
|
593
|
+
root = self._started_root(key)
|
|
594
|
+
return root
|
|
595
|
+
|
|
596
|
+
def __iter__(self):
|
|
597
|
+
return iter(self.keys())
|
|
598
|
+
|
|
599
|
+
def __contains__(self, key) -> bool:
|
|
600
|
+
return key in self._roots or key in self._doc.root_names()
|
|
601
|
+
|
|
602
|
+
def get(self, key: str, *, type: type = Map):
|
|
603
|
+
# A generic alias (`Array[bool]`) is not a class: dispatch on its origin.
|
|
604
|
+
type = get_origin(type) or type
|
|
605
|
+
if type is Text:
|
|
606
|
+
return self._text(key)
|
|
607
|
+
if type is Array:
|
|
608
|
+
return self._array(key)
|
|
609
|
+
if type is XmlFragment:
|
|
610
|
+
return self._fragment(key)
|
|
611
|
+
if type is not Map:
|
|
612
|
+
raise TypeError(
|
|
613
|
+
"ycpp supports Map, Text, Array and XmlFragment root types only"
|
|
614
|
+
)
|
|
615
|
+
return self._root(key)
|
|
616
|
+
|
|
617
|
+
def keys(self):
|
|
618
|
+
# The native roots, so a root that arrived through an update is listed too.
|
|
619
|
+
return self._doc.root_names()
|
|
620
|
+
|
|
621
|
+
def values(self):
|
|
622
|
+
return [self[key] for key in self._doc.root_names()]
|
|
623
|
+
|
|
624
|
+
def items(self):
|
|
625
|
+
return [(key, self[key]) for key in self._doc.root_names()]
|
|
626
|
+
|
|
627
|
+
def observe(self, callback) -> Subscription:
|
|
628
|
+
observer = self._doc.observe(self._dispatcher.doc_event)
|
|
629
|
+
self._doc_observers[observer] = (callback, arg_count(callback))
|
|
630
|
+
subscription = Subscription(self, observer)
|
|
631
|
+
self._subscriptions.append(subscription)
|
|
632
|
+
return subscription
|
|
633
|
+
|
|
634
|
+
def observe_subdocs(self, callback) -> Subscription:
|
|
635
|
+
observer = self._doc.observe_subdocs(self._dispatcher.subdocs_event)
|
|
636
|
+
self._subdoc_observers[observer] = (callback, arg_count(callback))
|
|
637
|
+
subscription = Subscription(self, observer)
|
|
638
|
+
self._subscriptions.append(subscription)
|
|
639
|
+
return subscription
|
|
640
|
+
|
|
641
|
+
def _observe_xml(self, ref) -> int:
|
|
642
|
+
return self._doc.observe_xml(ref, self._dispatcher.xml_event)
|
|
643
|
+
|
|
644
|
+
def _observe_deep_xml(self, ref) -> int:
|
|
645
|
+
return self._doc.observe_deep_xml(ref, self._dispatcher.deep_event)
|
|
646
|
+
|
|
647
|
+
def events(
|
|
648
|
+
self,
|
|
649
|
+
subdocs: bool = False,
|
|
650
|
+
max_buffer_size: float = float("inf"),
|
|
651
|
+
async_transactions: bool = False,
|
|
652
|
+
) -> UnsubscribableStream[TransactionEvent | SubdocsEvent]:
|
|
653
|
+
"""Iterates over the document events through a buffer, instead of a callback.
|
|
654
|
+
|
|
655
|
+
Unsubscribing stops collecting and leaves the buffered events readable.
|
|
656
|
+
"""
|
|
657
|
+
observe = self.observe_subdocs if subdocs else self.observe
|
|
658
|
+
with self._events_lock:
|
|
659
|
+
if not self._send_streams[subdocs]:
|
|
660
|
+
send = (
|
|
661
|
+
partial(self._async_send_event, subdocs)
|
|
662
|
+
if async_transactions
|
|
663
|
+
else partial(self._send_event, subdocs)
|
|
664
|
+
)
|
|
665
|
+
self._event_subscription[subdocs] = observe(send)
|
|
666
|
+
send_stream, receive_stream = create_memory_object_stream[
|
|
667
|
+
TransactionEvent | SubdocsEvent
|
|
668
|
+
](max_buffer_size=max_buffer_size)
|
|
669
|
+
self._send_streams[subdocs].add(send_stream)
|
|
670
|
+
return _with_unsubscribe(
|
|
671
|
+
receive_stream, partial(self._unsubscribe_events, subdocs, send_stream)
|
|
672
|
+
)
|
|
673
|
+
|
|
674
|
+
def _unsubscribe_events(self, subdocs, send_stream) -> None:
|
|
675
|
+
with self._events_lock:
|
|
676
|
+
send_streams = self._send_streams[subdocs]
|
|
677
|
+
if send_stream not in send_streams:
|
|
678
|
+
return
|
|
679
|
+
send_streams.remove(send_stream)
|
|
680
|
+
send_stream.close()
|
|
681
|
+
subscription = (
|
|
682
|
+
self._event_subscription.pop(subdocs) if not send_streams else None
|
|
683
|
+
)
|
|
684
|
+
if subscription is not None:
|
|
685
|
+
self.unobserve(subscription)
|
|
686
|
+
|
|
687
|
+
def _send_event(self, subdocs: bool, event) -> None:
|
|
688
|
+
with self._events_lock:
|
|
689
|
+
send_streams = tuple(self._send_streams[subdocs])
|
|
690
|
+
to_remove: list[MemoryObjectSendStream] = []
|
|
691
|
+
for send_stream in send_streams:
|
|
692
|
+
try:
|
|
693
|
+
send_stream.send_nowait(event)
|
|
694
|
+
except (BrokenResourceError, ClosedResourceError):
|
|
695
|
+
to_remove.append(send_stream)
|
|
696
|
+
for send_stream in to_remove:
|
|
697
|
+
self._unsubscribe_events(subdocs, send_stream)
|
|
698
|
+
|
|
699
|
+
async def _async_send_event(self, subdocs: bool, event) -> None:
|
|
700
|
+
with self._events_lock:
|
|
701
|
+
send_streams = tuple(self._send_streams[subdocs])
|
|
702
|
+
to_remove: list[MemoryObjectSendStream] = []
|
|
703
|
+
for send_stream in send_streams:
|
|
704
|
+
try:
|
|
705
|
+
# A clone keeps an in-flight send alive when unsubscribe closes the original.
|
|
706
|
+
async with send_stream.clone() as stream:
|
|
707
|
+
await stream.send(event)
|
|
708
|
+
except (BrokenResourceError, ClosedResourceError):
|
|
709
|
+
to_remove.append(send_stream)
|
|
710
|
+
for send_stream in to_remove:
|
|
711
|
+
self._unsubscribe_events(subdocs, send_stream)
|
|
712
|
+
|
|
713
|
+
def unobserve(self, subscription) -> None:
|
|
714
|
+
self._subscriptions.remove(subscription)
|
|
715
|
+
subscription.drop()
|
|
716
|
+
|
|
717
|
+
def __repr__(self) -> str:
|
|
718
|
+
return f"Doc(client_id={self.client_id}, guid={self.guid!r})"
|