Skip to content

Commit 7845ff7

Browse files
committed
Add an awareness in the YDoc on server side
1 parent 1ca7c5c commit 7845ff7

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

jupyter_ydoc/ybasedoc.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any, Callable, Dict, Optional
66

77
from pycrdt import Doc, Map, Subscription, UndoManager
8+
from pycrdt_websocket.awareness import Awareness
89

910

1011
class YBaseDoc(ABC):
@@ -20,7 +21,7 @@ class YBaseDoc(ABC):
2021
_subscriptions: Dict[Any, Subscription]
2122
_undo_manager: UndoManager
2223

23-
def __init__(self, ydoc: Optional[Doc] = None):
24+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
2425
"""
2526
Constructs a YBaseDoc.
2627
@@ -31,6 +32,9 @@ def __init__(self, ydoc: Optional[Doc] = None):
3132
self._ydoc = Doc()
3233
else:
3334
self._ydoc = ydoc
35+
36+
self._awareness = awareness
37+
3438
self._ystate = self._ydoc.get("state", type=Map)
3539
self._subscriptions = {}
3640
self._undo_manager = UndoManager(doc=self._ydoc, capture_timeout_millis=0)
@@ -74,6 +78,25 @@ def ydoc(self) -> Doc:
7478
"""
7579
return self._ydoc
7680

81+
@property
82+
def awareness(self) -> Awareness | None:
83+
"""
84+
Return the awareness.
85+
86+
:return: The document's awareness.
87+
:rtype: :class:`pycrdt_websocket.awareness.Awareness`
88+
"""
89+
return self._awareness
90+
91+
@awareness.setter
92+
def awareness(self, value: Awareness):
93+
"""
94+
Sets the awareness.
95+
96+
:param: The awareness.
97+
"""
98+
self._awareness = value
99+
77100
@property
78101
def source(self) -> Any:
79102
"""

jupyter_ydoc/yblob.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any, Callable, Optional
66

77
from pycrdt import Doc, Map
8+
from pycrdt_websocket.awareness import Awareness
89

910
from .ybasedoc import YBaseDoc
1011

@@ -24,14 +25,14 @@ class YBlob(YBaseDoc):
2425
}
2526
"""
2627

27-
def __init__(self, ydoc: Optional[Doc] = None):
28+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
2829
"""
2930
Constructs a YBlob.
3031
3132
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
3233
:type ydoc: :class:`pycrdt.Doc`, optional.
3334
"""
34-
super().__init__(ydoc)
35+
super().__init__(ydoc, awareness)
3536
self._ysource = self._ydoc.get("source", type=Map)
3637
self.undo_manager.expand_scope(self._ysource)
3738

jupyter_ydoc/ynotebook.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from uuid import uuid4
88

99
from pycrdt import Array, Doc, Map, Text
10+
from pycrdt_websocket.awareness import Awareness
1011

1112
from .utils import cast_all
1213
from .ybasedoc import YBaseDoc
@@ -47,14 +48,14 @@ class YNotebook(YBaseDoc):
4748
}
4849
"""
4950

50-
def __init__(self, ydoc: Optional[Doc] = None):
51+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
5152
"""
5253
Constructs a YNotebook.
5354
5455
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
5556
:type ydoc: :class:`pycrdt.Doc`, optional.
5657
"""
57-
super().__init__(ydoc)
58+
super().__init__(ydoc, awareness)
5859
self._ymeta = self._ydoc.get("meta", type=Map)
5960
self._ycells = self._ydoc.get("cells", type=Array)
6061
self.undo_manager.expand_scope(self._ycells)

jupyter_ydoc/yunicode.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any, Callable, Optional
66

77
from pycrdt import Doc, Text
8+
from pycrdt_websocket.awareness import Awareness
89

910
from .ybasedoc import YBaseDoc
1011

@@ -23,14 +24,14 @@ class YUnicode(YBaseDoc):
2324
}
2425
"""
2526

26-
def __init__(self, ydoc: Optional[Doc] = None):
27+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
2728
"""
2829
Constructs a YUnicode.
2930
3031
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
3132
:type ydoc: :class:`pycrdt.Doc`, optional.
3233
"""
33-
super().__init__(ydoc)
34+
super().__init__(ydoc, awareness)
3435
self._ysource = self._ydoc.get("source", type=Text)
3536
self.undo_manager.expand_scope(self._ysource)
3637

0 commit comments

Comments
 (0)