This commit is contained in:
Inex Code 2022-08-02 23:08:32 +03:00
parent 8ea0d89d71
commit 8f940e64fd

View file

@ -23,14 +23,13 @@ class ApiJob:
@strawberry.type @strawberry.type
class JobSubscription: class JobSubscription:
@strawberry.subscription(permission_classes=[IsAuthenticated]) @strawberry.subscription()
async def job_subscription(self) -> AsyncGenerator[typing.List[ApiJob], None]: async def job_subscription(self) -> AsyncGenerator[typing.List[ApiJob], None]:
is_updated = True is_updated = True
def callback(jobs: typing.List[Job]): def callback(jobs: typing.List[Job]):
nonlocal is_updated nonlocal is_updated
is_updated = True is_updated = True
Jobs().add_observer(callback) Jobs().add_observer(callback)
try:
while True: while True:
if is_updated: if is_updated:
is_updated = False is_updated = False
@ -46,6 +45,3 @@ class JobSubscription:
error=job.error, error=job.error,
result=job.result, result=job.result,
) for job in Jobs().get_jobs() ] ) for job in Jobs().get_jobs() ]
except GeneratorExit:
Jobs().remove_observer(callback)
return