Skip to content

Commit

Permalink
Update document addEventListener polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
hmallen99 committed Feb 18, 2025
1 parent 13a9168 commit f9b484d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
17 changes: 3 additions & 14 deletions packages/clientNative/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {TextEncoder, TextDecoder} from 'text-encoding-shim';
import structuredClone from '@ungap/structured-clone';
import performance from 'react-native-performance';
import {URL} from 'whatwg-url-without-unicode';
import {Dimensions} from 'react-native';

window.location = new URL('https://mariale.ir.world/location/marbar');

Expand All @@ -59,14 +58,8 @@ global.localStorage = {

// Window polyfill
const listenerRegistry = new Map();
const resizeSubscriptions = new Map();

window.addEventListener = (type, handler) => {
// if (type === 'resize') {
// const subscription = Dimensions.addEventListener('change', handler);
// resizeSubscriptions.set(handler, subscription);
// return;
// }
let registry = listenerRegistry.get(type);

if (!registry) {
Expand All @@ -76,13 +69,6 @@ window.addEventListener = (type, handler) => {
registry.add(handler);
};
window.removeEventListener = (type, handler) => {
// if (type === 'resize') {
// const subscription = resizeSubscriptions.get(handler);
// subscription?.remove();
// resizeSubscriptions.delete(handler);
// return;
// }

const registry = listenerRegistry.get(type);
if (registry) {
registry.delete(handler);
Expand All @@ -97,6 +83,9 @@ window.dispatchEvent = (eventType, evt) => {
}
};

document.addEventListener = window.addEventListener;
document.removeEventListener = window.removeEventListener;

// Using PixelRatio.get() was causing issues with frame buffers. Let's default to 1.
window.devicePixelRatio = 1;

Expand Down
39 changes: 19 additions & 20 deletions packages/spatial/src/input/functions/ClientInputHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/ir-engine/ir-engine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.
Software distributed under the License is distributed on an "AS IS" basis,
Expand All @@ -19,7 +19,7 @@ The Original Code is Infinite Reality Engine.
The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Infinite Reality Engine team.
All portions of the code written by the Infinite Reality Engine team are Copyright © 2021-2023
All portions of the code written by the Infinite Reality Engine team are Copyright © 2021-2023
Infinite Reality Engine. All Rights Reserved.
*/

Expand Down Expand Up @@ -63,9 +63,9 @@ export const useNonSpatialInputSources = () => {
setComponent(eid, NameComponent, 'InputSource-nonspatial')
const inputSourceComponent = getComponent(eid, InputSourceComponent)

// document.addEventListener('DOMMouseScroll', ClientInputFunctions.preventDefault, false)
// document.addEventListener('gesturestart', ClientInputFunctions.preventDefault)
// document.addEventListener('keydown', ClientInputFunctions.preventDefaultKeyDown, false)
document.addEventListener('DOMMouseScroll', ClientInputFunctions.preventDefault, false)
document.addEventListener('gesturestart', ClientInputFunctions.preventDefault)
document.addEventListener('keydown', ClientInputFunctions.preventDefaultKeyDown, false)

const onKeyEvent = (event: KeyboardEvent) => {
ClientInputFunctions.preventDefaultKeyDown(event)
Expand All @@ -80,8 +80,8 @@ export const useNonSpatialInputSources = () => {
if (down) buttonState[code] = createInitialButtonState(eid)
else if (buttonState[code]) buttonState[code].up = true
}
// document.addEventListener('keyup', onKeyEvent)
// document.addEventListener('keydown', onKeyEvent)
document.addEventListener('keyup', onKeyEvent)
document.addEventListener('keydown', onKeyEvent)

const handleTouchDirectionalPad = (event: CustomEvent): void => {
const { stick, value }: { stick: 'LeftStick' | 'RightStick'; value: { x: number; y: number } } = event.detail
Expand All @@ -91,29 +91,29 @@ export const useNonSpatialInputSources = () => {
axes[index + 0] = value.x
axes[index + 1] = value.y
}
window.addEventListener('touchstickmove', handleTouchDirectionalPad)
document.addEventListener('touchstickmove', handleTouchDirectionalPad)

const handleTouchGamepadButtonDown = (event: CustomEvent) => {
const buttonState = inputSourceComponent.buttons
buttonState[event.detail.button] = createInitialButtonState(eid)
}
window.addEventListener('touchgamepadbuttondown', handleTouchGamepadButtonDown)
document.addEventListener('touchgamepadbuttondown', handleTouchGamepadButtonDown)

const handleTouchGamepadButtonUp = (event: CustomEvent) => {
const buttonState = inputSourceComponent.buttons
if (buttonState[event.detail.button]) buttonState[event.detail.button].up = true
}
window.addEventListener('touchgamepadbuttonup', handleTouchGamepadButtonUp)
document.addEventListener('touchgamepadbuttonup', handleTouchGamepadButtonUp)

return () => {
// document.removeEventListener('DOMMouseScroll', ClientInputFunctions.preventDefault, false)
// document.removeEventListener('gesturestart', ClientInputFunctions.preventDefault)
// document.removeEventListener('keyup', onKeyEvent)
// document.removeEventListener('keydown', onKeyEvent)
// document.removeEventListener('keydown', ClientInputFunctions.preventDefaultKeyDown, false)
window.removeEventListener('touchstickmove', handleTouchDirectionalPad)
window.removeEventListener('touchgamepadbuttondown', handleTouchGamepadButtonDown)
window.removeEventListener('touchgamepadbuttonup', handleTouchGamepadButtonUp)
document.removeEventListener('DOMMouseScroll', ClientInputFunctions.preventDefault, false)
document.removeEventListener('gesturestart', ClientInputFunctions.preventDefault)
document.removeEventListener('keyup', onKeyEvent)
document.removeEventListener('keydown', onKeyEvent)
document.removeEventListener('keydown', ClientInputFunctions.preventDefaultKeyDown, false)
document.removeEventListener('touchstickmove', handleTouchDirectionalPad)
document.removeEventListener('touchgamepadbuttondown', handleTouchGamepadButtonDown)
document.removeEventListener('touchgamepadbuttonup', handleTouchGamepadButtonUp)
removeEntity(eid)
}
}, [])
Expand Down Expand Up @@ -208,7 +208,6 @@ export const CanvasInputReactor = () => {
const rendererComponent = useComponent(cameraEntity, RendererComponent)

useEffect(() => {
console.log('Loaded Canvas Input Reactor')
if (xrState.session.value) return // pointer input sources are automatically handled by webxr

const canvas = rendererComponent.canvas.value
Expand Down

0 comments on commit f9b484d

Please sign in to comment.