2021-06-04 07:01:53 +02:00
|
|
|
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') {
|
2021-06-04 10:59:50 +02:00
|
|
|
let numbers;
|
|
|
|
if (evt.data.numbers === 'rational') {
|
|
|
|
numbers = 'Rational';
|
|
|
|
} else if (evt.data.numbers === 'float64') {
|
|
|
|
numbers = 'NativeFloat64';
|
|
|
|
}
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
// Init election
|
2021-06-04 10:59:50 +02:00
|
|
|
let election = wasm['election_from_blt_' + numbers](evt.data.electionData);
|
2021-06-04 07:01:53 +02:00
|
|
|
|
|
|
|
// Init results table
|
2021-06-04 10:59:50 +02:00
|
|
|
postMessage({'type': 'initResultsTable', 'content': wasm['init_results_table_' + numbers](election)});
|
2021-06-04 07:01:53 +02:00
|
|
|
|
|
|
|
// Init STV options
|
|
|
|
let opts = wasm.STVOptions.new.apply(null, evt.data.optsStr);
|
|
|
|
|
|
|
|
// Describe count
|
2021-06-04 10:59:50 +02:00
|
|
|
postMessage({'type': 'describeCount', 'content': wasm['describe_count_' + numbers](evt.data.filePath, election, opts)});
|
2021-06-04 07:01:53 +02:00
|
|
|
|
|
|
|
// Step election
|
2021-06-04 10:59:50 +02:00
|
|
|
let state = wasm['CountState' + numbers].new(election);
|
|
|
|
wasm['count_init_' + numbers](state, opts);
|
2021-06-04 07:01:53 +02:00
|
|
|
|
2021-06-04 10:59:50 +02:00
|
|
|
postMessage({'type': 'updateResultsTable', 'result': wasm['update_results_table_' + numbers](1, state, opts)});
|
|
|
|
postMessage({'type': 'updateStageComments', 'comment': wasm['update_stage_comments_' + numbers](state)});
|
2021-06-04 07:01:53 +02:00
|
|
|
|
|
|
|
for (let stageNum = 2;; stageNum++) {
|
2021-06-04 10:59:50 +02:00
|
|
|
let isDone = wasm['count_one_stage_' + numbers](state, opts);
|
2021-06-04 07:01:53 +02:00
|
|
|
if (isDone) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-04 10:59:50 +02:00
|
|
|
postMessage({'type': 'updateResultsTable', 'result': wasm['update_results_table_' + numbers](stageNum, state, opts)});
|
|
|
|
postMessage({'type': 'updateStageComments', 'comment': wasm['update_stage_comments_' + numbers](state)});
|
2021-06-04 07:01:53 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 10:59:50 +02:00
|
|
|
postMessage({'type': 'updateResultsTable', 'result': wasm['finalise_results_table_' + numbers](state)});
|
|
|
|
postMessage({'type': 'finalResultSummary', 'summary': wasm['final_result_summary_' + numbers](state)});
|
2021-06-04 07:01:53 +02:00
|
|
|
}
|
|
|
|
}
|