From fce2a6e05a8e5f1dac4d19c143ac41e15c9f3091 Mon Sep 17 00:00:00 2001 From: joe Date: Tue, 13 Sep 2022 11:32:26 -0700 Subject: [PATCH 1/3] Use self to get global context before window in getEventPriority. Prior to this change, this code would throw an error when run inside of a WebWorker, since window is not defined inside of webworkers. This prevented us from using R3F inside of an offscreen canvas in a webworker. --- packages/fiber/src/core/events.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/fiber/src/core/events.ts b/packages/fiber/src/core/events.ts index c3749f0462..36b4297a27 100644 --- a/packages/fiber/src/core/events.ts +++ b/packages/fiber/src/core/events.ts @@ -100,7 +100,8 @@ function makeId(event: Intersection) { // https://github.com/facebook/react/tree/main/packages/react-reconciler#getcurrenteventpriority // Gives React a clue as to how import the current interaction is export function getEventPriority() { - let name = window?.event?.type + const globalScope = self || window + let name = globalScope?.event?.type switch (name) { case 'click': case 'contextmenu': From ea71f626b8b87bb189ffc682ce94e35963c91f8c Mon Sep 17 00:00:00 2001 From: Cody Bennett <23324155+CodyJasonBennett@users.noreply.github.com> Date: Wed, 14 Sep 2022 18:59:49 -0500 Subject: [PATCH 2/3] chore: harden global check --- packages/fiber/src/core/events.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/fiber/src/core/events.ts b/packages/fiber/src/core/events.ts index 36b4297a27..ccbb35ae4b 100644 --- a/packages/fiber/src/core/events.ts +++ b/packages/fiber/src/core/events.ts @@ -100,8 +100,12 @@ function makeId(event: Intersection) { // https://github.com/facebook/react/tree/main/packages/react-reconciler#getcurrenteventpriority // Gives React a clue as to how import the current interaction is export function getEventPriority() { - const globalScope = self || window - let name = globalScope?.event?.type + // Get a handle to the current global scope in window and worker contexts if able + // https://github.com/pmndrs/react-three-fiber/pull/2493 + const globalScope = (typeof self !== 'undefined' && self) || (typeof window !== 'undefined' && window) + if (!globalScope) return DefaultEventPriority + + const name = globalScope.event?.type switch (name) { case 'click': case 'contextmenu': From 2799a051cc1ba679efdc13d2add625d33686a893 Mon Sep 17 00:00:00 2001 From: Cody Bennett <23324155+CodyJasonBennett@users.noreply.github.com> Date: Wed, 14 Sep 2022 19:01:39 -0500 Subject: [PATCH 3/3] fix: add fallback DPR for headless contexts --- packages/fiber/src/core/utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/fiber/src/core/utils.ts b/packages/fiber/src/core/utils.ts index 155ac8dd5c..23cc8ae8be 100644 --- a/packages/fiber/src/core/utils.ts +++ b/packages/fiber/src/core/utils.ts @@ -71,7 +71,8 @@ export type ObjectMap = { } export function calculateDpr(dpr: Dpr) { - return Array.isArray(dpr) ? Math.min(Math.max(dpr[0], window.devicePixelRatio), dpr[1]) : dpr + const target = typeof window !== 'undefined' ? window.devicePixelRatio : 1 + return Array.isArray(dpr) ? Math.min(Math.max(dpr[0], target), dpr[1]) : dpr } /**