我有一个网络工作者处理一组保存的ImageData
帧,并且正在使用OffscreenCanvas
的上下文(在网络工作者中创建)到putImageData
。同时,在主线程中,我阻塞了另一个操作。如果我包含context.putImageData()
调用,Web Worker将阻塞直到我的主线程完成,然后再继续,但是如果我删除putImageData
调用或使用context.clearRect()
,则Web Worker将与我的主线程同时运行。
即使我是在Web工作者中运行,Web工作者是否有可能在OffscreenCanvas
的某些用途上受阻? convertToBlob
也会发生这种情况。
确实存在由this issue引起的回归,这会影响Chrome> 82。
我确实打开了a bug report,所以现在我们所能做的就是等待他们修复它。
这是我制作的MCVE:
const worker_script = `
postMessage('ready'); // main will block
const now = performance.now();
const off = new OffscreenCanvas(300, 150);
const off_ctx = off.getContext('2d');
off_ctx.fillRect(0,0,30,30)
postMessage( performance.now() - now );
`;
const worker_url = URL.createObjectURL( new Blob( [ worker_script ], { type: "text/javascript" } ) );
const worker = new Worker( worker_url );
worker.onmessage = evt => {
if (evt.data === "ready" ) {
blockMainThread( 3000 )
.then( () => log( 'now free' ) );
}
else log( `worker script took ${evt.data}ms to complete.` );
};
worker.postMessage( "" );
function blockMainThread( duration ) {
return new Promise( ( resolve ) => {
const now = performance.now();
while( performance.now() - now < duration ) {}
resolve();
} );
}
function log( content ) {
_log.textContent += content + '\n';
}
<pre id="_log">
The page will be frozen for 3s. Please wait.
</pre>
鉴于该错误的性质,我怀疑是否有任何解决方法。