import time
from typing import Callable, Optional


def wait_for_done(
    check_function: Callable,
    timeout: Optional[float],
    progress_callback: Optional[Callable] = None,
    sleep_time: float = 1,
) -> None:
    """Wait up to TIMEOUT seconds for the check function to return True"""
    end_time = time.time() + timeout if timeout else float("inf")
    while check_function() is False and time.time() < end_time:
        if progress_callback is not None:
            progress_callback()
        # Wait a max of sleep_time, but keep checking to see if
        # check_function is done. Allows wait_for_empty to end
        # before sleep_time has elapsed:
        end_sleep_time = time.time() + sleep_time
        while check_function() is False and time.time() < end_sleep_time:
            time.sleep(sleep_time / 20.0)
