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();
|
|
|
|
|
2021-06-11 18:09:26 +02:00
|
|
|
var numbers, election, opts, state, stageNum;
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
onmessage = function(evt) {
|
|
|
|
if (evt.data.type === 'countElection') {
|
2021-06-04 14:05:48 +02:00
|
|
|
if (evt.data.numbers === 'fixed') {
|
|
|
|
numbers = 'Fixed';
|
|
|
|
wasm.fixed_set_dps(evt.data.decimals);
|
2021-06-14 13:43:43 +02:00
|
|
|
} else if (evt.data.numbers === 'gfixed') {
|
|
|
|
numbers = 'GuardedFixed';
|
|
|
|
wasm.gfixed_set_dps(evt.data.decimals);
|
2021-06-04 10:59:50 +02:00
|
|
|
} else if (evt.data.numbers === 'float64') {
|
|
|
|
numbers = 'NativeFloat64';
|
2021-06-04 14:05:48 +02:00
|
|
|
} else if (evt.data.numbers === 'rational') {
|
|
|
|
numbers = 'Rational';
|
|
|
|
} else {
|
|
|
|
throw 'Unknown --numbers';
|
2021-06-04 10:59:50 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
// Init election
|
2021-06-11 18:09:26 +02:00
|
|
|
election = wasm['election_from_blt_' + numbers](evt.data.electionData);
|
2021-06-04 07:01:53 +02:00
|
|
|
|
2021-06-11 13:23:08 +02:00
|
|
|
if (evt.data.normaliseBallots) {
|
|
|
|
wasm['election_normalise_ballots_' + numbers](election);
|
|
|
|
}
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
// Init STV options
|
2021-06-11 18:09:26 +02:00
|
|
|
opts = wasm.STVOptions.new.apply(null, evt.data.optsStr);
|
2021-06-04 07:01:53 +02:00
|
|
|
|
2021-06-22 06:34:26 +02:00
|
|
|
// Validate options
|
|
|
|
opts.validate();
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
// 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
|
|
|
|
2021-06-07 12:52:18 +02:00
|
|
|
// Init results table
|
|
|
|
postMessage({'type': 'initResultsTable', 'content': wasm['init_results_table_' + numbers](election, opts)});
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
// Step election
|
2021-06-11 18:09:26 +02:00
|
|
|
state = wasm['CountState' + numbers].new(election);
|
2021-06-04 10:59:50 +02:00
|
|
|
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
|
|
|
|
2021-06-11 18:09:26 +02:00
|
|
|
stageNum = 2;
|
|
|
|
|
|
|
|
resume_count();
|
|
|
|
|
|
|
|
} else if (evt.data.type == 'userInput') {
|
|
|
|
user_input_buffer = evt.data.response;
|
|
|
|
resume_count();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resume_count() {
|
|
|
|
for (;; stageNum++) {
|
|
|
|
try {
|
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-11 18:09:26 +02:00
|
|
|
} catch (ex) {
|
|
|
|
if (ex === "RequireInput") {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw ex;
|
|
|
|
}
|
2021-06-04 07:01:53 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 18:09:26 +02:00
|
|
|
postMessage({'type': 'updateResultsTable', 'result': wasm['update_results_table_' + numbers](stageNum, state, opts)});
|
|
|
|
postMessage({'type': 'updateStageComments', 'comment': wasm['update_stage_comments_' + numbers](state)});
|
|
|
|
}
|
|
|
|
|
|
|
|
postMessage({'type': 'updateResultsTable', 'result': wasm['finalise_results_table_' + numbers](state)});
|
2021-06-16 05:00:54 +02:00
|
|
|
postMessage({'type': 'finalResultSummary', 'summary': wasm['final_result_summary_' + numbers](state, opts)});
|
2021-06-11 18:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var user_input_buffer = null;
|
|
|
|
|
|
|
|
function read_user_input_buffer(message) {
|
|
|
|
if (user_input_buffer === null) {
|
|
|
|
postMessage({'type': 'requireInput', 'message': message});
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
let user_input = user_input_buffer;
|
|
|
|
user_input_buffer = null;
|
|
|
|
return user_input;
|
2021-06-04 07:01:53 +02:00
|
|
|
}
|
|
|
|
}
|