Regardless of my efforts, whenever I create an array from a buffer (no need for sharing), the resulting array contains elements equal to the length of the buffer, with all values being zero. It seems like the documentation on typed arrays in Mozilla doesn't apply to Chrome.
I am trying to obtain an array representation of a buffer that already holds data. My specific scenario involves a web worker receiving a shared array buffer with pre-existing data. But so far, I haven't been able to create a view representing that buffer.
function testIntBuffer(n) {
return new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * n);
}
function fillIntBuffer(buffer, endInt) {
for (var i = 0; i <= endInt; i++) {
buffer[i] = i;
}
return buffer;
}
var a = testIntBuffer(10);
fillIntBuffer(a, 10);
console.log(a);
var b = new Int32Array(a);
console.log(b)
This is how the actual code in my project looks like (except for the fake data function):
//inside main.js
function fakeDataArray(n) {
const arr = [];
for(let i = 0; i <= n; i++) {
arr[i] = Math.floor(Math.random() * 100);
}
return arr;
}
function normalArrayToSBA(rawData) {
const n = rawData.length;
const m = Int32Array.BYTES_PER_ELEMENT;
const data = new SharedArrayBuffer(m * n);
rawData.forEach((datum, i) => {
data[i] = datum;
});
return [data, n, m];
}
function createWorker() {
const w = new Worker('workerBundle.js');
w.onmessage = (event) => {
const command = event.data.command;
if (command === 'workerFinish') {
console.log('event recieved from worker: ', event);
}
if (!command) {
return;
}
};
return w;
}
function startworker(dataInfo, options) {
const worker = createWorker();
function setWorkerData(dataInfo) {
const priceBuffer = dataInfo[0];
const size = dataInfo[1];
const offset = dataInfo[2];
worker.postMessage({
command: 'setData',
priceBuffer: priceBuffer,
size: size,
offset: offset
});
}
function getWorkerStats(_options) {
worker.postMessage({
command: 'getStats',
options: _options,
});
}
const id = Math.random();
setWorkerData(dataInfo);
getWorkerStats(options);
}
let data = fakeDataArray(5751);
let dataInfo = normalArrayToSBA(data);
startworker(dataInfo);
//inside workerBundle.js
//eslint-disable-next-line no-unused-vars
const WorkerMessageInterface = {
file: null,
priceData: null,
role: 'worker',
id: null,
pendingRequests: {},
setRole(event) {
WorkerMessageInterface.role = event.data.role;
WorkerMessageInterface.id = event.data.id;
self.postMessage({
success: true,
commandGiven: event.data.command
});
},
//command: 'setFile'
/*{ command: 'setFile', fileInfo: String }*/
setFile(event) {
WorkerMessageInterface.file = event.data.file;
self.postMessage({
success: true,
commandGiven: event.data.command
});
},
//command: 'setData'
/**
* @param {priceData} Array
* @param {source} String - Indicates what utility mapping is necesarry
* to convert data into the highcharts format
*/
setData(event) {
const data = event.data;
const priceBuffer = new Int32Array(data.priceBuffer);
WorkerMessageInterface.priceData = [priceBuffer, data.size];
},
//command: 'getSignals'
/**
* @param {options} Object
*/
getAverage(dataArray) {
let sum = 0;
let length = dataArray.length;
dataArray.forEach((n) => {
sum += n;
});
return sum / length;
},
getStats(event) {
const ops = event.data.options;
const id = WorkerMessageInterface.id;
const data = WorkerMessageInterface.priceData;
const stats = WorkerMessageInterface.getAverage(data);
if (WorkerMessageInterface.role === 'worker') {
self.postMessage({
results: stats,
command: 'workerFinish',
id: id
});
}
},
listenForCommands() {
self.onmessage = (event) => {
const command = event.data.command;
WorkerMessageInterface[command](event);
self.postMessage({
worker: true,
commandGiven: command,
status: 'recieved',
});
};
}
};
WorkerMessageInterface.listenForCommands();