From 8763d20885faf23c34413758c80a40657d01354a Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sat, 27 Jul 2024 20:38:55 -0300 Subject: Fix: change loading logic --- packages/finder/main.py | 23 +++++++++++++-------- packages/finder/plugin/searcher/file/name.py | 31 ++++++++++++++++------------ 2 files changed, 32 insertions(+), 22 deletions(-) (limited to 'packages') diff --git a/packages/finder/main.py b/packages/finder/main.py index 38c6c7f..41bca79 100644 --- a/packages/finder/main.py +++ b/packages/finder/main.py @@ -44,6 +44,9 @@ except ImportError: print("Please install prompt_toolkit first!") raise ImportError +from .plugin.searcher.file.name import FinderPluginFileNameSearcher +from .plugin.opener.file.xdg import FinderPluginFileXdgOpener + class FinderMain(): def accept(self, buff): # Default behavior when pressing Enter in the search field: @@ -63,7 +66,7 @@ class FinderMain(): await asyncio.sleep(0.01) # Update the list of items - self.update_list_buffer(await self.searcher.finder(use_cache=False)) + self.update_list_buffer(await self.searcher.finder(cache=False)) # Update the current selection self.change('') @@ -196,16 +199,11 @@ class FinderMain(): ] ) - async def load(self): - from .plugin.searcher.file.name import FinderPluginFileNameSearcher - from .plugin.opener.file.xdg import FinderPluginFileXdgOpener + async def load(self, cache=False): + self.update_list_buffer(await self.searcher.finder(cache=cache)) - self.searcher = FinderPluginFileNameSearcher(self.path) - self.opener = FinderPluginFileXdgOpener() self.state['loading'] = False - self.update_list_buffer(await self.searcher.finder()) - def run(self): return asyncio.run(self.main()) @@ -237,12 +235,19 @@ class FinderMain(): #editing_mode = EditingMode.VI, ) + # Searcher and opener objects + self.searcher = FinderPluginFileNameSearcher(self.path) + self.opener = FinderPluginFileXdgOpener() + async with asyncio.TaskGroup() as tg: # Run the application app = tg.create_task(self.app.run_async()) + # Load initial buffer content from cache, if available + cache = tg.create_task(self.load(cache=True)) + # Load initial buffer content - loader = tg.create_task(self.load()) + load = tg.create_task(self.load(cache=False)) return True diff --git a/packages/finder/plugin/searcher/file/name.py b/packages/finder/plugin/searcher/file/name.py index f01688f..6ea97b7 100644 --- a/packages/finder/plugin/searcher/file/name.py +++ b/packages/finder/plugin/searcher/file/name.py @@ -46,24 +46,29 @@ class FinderPluginFileNameSearcher(): self.path = path self.cache = os.path.join(cache_dir, 'plugin', 'searcher', 'file', os.path.abspath(self.path).replace(os.sep, '_') + '.json') - async def finder(self, use_cache = True): + async def finder(self, cache = False): # Check existing cache - if use_cache and os.path.exists(self.cache) and os.path.isfile(self.cache): - with open(self.cache, 'r') as f: - self.items = json.load(f) + if cache: + if os.path.exists(self.cache) and os.path.isfile(self.cache): + with open(self.cache, 'r') as f: + # Update the folder list in the background + walker = asyncio.create_task(self.walker()) - # Then update the folder list in the background - walker = asyncio.create_task(self.walker()) + # Meanwhile, return the items from cache + self.items = json.load(f) - return self.items + return self.items - # Update the folder list - await self.walker() + return [] - # Update the cache - os.makedirs(os.path.dirname(self.cache), exist_ok=True) - with open(self.cache, 'w') as f: - json.dump(self.items, f, sort_keys=True) + else: + # Update the folder list + await self.walker() + + # Update the cache + os.makedirs(os.path.dirname(self.cache), exist_ok=True) + with open(self.cache, 'w') as f: + json.dump(self.items, f, sort_keys=True) return self.items -- cgit v1.2.3