content: """
  Advanced modes of sleeping.
  """
  import asyncio
  import collections
  from typing import Optional, Collection, Union
  
  from kopf.structs import primitives
  
  
  async def sleep_or_wait(
          delays: Union[None, float, Collection[Union[None, float]]],
          wakeup: Optional[Union[asyncio.Event, primitives.DaemonStopper]] = None,
  ) -> Optional[float]:
      """
      Measure the sleep time: either until the timeout, or until the event is set.
  
      Returns the number of seconds left to sleep, or ``None`` if the sleep was
      not interrupted and reached its specified delay (an equivalent of ``0``).
      In theory, the result can be ``0`` if the sleep was interrupted precisely
      the last moment before timing out; this is unlikely to happen though.
      """
      passed_delays = delays if isinstance(delays, collections.abc.Collection) else [delays]
      actual_delays = [delay for delay in passed_delays if delay is not None]
      minimal_delay = min(actual_delays) if actual_delays else 0
  
      awakening_event = (
          wakeup.async_event if isinstance(wakeup, primitives.DaemonStopper) else
          wakeup if wakeup is not None else
          asyncio.Event())
  
      loop = asyncio.get_running_loop()
      try:
          start_time = loop.time()
          await asyncio.wait_for(awakening_event.wait(), timeout=minimal_delay)
      except asyncio.TimeoutError:
          return None  # interruptable sleep is over: uninterrupted.
      else:
          end_time = loop.time()
          duration = end_time - start_time
          return max(0, minimal_delay - duration)
  