Skip to content

Commit 6b0d34e

Browse files
committed
Support remote directory listing
1 parent 8f67a94 commit 6b0d34e

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

plugins/yjs/fps_yjs/ydocs/ydrive.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def _callback(self, events):
8383
for event in events:
8484
if isinstance(event, MapEvent):
8585
current = self._ycontent
86-
for path in event.path:
87-
current = current[path]
86+
for p in event.path:
87+
current = current[p]
8888
for key, val in event.keys.items():
8989
action = val.get("action")
9090
if action == "delete":
@@ -101,13 +101,17 @@ def _callback(self, events):
101101
self._task_group.start_soon(self._try_create_directory, path)
102102
else:
103103
self._task_group.start_soon(self._try_create_file, path)
104+
elif action == "update":
105+
if key == "populate" and val["oldValue"] == False and val["newValue"] == True:
106+
path = "/".join(event.path[1::2])
107+
self._task_group.start_soon(self.ls, path)
104108

105109
@property
106110
def version(self) -> str:
107111
return "1.0.0"
108112

109113
def _new_dir_content(self) -> Map:
110-
return Map({"is_dir": True, "content": None})
114+
return Map({"is_dir": True, "populate": False, "content": None})
111115

112116
def _new_file_content(self, content: Content | None = None) -> Map:
113117
if content is None:
@@ -153,7 +157,10 @@ async def _get_directory_content(self, path: Path) -> Map:
153157

154158
async def _maybe_populate_dir(self, path: Path, content: Map):
155159
if content["content"] is None:
156-
content["content"] = await self._get_directory_content(path)
160+
with content.doc.transaction():
161+
content["content"] = await self._get_directory_content(path)
162+
if not content["populate"]:
163+
content["populate"] = True
157164

158165
async def _get(self, path: Path | str | None = None) -> Map:
159166
path = Path() if path is None else Path(path)

plugins/yjs/tests/test_ydocs.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ async def test_ydrive():
5757
await ydrive.ls("doesnt_exist")
5858
assert str(exc_info.value) == "404: Item not found"
5959

60+
# remote ydrive.ls()
61+
assert not ydrive._ycontent["populate"]
62+
assert ydrive._ycontent["content"] is None
63+
ydrive._ycontent["populate"] = True
64+
await assert_with_timeout(lambda: ydrive._ycontent["content"] is not None)
65+
assert "file0" in ydrive._ycontent["content"]
66+
assert "file1" in ydrive._ycontent["content"]
67+
assert "dir0" in ydrive._ycontent["content"]
68+
assert "dir1" in ydrive._ycontent["content"]
69+
70+
assert not ydrive._ycontent["content"]["dir0"]["populate"]
71+
assert ydrive._ycontent["content"]["dir0"]["content"] is None
72+
ydrive._ycontent["content"]["dir0"]["populate"] = True
73+
await assert_with_timeout(
74+
lambda: ydrive._ycontent["content"]["dir0"]["content"] is not None
75+
)
76+
assert len(ydrive._ycontent["content"]["dir0"]["content"]) == 1
77+
assert "file2" in ydrive._ycontent["content"]["dir0"]["content"]
78+
6079
root_dir = await ydrive.ls()
6180
assert "file0" in root_dir
6281
assert "file1" in root_dir

0 commit comments

Comments
 (0)