admin管理员组文章数量:1399950
I'm working with Web Workers and Buffers recently.
Now I have a buffer and I want to check the sixth byte using SmartBuffer. If that byte is 0, send to worker1. Otherwise, send to worker2.
const buffer = SmartBuffer.fromBuffer(someBuffer);
if(buffer.toBuffer()[5] === 0){
worker1.postMessage(someBuffer, [someBuffer]);
}
if(buffer.toBuffer()[5] === 1){
worker2.postMessage(someBuffer, [someBuffer]);
}
Since I use a transferable object, if the someBuffer
has been transferred, I cannot access it in line 5 anymore. (It'll cause an error: Cannot perform Construct on a detached ArrayBuffer
.)
I know that's a bad example, and I can simply use else if
to prevent this error. But the question is: Is there any way to know if a buffer has been transferred?
I'm working with Web Workers and Buffers recently.
Now I have a buffer and I want to check the sixth byte using SmartBuffer. If that byte is 0, send to worker1. Otherwise, send to worker2.
const buffer = SmartBuffer.fromBuffer(someBuffer);
if(buffer.toBuffer()[5] === 0){
worker1.postMessage(someBuffer, [someBuffer]);
}
if(buffer.toBuffer()[5] === 1){
worker2.postMessage(someBuffer, [someBuffer]);
}
Since I use a transferable object, if the someBuffer
has been transferred, I cannot access it in line 5 anymore. (It'll cause an error: Cannot perform Construct on a detached ArrayBuffer
.)
I know that's a bad example, and I can simply use else if
to prevent this error. But the question is: Is there any way to know if a buffer has been transferred?
3 Answers
Reset to default 4The byteLength
property of a detached ArrayBuffer and each typed array which uses this ArrayBuffer will be zero.
I'm not sure about the internals of the SmartBuffer library but this check should work:
someBuffer.byteLength === 0
This does of course not help if you want to check an ArrayBuffer which has an initial byteLength of zero. In this case nothing will change if it gets detached. But I think this is only a problem in theory.
You can use ArrayBuffer.prototype.detached
const buffer = new ArrayBuffer(8);
console.log(buffer.detached); // false
const newBuffer = buffer.transfer();
console.log(buffer.detached); // true
console.log(newBuffer.detached); // false
Old answer
Currently, the most reliable way to check if it was detached is to use a TypedArray
constructor.
While all detached buffers will have byteLength === 0
, an empty ArrayBuffer
will too and is not considered to be detached.
function isDetached(ab) {
if(ab.byteLength != 0) {
// detached buffers will always have zero byteLength
return false;
}
try {
new Uint8Array(ab);
return false;
} catch {
// Uint8Array throws if using a detached buffer
return true;
}
}
const ab = new ArrayBuffer(0);
console.log('isDetached:', isDetached(ab)); // false
structuredClone(ab, { transfer: [ab] }); // detach buffer
console.log('isDetached:', isDetached(ab)); // true
In Node.js it is possible, might be the same (or at least similar) for browsers.
Using util.format()
, which is also used in console.log
, the detached ArrayBuffer is stringified to something like
ArrayBuffer { (detached), byteLength: 0 }
So checking for detached
in the formatted value will tell you exactly that.
Example:
function isDetached(buffer: ArrayBuffer): boolean {
if (buffer.byteLength === 0) {
const formatted = util.format(buffer)
return formatted.includes('detached')
}
return false
}
本文标签: javascriptHow to check if an ArrayBuffer is detached or transfered in JSStack Overflow
版权声明:本文标题:javascript - How to check if an ArrayBuffer is detached or transfered in JS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744168316a2593656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论