public function waitUntil(int $timeoutSeconds, callable $callback, bool $throwOnFailure = true)
    {
        if (!is_callable($callback)) {
            throw new \InvalidArgumentException('Given callback is not a valid callable');
        }

        $start = time();
        $end = $start + $timeoutSeconds;

        $lastInternalExceptionMessage = '';

        do {
            try {
                $result = $callback($this);

                if ($result) {
                    return $result;
                }
            } catch (Exception $e) {
                $lastInternalExceptionMessage = $e->getMessage();
            }
            usleep(250 * 1000);
        } while (time() < $end);

        if ($throwOnFailure) {
            throw new Exception('Spin function did not return in time. Internal exception:' . $lastInternalExceptionMessage);
        }
    }