你可以将 JS 对象存储在 SharedArrayBuffer 中吗?
Can you store a JS object in a SharedArrayBuffer?
我需要在客户端应用程序和网络工作者之间共享一个对象,我听说过 SharedArrayBuffers。这就是我想做的。
main.js
let myWorker = new Worker('/worker.js')
let mySab = new SharedArrayBuffer(1024)
let myObj = { foo: 'bar', bar: 'foo' }
// Save 'myObj' to 'mySab'
worker.postMessage(sab)
worker.js
self.onmessage = (e) => {
let myObj = BLANK // Get 'myObj' from SharedArrayBuffer
}
这可能吗?我见过的 ShareArrayBuffers 示例只在缓冲区中保存数字。感谢您的帮助!
不,SharedArrayBuffer
s 只存储二进制数据。您只能通过 built-in postMessage
函数发送对象的副本。
来源:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
我需要在客户端应用程序和网络工作者之间共享一个对象,我听说过 SharedArrayBuffers。这就是我想做的。
main.js
let myWorker = new Worker('/worker.js')
let mySab = new SharedArrayBuffer(1024)
let myObj = { foo: 'bar', bar: 'foo' }
// Save 'myObj' to 'mySab'
worker.postMessage(sab)
worker.js
self.onmessage = (e) => {
let myObj = BLANK // Get 'myObj' from SharedArrayBuffer
}
这可能吗?我见过的 ShareArrayBuffers 示例只在缓冲区中保存数字。感谢您的帮助!
不,SharedArrayBuffer
s 只存储二进制数据。您只能通过 built-in postMessage
函数发送对象的副本。
来源:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers