File size: 965 Bytes
4c3aac8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<button id="run">run</button>
<pre id="log"></pre>
<script type="module">
    import {Client} from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";

    const log = document.getElementById("log");
    const out = (...a) => {log.textContent += a.join(" ") + "\n";};

    document.getElementById("run").onclick = async () => {
        const client = await Client.connect(location.origin, {events: ["data"]});
        const job = client.submit("/stream", {message: "test"});

        const TIMEOUT = 4000;
        const timer = setTimeout(() => {
            out(`!! TIMEOUT after ${TIMEOUT}ms — iterator did not close`);
            job.return?.();
        }, TIMEOUT);

        let n = 0;
        for await (const msg of job) {
            n++;
            out(`#${n}`, JSON.stringify({type: msg.type, stage: msg.stage}));
        }
        clearTimeout(timer);
        out(`exited naturally after ${n} messages`);
    };
</script>