Spaces:
Running

File size: 2,522 Bytes
9a4ea8c
 
1e8420e
 
 
9a4ea8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e8420e
 
 
 
 
 
9a4ea8c
 
1e8420e
 
9a4ea8c
 
 
 
 
 
 
 
 
 
 
 
 
e3a78d8
9a4ea8c
 
 
 
 
 
1e8420e
 
9a4ea8c
 
 
 
 
 
1e8420e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/** Carry one selected gallery reference into the same-origin NGSpice controls. */
export function createSimulator(report, onSelection = () => {}) {
  const frame = document.querySelector("#simulator");
  const status = document.querySelector("#simulator-status");
  let ready = false,
    starting = null,
    pending = null,
    sentKey = null,
    awaitingKey = null;
  const key = (value) =>
    JSON.stringify([value.topology, value.ratio, value.vout, value.iout]);
  function deliver() {
    if (!ready || !pending || key(pending) === sentKey) return;
    sentKey = key(pending);
    awaitingKey = sentKey;
    frame.contentWindow.postMessage(
      { type: "pe:select-topology", ...pending },
      location.origin,
    );
  }
  window.addEventListener("message", (event) => {
    if (
      event.origin !== location.origin ||
      event.source !== frame.contentWindow
    )
      return;
    const data = event.data;
    if (data?.type === "pe:simulator-ready") {
      ready = true;
      status.textContent = "";
      deliver();
    }
    if (data?.type === "pe:simulator-busy")
      status.textContent =
        "Finishing the current run; your selected topology is next.";
    if (data?.type === "pe:reference-applied" && key(data) === awaitingKey) {
      awaitingKey = null;
      status.textContent = "";
    }
    if (data?.type === "pe:simulation-point") {
      if (awaitingKey !== null) return;
      const reference = {
        topology: data.topology,
        ratio: data.topology === "sp2" ? 2 : 4,
        vout: data.vout,
        iout: data.iout,
      };
      pending = reference;
      sentKey = key(reference);
      onSelection(data.topology, reference);
    }
  });
  async function show(reference = null) {
    if (reference) pending = reference;
    if (ready) {
      deliver();
      return;
    }
    if (starting) return starting;
    status.textContent = "Connecting to NGSpice…";
    starting = (async () => {
      try {
        const response = await fetch("./healthz", {
          cache: "no-store",
          signal: AbortSignal.timeout(10000),
        });
        if (!response.ok || !(await response.json()).simulator)
          throw Error("The simulator service is unavailable. Retry shortly.");
        if (!frame.getAttribute("src")) frame.src = "./simulator/";
      } catch (error) {
        status.textContent = error.message;
        report(error.message);
      } finally {
        starting = null;
      }
    })();
    return starting;
  }
  return { show };
}