From 5bd5d129efd3b97c6ccd84ff9b2c94daebd5c6e7 Mon Sep 17 00:00:00 2001 From: Brandon Jones Date: Mon, 11 Mar 2024 12:50:02 -0700 Subject: [PATCH] Remove React guard from worker sample Removes some code from the worker sample that only existed to catch errors when the code was executed twice by React. Now that React has been removed it's no longer necessary. --- sample/worker/main.ts | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/sample/worker/main.ts b/sample/worker/main.ts index c335ce7e..aa416d52 100644 --- a/sample/worker/main.ts +++ b/sample/worker/main.ts @@ -17,25 +17,17 @@ worker.addEventListener('message', (ev) => { } }); -try { - // In order for the worker to display anything on the page, an OffscreenCanvas must be used. - // Here we can create one from our normal canvas by calling transferControlToOffscreen(). - // Anything drawn to the OffscreenCanvas that call returns will automatically be displayed on - // the source canvas on the page. - const offscreenCanvas = canvas.transferControlToOffscreen(); - const devicePixelRatio = window.devicePixelRatio; - offscreenCanvas.width = canvas.clientWidth * devicePixelRatio; - offscreenCanvas.height = canvas.clientHeight * devicePixelRatio; +// In order for the worker to display anything on the page, an OffscreenCanvas must be used. +// Here we can create one from our normal canvas by calling transferControlToOffscreen(). +// Anything drawn to the OffscreenCanvas that call returns will automatically be displayed on +// the source canvas on the page. +const offscreenCanvas = canvas.transferControlToOffscreen(); +const devicePixelRatio = window.devicePixelRatio; +offscreenCanvas.width = canvas.clientWidth * devicePixelRatio; +offscreenCanvas.height = canvas.clientHeight * devicePixelRatio; - // Send a message to the worker telling it to initialize WebGPU with the OffscreenCanvas. The - // array passed as the second argument here indicates that the OffscreenCanvas is to be - // transferred to the worker, meaning this main thread will lose access to it and it will be - // fully owned by the worker. - worker.postMessage({ type: 'init', offscreenCanvas }, [offscreenCanvas]); -} catch (err) { - // TODO: This catch is added here because React will call init twice with the same canvas, and - // the second time will fail the transferControlToOffscreen() because it's already been - // transferred. I'd love to know how to get around that. - console.warn(err.message); - worker.terminate(); -} +// Send a message to the worker telling it to initialize WebGPU with the OffscreenCanvas. The +// array passed as the second argument here indicates that the OffscreenCanvas is to be +// transferred to the worker, meaning this main thread will lose access to it and it will be +// fully owned by the worker. +worker.postMessage({ type: 'init', offscreenCanvas }, [offscreenCanvas]);