Skip to content

Commit

Permalink
Experimenting with placing _load_directory in a worker
Browse files Browse the repository at this point in the history
This isn't the final form, not even close, this is more to help test out the
idea and how well it will work. Note the very deliberate sleep in the code
that's there to emulate loading from a slow blocking source.

This will be removed and tidied up before a final PR, of course. The main
aim here is to emulate a worst-case scenario so that the use of a worker can
be tried out with some confidence.

See Textualize#2456.
  • Loading branch information
davep committed May 10, 2023
1 parent 30a20ac commit d673175
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/textual/widgets/_directory_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rich.style import Style
from rich.text import Text, TextType

from .. import work
from ..events import Mount
from ..message import Message
from ..reactive import var
Expand Down Expand Up @@ -238,10 +239,18 @@ def _directory_content(self, directory: Path) -> Iterator[Path]:
Returns:
An iterator of `Path` objects.
"""
# TODO: Place this in a loop with a sleep to slow things down for
# testing.
return directory.iterdir()

# TODO: Not like this. Oh so very not like this. This is here to
# slow things down on purpose, to emulate loading directory
# information from a slow source.
#
# REMOVE BEFORE FLIGHT!
import time

for entry in directory.iterdir():
yield entry
time.sleep(0.05)

@work
def _load_directory(self, node: TreeNode[DirEntry]) -> None:
"""Load the directory contents for a given node.
Expand All @@ -255,12 +264,13 @@ def _load_directory(self, node: TreeNode[DirEntry]) -> None:
key=lambda path: (not path.is_dir(), path.name.lower()),
)
for path in directory:
node.add(
self.app.call_from_thread(
node.add,
path.name,
data=DirEntry(path),
allow_expand=path.is_dir(),
)
node.expand()
self.app.call_from_thread(node.expand)

def _on_mount(self, _: Mount) -> None:
self._load_directory(self.root)
Expand Down

0 comments on commit d673175

Please sign in to comment.