///|
/// The time in which a window can connect and show the project that a start asked
/// for: 1 minute. A start of the server and of the page needs less.
let wait_millis : Int64 = 60_000L

///|
/// The project that the next window must show, if there is one.
///
/// A start of the desktop app can ask for a project before the page of the window is
/// connected. The project then waits, and the first window that connects takes it.
#derive(Eq, Debug)
struct WaitingProject {
  asked : (@app.ProjectId, @app.UnixMillis)?
}

///|
/// No project waits.
pub fn WaitingProject::none() -> WaitingProject {
  { asked: None, }
}

///|
/// A project that waits from this time. Only the last project that a start asked for
/// can wait.
pub fn WaitingProject::WaitingProject(
  project : @app.ProjectId,
  now : @app.UnixMillis,
) -> WaitingProject {
  { asked: Some((project, now)), }
}

///|
/// The project for a window that connects at this time, and what waits after it. The
/// distance between the two times counts, as for a link, also when the clock went
/// back. Times are not negative, so it cannot overflow.
pub fn WaitingProject::take(
  self : WaitingProject,
  now : @app.UnixMillis,
) -> (@app.ProjectId?, WaitingProject) {
  let project = match self.asked {
    Some((project, asked)) if (now.to_int64() - asked.to_int64()).abs() <
      wait_millis => Some(project)
    _ => None
  }
  (project, WaitingProject::none())
}

///|
pub extend WaitingProject with Eq::{not_equal, equal}

///|
pub extend WaitingProject with @debug.Debug::{to_repr}