Skip to content

Commit

Permalink
Add subscribe() to CacheStdout
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Jun 25, 2024
1 parent c6102df commit 89540d6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
7 changes: 4 additions & 3 deletions nextlinegraphql/plugins/ctrl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ def schema(self) -> tuple[type, type | None, type | None]:
@spec.hookimpl
async def update_lifespan_context(self, context: MutableMapping) -> None:
self._nextline = Nextline(statement)
self._stdout_cache = list[str]()
context['nextline'] = self._nextline

@spec.hookimpl(trylast=True) # trylast so to be the innermost context
@asynccontextmanager
async def lifespan(self) -> AsyncIterator[None]:
'''Yield within the nextline context.'''
self._nextline.register(CacheStdout(self._stdout_cache))
self._cache_stdout = CacheStdout(self._nextline)
self._nextline.register(self._cache_stdout)
async with self._nextline:
yield

@spec.hookimpl
def update_strawberry_context(self, context: MutableMapping) -> None:
context['nextline'] = self._nextline
context['stdout_cache'] = self._stdout_cache
ctrl = {'cache_stdout': self._cache_stdout}
context['ctrl'] = ctrl
14 changes: 12 additions & 2 deletions nextlinegraphql/plugins/ctrl/cache.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
from collections.abc import AsyncIterator

from nextline import Nextline
from nextline.events import OnWriteStdout
from nextline.plugin.spec import hookimpl


class CacheStdout:
def __init__(self, cache: list[str]) -> None:
self._cache = cache
def __init__(self, nextline: Nextline) -> None:
self._nextline = nextline
self._cache = list[str]()

async def subscribe(self) -> AsyncIterator[str]:
yield ''.join(self._cache)
async for i in self._nextline.subscribe_stdout():
assert i.text is not None
yield i.text

@hookimpl
async def on_initialize_run(self) -> None:
Expand Down
16 changes: 6 additions & 10 deletions nextlinegraphql/plugins/ctrl/schema/subscription.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import asyncio
from collections.abc import AsyncIterator
from typing import TYPE_CHECKING

import strawberry
from nextline import Nextline
from strawberry.types import Info

if TYPE_CHECKING:
from nextline import Nextline
from nextlinegraphql.plugins.ctrl.cache import CacheStdout


@strawberry.type
Expand Down Expand Up @@ -57,13 +56,10 @@ async def subscribe_prompting(
yield y


async def subscribe_stdout(info: Info) -> AsyncIterator[str]:
nextline: Nextline = info.context["nextline"]
stdout_cache: list[str] = info.context["stdout_cache"]
yield ''.join(stdout_cache)
async for i in nextline.subscribe_stdout():
assert i.text is not None
yield i.text
def subscribe_stdout(info: Info) -> AsyncIterator[str]:
cache_stdout = info.context['ctrl']['cache_stdout']
assert isinstance(cache_stdout, CacheStdout)
return cache_stdout.subscribe()


def subscribe_continuous_enabled(info: Info) -> AsyncIterator[bool]:
Expand Down
6 changes: 3 additions & 3 deletions tests/plugins/ctrl/schema/subscriptions/test_stdout.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

async def test_schema(schema: Schema) -> None:
nextline = Nextline(SOURCE, trace_modules=True, trace_threads=True)
cache = list[str]()
nextline.register(CacheStdout(cache))
cache_stdout = CacheStdout(nextline)
nextline.register(cache_stdout)
started = asyncio.Event()
context = {'nextline': nextline, 'stdout_cache': cache}
context = {'nextline': nextline, 'ctrl': {'cache_stdout': cache_stdout}}
async with nextline:
task = asyncio.create_task(nextline.run_continue_and_wait(started=started))
await started.wait()
Expand Down

0 comments on commit 89540d6

Please sign in to comment.