Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

797 B
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

/**
 * Wait for a websocket to be available, with linear backoff.
 */
export async function waitForWs(url: string, tries = 5) {
  for (let i = 0; i < tries; i++) {
    try {
      return await tryConnection(url);
    } catch {
      // wait a second * i
      // linear backoff
      await new Promise((resolve) => setTimeout(resolve, 1000 * (i + 1)));
    }
  }
  throw new Error(`Failed to connect to ${url}`);
}

async function tryConnection(url: string) {
  return new Promise<string>((resolve, reject) => {
    const ws = new WebSocket(url);
    ws.onopen = () => {
      ws.close();
      resolve(url);
    };
    ws.onerror = (evt) => {
      ws.close();
      reject(new Error(`Failed to connect to ${url}: ${evt.type}`));
    };
  });
}