Rook
РусскийRUGet started
// A long job on another processor. It reports how far it got
// through a channel, the page stays free while it works, and
// Cancel stops it even in the middle of its loop

const LIMIT = 50_000_000
const STEP = LIMIT / 100

func isPrime(n number) bool {
    if n % 2 == 0 {
        return n == 2
    }
    for let d = 3; d * d <= n; d += 2 {
        if n % d == 0 {
            return false
        }
    }
    return n > 1
}

// What the goroutine reports: how many numbers it has checked and
// how many primes it has found among them
interface Progress {
    checked number
    found number
}

// Runs as a goroutine: counts the primes up to LIMIT and reports
// every 1%. A goroutine has no page, so it can loop as long as it
// needs to
async func countPrimes(updates chan<- Progress) {
    let found = 0
    for let n = 2; n <= LIMIT; n++ {
        if isPrime(n) {
            found++
        }
        if n % STEP == 0 {
            updates <- { checked: n, found }
        }
    }
    close(updates)
}

comp Primes() {
    state running = false
    state checked = 0
    state found = 0
    state seconds = 0
    state clicks = 0
    state report = "Count the primes up to 50 million"
    derived percent = Math.round(checked / LIMIT * 100)
    let stop = chan<bool>()

    async func run() {
        running = true
        checked = 0
        found = 0
        report = "Counting on another processor…"
        stop = chan<bool>()
        const updates = chan<Progress>()
        const job = go countPrimes(updates)
        const start = performance.now()
        for {
            select {
            case p = <-updates:
                seconds = (performance.now() - start) / 1000
                if p == null {              // closed: the goroutine is done
                    report = `Done: ${found.toLocaleString()} primes`
                    running = false
                    return
                }
                checked = p.checked
                found = p.found
            case <-stop:
                cancel(job)                 // stops its worker mid-loop
                report = `Cancelled at ${percent}%`
                running = false
                return
            }
        }
    }

    style {
        .job { display: grid; gap: 16px; max-width: 460px; }
        .stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
        .stat {
            display: grid;
            gap: 2px;
            padding: 10px 12px;
            border-radius: 10px;
            background: #ffffff0d;
        }
        .stat b { font-size: 1.3em; font-variant-numeric: tabular-nums; }
        .stat span { color: #ffffff80; font-size: 0.8em; }
        .track {
            height: 8px;
            border-radius: 999px;
            background: #ffffff14;
            overflow: hidden;
        }
        .fill {
            width: {percent}%;
            height: 100%;
            background: #f5a524;
            transition: width 0.2s;
        }
        .buttons { display: flex; gap: 8px; }
        button {
            padding: 8px 16px;
            border: 1px solid #ffffff2e;
            border-radius: 8px;
            background: #ffffff0a;
            color: inherit;
            font: inherit;
            cursor: pointer;
        }
        button:disabled { opacity: 0.4; cursor: default; }
        .start { border-color: #f5a524; background: #f5a524; color: #000; }
        .report { margin: 0; color: #ffffffb3; }
        .free {
            justify-self: start;
            border-style: dashed;
            color: #ffffffb3;
        }
    }

    return <div class="job">
        <div class="stats">
            <div class="stat">
                <b>{found.toLocaleString()}</b>
                <span>primes found</span>
            </div>
            <div class="stat">
                <b>{percent}%</b>
                <span>checked</span>
            </div>
            <div class="stat">
                <b>{seconds.toFixed(1)} s</b>
                <span>on the clock</span>
            </div>
        </div>
        <div class="track"><div class="fill" /></div>
        <div class="buttons">
            <button class="start" disabled={running} onClick={run()}>Start</button>
            <button disabled={!running} onClick={close(stop)}>Cancel</button>
        </div>
        <p class="report">{report}</p>
        <button class="free" onClick={clicks++}>
            The page is free, click me: {clicks}
        </button>
    </div>
}

document.body.append(<Primes />)
0primes found
0%checked
0.0 son the clock

Count the primes up to 50 million

The program printed nothing to the console.