Skip to content

Commit

Permalink
Fix screen flashing when switching to editor
Browse files Browse the repository at this point in the history
  • Loading branch information
gettinToasty committed Jul 19, 2024
1 parent 5d658f6 commit e611136
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/components-react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ export function useRenderInterval(callback: () => void, delay: number, condition
}, [tick, condition]);
}

/**
* Sets up a useEffect scenario that will only fire after the first render
* instead of the normal case that fires during the first render in addition to after
* dependency changes.
* @param callback Function to handle the desired effect
* @param deps If present, effect will only activate if the values in the list change
*/
export function useUpdateEffect(callback: React.EffectCallback, deps?: React.DependencyList) {
const hasRendered = useRef(false);

useEffect(() => {
if (hasRendered.current) {
callback();
} else {
hasRendered.current = true;
}
}, deps);
}

/**
* Useful for firing off an async request when the component mounts, but
* the result will be automatically discarded if the component unmounts
Expand Down
4 changes: 3 additions & 1 deletion app/components-react/shared/Display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Display as OBSDisplay } from '../../services/video';
import { TDisplayType } from 'services/settings-v2/video';
import uuid from 'uuid/v4';
import { useRealmObject } from 'components-react/hooks/realm';
import { useUpdateEffect } from 'components-react/hooks';

interface DisplayProps {
id?: string;
sourceId?: string;
Expand Down Expand Up @@ -43,7 +45,7 @@ export default function Display(props: DisplayProps) {
const displayEl = useRef<HTMLDivElement>(null);

useEffect(updateDisplay, [p.sourceId, paddingColor]);
useEffect(refreshOutputRegion, [v.baseResolution]);
useUpdateEffect(refreshOutputRegion, [v.baseResolution]);

function refreshOutputRegion() {
if (!obsDisplay.current) return;
Expand Down

0 comments on commit e611136

Please sign in to comment.