Skip to content

Commit

Permalink
Merge pull request #87 from simonsobs/dev
Browse files Browse the repository at this point in the history
Rewrite `CacheStdout` with the `cache` option of `PubSubItem`
  • Loading branch information
TaiSakuma authored Jul 1, 2024
2 parents 4828c93 + 3d51808 commit 537ec22
Showing 1 changed file with 3 additions and 10 deletions.
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)

0 comments on commit 537ec22

Please sign in to comment.