OpenTally/html/worker.js

46 lines
1.6 KiB
JavaScript

importScripts('opentally.js');
var wasm = wasm_bindgen;
async function initWasm() {
await wasm_bindgen('opentally_bg.wasm');
postMessage({'type': 'init', 'version': wasm.version()});
}
initWasm();
onmessage = function(evt) {
if (evt.data.type === 'countElection') {
// Init election
let election = wasm.election_from_blt_Rational(evt.data.electionData);
// Init results table
postMessage({'type': 'initResultsTable', 'content': wasm.init_results_table_Rational(election)});
// Init STV options
let opts = wasm.STVOptions.new.apply(null, evt.data.optsStr);
// Describe count
postMessage({'type': 'describeCount', 'content': wasm.describe_count_Rational(evt.data.filePath, election, opts)});
// Step election
let state = wasm.CountStateRational.new(election);
wasm.count_init_Rational(state, opts);
postMessage({'type': 'updateResultsTable', 'result': wasm.update_results_table_Rational(1, state, opts)});
postMessage({'type': 'updateStageComments', 'comment': wasm.update_stage_comments_Rational(state)});
for (let stageNum = 2;; stageNum++) {
let isDone = wasm.count_one_stage_Rational(state, opts);
if (isDone) {
break;
}
postMessage({'type': 'updateResultsTable', 'result': wasm.update_results_table_Rational(stageNum, state, opts)});
postMessage({'type': 'updateStageComments', 'comment': wasm.update_stage_comments_Rational(state)});
}
postMessage({'type': 'updateResultsTable', 'result': wasm.finalise_results_table_Rational(state)});
postMessage({'type': 'finalResultSummary', 'summary': wasm.final_result_summary_Rational(state)});
}
}