基本上,无论我做什么,任何由缓冲区构成的数组(不必共享)都会导致数组的元素长度与缓冲区相同,但其所有值都为零。注意我需要在chrome中执行此操作,并且类型化数组上的Mozilla文档似乎不适用于chrome。
我想要一个表示缓冲区的数组,该缓冲区中已有数据。我的用例是一个webworker接收一个已经有数据的共享数组缓冲区。但是,我没有成功创建表示缓冲区的视图。
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)
这就是我项目中真实代码的样子(除假数据函数外):
//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();
答案 0 :(得分:1)
在致电SharedArrayBuffer
new Int32Array()
传递给fillIntBuffer()
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);
var b = new Int32Array(a);
fillIntBuffer(b, 10);
console.log(b);