Skip to content

Commit

Permalink
🐛 Don't create coroutines before they're needed when loading items
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
davep committed Mar 9, 2024
1 parent 6078b2d commit 3aa218d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- Once the first viewed tab has loaded, other tabs will start to load in the
background (one after the other) as the user reads the first.
- Added a config option to turn off the above.
- Fixed a non-awaited-coroutine warning that could happen when quitting
while loading items. ([#26](https://github.com/davep/oshit/issues/26))

## 0.10.0

Expand Down
16 changes: 11 additions & 5 deletions oshit/hn/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,19 @@ async def _items_from_ids(
"""
concurrency_limit = Semaphore(self._max_concurrency)

async def limited(coroutine: Awaitable[ItemType]) -> ItemType:
async def item(item_id: int) -> ItemType:
"""Get an item, with a limit on concurrent requests.
Args:
item_id: The ID of the item to get.
Returns:
The item.
"""
async with concurrency_limit:
return await coroutine
return await self.item(item_type, item_id)

return await gather(
*[limited(self.item(item_type, item_id)) for item_id in item_ids]
)
return await gather(*[item(item_id) for item_id in item_ids])

async def _id_list(self, list_type: str, max_count: int | None = None) -> list[int]:
"""Get a given ID list.
Expand Down

0 comments on commit 3aa218d

Please sign in to comment.