Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removes cache layer from active sequence #320

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/posit/connect/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,7 @@ def _create_instance(self, path: str, /, **kwargs: Any) -> T:
"""Create an instance of 'T'."""
raise NotImplementedError()

def reload(self) -> Self:
"""Reloads the collection from Connect.

Returns
-------
Self
"""
self._cache = None
return self

def _fetch(self) -> List[T]:
def fetch(self) -> List[T]:
"""Fetch the collection.

Fetches the collection directly from Connect. This operation does not effect the cache state.
Expand All @@ -128,6 +118,16 @@ def _fetch(self) -> List[T]:
results = response.json()
return [self._to_instance(result) for result in results]

def reload(self) -> Self:
"""Reloads the collection from Connect.

Returns
-------
Self
"""
self._cache = None
return self

def _to_instance(self, result: dict) -> T:
"""Converts a result into an instance of T."""
uid = result[self._uid]
Expand All @@ -150,7 +150,7 @@ def _data(self) -> List[T]:
reload
"""
if self._cache is None:
self._cache = self._fetch()
self._cache = self.fetch()
return self._cache

@overload
Expand All @@ -162,6 +162,9 @@ def __getitem__(self, index: slice) -> Sequence[T]: ...
def __getitem__(self, index):
return self._data[index]

def __iter__(self):
return iter(self._data)

def __len__(self) -> int:
return len(self._data)

Expand Down Expand Up @@ -213,4 +216,5 @@ def find_by(self, **conditions: Any) -> Optional[T]:
Optional[T]
The first record matching the conditions, or `None` if no match is found.
"""
return next((v for v in self._data if v.items() >= conditions.items()), None)
collection = self.fetch()
return next((v for v in collection if v.items() >= conditions.items()), None)