I'm building a browser tool that samples a big file and shows some stats about it.
The program picks k random parts of a file, and processes each part of the file separately. Once each part is processed, an object is modified that keeps track of the rolling "stats" of the file (in the example below, I've simplified to incrementing a rolling counter).
The issue is that now every part is read in parallel, but I'd like it to be in series - so that the updates to the rolling counter are thread safe.
I think the next processFileChunk
the for-loop is executing before the other finishes. How do I get this to be done serially?
I'm fairly new to Vue, and frontend in general. Is this a simple asynchronicity problem? How do I tell if something is asynchronous?
Edit: the parsing step uses the papaparse library (which I bet is the asynchronous part)
import {parse} from 'papaparse'
export default {
data() {
counter: 0
},
methods() {
streamAndSample(file) {
var vm = this;
const k = 10 // number of samples
var pointers = PickRandomPointers(file) // this is an array of integers, representing a random byte location of a file
for (const k_th_random_pointer in pointers) {
processFileChunk(file, k_th_random_pointer)
}
}
processFileChunk(file, k_th_random_pointer){
var vm = this;
var reader = new FileReader();
reader.readAsText(file.slice(k_th_random_pointer, k_th_random_pointer + 100000)) // read 100 KB
reader.onload = function (oEvent) {
var text = oEvent.target.result
parse(text,{complete: function (res) {
for (var i = 0; i < res.data.length; i++) {
vm.counter = vm.counter + 1
}}})
}
}
}
}