Skip to content

Commit 6cd09ef

Browse files
committed
Add copy argument
1 parent 651084b commit 6cd09ef

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
@@ -140,6 +140,7 @@ async def get_document(
140140
path: str,
141141
content_type: Literal["notebook", "file"],
142142
file_format: Literal["json", "text"],
143+
copy: bool = True,
143144
) -> YBaseDoc | None:
144145
"""Get a read-only view of the shared model for the matching document.
145146
@@ -158,7 +159,15 @@ async def get_document(
158159
return None
159160

160161
if isinstance(room, DocumentRoom):
161-
return room._document
162+
if copy:
163+
update = room.ydoc.get_update()
164+
165+
fork_ydoc = Doc()
166+
fork_ydoc.apply_update(update)
167+
168+
return YDOCS.get(content_type, YDOCS["file"])(fork_ydoc)
169+
else:
170+
return room._document
162171

163172
return None
164173

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)