Skip to content

Commit 3a8ba7d

Browse files
committed
Add copy argument
1 parent 5f1ff45 commit 3a8ba7d

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

jupyter_collaboration/app.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ async def get_document(
128128
path: str,
129129
content_type: Literal["notebook", "file"],
130130
file_format: Literal["json", "text"],
131+
copy: bool = True,
131132
) -> YBaseDoc | None:
132133
"""Get a read-only view of the shared model for the matching document.
133134
@@ -146,7 +147,15 @@ async def get_document(
146147
return None
147148

148149
if isinstance(room, DocumentRoom):
149-
return room._document
150+
if copy:
151+
update = room.ydoc.get_update()
152+
153+
fork_ydoc = Doc()
154+
fork_ydoc.apply_update(update)
155+
156+
return YDOCS.get(content_type, YDOCS["file"])(fork_ydoc)
157+
else:
158+
return room._document
150159

151160
return None
152161

tests/test_app.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from __future__ import annotations
55

6+
import pytest
7+
68
from jupyter_collaboration.stores import SQLiteYStore, TempFileYStore
79

810

@@ -61,9 +63,28 @@ def test_settings_should_change_ystore_class(jp_configurable_serverapp):
6163
assert settings["ystore_class"] == TempFileYStore
6264

6365

64-
async def test_get_document_file(rtc_create_file, jp_serverapp):
66+
@pytest.mark.parametrize("copy", [True, False])
67+
async def test_get_document_file(rtc_create_file, jp_serverapp, copy):
6568
path, content = await rtc_create_file("test.txt", "test", store=True)
6669
collaboration = jp_serverapp.web_app.settings["jupyter_collaboration"]
67-
document = await collaboration.get_document(path=path, content_type="file", file_format="text")
70+
document = await collaboration.get_document(
71+
path=path, content_type="file", file_format="text", copy=copy
72+
)
6873
assert document.get() == content == "test"
6974
await collaboration.stop_extension()
75+
76+
77+
async def test_get_document_file_copy_is_independent(
78+
rtc_create_file, jp_serverapp, rtc_fetch_session
79+
):
80+
path, content = await rtc_create_file("test.txt", "test", store=True)
81+
collaboration = jp_serverapp.web_app.settings["jupyter_collaboration"]
82+
document = await collaboration.get_document(
83+
path=path, content_type="file", file_format="text", copy=True
84+
)
85+
document.set("other")
86+
fresh_copy = await collaboration.get_document(
87+
path=path, content_type="file", file_format="text"
88+
)
89+
assert fresh_copy.get() == "test"
90+
await collaboration.stop_extension()

0 commit comments

Comments
 (0)