from typing import Callable


class RefreshService:
    """
    rio.Components can subscribe to this service with their on_populate method.
    Those methods get called whenever a overall refresh is needed. Usually when the user logs in or out.
    """
    def __init__(self) -> None:
        self.subscribers: set[Callable] = set()

    def subscribe(self, refresh_cb: Callable) -> None:
        self.subscribers.add(refresh_cb)

    async def trigger_refresh(self) -> None:
        for refresh_cb in self.subscribers:
            await refresh_cb()
