Skip to content

Commit

Permalink
Merge pull request #45464 from margelo/compiler/mutating-refs
Browse files Browse the repository at this point in the history
compiler: allow mutating `ref`s in certain components
  • Loading branch information
mountiny authored Jul 22, 2024
2 parents d1fe4a4 + b0519f7 commit 06cc50d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 19 deletions.
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const IS_E2E_TESTING = process.env.E2E_TESTING === 'true';

const ReactCompilerConfig = {
runtimeModule: 'react-compiler-runtime',
environment: {
enableTreatRefLikeIdentifiersAsRefs: true,
},
};
const defaultPresets = ['@babel/preset-react', '@babel/preset-env', '@babel/preset-flow', '@babel/preset-typescript'];
const defaultPlugins = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
diff --git a/node_modules/react-compiler-healthcheck/dist/index.js b/node_modules/react-compiler-healthcheck/dist/index.js
index 4bf23db..fa2ab22 100755
--- a/node_modules/react-compiler-healthcheck/dist/index.js
+++ b/node_modules/react-compiler-healthcheck/dist/index.js
@@ -69088,6 +69088,9 @@ const COMPILER_OPTIONS = {
compilationMode: "infer",
panicThreshold: "critical_errors",
logger: logger,
+ environment: {
+ enableTreatRefLikeIdentifiersAsRefs: true,
+ },
};
function isActionableDiagnostic(detail) {
switch (detail.severity) {
diff --git a/node_modules/react-compiler-healthcheck/src/checks/reactCompiler.ts b/node_modules/react-compiler-healthcheck/src/checks/reactCompiler.ts
index 09c9b9b..d2418e0 100644
--- a/node_modules/react-compiler-healthcheck/src/checks/reactCompiler.ts
+++ b/node_modules/react-compiler-healthcheck/src/checks/reactCompiler.ts
@@ -51,6 +51,9 @@ const COMPILER_OPTIONS: Partial<PluginOptions> = {
compilationMode: "infer",
panicThreshold: "critical_errors",
logger,
+ environment: {
+ enableTreatRefLikeIdentifiersAsRefs: true,
+ },
};

function isActionableDiagnostic(detail: CompilerErrorDetailOptions) {
4 changes: 2 additions & 2 deletions src/components/Composer/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function Composer(
ref: ForwardedRef<TextInput>,
) {
const textInput = useRef<AnimatedMarkdownTextInputRef | null>(null);
const {isFocused, shouldResetFocus} = useResetComposerFocus(textInput);
const {isFocused, shouldResetFocusRef} = useResetComposerFocus(textInput);
const textContainsOnlyEmojis = useMemo(() => EmojiUtils.containsOnlyEmojis(value ?? ''), [value]);
const theme = useTheme();
const markdownStyle = useMarkdownStyle(value, !isGroupPolicyReport ? excludeReportMentionStyle : excludeNoStyles);
Expand Down Expand Up @@ -95,7 +95,7 @@ function Composer(
onBlur={(e) => {
if (!isFocused) {
// eslint-disable-next-line react-compiler/react-compiler
shouldResetFocus.current = true; // detect the input is blurred when the page is hidden
shouldResetFocusRef.current = true; // detect the input is blurred when the page is hidden
}
props?.onBlur?.(e);
}}
Expand Down
18 changes: 9 additions & 9 deletions src/components/VideoPlayer/BaseVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function BaseVideoPlayer({
shareVideoPlayerElements,
currentVideoPlayerRef,
updateCurrentlyPlayingURL,
videoResumeTryNumber,
videoResumeTryNumberRef,
setCurrentlyPlayingURL,
} = usePlaybackContext();
const {isFullScreenRef} = useFullScreenContext();
Expand All @@ -86,15 +86,15 @@ function BaseVideoPlayer({
const {videoPopoverMenuPlayerRef, setCurrentPlaybackSpeed} = useVideoPopoverMenuContext();

const togglePlayCurrentVideo = useCallback(() => {
videoResumeTryNumber.current = 0;
videoResumeTryNumberRef.current = 0;
if (!isCurrentlyURLSet) {
updateCurrentlyPlayingURL(url);
} else if (isPlaying) {
pauseVideo();
} else {
playVideo();
}
}, [isCurrentlyURLSet, isPlaying, pauseVideo, playVideo, updateCurrentlyPlayingURL, url, videoResumeTryNumber]);
}, [isCurrentlyURLSet, isPlaying, pauseVideo, playVideo, updateCurrentlyPlayingURL, url, videoResumeTryNumberRef]);

const showPopoverMenu = (event?: GestureResponderEvent | KeyboardEvent) => {
videoPopoverMenuPlayerRef.current = videoPlayerRef.current;
Expand All @@ -118,16 +118,16 @@ function BaseVideoPlayer({
// fix for iOS mWeb: preventing iOS native player default behavior from pausing the video when exiting fullscreen
const preventPausingWhenExitingFullscreen = useCallback(
(isVideoPlaying: boolean) => {
if (videoResumeTryNumber.current === 0 || isVideoPlaying) {
if (videoResumeTryNumberRef.current === 0 || isVideoPlaying) {
return;
}
if (videoResumeTryNumber.current === 1) {
if (videoResumeTryNumberRef.current === 1) {
playVideo();
}
// eslint-disable-next-line react-compiler/react-compiler
videoResumeTryNumber.current -= 1;
videoResumeTryNumberRef.current -= 1;
},
[playVideo, videoResumeTryNumber],
[playVideo, videoResumeTryNumberRef],
);

const handlePlaybackStatusUpdate = useCallback(
Expand Down Expand Up @@ -186,11 +186,11 @@ function BaseVideoPlayer({
if (videoStateRef.current && (!('isPlaying' in videoStateRef.current) || videoStateRef.current.isPlaying)) {
pauseVideo();
playVideo();
videoResumeTryNumber.current = 3;
videoResumeTryNumberRef.current = 3;
}
}
},
[isFullScreenRef, onFullscreenUpdate, pauseVideo, playVideo, videoResumeTryNumber, updateVolume, currentVideoPlayerRef],
[isFullScreenRef, onFullscreenUpdate, pauseVideo, playVideo, videoResumeTryNumberRef, updateVolume, currentVideoPlayerRef],
);

const bindFunctions = useCallback(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/VideoPlayerContexts/PlaybackContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
const [originalParent, setOriginalParent] = useState<View | HTMLDivElement | null>(null);
const currentVideoPlayerRef = useRef<VideoWithOnFullScreenUpdate | null>(null);
const {currentReportID} = useCurrentReportID() ?? {};
const videoResumeTryNumber = useRef<number>(0);
const videoResumeTryNumberRef = useRef<number>(0);

const pauseVideo = useCallback(() => {
currentVideoPlayerRef.current?.setStatusAsync?.({shouldPlay: false});
Expand Down Expand Up @@ -73,7 +73,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
);

const resetVideoPlayerData = useCallback(() => {
videoResumeTryNumber.current = 0;
videoResumeTryNumberRef.current = 0;
stopVideo();
setCurrentlyPlayingURL(null);
setSharedElement(null);
Expand Down Expand Up @@ -102,7 +102,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
playVideo,
pauseVideo,
checkVideoPlaying,
videoResumeTryNumber,
videoResumeTryNumberRef,
}),
[
updateCurrentlyPlayingURL,
Expand Down
2 changes: 1 addition & 1 deletion src/components/VideoPlayerContexts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PlaybackContext = {
currentlyPlayingURLReportID: string | undefined;
originalParent: View | HTMLDivElement | null;
sharedElement: View | HTMLDivElement | null;
videoResumeTryNumber: MutableRefObject<number>;
videoResumeTryNumberRef: MutableRefObject<number>;
currentVideoPlayerRef: MutableRefObject<VideoWithOnFullScreenUpdate | null>;
shareVideoPlayerElements: (ref: VideoWithOnFullScreenUpdate | null, parent: View | HTMLDivElement | null, child: View | HTMLDivElement | null, isUploading: boolean) => void;
playVideo: () => void;
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useResetComposerFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import type {TextInput} from 'react-native';

export default function useResetComposerFocus(inputRef: MutableRefObject<TextInput | null>) {
const isFocused = useIsFocused();
const shouldResetFocus = useRef(false);
const shouldResetFocusRef = useRef(false);

useEffect(() => {
if (!isFocused || !shouldResetFocus.current) {
if (!isFocused || !shouldResetFocusRef.current) {
return;
}
inputRef.current?.focus(); // focus input again
shouldResetFocus.current = false;
shouldResetFocusRef.current = false;
}, [isFocused, inputRef]);

return {isFocused, shouldResetFocus};
return {isFocused, shouldResetFocusRef};
}

0 comments on commit 06cc50d

Please sign in to comment.