diff --git a/html/index.html b/html/index.html index f3b6f40..a79a9ae 100644 --- a/html/index.html +++ b/html/index.html @@ -24,226 +24,228 @@
-Stage comments:
'; + olStageComments = document.createElement('ol'); + divLogs2.append(olStageComments); + + } else if (evt.data.type === 'describeCount') { + document.getElementById('resultLogs1').innerHTML = evt.data.content; + + } else if (evt.data.type === 'updateResultsTable') { + for (let i = 0; i < evt.data.result.length; i++) { + tblResult.rows[i].insertAdjacentHTML('beforeend', evt.data.result[i]); + } + + } else if (evt.data.type === 'updateStageComments') { + let elLi = document.createElement('li'); + elLi.innerHTML = evt.data.comment; + olStageComments.append(elLi); + + } else if (evt.data.type === 'finalResultSummary') { + divLogs2.insertAdjacentHTML('beforeend', evt.data.summary); + } } async function clickCount() { @@ -49,24 +67,14 @@ async function clickCount() { } // Read BLT file + let filePath = document.getElementById('bltFile').value; + filePath = filePath.substring(Math.max(filePath.lastIndexOf('\\'), filePath.lastIndexOf('/')) + 1); + let bltFile = document.getElementById('bltFile').files[0]; let electionData = await bltFile.text(); - // Load WASM - await wasm_bindgen('opentally_bg.wasm'); - - // Init election - let election = wasm.election_from_blt_Rational(electionData); - - // Init results table - tblResult.innerHTML = wasm.init_results_table_Rational(election); - - divLogs2.innerHTML = 'Stage comments:
'; - olStageComments = document.createElement('ol'); - divLogs2.append(olStageComments); - // Init STV options - let opts = wasm.STVOptions.new( + let optsStr = [ document.getElementById('chkRoundTVs').checked ? parseInt(document.getElementById('txtRoundTVs').value) : null, document.getElementById('chkRoundWeights').checked ? parseInt(document.getElementById('txtRoundWeights').value) : null, document.getElementById('chkRoundVotes').checked ? parseInt(document.getElementById('txtRoundVotes').value) : null, @@ -78,29 +86,13 @@ async function clickCount() { document.getElementById('selPapers').value == 'transferable', document.getElementById('selExclusion').value, parseInt(document.getElementById('txtPPDP').value), - ); + ]; - // Describe count - let filePath = document.getElementById('bltFile').value; - filePath = filePath.substring(Math.max(filePath.lastIndexOf('\\'), filePath.lastIndexOf('/')) + 1); - document.getElementById('resultLogs1').innerHTML = wasm.describe_count_Rational(filePath, election, opts); - - // Step election - let state = wasm.CountStateRational.new(election); - wasm.count_init_Rational(state, opts); - updateResultTable(wasm.update_results_table_Rational(1, state, opts)); - updateStageComments(wasm.update_stage_comments_Rational(state)); - - for (let stageNum = 2;; stageNum++) { - let isDone = wasm.count_one_stage_Rational(state, opts); - if (isDone) { - break; - } - updateResultTable(wasm.update_results_table_Rational(stageNum, state, opts)); - updateStageComments(wasm.update_stage_comments_Rational(state)); - } - - updateResultTable(wasm.finalise_results_table_Rational(state)); - - divLogs2.insertAdjacentHTML('beforeend', wasm.final_result_summary_Rational(state)); + // Dispatch to worker + worker.postMessage({ + 'type': 'countElection', + 'electionData': electionData, + 'optsStr': optsStr, + 'filePath': filePath + }); } diff --git a/html/worker.js b/html/worker.js new file mode 100644 index 0000000..39f82cd --- /dev/null +++ b/html/worker.js @@ -0,0 +1,45 @@ +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)}); + } +} diff --git a/src/lib.rs b/src/lib.rs index 3712bcf..0d2bd97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,5 +21,9 @@ pub mod numbers; pub mod stv; use git_version::git_version; +use wasm_bindgen::prelude::wasm_bindgen; pub const VERSION: &str = git_version!(args=["--always", "--dirty=-dev"], fallback="unknown"); + +#[wasm_bindgen] +pub fn version() -> String { VERSION.to_string() }