Skip to content

Commit 877e5b9

Browse files
Doc awareness (#277)
* Add an awareness in the YDoc on server side * Test awareness getter/setter * Depend on pycrdt instead of pycrdt_websocket * Apply suggestions from code review Co-authored-by: David Brochart <[email protected]> * Apply suggestions from review Co-authored-by: David Brochart <[email protected]> * Apply suggestions from code review Co-authored-by: David Brochart <[email protected]> * Apply suggestions from code review Co-authored-by: David Brochart <[email protected]> * Update pycrdt_websocket for tests --------- Co-authored-by: David Brochart <[email protected]> Co-authored-by: David Brochart <[email protected]>
1 parent bf2db86 commit 877e5b9

File tree

6 files changed

+39
-13
lines changed

6 files changed

+39
-13
lines changed

jupyter_ydoc/ybasedoc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from abc import ABC, abstractmethod
55
from typing import Any, Callable, Dict, Optional
66

7-
from pycrdt import Doc, Map, Subscription, UndoManager
7+
from pycrdt import Awareness, Doc, Map, Subscription, UndoManager
88

99

1010
class YBaseDoc(ABC):
@@ -20,17 +20,22 @@ class YBaseDoc(ABC):
2020
_subscriptions: Dict[Any, Subscription]
2121
_undo_manager: UndoManager
2222

23-
def __init__(self, ydoc: Optional[Doc] = None):
23+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
2424
"""
2525
Constructs a YBaseDoc.
2626
2727
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
2828
:type ydoc: :class:`pycrdt.Doc`, optional.
29+
:param awareness: The :class:`pycrdt.Awareness` that shares non persistent data
30+
between clients.
31+
:type awareness: :class:`pycrdt.Awareness`, optional.
2932
"""
3033
if ydoc is None:
3134
self._ydoc = Doc()
3235
else:
3336
self._ydoc = ydoc
37+
self.awareness = awareness
38+
3439
self._ystate = self._ydoc.get("state", type=Map)
3540
self._subscriptions = {}
3641
self._undo_manager = UndoManager(doc=self._ydoc, capture_timeout_millis=0)

jupyter_ydoc/yblob.py

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

7-
from pycrdt import Doc, Map
7+
from pycrdt import Awareness, Doc, Map
88

99
from .ybasedoc import YBaseDoc
1010

@@ -24,14 +24,17 @@ class YBlob(YBaseDoc):
2424
}
2525
"""
2626

27-
def __init__(self, ydoc: Optional[Doc] = None):
27+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
2828
"""
2929
Constructs a YBlob.
3030
3131
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
3232
:type ydoc: :class:`pycrdt.Doc`, optional.
33+
:param awareness: The :class:`pycrdt.Awareness` that shares non persistent data
34+
between clients.
35+
:type awareness: :class:`pycrdt.Awareness`, optional.
3336
"""
34-
super().__init__(ydoc)
37+
super().__init__(ydoc, awareness)
3538
self._ysource = self._ydoc.get("source", type=Map)
3639
self.undo_manager.expand_scope(self._ysource)
3740

jupyter_ydoc/ynotebook.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Any, Callable, Dict, Optional
77
from uuid import uuid4
88

9-
from pycrdt import Array, Doc, Map, Text
9+
from pycrdt import Array, Awareness, Doc, Map, Text
1010

1111
from .utils import cast_all
1212
from .ybasedoc import YBaseDoc
@@ -47,14 +47,17 @@ class YNotebook(YBaseDoc):
4747
}
4848
"""
4949

50-
def __init__(self, ydoc: Optional[Doc] = None):
50+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
5151
"""
5252
Constructs a YNotebook.
5353
5454
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
5555
:type ydoc: :class:`pycrdt.Doc`, optional.
56+
:param awareness: The :class:`pycrdt.Awareness` that shares non persistent data
57+
between clients.
58+
:type awareness: :class:`pycrdt.Awareness`, optional.
5659
"""
57-
super().__init__(ydoc)
60+
super().__init__(ydoc, awareness)
5861
self._ymeta = self._ydoc.get("meta", type=Map)
5962
self._ycells = self._ydoc.get("cells", type=Array)
6063
self.undo_manager.expand_scope(self._ycells)

jupyter_ydoc/yunicode.py

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

7-
from pycrdt import Doc, Text
7+
from pycrdt import Awareness, Doc, Text
88

99
from .ybasedoc import YBaseDoc
1010

@@ -23,14 +23,17 @@ class YUnicode(YBaseDoc):
2323
}
2424
"""
2525

26-
def __init__(self, ydoc: Optional[Doc] = None):
26+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
2727
"""
2828
Constructs a YUnicode.
2929
3030
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
3131
:type ydoc: :class:`pycrdt.Doc`, optional.
32+
:param awareness: The :class:`pycrdt.Awareness` that shares non persistent data
33+
between clients.
34+
:type awareness: :class:`pycrdt.Awareness`, optional.
3235
"""
33-
super().__init__(ydoc)
36+
super().__init__(ydoc, awareness)
3437
self._ysource = self._ydoc.get("source", type=Text)
3538
self.undo_manager.expand_scope(self._ysource)
3639

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ requires-python = ">=3.8"
1313
keywords = ["jupyter", "pycrdt", "yjs"]
1414
dependencies = [
1515
"importlib_metadata >=3.6; python_version<'3.10'",
16-
"pycrdt >=0.9.0,<0.10.0",
16+
"pycrdt >=0.10.1,<0.11.0",
1717
]
1818

1919
[[project.authors]]
@@ -31,7 +31,7 @@ test = [
3131
"pytest",
3232
"pytest-asyncio",
3333
"websockets >=10.0",
34-
"pycrdt-websocket >=0.14.1,<0.15.0",
34+
"pycrdt-websocket >=0.15.0,<0.16.0",
3535
]
3636
docs = [
3737
"sphinx",

tests/test_ydocs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
from pycrdt import Awareness, Doc
5+
46
from jupyter_ydoc import YBlob, YNotebook
57

68

@@ -53,3 +55,13 @@ def test_ynotebook_undo_manager():
5355
ynotebook.undo_manager.undo()
5456
assert len(ynotebook.ycells) == 0
5557
assert not ynotebook.undo_manager.can_undo()
58+
59+
60+
def test_awareness():
61+
yblob = YBlob()
62+
assert yblob.awareness is None
63+
64+
ydoc = Doc()
65+
awareness = Awareness(ydoc)
66+
yblob = YBlob(ydoc, awareness)
67+
assert yblob.awareness == awareness

0 commit comments

Comments
 (0)