from typing import Callable, Optional


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

    def subscribe(self, refresh_cb: Callable) -> None:
        self.subscriber = refresh_cb

    async def trigger_refresh(self) -> None:
        if self.subscriber is not None:
            await self.subscriber()
