Skip to content

Commit 5473320

Browse files
authored
Backport PR #277: Doc awareness (#279)
1 parent a68a8c5 commit 5473320

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
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
@@ -5,7 +5,7 @@
55
from functools import partial
66
from typing import Any, Callable, Optional, Union
77

8-
from pycrdt import Doc, Map
8+
from pycrdt import Awareness, Doc, Map
99

1010
from .ybasedoc import YBaseDoc
1111

@@ -28,14 +28,17 @@ class YBlob(YBaseDoc):
2828
}
2929
"""
3030

31-
def __init__(self, ydoc: Optional[Doc] = None):
31+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
3232
"""
3333
Constructs a YBlob.
3434
3535
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
3636
:type ydoc: :class:`pycrdt.Doc`, optional.
37+
:param awareness: The :class:`pycrdt.Awareness` that shares non persistent data
38+
between clients.
39+
:type awareness: :class:`pycrdt.Awareness`, optional.
3740
"""
38-
super().__init__(ydoc)
41+
super().__init__(ydoc, awareness)
3942
self._ysource = self._ydoc.get("source", type=Map)
4043
self.undo_manager.expand_scope(self._ysource)
4144

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
@@ -46,14 +46,17 @@ class YNotebook(YBaseDoc):
4646
}
4747
"""
4848

49-
def __init__(self, ydoc: Optional[Doc] = None):
49+
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
5050
"""
5151
Constructs a YNotebook.
5252
5353
:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
5454
:type ydoc: :class:`pycrdt.Doc`, optional.
55+
:param awareness: The :class:`pycrdt.Awareness` that shares non persistent data
56+
between clients.
57+
:type awareness: :class:`pycrdt.Awareness`, optional.
5558
"""
56-
super().__init__(ydoc)
59+
super().__init__(ydoc, awareness)
5760
self._ymeta = self._ydoc.get("meta", type=Map)
5861
self._ycells = self._ydoc.get("cells", type=Array)
5962
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.7"
1313
keywords = ["jupyter", "ypy"]
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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

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

68

79
def test_ynotebook_undo_manager():
@@ -33,3 +35,13 @@ def test_ynotebook_undo_manager():
3335
ynotebook.undo_manager.undo()
3436
assert len(ynotebook.ycells) == 0
3537
assert not ynotebook.undo_manager.can_undo()
38+
39+
40+
def test_awareness():
41+
yblob = YBlob()
42+
assert yblob.awareness is None
43+
44+
ydoc = Doc()
45+
awareness = Awareness(ydoc)
46+
yblob = YBlob(ydoc, awareness)
47+
assert yblob.awareness == awareness

0 commit comments

Comments
 (0)