Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc awareness #277

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions jupyter_ydoc/ybasedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from abc import ABC, abstractmethod
from typing import Any, Callable, Dict, Optional

from pycrdt import Doc, Map, Subscription, UndoManager
from pycrdt import Awareness, Doc, Map, Subscription, UndoManager


class YBaseDoc(ABC):
Expand All @@ -20,17 +20,22 @@ class YBaseDoc(ABC):
_subscriptions: Dict[Any, Subscription]
_undo_manager: UndoManager

def __init__(self, ydoc: Optional[Doc] = None):
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
"""
Constructs a YBaseDoc.

:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
:type ydoc: :class:`pycrdt.Doc`, optional.
:param awareness: The :class:`pycrdt.Awareness` that share non persistent data
brichet marked this conversation as resolved.
Show resolved Hide resolved
between clients.
:type awareness: :class:`pycrdt.Awareness`, optional.
"""
if ydoc is None:
self._ydoc = Doc()
else:
self._ydoc = ydoc
self._awareness = awareness
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just have:

Suggested change
self._awareness = awareness
self.awareness = awareness

And remove the getter and setter as they do nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we should only remove the setter, to make awareness read only, it is not supposed to be set later.
What do you think ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the getter and setter.


self._ystate = self._ydoc.get("state", type=Map)
self._subscriptions = {}
self._undo_manager = UndoManager(doc=self._ydoc, capture_timeout_millis=0)
Expand Down Expand Up @@ -74,6 +79,26 @@ def ydoc(self) -> Doc:
"""
return self._ydoc

@property
def awareness(self) -> Awareness | None:
brichet marked this conversation as resolved.
Show resolved Hide resolved
"""
Returns the awareness.

:return: The document's awareness.
:rtype: :class:`pycrdt.Awareness` or None.
"""
return self._awareness

@awareness.setter
def awareness(self, value: Awareness) -> None:
"""
Sets the awareness.

:param value: The awareness to set.
:type value: :class:`pycrdt.Awareness`.
"""
self._awareness = value

@property
def source(self) -> Any:
"""
Expand Down
9 changes: 6 additions & 3 deletions jupyter_ydoc/yblob.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from typing import Any, Callable, Optional

from pycrdt import Doc, Map
from pycrdt import Awareness, Doc, Map

from .ybasedoc import YBaseDoc

Expand All @@ -24,14 +24,17 @@ class YBlob(YBaseDoc):
}
"""

def __init__(self, ydoc: Optional[Doc] = None):
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
"""
Constructs a YBlob.

:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
brichet marked this conversation as resolved.
Show resolved Hide resolved
:type ydoc: :class:`pycrdt.Doc`, optional.
:param awareness: The :class:`pycrdt.Awareness` that share non persistent data
brichet marked this conversation as resolved.
Show resolved Hide resolved
between clients.
:type awareness: :class:`pycrdt.Awareness`, optional.
"""
super().__init__(ydoc)
super().__init__(ydoc, awareness)
self._ysource = self._ydoc.get("source", type=Map)
self.undo_manager.expand_scope(self._ysource)

Expand Down
9 changes: 6 additions & 3 deletions jupyter_ydoc/ynotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any, Callable, Dict, Optional
from uuid import uuid4

from pycrdt import Array, Doc, Map, Text
from pycrdt import Array, Awareness, Doc, Map, Text

from .utils import cast_all
from .ybasedoc import YBaseDoc
Expand Down Expand Up @@ -47,14 +47,17 @@ class YNotebook(YBaseDoc):
}
"""

def __init__(self, ydoc: Optional[Doc] = None):
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
"""
Constructs a YNotebook.

:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
brichet marked this conversation as resolved.
Show resolved Hide resolved
:type ydoc: :class:`pycrdt.Doc`, optional.
:param awareness: The :class:`pycrdt.Awareness` that share non persistent data
brichet marked this conversation as resolved.
Show resolved Hide resolved
between clients.
:type awareness: :class:`pycrdt.Awareness`, optional.
"""
super().__init__(ydoc)
super().__init__(ydoc, awareness)
self._ymeta = self._ydoc.get("meta", type=Map)
self._ycells = self._ydoc.get("cells", type=Array)
self.undo_manager.expand_scope(self._ycells)
Expand Down
9 changes: 6 additions & 3 deletions jupyter_ydoc/yunicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from typing import Any, Callable, Optional

from pycrdt import Doc, Text
from pycrdt import Awareness, Doc, Text

from .ybasedoc import YBaseDoc

Expand All @@ -23,14 +23,17 @@ class YUnicode(YBaseDoc):
}
"""

def __init__(self, ydoc: Optional[Doc] = None):
def __init__(self, ydoc: Optional[Doc] = None, awareness: Optional[Awareness] = None):
"""
Constructs a YUnicode.

:param ydoc: The :class:`pycrdt.Doc` that will hold the data of the document, if provided.
brichet marked this conversation as resolved.
Show resolved Hide resolved
:type ydoc: :class:`pycrdt.Doc`, optional.
:param awareness: The :class:`pycrdt.Awareness` that share non persistent data
brichet marked this conversation as resolved.
Show resolved Hide resolved
between clients.
:type awareness: :class:`pycrdt.Awareness`, optional.
"""
super().__init__(ydoc)
super().__init__(ydoc, awareness)
self._ysource = self._ydoc.get("source", type=Text)
self.undo_manager.expand_scope(self._ysource)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ requires-python = ">=3.8"
keywords = ["jupyter", "pycrdt", "yjs"]
dependencies = [
"importlib_metadata >=3.6; python_version<'3.10'",
"pycrdt >=0.9.0,<0.10.0",
"pycrdt >=0.9.16,<0.10.0",
brichet marked this conversation as resolved.
Show resolved Hide resolved
]

[[project.authors]]
Expand Down
13 changes: 13 additions & 0 deletions tests/test_ydocs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from pycrdt import Awareness
brichet marked this conversation as resolved.
Show resolved Hide resolved

from jupyter_ydoc import YBlob, YNotebook


Expand Down Expand Up @@ -53,3 +55,14 @@ def test_ynotebook_undo_manager():
ynotebook.undo_manager.undo()
assert len(ynotebook.ycells) == 0
assert not ynotebook.undo_manager.can_undo()


def test_awareness():
yblob = YBlob()

assert yblob.awareness is None

awareness = Awareness(yblob.ydoc)
yblob.awareness = awareness

assert yblob.awareness == awareness
brichet marked this conversation as resolved.
Show resolved Hide resolved
Loading