1
1
# Copyright (c) Jupyter Development Team.
2
2
# Distributed under the terms of the Modified BSD License.
3
3
4
+ from __future__ import annotations
5
+
4
6
from abc import ABC , abstractmethod
5
- from typing import Any , Callable , Dict , Optional
7
+ from typing import Any , Callable
8
+
9
+ from pycrdt import Awareness , Doc , Subscription , TypedDoc , TypedMap , UndoManager
10
+
11
+
12
+ class YState (TypedMap ):
13
+ dirty : bool
14
+ hash : str
15
+ path : str
6
16
7
- from pycrdt import Awareness , Doc , Map , Subscription , UndoManager
17
+
18
+ class YDoc (TypedDoc ):
19
+ state : YState
8
20
9
21
10
22
class YBaseDoc (ABC ):
@@ -15,12 +27,12 @@ class YBaseDoc(ABC):
15
27
subscribe to changes in the document.
16
28
"""
17
29
18
- _ydoc : Doc
19
- _ystate : Map
20
- _subscriptions : Dict [Any , Subscription ]
30
+ _ydoc : YDoc
31
+ _ystate : YState
32
+ _subscriptions : dict [Any , Subscription ]
21
33
_undo_manager : UndoManager
22
34
23
- def __init__ (self , ydoc : Optional [ Doc ] = None , awareness : Optional [ Awareness ] = None ):
35
+ def __init__ (self , ydoc : YDoc | Doc | None = None , awareness : Awareness | None = None ):
24
36
"""
25
37
Constructs a YBaseDoc.
26
38
@@ -30,15 +42,15 @@ def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] =
30
42
between clients.
31
43
:type awareness: :class:`pycrdt.Awareness`, optional.
32
44
"""
33
- if ydoc is None :
34
- self ._ydoc = Doc ()
35
- else :
45
+ if isinstance (ydoc , YDoc ):
36
46
self ._ydoc = ydoc
47
+ else :
48
+ self ._ydoc = YDoc (ydoc )
37
49
self .awareness = awareness
38
50
39
- self ._ystate = self ._ydoc . get ( "state" , type = Map )
51
+ self ._ydoc . state = self ._ystate = YState ( )
40
52
self ._subscriptions = {}
41
- self ._undo_manager = UndoManager (doc = self ._ydoc , capture_timeout_millis = 0 )
53
+ self ._undo_manager = UndoManager (doc = self ._ydoc . _ , capture_timeout_millis = 0 )
42
54
43
55
@property
44
56
@abstractmethod
@@ -60,22 +72,22 @@ def undo_manager(self) -> UndoManager:
60
72
"""
61
73
return self ._undo_manager
62
74
63
- def ystate (self ) -> Map :
75
+ def ystate (self ) -> YState :
64
76
"""
65
- A :class:`pycrdt.Map ` containing the state of the document.
77
+ A :class:`YState ` containing the state of the document.
66
78
67
79
:return: The document's state.
68
- :rtype: :class:`pycrdt.Map `
80
+ :rtype: :class:`YState `
69
81
"""
70
82
return self ._ystate
71
83
72
84
@property
73
- def ydoc (self ) -> Doc :
85
+ def ydoc (self ) -> YDoc :
74
86
"""
75
- The underlying :class:`pycrdt.Doc ` that contains the data.
87
+ The :class:`YDoc ` that contains the data.
76
88
77
89
:return: The document's ydoc.
78
- :rtype: :class:`pycrdt.Doc `
90
+ :rtype: :class:`YDoc `
79
91
"""
80
92
return self ._ydoc
81
93
@@ -100,14 +112,17 @@ def source(self, value: Any):
100
112
return self .set (value )
101
113
102
114
@property
103
- def dirty (self ) -> Optional [ bool ] :
115
+ def dirty (self ) -> bool | None :
104
116
"""
105
117
Returns whether the document is dirty.
106
118
107
119
:return: Whether the document is dirty.
108
- :rtype: Optional[ bool]
120
+ :rtype: bool | None
109
121
"""
110
- return self ._ystate .get ("dirty" )
122
+ try :
123
+ return self ._ystate .dirty
124
+ except KeyError :
125
+ return None
111
126
112
127
@dirty .setter
113
128
def dirty (self , value : bool ) -> None :
@@ -117,17 +132,20 @@ def dirty(self, value: bool) -> None:
117
132
:param value: Whether the document is clean or dirty.
118
133
:type value: bool
119
134
"""
120
- self ._ystate [ " dirty" ] = value
135
+ self ._ystate . dirty = value
121
136
122
137
@property
123
- def hash (self ) -> Optional [ str ] :
138
+ def hash (self ) -> str | None :
124
139
"""
125
140
Returns the document hash as computed by contents manager.
126
141
127
142
:return: The document hash.
128
- :rtype: Optional[ str]
143
+ :rtype: str | None
129
144
"""
130
- return self ._ystate .get ("hash" )
145
+ try :
146
+ return self ._ystate .hash
147
+ except KeyError :
148
+ return None
131
149
132
150
@hash .setter
133
151
def hash (self , value : str ) -> None :
@@ -137,17 +155,20 @@ def hash(self, value: str) -> None:
137
155
:param value: The document hash.
138
156
:type value: str
139
157
"""
140
- self ._ystate [ " hash" ] = value
158
+ self ._ystate . hash = value
141
159
142
160
@property
143
- def path (self ) -> Optional [ str ] :
161
+ def path (self ) -> str | None :
144
162
"""
145
163
Returns document's path.
146
164
147
165
:return: Document's path.
148
- :rtype: Optional[ str]
166
+ :rtype: str | None
149
167
"""
150
- return self ._ystate .get ("path" )
168
+ try :
169
+ return self ._ystate .path
170
+ except KeyError :
171
+ return None
151
172
152
173
@path .setter
153
174
def path (self , value : str ) -> None :
@@ -157,7 +178,7 @@ def path(self, value: str) -> None:
157
178
:param value: Document's path.
158
179
:type value: str
159
180
"""
160
- self ._ystate [ " path" ] = value
181
+ self ._ystate . path = value
161
182
162
183
@abstractmethod
163
184
def get (self ) -> Any :
0 commit comments