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

Rewrite CacheStdout with the cache option of PubSubItem #87

Merged
merged 1 commit into from
Jul 1, 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
13 changes: 3 additions & 10 deletions nextlinegraphql/plugins/ctrl/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@

class CacheStdout:
def __init__(self) -> None:
self._cache = list[str]()
self._pubsub = PubSubItem[str]()
self._pubsub = PubSubItem[str](cache=True)

async def subscribe(self) -> AsyncIterator[str]:
# TODO: Make sure no missing or duplicated items are yielded. If the
# `last` option is `True`, duplicate items can be yielded and shown on
# the web client. However, the tests still pass.
# NOTE: The cache can be implemented in `PubSubItem` itself.
yield ''.join(self._cache)
async for text in self._pubsub.subscribe(last=False):
async for text in self._pubsub.subscribe():
yield text

async def aclose(self) -> None:
Expand All @@ -31,9 +25,8 @@ async def __aexit__(self, *_: Any, **__: Any) -> None:

@hookimpl
async def on_initialize_run(self) -> None:
self._cache.clear()
self._pubsub.clear()

@hookimpl
async def on_write_stdout(self, event: OnWriteStdout) -> None:
self._cache.append(event.text)
await self._pubsub.publish(event.text)
Loading