import queue
from typing import Any, Generator

from ldnode.calculator.schemas import ResultCollector


class Collector(ResultCollector):
    """ 结果收集器 """
    def __init__(self):
        self.queue = queue.Queue()
        self.is_closed = False

    def collect(self, result: Any) -> None:
        self.queue.put(result)

    def close(self) -> None:
        self.is_closed = True
    
    def read(self) -> Generator[Any]:
        while not self.is_closed:
            try:
                yield self.queue.get(timeout=0.1)
            except queue.Empty:
                continue
