Spaces:
Sleeping
Sleeping
Add asset cache-busting hashes, Cache-Control headers, and squiggle-windowed long-file cleanup
7667cef verified Download web/src/scoring.js from brianbellx/codehygiene: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/src/scoring.js
- Command line
-
hf download hf://spaces/brianbellx/codehygiene/web/src/scoring.js
-
curl -L -o scoring.js https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/src/scoring.js
1.69 kB
| export const THRESHOLD = 0.55; | |
| const ASSET_QUERY = new URL(import.meta.url).search; | |
| const WORKER_URL = new URL(`./score-worker.js${ASSET_QUERY}`, import.meta.url); | |
| class ScoreWorker { | |
| constructor() { | |
| this.worker = new Worker(WORKER_URL, { type: "module" }); | |
| this.pending = new Map(); | |
| this.nextId = 1; | |
| this.worker.addEventListener("message", (event) => this.settle(event.data)); | |
| } | |
| settle({ id, result, error }) { | |
| const job = this.pending.get(id); | |
| this.pending.delete(id); | |
| if (error) { | |
| job.reject(new Error(error)); | |
| } else { | |
| job.resolve(result); | |
| } | |
| } | |
| score(code) { | |
| const id = this.nextId; | |
| this.nextId += 1; | |
| return new Promise((resolve, reject) => { | |
| this.pending.set(id, { resolve, reject }); | |
| this.worker.postMessage({ id, code, threshold: THRESHOLD }); | |
| }); | |
| } | |
| } | |
| export function createScorer() { | |
| const cores = navigator.hardwareConcurrency || 4; | |
| const workers = Array.from({ length: Math.min(4, Math.max(2, cores - 1)) }, () => new ScoreWorker()); | |
| let turn = 0; | |
| function scoreOne(code) { | |
| turn = (turn + 1) % workers.length; | |
| return workers[turn].score(code); | |
| } | |
| async function scoreAll(codes, onProgress = () => {}) { | |
| const results = new Array(codes.length); | |
| let cursor = 0; | |
| let completed = 0; | |
| async function drain(worker) { | |
| while (cursor < codes.length) { | |
| const index = cursor; | |
| cursor += 1; | |
| results[index] = await worker.score(codes[index]); | |
| completed += 1; | |
| onProgress(completed, codes.length); | |
| } | |
| } | |
| await Promise.all(workers.map(drain)); | |
| return results; | |
| } | |
| return { scoreOne, scoreAll }; | |
| } | |